1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <gtest/gtest.h> 16 17 #include "begetctl.h" 18 #include "securec.h" 19 #include "shell.h" 20 #include "shell_utils.h" 21 #include "shell_bas.h" 22 #include "init_param.h" 23 24 using namespace std; 25 using namespace testing::ext; 26 27 namespace init_ut { 28 class ParamShellUnitTest : public testing::Test { 29 public: ParamShellUnitTest()30 ParamShellUnitTest() {}; ~ParamShellUnitTest()31 virtual ~ParamShellUnitTest() {}; SetUpTestCase(void)32 static void SetUpTestCase(void) {}; TearDownTestCase(void)33 static void TearDownTestCase(void) {}; SetUp(void)34 void SetUp(void) {}; TearDown(void)35 void TearDown(void) {}; TestBody(void)36 void TestBody(void) {}; TestInitParamShell()37 void TestInitParamShell() 38 { 39 SystemSetParameter("aaa", "aaa"); 40 BShellHandle bshd = GetShellHandle(); 41 if (bshd == nullptr) { 42 return; 43 } 44 const char *args[] = {"paramshell", "\n"}; 45 const ParamInfo *param = BShellEnvGetReservedParam(bshd, PARAM_REVERESD_NAME_CURR_PARAMETER); 46 int ret = BShellEnvSetParam(bshd, PARAM_REVERESD_NAME_CURR_PARAMETER, "..a", PARAM_STRING, (void *)"..a"); 47 EXPECT_EQ(ret, 0); 48 SetParamShellPrompt(bshd, args[1]); 49 SetParamShellPrompt(bshd, ".."); 50 51 ret = BShellEnvSetParam(bshd, param->name, param->desc, param->type, (void *)""); 52 SetParamShellPrompt(bshd, ".."); 53 54 SetParamShellPrompt(bshd, ".a"); 55 SetParamShellPrompt(bshd, "."); 56 SetParamShellPrompt(bshd, args[1]); 57 BShellParamCmdRegister(bshd, 1); 58 BShellEnvStart(bshd); 59 ret = BShellEnvOutputPrompt(bshd, "testprompt"); 60 ret = BShellEnvOutputPrompt(bshd, "testprompt1111111111111111111111111111111111111111111111111111111111"); 61 BShellEnvOutputByte(bshd, 'o'); 62 EXPECT_EQ(ret, 0); 63 } TestParamShellCmd()64 void TestParamShellCmd() 65 { 66 BShellHandle bshd = GetShellHandle(); 67 BShellKey *key = BShellEnvGetDefaultKey('\n'); 68 EXPECT_NE(key, nullptr); 69 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "cd const") != EOK) { 70 return; 71 } 72 bshd->length = strlen(bshd->buffer); 73 int ret = key->keyHandle(bshd, '\n'); 74 EXPECT_EQ(ret, 0); 75 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "cat aaa") != EOK) { 76 return; 77 } 78 bshd->length = strlen(bshd->buffer); 79 ret = key->keyHandle(bshd, '\n'); 80 EXPECT_EQ(ret, 0); 81 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testnotcmd") != EOK) { 82 return; 83 } 84 bshd->length = strlen(bshd->buffer); 85 ret = key->keyHandle(bshd, '\n'); 86 EXPECT_EQ(ret, 0); 87 // test param start with " 88 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "\"ls") != EOK) { 89 return; 90 } 91 bshd->length = strlen(bshd->buffer); 92 ret = key->keyHandle(bshd, '\n'); 93 EXPECT_EQ(ret, 0); 94 // test argc is 0 95 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), ",ls") != EOK) { 96 return; 97 } 98 bshd->length = strlen(bshd->buffer); 99 ret = key->keyHandle(bshd, '\n'); 100 EXPECT_EQ(ret, 0); 101 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "$test$") != EOK) { 102 return; 103 } 104 bshd->length = strlen(bshd->buffer); 105 ret = key->keyHandle(bshd, '\n'); 106 EXPECT_EQ(ret, 0); 107 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "exit") != EOK) { 108 return; 109 } 110 bshd->length = strlen(bshd->buffer); 111 ret = key->keyHandle(bshd, '\n'); 112 EXPECT_EQ(ret, 0); 113 } TestParamShellCmd1()114 void TestParamShellCmd1() 115 { 116 BShellHandle bshd = GetShellHandle(); 117 BShellKey *key = BShellEnvGetDefaultKey('\n'); 118 EXPECT_NE(key, nullptr); 119 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "pwd") != EOK) { 120 return; 121 } 122 bshd->length = strlen(bshd->buffer); 123 int ret = key->keyHandle(bshd, '\n'); 124 EXPECT_EQ(ret, 0); 125 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "help") != EOK) { 126 return; 127 } 128 bshd->length = strlen(bshd->buffer); 129 ret = key->keyHandle(bshd, '\n'); 130 EXPECT_EQ(ret, 0); 131 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "dump") != EOK) { 132 return; 133 } 134 bshd->length = strlen(bshd->buffer); 135 ret = key->keyHandle(bshd, '\n'); 136 EXPECT_EQ(ret, 0); 137 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "dump verbose") != EOK) { 138 return; 139 } 140 bshd->length = strlen(bshd->buffer); 141 ret = key->keyHandle(bshd, '\n'); 142 EXPECT_EQ(ret, 0); 143 } TestParamShellcmdEndkey()144 void TestParamShellcmdEndkey() 145 { 146 BShellHandle bshd = GetShellHandle(); 147 bshd->input(nullptr, 0); 148 BShellKey *key = BShellEnvGetDefaultKey('\b'); 149 EXPECT_NE(key, nullptr); 150 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testbbackspace") != EOK) { 151 return; 152 } 153 bshd->length = strlen(bshd->buffer); 154 bshd->cursor = strlen("testb"); 155 int ret = key->keyHandle(bshd, '\b'); 156 EXPECT_EQ(ret, 0); 157 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testbbackspace") != EOK) { 158 return; 159 } 160 bshd->length = strlen(bshd->buffer); 161 bshd->cursor = strlen(bshd->buffer); 162 ret = key->keyHandle(bshd, '\b'); 163 EXPECT_EQ(ret, 0); 164 165 key = BShellEnvGetDefaultKey('\t'); 166 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testtab") != EOK) { 167 return; 168 } 169 bshd->length = strlen(bshd->buffer); 170 ret = key->keyHandle(bshd, '\t'); 171 EXPECT_NE(key, nullptr); 172 BShellEnvProcessInput(bshd, (char)3); // 3 is ctrl c 173 BShellEnvProcessInput(bshd, '\e'); 174 BShellEnvProcessInput(bshd, '['); 175 bshd->length = 1; 176 bshd->cursor = 1; 177 BShellEnvProcessInput(bshd, 'C'); 178 BShellEnvProcessInput(bshd, 'D'); 179 } 180 }; 181 182 HWTEST_F(ParamShellUnitTest, TestInitParamShell, TestSize.Level1) 183 { 184 ParamShellUnitTest test; 185 test.TestInitParamShell(); 186 test.TestParamShellCmd(); 187 test.TestParamShellCmd1(); 188 } 189 HWTEST_F(ParamShellUnitTest, TestParamShellInput, TestSize.Level1) 190 { 191 BShellHandle bshd = GetShellHandle(); 192 BShellEnvProcessInput(bshd, '\n'); 193 194 BShellEnvProcessInput(bshd, 'l'); 195 bshd->length = BSH_COMMAND_MAX_LENGTH; 196 197 BShellEnvProcessInput(bshd, 'l'); 198 bshd->length = sizeof('l'); 199 bshd->cursor = 0; 200 BShellEnvProcessInput(bshd, 's'); 201 BShellEnvProcessInput(bshd, '\n'); 202 203 BShellEnvProcessInput(bshd, '\n'); // test bshd buff length is 0 204 205 int ret = BShellEnvRegisterKeyHandle(bshd, 'z', (BShellkeyHandle)(void*)0x409600); // 0x409600 construct address 206 EXPECT_EQ(ret, 0); 207 } 208 HWTEST_F(ParamShellUnitTest, TestParamShellcmd2, TestSize.Level1) 209 { 210 ParamShellUnitTest test; 211 test.TestParamShellcmdEndkey(); 212 GetSystemCommitId(); 213 BShellEnvLoop(nullptr); 214 BShellEnvErrString(GetShellHandle(), 1); 215 BShellEnvOutputResult(GetShellHandle(), 1); 216 demoExit(); 217 } 218 } // namespace init_ut 219