Browse Source

removed hide mode from insert char method again and temporarily disabling echo to achieve hide mode
The hide mode ignored escape and backspace inputs

stefan 9 tháng trước cách đây
mục cha
commit
75c4167e27

+ 4 - 4
src/shellmatta.c

@@ -132,7 +132,7 @@ shellmatta_retCode_t shellmatta_doInit(
  * It resets all internal states - the buffers are left as they are - they will be overwritten.
  * The history buffer is deleted as well.
  */
-shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle, bool printPrompt)
+shellmatta_retCode_t shellmatta_resetShell(shellmatta_handle_t handle, bool printPrompt)
 {
     shellmatta_instance_t *inst = (shellmatta_instance_t *)handle;
     shellmatta_retCode_t ret = SHELLMATTA_OK;
@@ -509,7 +509,7 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t     handle,
                             inst->hereLength = inst->inputCount - inst->hereDelimiterIdx;
 
                             inst->dirty = true;
-                            utils_insertChars(inst, &data[inst->byteCounter], 1u, false);
+                            utils_insertChars(inst, &data[inst->byteCounter], 1u);
                             inst->lastNewlineIdx = inst->inputCount;
                         }
                         else
@@ -582,7 +582,7 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t     handle,
                     {
                         /** -# the party goes on - just print the delimiter and store the position */
                         inst->lastNewlineIdx = inst->inputCount;
-                        utils_insertChars(inst, &data[inst->byteCounter], 1u, false);
+                        utils_insertChars(inst, &data[inst->byteCounter], 1u);
                     }
                 }
 
@@ -704,7 +704,7 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t     handle,
             else
             {
                 inst->dirty = true;
-                utils_insertChars(inst, &data[inst->byteCounter], 1u, false);
+                utils_insertChars(inst, &data[inst->byteCounter], 1u);
             }
 
             /** -# reset tab counter on not a tab */

+ 8 - 1
src/shellmatta_auth.c

@@ -37,6 +37,10 @@
 shellmatta_retCode_t inputWrapper(shellmatta_instance_t *inst, char data, bool hide)
 {
     shellmatta_retCode_t ret = SHELLMATTA_BUSY;
+    bool echoEnabledBackup = inst->echoEnabled;
+
+    /** -# disable echo when hidden */
+    inst->echoEnabled &= !hide;
 
     /** -# handle escape sequences */
     if(inst->escapeCounter != 0u)
@@ -66,8 +70,11 @@ shellmatta_retCode_t inputWrapper(shellmatta_instance_t *inst, char data, bool h
     }
     else
     {
-        utils_insertChars(inst, &data, 1u, hide);
+        utils_insertChars(inst, &data, 1u);
     }
+
+    inst->echoEnabled = echoEnabledBackup;
+
     return ret;
 }
 

+ 1 - 1
src/shellmatta_autocomplete.c

@@ -182,7 +182,7 @@ void autocomplete_run(shellmatta_instance_t *inst)
             sizeDiff = minLen - inst->cursor;
 
             /** -# copy the found part into the buffer and display it */
-            utils_insertChars(inst, &(tempCmd[inst->cursor]), sizeDiff, false);
+            utils_insertChars(inst, &(tempCmd[inst->cursor]), sizeDiff);
 
             /** -# on exact match there is no need to double Tab to display all */
             if(true == exactMatch)

+ 13 - 21
src/shellmatta_utils.c

@@ -165,12 +165,10 @@ void utils_forwardCursor(shellmatta_instance_t *inst, uint32_t length)
  * @param[in]   inst    pointer to shellmatta instance
  * @param[in]   data    pointer to the data to be inserted
  * @param[in]   length  length of the data to be inserted
