test_integration_auth.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  22. {
  23. write_callCnt ++;
  24. while((length > 0) && (write_length < sizeof(write_data)))
  25. {
  26. write_data[write_length] = *data;
  27. data ++;
  28. length --;
  29. write_length ++;
  30. }
  31. return SHELLMATTA_OK;
  32. }
  33. static shellmatta_retCode_t publicCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  34. {
  35. (void) handle;
  36. (void) arguments;
  37. (void) length;
  38. shellmatta_retCode_t ret = SHELLMATTA_OK;
  39. return ret;
  40. }
  41. shellmatta_cmd_t publicCmd = {(char*)"public", (char*)"p", NULL, NULL, publicCmdFct, NULL, NULL};
  42. static shellmatta_retCode_t privateCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  43. {
  44. (void) handle;
  45. (void) arguments;
  46. (void) length;
  47. return SHELLMATTA_OK;
  48. }
  49. shellmatta_cmd_t privateCmd = {(char*)"private", (char*)"r", NULL, NULL, privateCmdFct, NULL, NULL};
  50. TEST_CASE( "check unauthenticated help" ) {
  51. shellmatta_retCode_t ret;
  52. shellmatta_instance_t inst;
  53. shellmatta_handle_t handle;
  54. char buffer[1024] = {0};
  55. char historyBuffer[1024] = {0};
  56. char *dummyData = (char*) "help\r\n"
  57. "help ? help [command] - print help or usage information\r\n"
  58. "login li Login command\r\n"
  59. "logout lo Logout command\r\n"
  60. "private r\r\n"
  61. "public p\r\n"
  62. "\r\n"
  63. "shellmatta->";
  64. shellmatta_doInit( &inst,
  65. &handle,
  66. buffer,
  67. sizeof(buffer),
  68. historyBuffer,
  69. sizeof(historyBuffer),
  70. "shellmatta->",
  71. NULL,
  72. writeFct);
  73. shellmatta_auth_init(handle, NULL, 0, NULL, 0, false, NULL, NULL);
  74. write_callCnt = 0u;
  75. memset(write_data, 0, sizeof(write_data));
  76. write_length = 0u;
  77. shellmatta_addCmd(handle, &publicCmd);
  78. shellmatta_addCmd(handle, &privateCmd);
  79. ret = shellmatta_processData(handle, (char*)"help\r", 5);
  80. CHECK(ret == SHELLMATTA_OK);
  81. CHECK(write_length == strlen(dummyData));
  82. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  83. }