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