test_integration_auth.cpp 36 KB

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