test_integration_auth.cpp 31 KB

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