- * @param[in]   hide    true: only manipulate buffer do not print anything
  */
 void utils_insertChars( shellmatta_instance_t   *inst,
                         char                    *data,
-                        uint32_t                 length,
-                        bool                     hide)
+                        uint32_t                 length)
 {
     uint32_t tmpLength = length;
     /** -# limit the length to the space left in the buffer */
@@ -195,29 +193,23 @@ void utils_insertChars( shellmatta_instance_t   *inst,
             /** -# store and print the new chars */
             memcpy(&(inst->buffer[inst->cursor]), data, tmpLength);
 
-            if (false == hide)
-            {
-                utils_writeEcho(inst, data, tmpLength);
-
-                /** -# print the other chars and restore the cursor to this position */
-                utils_eraseLine(inst);
-                utils_saveCursorPos(inst);
-                utils_writeEcho(  inst,
-                            &(inst->buffer[inst->cursor + tmpLength]),
-                            inst->inputCount - inst->cursor);
-                utils_restoreCursorPos(inst);
-            }
-                inst->cursor        += tmpLength;
-                inst->inputCount    += tmpLength;
+            utils_writeEcho(inst, data, tmpLength);
+
+            /** -# print the other chars and restore the cursor to this position */
+            utils_eraseLine(inst);
+            utils_saveCursorPos(inst);
+            utils_writeEcho(  inst,
+                        &(inst->buffer[inst->cursor + tmpLength]),
+                        inst->inputCount - inst->cursor);
+            utils_restoreCursorPos(inst);
+            inst->cursor        += tmpLength;
+            inst->inputCount    += tmpLength;
         }
         /** -# overwrite - if the cursor reaches the end of the input it is pushed further */
         else
         {
             memcpy(&(inst->buffer[inst->cursor]), data, tmpLength);
-            if (false == hide)
-            {
-                utils_writeEcho(inst, data, tmpLength);
-            }
+            utils_writeEcho(inst, data, tmpLength);
             inst->cursor += tmpLength;
             if(inst->cursor > inst->inputCount)
             {

+ 1 - 2
src/shellmatta_utils.h

@@ -114,8 +114,7 @@ void utils_rewindCursor(shellmatta_instance_t *inst, uint32_t length);
 void utils_forwardCursor(shellmatta_instance_t *inst, uint32_t length);
 void utils_insertChars( shellmatta_instance_t   *inst,
                         char                    *data,
-                        uint32_t                 length,
-                        bool                     hide);
+                        uint32_t                 length);
 void utils_removeChars( shellmatta_instance_t   *inst,
                         uint32_t                 length,
                         bool                     backspace);

+ 91 - 13
test/integrationtest_auth/test_integration_auth.cpp

@@ -105,14 +105,14 @@ SCENARIO("Check help auth uninitialized") {
 
             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"
-                                "shellmatta->";
+                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"
+                                            "shellmatta->";
 
                 CHECK(ret == SHELLMATTA_OK);
                 CHECK(write_length == strlen(dummyData));
@@ -166,7 +166,7 @@ SCENARIO("Check help authorized") {
 
             AND_WHEN("The help command is called") {
 
-    ret = shellmatta_processData(handle, (char*)"help\r", 5);
+                ret = shellmatta_processData(handle, (char*)"help\r", 5);
                 CHECK(ret == SHELLMATTA_OK);
 
                 THEN("The help command prints all commands.") {
@@ -180,10 +180,10 @@ SCENARIO("Check help authorized") {
                                                 "\r\n"
                                                 "shimatta@shellmatta->";
 
-    CHECK(ret == SHELLMATTA_OK);
-    CHECK(write_length == strlen(dummyData));
-    REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
-}
+                    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;
@@ -227,3 +227,81 @@ SCENARIO("Check help authorized") {
     }
 }
 
+SCENARIO("Check login command") {
+    GIVEN("An initialized shellmatta instance with initialized auth") {
+
+        TEST_SHELLMATTA_SETUP;
+
+        TEST_SHELLMATTA_AUTH_SETUP;
+
+        WHEN("The user shellmatta logges in interactively using the correct credentials") {
+
+            ret = shellmatta_processData(handle, (char*)"login\r"   \
+                                                        "shimatta\r"\
+                                                        "12345678\r", 24);
+            CHECK(ret == SHELLMATTA_OK);
+
+            THEN("The login message is printed - password is hidden") {
+                char *dummyData =   (char*) "login\r\n"             \
+                                            "enter username:\r\n"   \
+                                            "shimatta\r\n"          \
+                                            "enter password:\r\n"   \
+                                            "\r\n"                  \
+                                            "login successful\r\n"  \
+                                            "\r\n"                  \
+                                            "shimatta@shellmatta->";
+                CHECK(write_length == strlen(dummyData));
+                REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
+
+                AND_THEN("The shimatta user is logged in") {
+                    REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
+                }
+            }
+        }
+
+        WHEN("The user shellmatta logges in interactively manipulating the input") {
+
+            ret = shellmatta_processData(handle, (char*)"login\r"           \
+                                                        "shimg\batta\r"     \
+                                                        "12346\033[D5\033[C78\r", 32);
+            CHECK(ret == SHELLMATTA_OK);
+
+            THEN("The login message is printed - password is hidden") {
+                char *dummyData =   (char*) "login\r\n"                                 \
+                                            "enter username:\r\n"                       \
+                                            "shimg\033[1D\033[K\033[s\033[uatta\r\n"    \
+                                            "enter password:\r\n"                       \
+                                            "\r\n"                                      \
+                                            "login successful\r\n"                      \
+                                            "\r\n"                                      \
+                                            "shimatta@shellmatta->";
+                CHECK(write_length == strlen(dummyData));
+                REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
+
+                AND_THEN("The shimatta user is logged in") {
+                    REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
+                }
+            }
+        }
+
+        WHEN("The user shellmatta logges in passing the credentials none interactive") {
+
+            ret = shellmatta_processData(handle, (char*)"login -u shimatta -p 12345678\r", 30);
+            CHECK(ret == SHELLMATTA_OK);
+
+            THEN("The login message is printed - password is hidden") {
+                char *dummyData =   (char*) "login -u shimatta -p 12345678\r\n" \
+                                            "login successful\r\n"              \
+                                            "\r\n"                              \
+                                            "shimatta@shellmatta->";
+                CHECK(write_length == strlen(dummyData));
+                REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
+
+                AND_THEN("The shimatta user is logged in") {
+                    REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
+                }
+            }
+        }
+    }
+}
+

+ 6 - 6
test/unittest/shellmatta_utils/test_utils_insertChars.cpp

@@ -32,7 +32,7 @@ TEST_CASE( "shellmatta_insertChars normal call" ) {
     memset(write_data, 0, sizeof(write_data));
     write_idx = 0u;
 
-    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u, false);
+    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
 
     CHECK( inst.cursor == 12);
     CHECK( inst.inputCount == 14);
@@ -60,7 +60,7 @@ TEST_CASE( "shellmatta_insertChars overwrite" ) {
     memset(write_data, 0, sizeof(write_data));
     write_idx = 0u;
 
-    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u, false);
+    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
 
     CHECK( inst.cursor == 12);
     CHECK( inst.inputCount == 12);
@@ -88,7 +88,7 @@ TEST_CASE( "shellmatta_insertChars append" ) {
     memset(write_data, 0, sizeof(write_data));
     write_idx = 0u;
 
-    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u, false);
+    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
 
     CHECK( inst.cursor == 14);
     CHECK( inst.inputCount == 14);
@@ -115,7 +115,7 @@ TEST_CASE( "shellmatta_insertChars 0 length" ) {
     memset(write_data, 0, sizeof(write_data));
     write_idx = 0u;
 
-    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 0u, false);
+    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 0u);
 
     CHECK( inst.cursor == 8u );
     CHECK( inst.inputCount == 10u );
@@ -142,7 +142,7 @@ TEST_CASE( "shellmatta_insertChars buffer full" ) {
     memset(write_data, 0, sizeof(write_data));
     write_idx = 0u;
 
-    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 10u, false);
+    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 10u);
 
     CHECK( inst.cursor == 18u );
     CHECK( inst.inputCount == 20u );
@@ -170,7 +170,7 @@ TEST_CASE( "shellmatta_insertChars buffer overflow by 1" ) {
     memset(write_data, 0, sizeof(write_data));
     write_idx = 0u;
 
-    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 11u, false);
+    utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 11u);
 
     CHECK( inst.cursor == 18u );
     CHECK( inst.inputCount == 20u );