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 #include "param_init.h" 24 25 using namespace std; 26 using namespace testing::ext; 27 28 namespace init_ut { 29 class ParamShellUnitTest : public testing::Test { 30 public: ParamShellUnitTest()31 ParamShellUnitTest() {}; ~ParamShellUnitTest()32 virtual ~ParamShellUnitTest() {}; SetUpTestCase(void)33 static void SetUpTestCase(void) {}; TearDownTestCase(void)34 static void TearDownTestCase(void) {}; SetUp(void)35 void SetUp(void) {}; TearDown(void)36 void TearDown(void) {}; TestBody(void)37 void TestBody(void) {}; TestInitParamShell()38 void TestInitParamShell() 39 { 40 SystemSetParameter("aaa", "aaa"); 41 BShellHandle bshd = GetShellHandle(); 42 if (bshd == nullptr) { 43 return; 44 } 45 const char *args[] = {"paramshell", "\n"}; 46 const ParamInfo *param = BShellEnvGetReservedParam(bshd, PARAM_REVERESD_NAME_CURR_PARAMETER); 47 EXPECT_NE(param, nullptr); 48 int ret = BShellEnvSetParam(bshd, PARAM_REVERESD_NAME_CURR_PARAMETER, "..a", PARAM_STRING, (void *)"..a"); 49 EXPECT_EQ(ret, 0); 50 SetParamShellPrompt(bshd, args[1]); 51 SetParamShellPrompt(bshd, ".."); 52 53 ret = BShellEnvSetParam(bshd, param->name, param->desc, param->type, (void *)""); 54 SetParamShellPrompt(bshd, ".."); 55 56 SetParamShellPrompt(bshd, ".a"); 57 SetParamShellPrompt(bshd, "."); 58 SetParamShellPrompt(bshd, args[1]); 59 BShellParamCmdRegister(bshd, 1); 60 BShellEnvStart(bshd); 61 ret = BShellEnvOutputPrompt(bshd, "testprompt"); 62 ret = BShellEnvOutputPrompt(bshd, "testprompt1111111111111111111111111111111111111111111111111111111111"); 63 BShellEnvOutputByte(bshd, 'o'); 64 EXPECT_EQ(ret, 0); 65 } TestParamShellCmd()66 void TestParamShellCmd() 67 { 68 BShellHandle bshd = GetShellHandle(); 69 BShellKey *key = BShellEnvGetDefaultKey('\n'); 70 EXPECT_NE(key, nullptr); 71 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "cd const") != EOK) { 72 return; 73 } 74 bshd->length = strlen(bshd->buffer); 75 int ret = key->keyHandle(bshd, '\n'); 76 EXPECT_EQ(ret, 0); 77 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "cat aaa") != EOK) { 78 return; 79 } 80 bshd->length = strlen(bshd->buffer); 81 ret = key->keyHandle(bshd, '\n'); 82 EXPECT_EQ(ret, 0); 83 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testnotcmd") != EOK) { 84 return; 85 } 86 bshd->length = strlen(bshd->buffer); 87 ret = key->keyHandle(bshd, '\n'); 88 EXPECT_EQ(ret, 0); 89 // test param start with " 90 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "\"ls") != EOK) { 91 return; 92 } 93 bshd->length = strlen(bshd->buffer); 94 ret = key->keyHandle(bshd, '\n'); 95 EXPECT_EQ(ret, 0); 96 // test argc is 0 97 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), ",ls") != EOK) { 98 return; 99 } 100 bshd->length = strlen(bshd->buffer); 101 ret = key->keyHandle(bshd, '\n'); 102 EXPECT_EQ(ret, 0); 103 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "$test$") != EOK) { 104 return; 105 } 106 bshd->length = strlen(bshd->buffer); 107 ret = key->keyHandle(bshd, '\n'); 108 EXPECT_EQ(ret, 0); 109 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "exit") != EOK) { 110 return; 111 } 112 bshd->length = strlen(bshd->buffer); 113 ret = key->keyHandle(bshd, '\n'); 114 EXPECT_EQ(ret, 0); 115 } TestParamShellCmd1()116 void TestParamShellCmd1() 117 { 118 BShellHandle bshd = GetShellHandle(); 119 BShellKey *key = BShellEnvGetDefaultKey('\n'); 120 EXPECT_NE(key, nullptr); 121 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "pwd") != EOK) { 122 return; 123 } 124 bshd->length = strlen(bshd->buffer); 125 int ret = key->keyHandle(bshd, '\n'); 126 EXPECT_EQ(ret, 0); 127 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "help") != EOK) { 128 return; 129 } 130 bshd->length = strlen(bshd->buffer); 131 ret = key->keyHandle(bshd, '\n'); 132 EXPECT_EQ(ret, 0); 133 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "dump") != EOK) { 134 return; 135 } 136 bshd->length = strlen(bshd->buffer); 137 ret = key->keyHandle(bshd, '\n'); 138 EXPECT_EQ(ret, 0); 139 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "dump verbose") != EOK) { 140 return; 141 } 142 bshd->length = strlen(bshd->buffer); 143 ret = key->keyHandle(bshd, '\n'); 144 EXPECT_EQ(ret, 0); 145 } TestParamShellcmdEndkey()146 void TestParamShellcmdEndkey() 147 { 148 BShellHandle bshd = GetShellHandle(); 149 bshd->input(nullptr, 0); 150 BShellKey *key = BShellEnvGetDefaultKey('\b'); 151 EXPECT_NE(key, nullptr); 152 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testbbackspace") != EOK) { 153 return; 154 } 155 bshd->length = strlen(bshd->buffer); 156 bshd->cursor = strlen("testb"); 157 int ret = key->keyHandle(bshd, '\b'); 158 EXPECT_EQ(ret, 0); 159 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testbbackspace") != EOK) { 160 return; 161 } 162 bshd->length = strlen(bshd->buffer); 163 bshd->cursor = strlen(bshd->buffer); 164 ret = key->keyHandle(bshd, '\b'); 165 EXPECT_EQ(ret, 0); 166 167 key = BShellEnvGetDefaultKey('\t'); 168 if (strcpy_s(bshd->buffer, sizeof(bshd->buffer), "testtab") != EOK) { 169 return; 170 } 171 bshd->length = strlen(bshd->buffer); 172 ret = key->keyHandle(bshd, '\t'); 173 EXPECT_NE(key, nullptr); 174 BShellEnvProcessInput(bshd, (char)3); // 3 is ctrl c 175 BShellEnvProcessInput(bshd, '\e'); 176 BShellEnvProcessInput(bshd, '['); 177 bshd->length = 1; 178 bshd->cursor = 1; 179 BShellEnvProcessInput(bshd, 'C'); 180 BShellEnvProcessInput(bshd, 'D'); 181 } 182 }; 183 184 HWTEST_F(ParamShellUnitTest, Init_TestInitParamShell_001, TestSize.Level1) 185 { 186 ParamShellUnitTest test; 187 test.TestInitParamShell(); 188 test.TestParamShellCmd(); 189 test.TestParamShellCmd1(); 190 } 191 HWTEST_F(ParamShellUnitTest, Init_TestParamShellInput_001, TestSize.Level1) 192 { 193 BShellHandle bshd = GetShellHandle(); 194 BShellEnvProcessInput(bshd, '\n'); 195 196 BShellEnvProcessInput(bshd, 'l'); 197 bshd->length = BSH_COMMAND_MAX_LENGTH; 198 199 BShellEnvProcessInput(bshd, 'l'); 200 bshd->length = sizeof('l'); 201 bshd->cursor = 0; 202 BShellEnvProcessInput(bshd, 's'); 203 BShellEnvProcessInput(bshd, '\n'); 204 205 BShellEnvProcessInput(bshd, '\n'); // test bshd buff length is 0 206 207 int ret = BShellEnvRegisterKeyHandle(bshd, 'z', (BShellkeyHandle)(void*)0x409600); // 0x409600 construct address 208 EXPECT_EQ(ret, 0); 209 } 210 HWTEST_F(ParamShellUnitTest, Init_TestParamShellcmd_001, TestSize.Level1) 211 { 212 ParamShellUnitTest test; 213 test.TestParamShellcmdEndkey(); 214 GetSystemCommitId(); 215 BShellEnvLoop(nullptr); 216 BShellEnvErrString(GetShellHandle(), 1); 217 BShellEnvOutputResult(GetShellHandle(), 1); 218 } 219 } // namespace init_ut 220