test_integration_auth.cpp 35 KB

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