test_integration_auth.cpp 35 KB

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