Ver código fonte

fixed logout with more than one character being passed - fixed logout function not clearing history buffer

stefan 9 meses atrás
pai
commit
66624e0a50

+ 4 - 3
src/shellmatta_auth.c

@@ -280,9 +280,7 @@ const shellmatta_cmd_t shellmatta_auth_loginCmd = {"login"
  */
 static shellmatta_retCode_t logoutCmdFct(const shellmatta_handle_t handle, const char *arguments, uint32_t length)
 {
-    /** -# reset shell and with it the history buffer */
-    (void)shellmatta_resetShell(handle, false);
-
+    shellmatta_auth_logout(handle);
     shellmatta_write(handle, "good bye\r\n", 10);
 
     (void)arguments;
@@ -441,6 +439,9 @@ shellmatta_retCode_t shellmatta_auth_logout(shellmatta_handle_t handle)
     {
         inst->userId = 0u;
         inst->userPointer = NULL;
+
+        /** -# clear the history buffer */
+        (void)history_clear(handle);
     }
     else
     {

+ 14 - 1
src/shellmatta_history.c

@@ -16,7 +16,7 @@
  * @addtogroup shellmatta_history
  * @{
  */
-
+#include <string.h>
 #include "shellmatta_history.h"
 #include "shellmatta.h"
 #include "shellmatta_utils.h"
@@ -286,6 +286,19 @@ void history_reset(shellmatta_instance_t *inst)
     inst->historyReadUp = true;
 }
 
+/**
+ * @brief       clears the history buffer
+ * @param[in]   inst    pointer to a shellmatta instance
+ */
+void history_clear(shellmatta_instance_t *inst)
+{
+    inst->historyStart      = 0u;
+    inst->historyEnd        = 0u;
+    inst->historyRead       = 0u;
+    inst->historyReadUp     = true;
+    memset(inst->historyBuffer, 0, inst->historyBufferSize);
+}
+
 /**
  * @}
  */

+ 1 - 0
src/shellmatta_history.h

@@ -26,6 +26,7 @@ bool history_navigate(shellmatta_instance_t *inst, int32_t cnt);
 void history_storeCmd(shellmatta_instance_t *inst);
 void history_restoreCmd(shellmatta_instance_t *inst);
 void history_reset(shellmatta_instance_t *inst);
+void history_clear(shellmatta_instance_t *inst);
 
 #endif
 

+ 149 - 21
test/integrationtest_auth/test_integration_auth.cpp

@@ -22,6 +22,41 @@ static uint32_t write_callCnt = 0u;
 static char write_data[1024];
 static uint32_t write_length;
 
+#define TEST_SHELLMATTA_SETUP   shellmatta_retCode_t ret;                   \
+                                shellmatta_instance_t inst;                 \
+                                shellmatta_handle_t handle;                 \
+                                char buffer[1024] = {0};                    \
+                                char historyBuffer[1024] = {0};             \
+                                                                            \
+                                shellmatta_doInit(  &inst,                  \
+                                                    &handle,                \
+                                                    buffer,                 \
+                                                    sizeof(buffer),         \
+                                                    historyBuffer,          \
+                                                    sizeof(historyBuffer),  \
+                                                    "shellmatta->",         \
+                                                    NULL,                   \
+                                                    writeFct);              \
+                                                                            \
+                                write_callCnt = 0u;                         \
+                                memset(write_data, 0, sizeof(write_data));  \
+                                write_length = 0u;                          \
+                                                                            \
+                                shellmatta_addCmd(handle, &publicCmd);      \
+                                shellmatta_addCmd(handle, &privateCmd);
+
+#define TEST_SHELLMATTA_AUTH_SETUP  shellmatta_auth_user_t userList[] = {                                                   \
+                                        {1, "shimatta", "12345678"},                                                        \
+                                        {2, "not_shimatta", "87654321"}                                                     \
+                                    };                                                                                      \
+                                                                                                                            \
+                                    uint32_t privateCmdPerms[] = {1};                                                       \
+                                    shellmatta_auth_perm_t permList[] = {                                                   \
+                                        {"private", privateCmdPerms, sizeof(privateCmdPerms)/sizeof(privateCmdPerms[0])}    \
+                                    };                                                                                      \
+                                                                                                                            \
+                                    shellmatta_auth_init(handle, userList, 1, permList, 1, false, NULL, NULL);
+
 static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
 {
     write_callCnt ++;
@@ -58,13 +93,18 @@ static shellmatta_retCode_t privateCmdFct(shellmatta_handle_t handle, const char
 }
 shellmatta_cmd_t privateCmd = {(char*)"private", (char*)"r", NULL, NULL, privateCmdFct, NULL, NULL};
 
-TEST_CASE( "check unauthenticated help" ) {
+SCENARIO("Check help auth uninitialized") {
+    GIVEN("An initialized shellmatta instance without initialized auth") {
+
+        TEST_SHELLMATTA_SETUP;
+
+        WHEN("The help command is called") {
+
+            ret = shellmatta_processData(handle, (char*)"help\r", 5);
+            CHECK(ret == SHELLMATTA_OK);
+
+            THEN("The help command prints all commands.") {
 
-    shellmatta_retCode_t ret;
-    shellmatta_instance_t inst;
-    shellmatta_handle_t handle;
-    char buffer[1024] = {0};
-    char historyBuffer[1024] = {0};
     char *dummyData =   (char*) "help\r\n"
                                 "help     ?   help [command] - print help or usage information\r\n"
                                 "login    li  Login command\r\n"
@@ -74,28 +114,116 @@ TEST_CASE( "check unauthenticated help" ) {
                                 "\r\n"
                                 "shellmatta->";
 
-    shellmatta_doInit(  &inst,
-                        &handle,
-                        buffer,
-                        sizeof(buffer),
-                        historyBuffer,
-                        sizeof(historyBuffer),
-                        "shellmatta->",
-                        NULL,
-                        writeFct);
+                CHECK(ret == SHELLMATTA_OK);
+                CHECK(write_length == strlen(dummyData));
+                REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
+            }
+        }
+    }
+}
+
+SCENARIO("Check help auth unauthorized") {
+    GIVEN("An initialized shellmatta instance with initialized auth") {
+
+        TEST_SHELLMATTA_SETUP;
+
+        TEST_SHELLMATTA_AUTH_SETUP;
+
+        WHEN("The help command is called") {
+
+            ret = shellmatta_processData(handle, (char*)"help\r", 5);
+            CHECK(ret == SHELLMATTA_OK);
+
+            THEN("The help command prints only public commands.") {
 
-    shellmatta_auth_init(handle, NULL, 0, NULL, 0, false, NULL, NULL);
+                char *dummyData =   (char*) "help\r\n"
+                                            "help    ?   help [command] - print help or usage information\r\n"
+                                            "login   li  Login command\r\n"
+                                            "logout  lo  Logout command\r\n"
+                                            "public  p\r\n"
+                                            "\r\n"
+                                            "shellmatta->";
 
-    write_callCnt = 0u;
-    memset(write_data, 0, sizeof(write_data));
-    write_length = 0u;
+                CHECK(ret == SHELLMATTA_OK);
+                CHECK(write_length == strlen(dummyData));
+                REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
+            }
+        }
+    }
+}
+
+SCENARIO("Check help authorized") {
+    GIVEN("An initialized shellmatta instance with initialized auth") {
+
+        TEST_SHELLMATTA_SETUP;
+
+        TEST_SHELLMATTA_AUTH_SETUP;
+
+        WHEN("The user shellmatta is logged in") {
+
+            ret = shellmatta_auth_login(handle, 1);
+            CHECK(ret == SHELLMATTA_OK);
 
-    shellmatta_addCmd(handle, &publicCmd);
-    shellmatta_addCmd(handle, &privateCmd);
+            AND_WHEN("The help command is called") {
 
     ret = shellmatta_processData(handle, (char*)"help\r", 5);
+                CHECK(ret == SHELLMATTA_OK);
+
+                THEN("The help command prints all commands.") {
+
+                    char *dummyData =   (char*) "help\r\n"
+                                                "help     ?   help [command] - print help or usage information\r\n"
+                                                "login    li  Login command\r\n"
+                                                "logout   lo  Logout command\r\n"
+                                                "private  r\r\n"
+                                                "public   p\r\n"
+                                                "\r\n"
+                                                "shimatta@shellmatta->";
 
     CHECK(ret == SHELLMATTA_OK);
     CHECK(write_length == strlen(dummyData));
     REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
 }
+
+                AND_WHEN("The user is logged out using the logout command") {
+                    write_length = 0;
+                    memset(write_data, 0, sizeof(write_data));
+                    ret = shellmatta_processData(handle, (char*)"logout\r", 7);
+                    CHECK(ret == SHELLMATTA_OK);
+
+                    THEN("The Shellmatta prints the logout message") {
+                        char *dummyData =   (char*) "logout\r\n"    \
+                                                    "good bye\r\n\r\n"  \
+                                                    "shellmatta->";
+                        CHECK(write_length == strlen(dummyData));
+                        REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
+                    }
+
+                    AND_WHEN("The help command is called") {
+
+                        write_length = 0;
+                        memset(write_data, 0, sizeof(write_data));
+                        ret = shellmatta_processData(handle, (char*)"help\r", 5);
+                        CHECK(ret == SHELLMATTA_OK);
+
+                        THEN("The help command prints only public commands.") {
+
+                            char *dummyData =   (char*) "help\r\n"
+                                                        "help    ?   help [command] - print help or usage information\r\n"
+                                                        "login   li  Login command\r\n"
+                                                        "logout  lo  Logout command\r\n"
+                                                        "public  p\r\n"
+                                                        "\r\n"
+                                                        "shellmatta->";
+
+                            CHECK(ret == SHELLMATTA_OK);
+                            CHECK(write_length == strlen(dummyData));
+                            REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+