test_integration_auth.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Copyright (c) 2019 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file test_integration_auth.cpp
  10. * @brief integration test implementation for the authentication functions
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. extern "C" {
  15. #include "shellmatta.h"
  16. }
  17. #include <string.h>
  18. static uint32_t write_callCnt = 0u;
  19. static char write_data[1024];
  20. static uint32_t write_length;
  21. static uint32_t privateTemp;
  22. static uint32_t userIdTemp;
  23. static bool successTemp;
  24. #define TEST_SHELLMATTA_SETUP shellmatta_retCode_t ret; \
  25. shellmatta_instance_t inst; \
  26. shellmatta_handle_t handle; \
  27. char buffer[1024] = {0}; \
  28. char historyBuffer[1024] = {0}; \
  29. \
  30. shellmatta_doInit( &inst, \
  31. &handle, \
  32. buffer, \
  33. sizeof(buffer), \
  34. historyBuffer, \
  35. sizeof(historyBuffer), \
  36. "shellmatta->", \
  37. NULL, \
  38. writeFct); \
  39. \
  40. write_callCnt = 0u; \
  41. memset(write_data, 0, sizeof(write_data)); \
  42. write_length = 0u; \
  43. privateTemp = 0u; \
  44. userIdTemp = 255u; \
  45. successTemp = false; \
  46. \
  47. shellmatta_addCmd(handle, &publicCmd); \
  48. shellmatta_addCmd(handle, &privateCmd);
  49. #define TEST_SHELLMATTA_AUTH_SETUP shellmatta_auth_user_t userList[] = { \
  50. {1, false, "shimatta", "12345678"}, \
  51. {2, false, "not_shimatta", "87654321"}, \
  52. {3, true, "root", "rootpw"} \
  53. }; \
  54. \
  55. uint32_t privateCmdPerms[] = {1}; \
  56. shellmatta_auth_perm_t permList[] = { \
  57. {"private", privateCmdPerms, sizeof(privateCmdPerms)/sizeof(privateCmdPerms[0])}, \
  58. {"additional", privateCmdPerms, sizeof(privateCmdPerms)/sizeof(privateCmdPerms[0])} \
  59. }; \
  60. \
  61. shellmatta_auth_init(handle, userList, 3, permList, 2, false, NULL, NULL);
  62. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  63. {
  64. write_callCnt ++;
  65. while((length > 0) && (write_length < sizeof(write_data)))
  66. {
  67. write_data[write_length] = *data;
  68. data ++;
  69. length --;
  70. write_length ++;
  71. }
  72. return SHELLMATTA_OK;
  73. }
  74. static shellmatta_retCode_t publicCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  75. {
  76. (void) handle;
  77. (void) arguments;
  78. (void) length;
  79. shellmatta_retCode_t ret = SHELLMATTA_OK;
  80. return ret;
  81. }
  82. shellmatta_cmd_t publicCmd = {(char*)"public", (char*)"p", NULL, NULL, publicCmdFct, NULL, NULL};
  83. static shellmatta_retCode_t privateCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  84. {
  85. (void) handle;
  86. (void) arguments;
  87. (void) length;
  88. privateTemp ++;
  89. return SHELLMATTA_OK;
  90. }
  91. shellmatta_cmd_t privateCmd = {(char*)"private", (char*)"r", NULL, NULL, privateCmdFct, NULL, NULL};
  92. static shellmatta_retCode_t additionalCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  93. {
  94. (void) handle;
  95. (void) arguments;
  96. (void) length;
  97. return SHELLMATTA_OK;
  98. }
  99. shellmatta_cmd_t additionalCmd = {(char*)"additional", (char*)"a", NULL, NULL, additionalCmdFct, NULL, NULL};
  100. SCENARIO("Check help auth uninitialized") {
  101. GIVEN("An initialized shellmatta instance without initialized auth") {
  102. TEST_SHELLMATTA_SETUP;
  103. WHEN("The help command is called") {
  104. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  105. CHECK(ret == SHELLMATTA_OK);
  106. THEN("The help command prints all commands.") {
  107. char *dummyData = (char*) "help\r\n"
  108. "help ? help [command] - print help or usage information\r\n"
  109. "login li Login command\r\n"
  110. "logout lo Logout command\r\n"
  111. "private r\r\n"
  112. "public p\r\n"
  113. "\r\n"
  114. "shellmatta->";
  115. CHECK(ret == SHELLMATTA_OK);
  116. CHECK(write_length == strlen(dummyData));
  117. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  118. }
  119. }
  120. }
  121. }
  122. SCENARIO("Check auth unauthorized") {
  123. GIVEN("An initialized shellmatta instance with initialized auth") {
  124. TEST_SHELLMATTA_SETUP;
  125. TEST_SHELLMATTA_AUTH_SETUP;
  126. WHEN("The help command is called") {
  127. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  128. CHECK(ret == SHELLMATTA_OK);
  129. THEN("The help command prints only public commands.") {
  130. char *dummyData = (char*) "help\r\n"
  131. "help ? help [command] - print help or usage information\r\n"
  132. "login li Login command\r\n"
  133. "logout lo Logout command\r\n"
  134. "public p\r\n"
  135. "\r\n"
  136. "shellmatta->";
  137. CHECK(write_length == strlen(dummyData));
  138. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  139. }
  140. }
  141. WHEN("The a private function is called") {
  142. ret = shellmatta_processData(handle, (char*)"private\r", 8);
  143. CHECK(ret == SHELLMATTA_OK);
  144. THEN("The command is not executed.") {
  145. char *dummyData = (char*) "private\r\n"
  146. "\r\n"
  147. "Command: private not found\r\n"
  148. "shellmatta->";
  149. CHECK(privateTemp == 0u);
  150. CHECK(write_length == strlen(dummyData));
  151. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  152. }
  153. }
  154. }
  155. }
  156. SCENARIO("Check authorized") {
  157. GIVEN("An initialized shellmatta instance with initialized auth") {
  158. TEST_SHELLMATTA_SETUP;
  159. TEST_SHELLMATTA_AUTH_SETUP;
  160. WHEN("The user shellmatta is logged in") {
  161. ret = shellmatta_auth_login(handle, 1);
  162. CHECK(ret == SHELLMATTA_OK);
  163. AND_WHEN("The help command is called") {
  164. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  165. CHECK(ret == SHELLMATTA_OK);
  166. THEN("The help command prints all commands.") {
  167. char *dummyData = (char*) "help\r\n"
  168. "help ? help [command] - print help or usage information\r\n"
  169. "login li Login command\r\n"
  170. "logout lo Logout command\r\n"
  171. "private r\r\n"
  172. "public p\r\n"
  173. "\r\n"
  174. "shimatta@shellmatta->";
  175. CHECK(ret == SHELLMATTA_OK);
  176. CHECK(write_length == strlen(dummyData));
  177. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  178. AND_THEN("The shimatta user is logged in") {
  179. char usernameBuffer[16] = {0};
  180. uint32_t usernameBufferLength = sizeof(usernameBuffer);
  181. CHECK(1 == shellmatta_auth_getLoggedInUserId(handle));
  182. ret = shellmatta_auth_getLoggedInUserName(handle, usernameBuffer, &usernameBufferLength);
  183. CHECK(ret == SHELLMATTA_OK);
  184. CHECK(usernameBufferLength == strlen("shimatta"));
  185. REQUIRE_THAT(usernameBuffer, Catch::Matchers::Equals("shimatta"));
  186. }
  187. }
  188. AND_WHEN("The user is logged out using the logout command") {
  189. write_length = 0;
  190. memset(write_data, 0, sizeof(write_data));
  191. ret = shellmatta_processData(handle, (char*)"logout\r", 7);
  192. CHECK(ret == SHELLMATTA_OK);
  193. THEN("The Shellmatta prints the logout message") {
  194. char *dummyData = (char*) "logout\r\n"
  195. "good bye\r\n\r\n"
  196. "shellmatta->";
  197. CHECK(write_length == strlen(dummyData));
  198. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  199. }
  200. AND_WHEN("The help command is called") {
  201. write_length = 0;
  202. memset(write_data, 0, sizeof(write_data));
  203. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  204. CHECK(ret == SHELLMATTA_OK);
  205. THEN("The help command prints only public commands.") {
  206. char *dummyData = (char*) "help\r\n"
  207. "help ? help [command] - print help or usage information\r\n"
  208. "login li Login command\r\n"
  209. "logout lo Logout command\r\n"
  210. "public p\r\n"
  211. "\r\n"
  212. "shellmatta->";
  213. CHECK(ret == SHELLMATTA_OK);
  214. CHECK(write_length == strlen(dummyData));
  215. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  216. }
  217. }
  218. }
  219. }
  220. AND_WHEN("The a private function is called") {
  221. ret = shellmatta_processData(handle, (char*)"private\r", 8);
  222. CHECK(ret == SHELLMATTA_OK);
  223. THEN("The command executed.") {
  224. char *dummyData = (char*) "private\r\n"
  225. "\r\n"
  226. "shimatta@shellmatta->";
  227. CHECK(privateTemp == 1u);
  228. CHECK(write_length == strlen(dummyData));
  229. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  230. }
  231. }
  232. }
  233. WHEN("The user not_shellmatta is logged in") {
  234. ret = shellmatta_auth_login(handle, 2);
  235. CHECK(ret == SHELLMATTA_OK);
  236. AND_WHEN("The help command is called") {
  237. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  238. CHECK(ret == SHELLMATTA_OK);
  239. THEN("The help command prints not all commands.") {
  240. char *dummyData = (char*) "help\r\n"
  241. "help ? help [command] - print help or usage information\r\n"
  242. "login li Login command\r\n"
  243. "logout lo Logout command\r\n"
  244. "public p\r\n"
  245. "\r\n"
  246. "not_shimatta@shellmatta->";
  247. CHECK(ret == SHELLMATTA_OK);
  248. CHECK(write_length == strlen(dummyData));
  249. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  250. AND_THEN("The not_shimatta user is logged in") {
  251. char usernameBuffer[16] = {0};
  252. uint32_t usernameBufferLength = sizeof(usernameBuffer);
  253. CHECK(2 == shellmatta_auth_getLoggedInUserId(handle));
  254. ret = shellmatta_auth_getLoggedInUserName(handle, usernameBuffer, &usernameBufferLength);
  255. CHECK(ret == SHELLMATTA_OK);
  256. CHECK(usernameBufferLength == strlen("not_shimatta"));
  257. REQUIRE_THAT(usernameBuffer, Catch::Matchers::Equals("not_shimatta"));
  258. }
  259. }
  260. }
  261. AND_WHEN("The a private function is called") {
  262. ret = shellmatta_processData(handle, (char*)"private\r", 8);
  263. CHECK(ret == SHELLMATTA_OK);
  264. THEN("The command is not executed.") {
  265. char *dummyData = (char*) "private\r\n"
  266. "\r\n"
  267. "Command: private not found\r\n"
  268. "not_shimatta@shellmatta->";
  269. CHECK(privateTemp == 0u);
  270. CHECK(write_length == strlen(dummyData));
  271. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  272. }
  273. }
  274. }
  275. WHEN("The user root is logged in") {
  276. ret = shellmatta_auth_login(handle, 3);
  277. CHECK(ret == SHELLMATTA_OK);
  278. AND_WHEN("The help command is called") {
  279. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  280. CHECK(ret == SHELLMATTA_OK);
  281. THEN("The help command prints all commands.") {
  282. char *dummyData = (char*) "help\r\n"
  283. "help ? help [command] - print help or usage information\r\n"
  284. "login li Login command\r\n"
  285. "logout lo Logout command\r\n"
  286. "private r\r\n"
  287. "public p\r\n"
  288. "\r\n"
  289. "root@shellmatta->";
  290. CHECK(ret == SHELLMATTA_OK);
  291. CHECK(write_length == strlen(dummyData));
  292. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  293. }
  294. }
  295. }
  296. }
  297. }
  298. SCENARIO("Check login command with privileged user") {
  299. GIVEN("An initialized shellmatta instance with initialized auth") {
  300. TEST_SHELLMATTA_SETUP;
  301. TEST_SHELLMATTA_AUTH_SETUP;
  302. WHEN("The user shellmatta logs in interactively using the correct credentials") {
  303. ret = shellmatta_processData(handle, (char*)"login\r\n"
  304. "shimatta\r\n"
  305. "12345678\r", 26);
  306. CHECK(ret == SHELLMATTA_OK);
  307. THEN("The login message is printed - password is hidden") {
  308. char *dummyData = (char*) "login\r\n"
  309. "enter username:\r\n"
  310. "shimatta\r\n"
  311. "enter password:\r\n"
  312. "\r\n"
  313. "login successful\r\n"
  314. "\r\n"
  315. "shimatta@shellmatta->";
  316. CHECK(write_length == strlen(dummyData));
  317. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  318. AND_THEN("The shimatta user is logged in") {
  319. REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
  320. }
  321. }
  322. }
  323. WHEN("The user shellmatta logs in interactively using wrong credentials") {
  324. ret = shellmatta_processData(handle, (char*)"login\r\n"
  325. "shimatta\r\n"
  326. "11111111\r", 26);
  327. CHECK(ret == SHELLMATTA_OK);
  328. THEN("A warning message is printed") {
  329. char *dummyData = (char*) "login\r\n"
  330. "enter username:\r\n"
  331. "shimatta\r\n"
  332. "enter password:\r\n"
  333. "\r\n"
  334. "username or password is wrong\r\n"
  335. "\r\n"
  336. "shellmatta->";
  337. CHECK(write_length == strlen(dummyData));
  338. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  339. AND_THEN("The shimatta user is not logged in") {
  340. REQUIRE(0 == shellmatta_auth_getLoggedInUserId(handle));
  341. }
  342. }
  343. }
  344. WHEN("The user shellmatta logs in interactively manipulating the input") {
  345. ret = shellmatta_processData(handle, (char*)"login\r"
  346. "shimg\batta\r"
  347. "1h" "\x7f" "2346\033[D5\033[C78\b8\r", 36);
  348. CHECK(ret == SHELLMATTA_OK);
  349. THEN("The login message is printed - password is hidden") {
  350. char *dummyData = (char*) "login\r\n"
  351. "enter username:\r\n"
  352. "shimg\033[1D\033[K\033[s\033[uatta\r\n"
  353. "enter password:\r\n"
  354. "\r\n"
  355. "login successful\r\n"
  356. "\r\n"
  357. "shimatta@shellmatta->";
  358. CHECK(write_length == strlen(dummyData));
  359. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  360. AND_THEN("The shimatta user is logged in") {
  361. REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
  362. }
  363. }
  364. }
  365. WHEN("The user shellmatta logs in passing the credentials none interactive") {
  366. ret = shellmatta_processData(handle, (char*)"login -u shimatta -p 12345678\r", 30);
  367. CHECK(ret == SHELLMATTA_OK);
  368. THEN("The login message is printed") {
  369. char *dummyData = (char*) "login -u shimatta -p 12345678\r\n"
  370. "login successful\r\n"
  371. "\r\n"
  372. "shimatta@shellmatta->";
  373. CHECK(write_length == strlen(dummyData));
  374. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  375. AND_THEN("The shimatta user is logged in") {
  376. REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
  377. }
  378. }
  379. }
  380. WHEN("The user shellmatta logs in passing the credentials half interactive") {
  381. ret = shellmatta_processData(handle, (char*)"login -u shimatta\r12345678\r", 27);
  382. CHECK(ret == SHELLMATTA_OK);
  383. THEN("The login message is printed - password is hidden") {
  384. char *dummyData = (char*) "login -u shimatta\r\n\r\n"
  385. "enter password:\r\n"
  386. "\r\n"
  387. "login successful\r\n"
  388. "\r\n"
  389. "shimatta@shellmatta->";
  390. CHECK(write_length == strlen(dummyData));
  391. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  392. AND_THEN("The shimatta user is logged in") {
  393. REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
  394. }
  395. }
  396. }
  397. WHEN("The user shellmatta tries to login non interactive without username") {
  398. ret = shellmatta_processData(handle, (char*)"login -p 12345678\r", 18);
  399. CHECK(ret == SHELLMATTA_OK);
  400. THEN("A warning message is printed") {
  401. char *dummyData = (char*) "login -p 12345678\r\n"
  402. "Missing username\r\n"
  403. "\r\n"
  404. "shellmatta->";
  405. CHECK(write_length == strlen(dummyData));
  406. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  407. AND_THEN("The shimatta user is not logged in") {
  408. REQUIRE(0 == shellmatta_auth_getLoggedInUserId(handle));
  409. }
  410. }
  411. }
  412. WHEN("The user shellmatta tries to login using the wrong options") {
  413. ret = shellmatta_processData(handle, (char*)"login -o meow\r", 14);
  414. CHECK(ret == SHELLMATTA_OK);
  415. THEN("A warning message is printed") {
  416. char *dummyData = (char*) "login -o meow\r\n"
  417. "Unknown option\r\n"
  418. "\r\n"
  419. "shellmatta->";
  420. CHECK(write_length == strlen(dummyData));
  421. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  422. AND_THEN("The shimatta user is not logged in") {
  423. REQUIRE(0 == shellmatta_auth_getLoggedInUserId(handle));
  424. }
  425. }
  426. }
  427. }
  428. }
  429. SCENARIO("Check login command with unprivileged user") {
  430. GIVEN("An initialized shellmatta instance with initialized auth") {
  431. TEST_SHELLMATTA_SETUP;
  432. TEST_SHELLMATTA_AUTH_SETUP;
  433. WHEN("The user not_shimatta logs in interactively using the correct credentials") {
  434. ret = shellmatta_processData(handle, (char*)"login\r"
  435. "not_shimatta\r"
  436. "87654321\r", 28);
  437. CHECK(ret == SHELLMATTA_OK);
  438. THEN("The login message is printed - password is hidden") {
  439. char *dummyData = (char*) "login\r\n"
  440. "enter username:\r\n"
  441. "not_shimatta\r\n"
  442. "enter password:\r\n"
  443. "\r\n"
  444. "login successful\r\n"
  445. "\r\n"
  446. "not_shimatta@shellmatta->";
  447. CHECK(write_length == strlen(dummyData));
  448. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  449. AND_THEN("The not_shimatta user is logged in") {
  450. REQUIRE(2 == shellmatta_auth_getLoggedInUserId(handle));
  451. }
  452. }
  453. }
  454. }
  455. }
  456. SCENARIO("Check adding commands after the authentication is initialized") {
  457. GIVEN("An initialized shellmatta instance with initialized auth") {
  458. TEST_SHELLMATTA_SETUP;
  459. TEST_SHELLMATTA_AUTH_SETUP;
  460. WHEN("A command is added") {
  461. ret = shellmatta_addCmd(handle, &additionalCmd);
  462. CHECK(ret == SHELLMATTA_OK);
  463. AND_WHEN("The help command is called") {
  464. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  465. CHECK(ret == SHELLMATTA_OK);
  466. THEN("The additional command is not shown as it requires login") {
  467. char *dummyData = (char*) "help\r\n"
  468. "help ? help [command] - print help or usage information\r\n"
  469. "login li Login command\r\n"
  470. "logout lo Logout command\r\n"
  471. "public p\r\n"
  472. "\r\n"
  473. "shellmatta->";
  474. CHECK(write_length == strlen(dummyData));
  475. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  476. AND_WHEN("The rot user is logged in") {
  477. write_length = 0;
  478. memset(write_data, 0, sizeof(write_data));
  479. ret = shellmatta_auth_login(handle, 3);
  480. CHECK(ret == SHELLMATTA_OK);
  481. AND_WHEN("The help command is called") {
  482. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  483. CHECK(ret == SHELLMATTA_OK);
  484. THEN("all commands are shown") {
  485. char *dummyData = (char*) "help\r\n"
  486. "additional a\r\n"
  487. "help ? help [command] - print help or usage information\r\n"
  488. "login li Login command\r\n"
  489. "logout lo Logout command\r\n"
  490. "private r\r\n"
  491. "public p\r\n"
  492. "\r\n"
  493. "root@shellmatta->";
  494. CHECK(write_length == strlen(dummyData));
  495. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  496. }
  497. }
  498. }
  499. }
  500. }
  501. }
  502. }
  503. }
  504. shellmatta_retCode_t customLogin(const uint32_t userId, const char *password)
  505. {
  506. if ((userId == 1) && (0 == strcmp(password, "12345678")))
  507. {
  508. return SHELLMATTA_OK;
  509. }
  510. else if ((userId == 2) && (0 == strcmp(password, "87654321")))
  511. {
  512. return SHELLMATTA_OK;
  513. }
  514. return SHELLMATTA_ERROR;
  515. }
  516. void logFct(const uint32_t userId, bool success)
  517. {
  518. userIdTemp = userId;
  519. successTemp = success;
  520. }
  521. SCENARIO("Check custom login") {
  522. GIVEN("An initialized shellmatta instance with initialized auth with custom login and log") {
  523. TEST_SHELLMATTA_SETUP;
  524. shellmatta_auth_user_t userList[] = {
  525. {1, false, "shimatta", "12345678"},
  526. {2, false, "not_shimatta", "87654321"}
  527. };
  528. uint32_t privateCmdPerms[] = {1};
  529. shellmatta_auth_perm_t permList[] = {
  530. {"private", privateCmdPerms, sizeof(privateCmdPerms)/sizeof(privateCmdPerms[0])}
  531. };
  532. shellmatta_auth_init(handle, userList, 2, permList, 1, false, customLogin, logFct);
  533. WHEN("The user shimatta logs in interactively using the correct credentials") {
  534. ret = shellmatta_processData(handle, (char*)"login\r"
  535. "shimatta\r"
  536. "12345678\r", 24);
  537. CHECK(ret == SHELLMATTA_OK);
  538. THEN("The login message is printed - password is hidden") {
  539. char *dummyData = (char*) "login\r\n"
  540. "enter username:\r\n"
  541. "shimatta\r\n"
  542. "enter password:\r\n"
  543. "\r\n"
  544. "login successful\r\n"
  545. "\r\n"
  546. "shimatta@shellmatta->";
  547. CHECK(write_length == strlen(dummyData));
  548. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  549. AND_THEN("The shimatta user is logged in") {
  550. REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
  551. }
  552. AND_THEN("The login event is logged") {
  553. CHECK(1 == userIdTemp);
  554. REQUIRE(true == successTemp);
  555. }
  556. }
  557. }
  558. WHEN("The user not_shimatta logs in interactively using the wrong credentials") {
  559. ret = shellmatta_processData(handle, (char*)"login\r"
  560. "shimatta\r"
  561. "12345679\r", 24);
  562. CHECK(ret == SHELLMATTA_OK);
  563. THEN("Login error message is printed") {
  564. char *dummyData = (char*) "login\r\n"
  565. "enter username:\r\n"
  566. "shimatta\r\n"
  567. "enter password:\r\n"
  568. "\r\n"
  569. "username or password is wrong\r\n"
  570. "\r\n"
  571. "shellmatta->";
  572. CHECK(write_length == strlen(dummyData));
  573. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  574. AND_THEN("The shimatta user is not logged in") {
  575. REQUIRE(0 == shellmatta_auth_getLoggedInUserId(handle));
  576. }
  577. AND_THEN("The failed login event is logged") {
  578. CHECK(1 == userIdTemp);
  579. REQUIRE(false == successTemp);
  580. }
  581. }
  582. }
  583. }
  584. }
  585. SCENARIO("Check custom login with custom login function") {
  586. GIVEN("An initialized shellmatta instance with initialized auth with custom login and log") {
  587. TEST_SHELLMATTA_SETUP;
  588. shellmatta_auth_user_t userList[] = {
  589. {1, false, "shimatta", "12345678"},
  590. {2, false, "not_shimatta", "87654321"}
  591. };
  592. uint32_t privateCmdPerms[] = {1};
  593. shellmatta_auth_perm_t permList[] = {
  594. {"private", privateCmdPerms, sizeof(privateCmdPerms)/sizeof(privateCmdPerms[0])}
  595. };
  596. shellmatta_auth_init(handle, userList, 2, permList, 1, true, customLogin, logFct);
  597. WHEN("The help command is called") {
  598. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  599. CHECK(ret == SHELLMATTA_OK);
  600. THEN("There is no login command") {
  601. char *dummyData = (char*) "help\r\n"
  602. "help ? help [command] - print help or usage information\r\n"
  603. "logout lo Logout command\r\n"
  604. "public p\r\n"
  605. "\r\n"
  606. "shellmatta->";
  607. CHECK(write_length == strlen(dummyData));
  608. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  609. }
  610. }
  611. }
  612. }
  613. SCENARIO("Check if passwords can be changed") {
  614. GIVEN("An initialized shellmatta instance with initialized auth") {
  615. TEST_SHELLMATTA_SETUP;
  616. TEST_SHELLMATTA_AUTH_SETUP;
  617. WHEN("The password of the shellmatta user is changed") {
  618. ret = shellmatta_auth_chpasswd(handle, "shimatta", "new_password");
  619. CHECK(ret == SHELLMATTA_OK);
  620. AND_WHEN("The user shellmatta logs in passing the new credentials") {
  621. ret = shellmatta_processData(handle, (char*)"login -u shimatta -p new_password\r", 34);
  622. CHECK(ret == SHELLMATTA_OK);
  623. THEN("The shimatta user is logged in") {
  624. REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
  625. }
  626. }
  627. AND_WHEN("The user shellmatta logs in passing the old credentials") {
  628. ret = shellmatta_processData(handle, (char*)"login -u shimatta -p 12345678\r", 30);
  629. CHECK(ret == SHELLMATTA_OK);
  630. THEN("The shimatta user is not logged in") {
  631. REQUIRE(0 == shellmatta_auth_getLoggedInUserId(handle));
  632. }
  633. }
  634. }
  635. }
  636. }
  637. SCENARIO("User functions with wrong parameters") {
  638. GIVEN("An initialized shellmatta instance with initialized auth") {
  639. TEST_SHELLMATTA_SETUP;
  640. TEST_SHELLMATTA_AUTH_SETUP;
  641. WHEN("Trying to log in user 0") {
  642. ret = shellmatta_auth_login(handle, 0);
  643. THEN("Shellmatta returns SHELLMATTA_ERROR") {
  644. CHECK(ret == SHELLMATTA_ERROR);
  645. AND_THEN("No user is logged in") {
  646. REQUIRE(0 == shellmatta_auth_getLoggedInUserId(handle));
  647. }
  648. }
  649. }
  650. }
  651. }