test_integration_auth.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #define TEST_SHELLMATTA_SETUP shellmatta_retCode_t ret; \
  22. shellmatta_instance_t inst; \
  23. shellmatta_handle_t handle; \
  24. char buffer[1024] = {0}; \
  25. char historyBuffer[1024] = {0}; \
  26. \
  27. shellmatta_doInit( &inst, \
  28. &handle, \
  29. buffer, \
  30. sizeof(buffer), \
  31. historyBuffer, \
  32. sizeof(historyBuffer), \
  33. "shellmatta->", \
  34. NULL, \
  35. writeFct); \
  36. \
  37. write_callCnt = 0u; \
  38. memset(write_data, 0, sizeof(write_data)); \
  39. write_length = 0u; \
  40. \
  41. shellmatta_addCmd(handle, &publicCmd); \
  42. shellmatta_addCmd(handle, &privateCmd);
  43. #define TEST_SHELLMATTA_AUTH_SETUP shellmatta_auth_user_t userList[] = { \
  44. {1, "shimatta", "12345678"}, \
  45. {2, "not_shimatta", "87654321"} \
  46. }; \
  47. \
  48. uint32_t privateCmdPerms[] = {1}; \
  49. shellmatta_auth_perm_t permList[] = { \
  50. {"private", privateCmdPerms, sizeof(privateCmdPerms)/sizeof(privateCmdPerms[0])} \
  51. }; \
  52. \
  53. shellmatta_auth_init(handle, userList, 1, permList, 1, false, NULL, NULL);
  54. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  55. {
  56. write_callCnt ++;
  57. while((length > 0) && (write_length < sizeof(write_data)))
  58. {
  59. write_data[write_length] = *data;
  60. data ++;
  61. length --;
  62. write_length ++;
  63. }
  64. return SHELLMATTA_OK;
  65. }
  66. static shellmatta_retCode_t publicCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  67. {
  68. (void) handle;
  69. (void) arguments;
  70. (void) length;
  71. shellmatta_retCode_t ret = SHELLMATTA_OK;
  72. return ret;
  73. }
  74. shellmatta_cmd_t publicCmd = {(char*)"public", (char*)"p", NULL, NULL, publicCmdFct, NULL, NULL};
  75. static shellmatta_retCode_t privateCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  76. {
  77. (void) handle;
  78. (void) arguments;
  79. (void) length;
  80. return SHELLMATTA_OK;
  81. }
  82. shellmatta_cmd_t privateCmd = {(char*)"private", (char*)"r", NULL, NULL, privateCmdFct, NULL, NULL};
  83. SCENARIO("Check help auth uninitialized") {
  84. GIVEN("An initialized shellmatta instance without initialized auth") {
  85. TEST_SHELLMATTA_SETUP;
  86. WHEN("The help command is called") {
  87. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  88. CHECK(ret == SHELLMATTA_OK);
  89. THEN("The help command prints all commands.") {
  90. char *dummyData = (char*) "help\r\n"
  91. "help ? help [command] - print help or usage information\r\n"
  92. "login li Login command\r\n"
  93. "logout lo Logout command\r\n"
  94. "private r\r\n"
  95. "public p\r\n"
  96. "\r\n"
  97. "shellmatta->";
  98. CHECK(ret == SHELLMATTA_OK);
  99. CHECK(write_length == strlen(dummyData));
  100. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  101. }
  102. }
  103. }
  104. }
  105. SCENARIO("Check help auth unauthorized") {
  106. GIVEN("An initialized shellmatta instance with initialized auth") {
  107. TEST_SHELLMATTA_SETUP;
  108. TEST_SHELLMATTA_AUTH_SETUP;
  109. WHEN("The help command is called") {
  110. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  111. CHECK(ret == SHELLMATTA_OK);
  112. THEN("The help command prints only public commands.") {
  113. char *dummyData = (char*) "help\r\n"
  114. "help ? help [command] - print help or usage information\r\n"
  115. "login li Login command\r\n"
  116. "logout lo Logout command\r\n"
  117. "public p\r\n"
  118. "\r\n"
  119. "shellmatta->";
  120. CHECK(ret == SHELLMATTA_OK);
  121. CHECK(write_length == strlen(dummyData));
  122. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  123. }
  124. }
  125. }
  126. }
  127. SCENARIO("Check help authorized") {
  128. GIVEN("An initialized shellmatta instance with initialized auth") {
  129. TEST_SHELLMATTA_SETUP;
  130. TEST_SHELLMATTA_AUTH_SETUP;
  131. WHEN("The user shellmatta is logged in") {
  132. ret = shellmatta_auth_login(handle, 1);
  133. CHECK(ret == SHELLMATTA_OK);
  134. AND_WHEN("The help command is called") {
  135. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  136. CHECK(ret == SHELLMATTA_OK);
  137. THEN("The help command prints all commands.") {
  138. char *dummyData = (char*) "help\r\n"
  139. "help ? help [command] - print help or usage information\r\n"
  140. "login li Login command\r\n"
  141. "logout lo Logout command\r\n"
  142. "private r\r\n"
  143. "public p\r\n"
  144. "\r\n"
  145. "shimatta@shellmatta->";
  146. CHECK(ret == SHELLMATTA_OK);
  147. CHECK(write_length == strlen(dummyData));
  148. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  149. }
  150. AND_WHEN("The user is logged out using the logout command") {
  151. write_length = 0;
  152. memset(write_data, 0, sizeof(write_data));
  153. ret = shellmatta_processData(handle, (char*)"logout\r", 7);
  154. CHECK(ret == SHELLMATTA_OK);
  155. THEN("The Shellmatta prints the logout message") {
  156. char *dummyData = (char*) "logout\r\n" \
  157. "good bye\r\n\r\n" \
  158. "shellmatta->";
  159. CHECK(write_length == strlen(dummyData));
  160. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  161. }
  162. AND_WHEN("The help command is called") {
  163. write_length = 0;
  164. memset(write_data, 0, sizeof(write_data));
  165. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  166. CHECK(ret == SHELLMATTA_OK);
  167. THEN("The help command prints only public commands.") {
  168. char *dummyData = (char*) "help\r\n"
  169. "help ? help [command] - print help or usage information\r\n"
  170. "login li Login command\r\n"
  171. "logout lo Logout command\r\n"
  172. "public p\r\n"
  173. "\r\n"
  174. "shellmatta->";
  175. CHECK(ret == SHELLMATTA_OK);
  176. CHECK(write_length == strlen(dummyData));
  177. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }