1 /*
2 * Copyright (c) 2025 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 #include "test_log.h"
17 #include "string_ex.h"
18
19 #define private public
20
21 #include "svc_control.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace SAFWK {
28
29 namespace {
30 char g_svcStr[] = "svc";
31 char g_wifiStr[] = "wifi";
32 char g_bluetoothStr[] = "bluetooth";
33 char g_nearlinkStr[] = "nearlink";
34 char g_helpStr[] = "help";
35 char g_otherStr[] = "other";
36 }
37
38 class SvcTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase()46 void SvcTest::SetUpTestCase()
47 {
48 DTEST_LOG << "SetUpTestCase" << std::endl;
49 }
50
TearDownTestCase()51 void SvcTest::TearDownTestCase()
52 {
53 DTEST_LOG << "TearDownTestCase" << std::endl;
54 }
55
SetUp()56 void SvcTest::SetUp()
57 {
58 DTEST_LOG << "SetUp" << std::endl;
59 }
60
TearDown()61 void SvcTest::TearDown()
62 {
63 DTEST_LOG << "TearDown" << std::endl;
64 }
65
66 /**
67 * @tc.name: HelpInfo001
68 * @tc.desc: Check HelpInfo
69 * @tc.type: FUNC
70 * @tc.require: I5KMF7
71 */
72 HWTEST_F(SvcTest, HelpInfo001, TestSize.Level2)
73 {
74 DTEST_LOG << "HelpInfo001 start" << std::endl;
75 int32_t fds[2];
76 if (pipe(fds) == -1) {
77 EXPECT_TRUE(false);
78 DTEST_LOG << "create pipe failed" << std::endl;
79 } else {
80 SvcControl::HelpInfo(fds[1]);
81 char buffer[0x400] = { 0 };
82 EXPECT_NE(read(fds[0], buffer, sizeof(buffer) - 1), -1);
83 const char *expect = "svc wifi | bluetooth | nearlink enable | disable | help\n";
84 EXPECT_STREQ(buffer, expect);
85 close(fds[0]);
86 close(fds[1]);
87 }
88 DTEST_LOG << "HelpInfo001 end" << std::endl;
89 }
90
91 /**
92 * @tc.name: Parse001
93 * @tc.desc: Check Parse
94 * @tc.type: FUNC
95 * @tc.require: I5KMF7
96 */
97 HWTEST_F(SvcTest, Parse001, TestSize.Level2)
98 {
99 DTEST_LOG << "Parse001 start" << std::endl;
100 char *argv[] = { g_svcStr };
101 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
102 EXPECT_EQ(cmd, SvcCmd::ARGS_CNT_NOT_ENOUGH);
103 DTEST_LOG << "Parse001 end" << std::endl;
104 }
105
106 /**
107 * @tc.name: Parse002
108 * @tc.desc: Check Parse
109 * @tc.type: FUNC
110 * @tc.require: I5KMF7
111 */
112 HWTEST_F(SvcTest, Parse002, TestSize.Level2)
113 {
114 DTEST_LOG << "Parse002 start" << std::endl;
115 char *argv[] = { g_svcStr, g_helpStr };
116 SvcCmd cmd = SvcControl::GetInstance().Parse(SvcControl::ARG_MAX_COUNT + 1, argv);
117 EXPECT_EQ(cmd, SvcCmd::ARGS_CNT_EXCEED);
118 DTEST_LOG << "Parse002 end" << std::endl;
119 }
120
121 /**
122 * @tc.name: Parse003
123 * @tc.desc: Check Parse
124 * @tc.type: FUNC
125 * @tc.require: I5KMF7
126 */
127 HWTEST_F(SvcTest, Parse003, TestSize.Level2)
128 {
129 DTEST_LOG << "Parse003 start" << std::endl;
130 char *argv[] = { g_svcStr, g_otherStr };
131 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
132 EXPECT_EQ(cmd, SvcCmd::ARGS_INVALID);
133 DTEST_LOG << "Parse003 end" << std::endl;
134 }
135
136 /**
137 * @tc.name: Parse004
138 * @tc.desc: Check Parse
139 * @tc.type: FUNC
140 * @tc.require: I5KMF7
141 */
142 HWTEST_F(SvcTest, Parse004, TestSize.Level2)
143 {
144 DTEST_LOG << "Parse004 start" << std::endl;
145 char *argv[] = { g_svcStr, g_helpStr, g_otherStr };
146 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
147 EXPECT_EQ(cmd, SvcCmd::ARGS_CNT_EXCEED);
148 DTEST_LOG << "Parse004 end" << std::endl;
149 }
150
151 /**
152 * @tc.name: Parse005
153 * @tc.desc: Check Parse
154 * @tc.type: FUNC
155 * @tc.require: I5KMF7
156 */
157 HWTEST_F(SvcTest, Parse005, TestSize.Level2)
158 {
159 DTEST_LOG << "Parse005 start" << std::endl;
160 char *argv[] = { g_svcStr, g_wifiStr };
161 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
162 EXPECT_EQ(cmd, SvcCmd::ARGS_CNT_NOT_ENOUGH);
163 DTEST_LOG << "Parse005 end" << std::endl;
164 }
165
166 /**
167 * @tc.name: Parse006
168 * @tc.desc: Check Parse
169 * @tc.type: FUNC
170 * @tc.require: I5KMF7
171 */
172 HWTEST_F(SvcTest, Parse006, TestSize.Level2)
173 {
174 DTEST_LOG << "Parse006 start" << std::endl;
175 char *argv[] = { g_svcStr, g_wifiStr, g_helpStr };
176 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
177 EXPECT_EQ(cmd, SvcCmd::WIFI_CMD);
178 DTEST_LOG << "Parse006 end" << std::endl;
179 }
180
181 /**
182 * @tc.name: Parse007
183 * @tc.desc: Check Parse
184 * @tc.type: FUNC
185 * @tc.require: I5KMF7
186 */
187 HWTEST_F(SvcTest, Parse007, TestSize.Level2)
188 {
189 DTEST_LOG << "Parse007 start" << std::endl;
190 char *argv[] = { g_svcStr, g_bluetoothStr, g_otherStr };
191 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
192 EXPECT_EQ(cmd, SvcCmd::BLUETOOTH_CMD);
193 DTEST_LOG << "Parse007 end" << std::endl;
194 }
195
196 /**
197 * @tc.name: Parse008
198 * @tc.desc: Check Parse
199 * @tc.type: FUNC
200 * @tc.require: I5KMF7
201 */
202 HWTEST_F(SvcTest, Parse008, TestSize.Level2)
203 {
204 DTEST_LOG << "Parse008 start" << std::endl;
205 char *argv[] = { g_svcStr, g_nearlinkStr, g_otherStr };
206 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
207 EXPECT_EQ(cmd, SvcCmd::NEARLINK_CMD);
208 DTEST_LOG << "Parse008 end" << std::endl;
209 }
210
211 /**
212 * @tc.name: Parse009
213 * @tc.desc: Check Parse
214 * @tc.type: FUNC
215 * @tc.require: I5KMF7
216 */
217 HWTEST_F(SvcTest, Parse009, TestSize.Level2)
218 {
219 DTEST_LOG << "Parse009 start" << std::endl;
220 char *argv[] = { g_svcStr, g_helpStr };
221 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
222 EXPECT_EQ(cmd, SvcCmd::HELP_CMD);
223 DTEST_LOG << "Parse009 end" << std::endl;
224 }
225
226 /**
227 * @tc.name: Parse010
228 * @tc.desc: Check Parse
229 * @tc.type: FUNC
230 * @tc.require: I5KMF7
231 */
232 HWTEST_F(SvcTest, Parse010, TestSize.Level2)
233 {
234 DTEST_LOG << "Parse010 start" << std::endl;
235 char *argv[] = { g_svcStr, nullptr, g_helpStr };
236 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
237 EXPECT_EQ(cmd, SvcCmd::ARGS_INVALID);
238 DTEST_LOG << "Parse010 end" << std::endl;
239 }
240
241 /**
242 * @tc.name: Parse011
243 * @tc.desc: Check Parse
244 * @tc.type: FUNC
245 * @tc.require: I5KMF7
246 */
247 HWTEST_F(SvcTest, Parse011, TestSize.Level2)
248 {
249 DTEST_LOG << "Parse011 start" << std::endl;
250 char longestStr[SvcControl::SINGLE_ARG_MAXLEN + 2];
251 for (auto &ch: longestStr) {
252 ch = 'a';
253 }
254 longestStr[SvcControl::SINGLE_ARG_MAXLEN + 1] = 0;
255 char *argv[] = { g_svcStr, g_wifiStr, longestStr };
256 SvcCmd cmd = SvcControl::GetInstance().Parse(sizeof(argv) / sizeof(argv[0]), argv);
257 EXPECT_EQ(cmd, SvcCmd::ARGS_INVALID);
258 DTEST_LOG << "Parse011 end" << std::endl;
259 }
260
261 /**
262 * @tc.name: SetCmdArgs001
263 * @tc.desc: Check SetCmdArgs
264 * @tc.type: FUNC
265 * @tc.require: I5KMF7
266 */
267 HWTEST_F(SvcTest, SetCmdArgs001, TestSize.Level0)
268 {
269 DTEST_LOG << "SetCmdArgs001 start" << std::endl;
270 char *argv[] = { g_svcStr, g_wifiStr, g_helpStr };
271 std::vector<std::u16string> u16Args;
272 SvcControl::GetInstance().SetCmdArgs(sizeof(argv) / sizeof(argv[0]), argv, u16Args);
273 EXPECT_EQ(u16Args.size(), 1ULL);
274 std::string args = Str16ToStr8(u16Args[0]);
275 const char *expect = "help";
276 EXPECT_STREQ(args.c_str(), expect);
277 DTEST_LOG << "SetCmdArgs001 end" << std::endl;
278 }
279
280 /**
281 * @tc.name: CmdErrorToString001
282 * @tc.desc: Check CmdErrorToString
283 * @tc.type: FUNC
284 * @tc.require: I5KMF7
285 */
286 HWTEST_F(SvcTest, CmdErrorToString001, TestSize.Level2)
287 {
288 DTEST_LOG << "CmdErrorToString001 start" << std::endl;
289 std::string str = SvcControl::GetInstance().CmdErrorToString(SvcCmd::ARGS_INVALID);
290 const char *expect = "argument invalid";
291 EXPECT_STREQ(str.c_str(), expect);
292 DTEST_LOG << "CmdErrorToString001 end" << std::endl;
293 }
294
295 /**
296 * @tc.name: CmdErrorToString002
297 * @tc.desc: Check CmdErrorToString
298 * @tc.type: FUNC
299 * @tc.require: I5KMF7
300 */
301 HWTEST_F(SvcTest, CmdErrorToString002, TestSize.Level2)
302 {
303 DTEST_LOG << "CmdErrorToString002 start" << std::endl;
304 std::string str = SvcControl::GetInstance().CmdErrorToString(SvcCmd::ARGS_CNT_EXCEED);
305 const char *expect = "argument's cnt exceed";
306 EXPECT_STREQ(str.c_str(), expect);
307 DTEST_LOG << "CmdErrorToString002 end" << std::endl;
308 }
309
310 /**
311 * @tc.name: CmdErrorToString003
312 * @tc.desc: Check CmdErrorToString
313 * @tc.type: FUNC
314 * @tc.require: I5KMF7
315 */
316 HWTEST_F(SvcTest, CmdErrorToString003, TestSize.Level2)
317 {
318 DTEST_LOG << "CmdErrorToString003 start" << std::endl;
319 std::string str = SvcControl::GetInstance().CmdErrorToString(SvcCmd::ARGS_CNT_NOT_ENOUGH);
320 const char *expect = "argument's cnt not enough";
321 EXPECT_STREQ(str.c_str(), expect);
322 DTEST_LOG << "CmdErrorToString003 end" << std::endl;
323 }
324
325 /**
326 * @tc.name: CmdErrorToString004
327 * @tc.desc: Check CmdErrorToString
328 * @tc.type: FUNC
329 * @tc.require: I5KMF7
330 */
331 HWTEST_F(SvcTest, CmdErrorToString004, TestSize.Level2)
332 {
333 DTEST_LOG << "CmdErrorToString004 start" << std::endl;
334 std::string str = SvcControl::GetInstance().CmdErrorToString(
335 static_cast<SvcCmd>(static_cast<int32_t>(SvcCmd::ARGS_CNT_NOT_ENOUGH) - 1));
336 const char *expect = "exception error code";
337 EXPECT_STREQ(str.c_str(), expect);
338 DTEST_LOG << "CmdErrorToString004 end" << std::endl;
339 }
340
341 /**
342 * @tc.name: Main001
343 * @tc.desc: Check Main
344 * @tc.type: FUNC
345 * @tc.require: I5KMF7
346 */
347 HWTEST_F(SvcTest, Main001, TestSize.Level0)
348 {
349 DTEST_LOG << "Main001 start" << std::endl;
350 int32_t fds[2];
351 if (pipe(fds) == -1) {
352 EXPECT_TRUE(false);
353 DTEST_LOG << "create pipe failed" << std::endl;
354 } else {
355 char *argv[] = { g_svcStr, g_helpStr };
356 int32_t ret = SvcControl::GetInstance().Main(sizeof(argv) / sizeof(argv[0]), argv, fds[1]);
357 char buffer[0x400] = { 0 };
358 EXPECT_NE(read(fds[0], buffer, sizeof(buffer) - 1), -1);
359 const char *expect = "svc wifi | bluetooth | nearlink enable | disable | help\n";
360 EXPECT_STREQ(buffer, expect);
361 EXPECT_EQ(ret, 0);
362 close(fds[0]);
363 close(fds[1]);
364 }
365 DTEST_LOG << "Main001 end" << std::endl;
366 }
367
368 /**
369 * @tc.name: Main002
370 * @tc.desc: Check Main
371 * @tc.type: FUNC
372 * @tc.require: I5KMF7
373 */
374 HWTEST_F(SvcTest, Main002, TestSize.Level2)
375 {
376 DTEST_LOG << "Main002 start" << std::endl;
377 int32_t fds[2];
378 if (pipe(fds) == -1) {
379 EXPECT_TRUE(false);
380 DTEST_LOG << "create pipe failed" << std::endl;
381 } else {
382 int32_t ret = SvcControl::GetInstance().Main(2, nullptr, fds[1]);
383 char buffer[0x400] = { 0 };
384 EXPECT_NE(read(fds[0], buffer, sizeof(buffer) - 1), -1);
385 const char *expect = "argument is null\n"
386 "svc wifi | bluetooth | nearlink enable | disable | help\n";
387 EXPECT_STREQ(buffer, expect);
388 EXPECT_EQ(ret, -1);
389 close(fds[0]);
390 close(fds[1]);
391 }
392 DTEST_LOG << "Main002 end" << std::endl;
393 }
394 }
395 }