1 /*
2 * Copyright (c) 2024 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
16 #include <gtest/gtest.h>
17
18 #define private public
19 #include "app_spawn_client.h"
20 #undef private
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 class AppSpawnClientSecondTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32 };
33
SetUpTestCase(void)34 void AppSpawnClientSecondTest::SetUpTestCase(void)
35 {}
36
TearDownTestCase(void)37 void AppSpawnClientSecondTest::TearDownTestCase(void)
38 {}
39
SetUp()40 void AppSpawnClientSecondTest::SetUp()
41 {}
42
TearDown()43 void AppSpawnClientSecondTest::TearDown()
44 {}
45
46 // Scenario1: Test when startMsg.flags is 0 and all other flags are false.
47 HWTEST_F(AppSpawnClientSecondTest, SetStartFlags_001, TestSize.Level2)
48 {
49 AppSpawnClient appSpawnClient;
50 AppSpawnStartMsg startMsg;
51 startMsg.flags = 0;
52 startMsg.atomicServiceFlag = false;
53 startMsg.strictMode = false;
54 startMsg.isolatedExtension = false;
55 #ifdef SUPPORT_CHILD_PROCESS
56 startMsg.childProcessType = 0;
57 #endif // SUPPORT_CHILD_PROCESS
58 AppSpawnReqMsgHandle reqHandle = nullptr;
59 EXPECT_NE(appSpawnClient.SetStartFlags(startMsg, reqHandle), 0);
60 }
61
62 // Scenario2: Test when startMsg.flags is START_FLAG_TEST_NUM and all other flags are false.
63 HWTEST_F(AppSpawnClientSecondTest, SetStartFlags_002, TestSize.Level2)
64 {
65 AppSpawnClient appSpawnClient;
66 AppSpawnStartMsg startMsg;
67 startMsg.flags = 1;
68 startMsg.atomicServiceFlag = false;
69 startMsg.strictMode = false;
70 startMsg.isolatedExtension = false;
71 #ifdef SUPPORT_CHILD_PROCESS
72 startMsg.childProcessType = 0;
73 #endif // SUPPORT_CHILD_PROCESS
74 AppSpawnReqMsgHandle reqHandle = nullptr;
75 EXPECT_NE(appSpawnClient.SetStartFlags(startMsg, reqHandle), 0);
76 }
77
78 // Scenario3: Test when startMsg.atomicServiceFlag is true and all other flags are false.
79 HWTEST_F(AppSpawnClientSecondTest, SetStartFlags_003, TestSize.Level2)
80 {
81 AppSpawnClient appSpawnClient;
82 AppSpawnStartMsg startMsg;
83 startMsg.flags = 0;
84 startMsg.atomicServiceFlag = true;
85 startMsg.strictMode = false;
86 startMsg.isolatedExtension = false;
87 #ifdef SUPPORT_CHILD_PROCESS
88 startMsg.childProcessType = 0;
89 #endif // SUPPORT_CHILD_PROCESS
90 AppSpawnReqMsgHandle reqHandle = nullptr;
91 EXPECT_NE(appSpawnClient.SetStartFlags(startMsg, reqHandle), 0);
92 }
93
94 // Scenario4: Test when startMsg.strictMode is true and all other flags are false.
95 HWTEST_F(AppSpawnClientSecondTest, SetStartFlags_004, TestSize.Level2)
96 {
97 AppSpawnClient appSpawnClient;
98 AppSpawnStartMsg startMsg;
99 startMsg.flags = 0;
100 startMsg.atomicServiceFlag = false;
101 startMsg.strictMode = true;
102 startMsg.isolatedExtension = false;
103 #ifdef SUPPORT_CHILD_PROCESS
104 startMsg.childProcessType = 0;
105 #endif // SUPPORT_CHILD_PROCESS
106 AppSpawnReqMsgHandle reqHandle = nullptr;
107 EXPECT_NE(appSpawnClient.SetStartFlags(startMsg, reqHandle), 0);
108 }
109
110 // Scenario5: Test when startMsg.isolatedExtension is true and all other flags are false.
111 HWTEST_F(AppSpawnClientSecondTest, SetStartFlags_005, TestSize.Level2)
112 {
113 AppSpawnClient appSpawnClient;
114 AppSpawnStartMsg startMsg;
115 startMsg.flags = 0;
116 startMsg.atomicServiceFlag = false;
117 startMsg.strictMode = false;
118 startMsg.isolatedExtension = true;
119 #ifdef SUPPORT_CHILD_PROCESS
120 startMsg.childProcessType = 0;
121 #endif // SUPPORT_CHILD_PROCESS
122 AppSpawnReqMsgHandle reqHandle = nullptr;
123 EXPECT_NE(appSpawnClient.SetStartFlags(startMsg, reqHandle), 0);
124 }
125
126 // Scenario6: Test when startMsg.childProcessType is not 0 and all other flags are false.
127 HWTEST_F(AppSpawnClientSecondTest, SetStartFlags_006, TestSize.Level2)
128 {
129 AppSpawnClient appSpawnClient;
130 AppSpawnStartMsg startMsg;
131 startMsg.flags = 0;
132 startMsg.atomicServiceFlag = false;
133 startMsg.strictMode = false;
134 startMsg.isolatedExtension = false;
135 #ifdef SUPPORT_CHILD_PROCESS
136 startMsg.childProcessType = 1;
137 #endif // SUPPORT_CHILD_PROCESS
138 AppSpawnReqMsgHandle reqHandle = nullptr;
139 EXPECT_NE(appSpawnClient.SetStartFlags(startMsg, reqHandle), 0);
140 }
141
142 // Scenario7: Test when startMsg.flags is START_FLAG_TEST_NUM and all other flags are true.
143 HWTEST_F(AppSpawnClientSecondTest, SetStartFlags_007, TestSize.Level2)
144 {
145 AppSpawnClient appSpawnClient;
146 AppSpawnStartMsg startMsg;
147 startMsg.flags = 1;
148 startMsg.atomicServiceFlag = true;
149 startMsg.strictMode = true;
150 startMsg.isolatedExtension = true;
151 #ifdef SUPPORT_CHILD_PROCESS
152 startMsg.childProcessType = 1;
153 #endif // SUPPORT_CHILD_PROCESS
154 AppSpawnReqMsgHandle reqHandle = nullptr;
155 EXPECT_NE(appSpawnClient.SetStartFlags(startMsg, reqHandle), 0);
156 }
157
158 // Scenario8: Test when startMsg.isCustomSandboxFlag is true and all other flags are false.
159 HWTEST_F(AppSpawnClientSecondTest, SetStartFlags_008, TestSize.Level2)
160 {
161 AppSpawnClient appSpawnClient;
162 AppSpawnStartMsg startMsg;
163 startMsg.flags = 0;
164 startMsg.atomicServiceFlag = false;
165 startMsg.strictMode = false;
166 startMsg.isolatedExtension = false;
167 startMsg.isCustomSandboxFlag = true;
168 #ifdef SUPPORT_CHILD_PROCESS
169 startMsg.childProcessType = 0;
170 #endif // SUPPORT_CHILD_PROCESS
171 AppSpawnReqMsgHandle reqHandle = nullptr;
172 EXPECT_NE(appSpawnClient.SetStartFlags(startMsg, reqHandle), 0);
173 }
174
175 // Scenario1: Test when provisionType is empty then function returns 0.
176 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_001, TestSize.Level2)
177 {
178 AppSpawnClient appSpawnClient;
179 AppSpawnStartMsg startMsg;
180 startMsg.code = MSG_APP_SPAWN;
181 startMsg.procName = "testProcName";
182 AppSpawnReqMsgHandle reqHandle = nullptr;
183 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
184 EXPECT_EQ(ret, ERR_OK);
185 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
186 ASSERT_EQ(ret, 0);
187 }
188
189 // Scenario2: Test when provisionType is not empty then function returns 0.
190 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_002, TestSize.Level2)
191 {
192 AppSpawnClient appSpawnClient;
193 AppSpawnStartMsg startMsg;
194 startMsg.provisionType = "test";
195 startMsg.code = MSG_APP_SPAWN;
196 startMsg.procName = "testProcName";
197 AppSpawnReqMsgHandle reqHandle = nullptr;
198 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
199 EXPECT_EQ(ret, ERR_OK);
200 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
201 ASSERT_EQ(ret, 0);
202 }
203
204 // Scenario3: Test when extensionSandboxPath is empty then function returns 0.
205 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_003, TestSize.Level2)
206 {
207 AppSpawnClient appSpawnClient;
208 AppSpawnStartMsg startMsg;
209 startMsg.code = MSG_APP_SPAWN;
210 startMsg.procName = "testProcName";
211 AppSpawnReqMsgHandle reqHandle = nullptr;
212 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
213 EXPECT_EQ(ret, ERR_OK);
214 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
215 ASSERT_EQ(ret, 0);
216 }
217
218 // Scenario4: Test when extensionSandboxPath is not empty then function returns 0.
219 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_004, TestSize.Level2)
220 {
221 AppSpawnClient appSpawnClient;
222 AppSpawnStartMsg startMsg;
223 startMsg.extensionSandboxPath = "test";
224 startMsg.code = MSG_APP_SPAWN;
225 startMsg.procName = "testProcName";
226 AppSpawnReqMsgHandle reqHandle = nullptr;
227 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
228 EXPECT_EQ(ret, ERR_OK);
229 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
230 ASSERT_EQ(ret, 0);
231 }
232
233 // Scenario5: Test when processType is empty then function returns 0.
234 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_005, TestSize.Level2)
235 {
236 AppSpawnClient appSpawnClient;
237 AppSpawnStartMsg startMsg;
238 startMsg.code = MSG_APP_SPAWN;
239 startMsg.procName = "testProcName";
240 AppSpawnReqMsgHandle reqHandle = nullptr;
241 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
242 EXPECT_EQ(ret, ERR_OK);
243 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
244 ASSERT_EQ(ret, 0);
245 }
246
247 // Scenario6: Test when processType is not empty then function returns 0.
248 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_006, TestSize.Level2)
249 {
250 AppSpawnClient appSpawnClient;
251 AppSpawnStartMsg startMsg;
252 startMsg.processType = "test";
253 startMsg.code = MSG_APP_SPAWN;
254 startMsg.procName = "testProcName";
255 AppSpawnReqMsgHandle reqHandle = nullptr;
256 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
257 EXPECT_EQ(ret, ERR_OK);
258 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
259 ASSERT_EQ(ret, 0);
260 }
261
262 #ifdef SUPPORT_CHILD_PROCESS
263 // Scenario7: Test when maxChildProcess is 0 then function returns 0.
264 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_007, TestSize.Level2)
265 {
266 AppSpawnClient appSpawnClient;
267 AppSpawnStartMsg startMsg;
268 startMsg.maxChildProcess = 0;
269 startMsg.code = MSG_APP_SPAWN;
270 startMsg.procName = "testProcName";
271 AppSpawnReqMsgHandle reqHandle = nullptr;
272 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
273 EXPECT_EQ(ret, ERR_OK);
274 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
275 ASSERT_EQ(ret, 0);
276 }
277
278 // Scenario8: Test when maxChildProcess is not 0 then function returns 0.
279 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_008, TestSize.Level2)
280 {
281 AppSpawnClient appSpawnClient;
282 AppSpawnStartMsg startMsg;
283 startMsg.maxChildProcess = 1;
284 startMsg.code = MSG_APP_SPAWN;
285 startMsg.procName = "testProcName";
286 AppSpawnReqMsgHandle reqHandle = nullptr;
287 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
288 EXPECT_EQ(ret, ERR_OK);
289 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
290 ASSERT_EQ(ret, 0);
291 }
292 #endif // SUPPORT_CHILD_PROCESS
293
294 // Scenario9: Test when fds is empty then function returns 0.
295 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_009, TestSize.Level2)
296 {
297 AppSpawnClient appSpawnClient;
298 AppSpawnStartMsg startMsg;
299 startMsg.code = MSG_APP_SPAWN;
300 startMsg.procName = "testProcName";
301 AppSpawnReqMsgHandle reqHandle = nullptr;
302 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
303 EXPECT_EQ(ret, ERR_OK);
304 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
305 ASSERT_EQ(ret, 0);
306 }
307
308 // Scenario10: Test when fds is not empty then function returns 0.
309 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsgMore_010, TestSize.Level2)
310 {
311 AppSpawnClient appSpawnClient;
312 AppSpawnStartMsg startMsg;
313 startMsg.fds["fds"] = 1;
314 startMsg.code = MSG_APP_SPAWN;
315 startMsg.procName = "testProcName";
316 AppSpawnReqMsgHandle reqHandle = nullptr;
317 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
318 EXPECT_EQ(ret, ERR_OK);
319 ret = appSpawnClient.AppspawnSetExtMsgMore(startMsg, reqHandle);
320 ASSERT_EQ(ret, 0);
321 }
322
323 /**
324 * @tc.name: AppspawnSetExtMsg_001
325 * @tc.desc: AppspawnSetExtMsg.
326 * @tc.type: FUNC
327 */
328 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsg_001, TestSize.Level2)
329 {
330 auto asc = std::make_shared<AppSpawnClient>(true);
331 AppSpawnStartMsg startMsg;
332 startMsg.renderParam = "test";
333 startMsg.code = MSG_APP_SPAWN;
334 startMsg.procName = "testProcName";
335 AppSpawnReqMsgHandle reqHandle = nullptr;
336 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
337 EXPECT_EQ(ret, ERR_OK);
338 ret = asc->AppspawnSetExtMsg(startMsg, reqHandle);
339 EXPECT_EQ(ret, ERR_OK);
340 }
341
342 /**
343 * @tc.name: AppspawnSetExtMsg_002
344 * @tc.desc: AppspawnSetExtMsg.
345 * @tc.type: FUNC
346 */
347 HWTEST_F(AppSpawnClientSecondTest, AppspawnSetExtMsg_002, TestSize.Level2)
348 {
349 auto asc = std::make_shared<AppSpawnClient>(true);
350 AppSpawnStartMsg startMsg;
351 startMsg.renderParam = "test";
352 BaseSharedBundleInfo bsbi;
353 startMsg.hspList.push_back(bsbi);
354 DataGroupInfo dgi;
355 startMsg.dataGroupInfoList.push_back(dgi);
356 startMsg.overlayInfo = "testOverlayinfo";
357 startMsg.appEnv["one"] = "testAppEnv";
358 startMsg.atomicAccount = "testAtomicAccount";
359 startMsg.code = MSG_APP_SPAWN;
360 startMsg.procName = "testProcName";
361 startMsg.apiTargetVersion = 1;
362 AppSpawnReqMsgHandle reqHandle = nullptr;
363 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
364 EXPECT_EQ(ret, ERR_OK);
365 ret = asc->AppspawnSetExtMsg(startMsg, reqHandle);
366 EXPECT_EQ(ret, ERR_OK);
367 }
368
369 // Scenario1: Test when reqHandle is nullptr.
370 HWTEST_F(AppSpawnClientSecondTest, AppspawnCreateDefaultMsg_001, TestSize.Level2)
371 {
372 auto asc = std::make_shared<AppSpawnClient>(true);
373 AppSpawnReqMsgHandle reqHandle = nullptr;
374 AppSpawnStartMsg startMsg;
375 int ret = asc->AppspawnCreateDefaultMsg(startMsg, reqHandle);
376 EXPECT_NE(ret, ERR_OK);
377 }
378
379 // Scenario1: Test when reqHandle is not nullptr.
380 HWTEST_F(AppSpawnClientSecondTest, AppspawnCreateDefaultMsg_002, TestSize.Level2)
381 {
382 auto asc = std::make_shared<AppSpawnClient>(true);
383 AppSpawnStartMsg startMsg;
384 startMsg.uid = 1;
385 startMsg.gid = 1;
386 startMsg.gids.push_back(1);
387 DataGroupInfo dgi;
388 startMsg.dataGroupInfoList.push_back(dgi);
389 startMsg.code = MSG_APP_SPAWN;
390 startMsg.procName = "testProcName";
391 AppSpawnReqMsgHandle reqHandle = nullptr;
392 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
393 EXPECT_EQ(ret, ERR_OK);
394 ret = asc->AppspawnCreateDefaultMsg(startMsg, reqHandle);
395 EXPECT_NE(ret, ERR_OK);
396 }
397
398 // Scenario1: Test when startMsg.code is MSG_APP_SPAWN and uid is negative.
399 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenUidIsNegative, TestSize.Level2)
400 {
401 AppSpawnClient appSpawnClient;
402 AppSpawnStartMsg startMsg;
403 startMsg.code = MSG_APP_SPAWN;
404 startMsg.uid = -1;
405 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
406 }
407
408 // Scenario2: Test when startMsg.code is MSG_APP_SPAWN and gid is negative.
409 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenGidIsNegative, TestSize.Level2)
410 {
411 AppSpawnClient appSpawnClient;
412 AppSpawnStartMsg startMsg;
413 startMsg.code = MSG_APP_SPAWN;
414 startMsg.gid = -1;
415 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
416 }
417
418 // Scenario3: Test when startMsg.code is MSG_APP_SPAWN and gids size is more than APP_MAX_GIDS.
419 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenGidsSizeIsMoreThanMax, TestSize.Level2)
420 {
421 AppSpawnClient appSpawnClient;
422 AppSpawnStartMsg startMsg;
423 startMsg.code = MSG_APP_SPAWN;
424 startMsg.gids.push_back(1);
425 startMsg.gids.push_back(2);
426 startMsg.gids.push_back(3);
427 startMsg.gids.push_back(4);
428 startMsg.gids.push_back(5);
429 startMsg.gids.push_back(6);
430 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
431 }
432
433 // Scenario4: Test when startMsg.code is MSG_APP_SPAWN and gids array contains negative value.
434 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenGidsArrayContainsNegative, TestSize.Level2)
435 {
436 AppSpawnClient appSpawnClient;
437 AppSpawnStartMsg startMsg;
438 startMsg.code = MSG_APP_SPAWN;
439 startMsg.gids.push_back(1);
440 startMsg.gids.push_back(-2);
441 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
442 }
443
444 // Scenario5: Test when startMsg.code is MSG_APP_SPAWN and procName is empty.
445 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenProcNameIsEmpty, TestSize.Level2)
446 {
447 AppSpawnClient appSpawnClient;
448 AppSpawnStartMsg startMsg;
449 startMsg.code = MSG_APP_SPAWN;
450 startMsg.procName = "";
451 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
452 }
453
454 // Scenario6: Test when startMsg.code is MSG_APP_SPAWN and procName size is more than MAX_PROC_NAME_LEN.
455 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenProcNameSizeIsMoreThanMax, TestSize.Level2)
456 {
457 AppSpawnClient appSpawnClient;
458 AppSpawnStartMsg startMsg;
459 startMsg.code = MSG_APP_SPAWN;
460 startMsg.procName = "a";
461 startMsg.procName.append(MAX_PROC_NAME_LEN, 'a');
462 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
463 }
464
465 // Scenario7: Test when startMsg.code is MSG_GET_RENDER_TERMINATION_STATUS and pid is negative.
466 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenPidIsNegative, TestSize.Level2)
467 {
468 AppSpawnClient appSpawnClient;
469 AppSpawnStartMsg startMsg;
470 startMsg.code = MSG_GET_RENDER_TERMINATION_STATUS;
471 startMsg.pid = -1;
472 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
473 }
474
475 // Scenario8: Test when startMsg.code is not MSG_APP_SPAWN and not MSG_GET_RENDER_TERMINATION_STATUS.
476 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenCodeIsInvalid, TestSize.Level2)
477 {
478 AppSpawnClient appSpawnClient;
479 AppSpawnStartMsg startMsg;
480 startMsg.code = 9999;
481 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
482 }
483
484 // Scenario9: Test when startMsg.code is MSG_APP_SPAWN and all conditions are valid.
485 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnTrue_WhenAllConditionsAreValid, TestSize.Level2)
486 {
487 AppSpawnClient appSpawnClient;
488 AppSpawnStartMsg startMsg;
489 startMsg.code = MSG_APP_SPAWN;
490 startMsg.uid = 1;
491 startMsg.gid = 1;
492 startMsg.procName = "test";
493 EXPECT_TRUE(appSpawnClient.VerifyMsg(startMsg));
494 }
495
496 // Scenario10: Test when startMsg.code is MSG_GET_RENDER_TERMINATION_STATUS and pid is valid.
497 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnTrue_WhenPidIsValid, TestSize.Level2)
498 {
499 AppSpawnClient appSpawnClient;
500 AppSpawnStartMsg startMsg;
501 startMsg.code = MSG_GET_RENDER_TERMINATION_STATUS;
502 startMsg.pid = 1;
503 EXPECT_TRUE(appSpawnClient.VerifyMsg(startMsg));
504 }
505
506
507 // Scenario1: Test when VerifyMsg returns false then StartProcess returns ERR_INVALID_VALUE.
508 HWTEST_F(AppSpawnClientSecondTest, StartProcess_001, TestSize.Level2)
509 {
510 AppSpawnClient appSpawnClient;
511 AppSpawnStartMsg startMsg;
512 pid_t pid;
513 EXPECT_EQ(appSpawnClient.StartProcess(startMsg, pid), ERR_INVALID_VALUE);
514 }
515
516 // Scenario2: Test when startMsg.procName is null returns false.
517 HWTEST_F(AppSpawnClientSecondTest, StartProcess_002, TestSize.Level2)
518 {
519 auto asc = std::make_shared<AppSpawnClient>(true);
520 AppSpawnStartMsg startMsg;
521 pid_t pid;
522 startMsg.uid = 0;
523 startMsg.gids.push_back(1);
524 startMsg.procName = "";
525 int ret = asc->StartProcess(startMsg, pid);
526 EXPECT_EQ(ret, ERR_INVALID_VALUE);
527
528 startMsg.uid = 0;
529 startMsg.gids.push_back(1);
530 startMsg.procName = "testProcName";
531 ret = asc->StartProcess(startMsg, pid);
532 EXPECT_NE(ret, ERR_OK);
533 }
534
535 // Scenario1: Test when reqHandle is nullptr returns ERR_OK
536 HWTEST_F(AppSpawnClientSecondTest, SetIsolationModeFlag_001, TestSize.Level2)
537 {
538 auto asc = std::make_shared<AppSpawnClient>(true);
539 AppSpawnReqMsgHandle reqHandle = nullptr;
540 AppSpawnStartMsg startMsg;
541 startMsg.isolationMode = false;
542 int ret = asc->SetIsolationModeFlag(startMsg, reqHandle);
543 EXPECT_EQ(ret, ERR_OK);
544 }
545
546 // Scenario2: Test when startMsg.isolationMode is true returns ERR_OK
547 HWTEST_F(AppSpawnClientSecondTest, SetIsolationModeFlag_002, TestSize.Level2)
548 {
549 auto asc = std::make_shared<AppSpawnClient>(true);
550 AppSpawnStartMsg startMsg;
551 startMsg.isolationMode = true;
552 startMsg.code = MSG_APP_SPAWN;
553 startMsg.procName = "testProcName";
554 AppSpawnReqMsgHandle reqHandle = nullptr;
555 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
556 EXPECT_EQ(ret, ERR_OK);
557 ret = asc->SetIsolationModeFlag(startMsg, reqHandle);
558 EXPECT_EQ(ret, ERR_OK);
559 }
560
561 // Scenario1: Test when AppSpawnReqMsgAddFd returns ERR_OK for all items in fds.
562 HWTEST_F(AppSpawnClientSecondTest, SetExtMsgFds_ShouldReturnErrOk_WhenAllItemsInFdsReturnOk, TestSize.Level2)
563 {
564 AppSpawnClient appSpawnClient;
565 AppSpawnReqMsgHandle reqHandle = nullptr;
566 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
567 EXPECT_EQ(ret, ERR_OK);
568 std::map<std::string, int32_t> fds = {{"fd1", 1}, {"fd2", 2}};
569 ret = appSpawnClient.SetExtMsgFds(reqHandle, fds);
570 ASSERT_EQ(ret, ERR_OK);
571 }
572
573 // Scenario2: Test when AppSpawnReqMsgAddFd returns ERR_NO_PERMISSION for some items in fds.
574 HWTEST_F(AppSpawnClientSecondTest,
575 SetExtMsgFds_ShouldReturnErrNoPermission_WhenSomeItemsInFdsReturnNoPermission, TestSize.Level2)
576 {
577 AppSpawnClient appSpawnClient;
578 AppSpawnReqMsgHandle reqHandle = nullptr;
579 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
580 EXPECT_EQ(ret, ERR_OK);
581 std::map<std::string, int32_t> fds = {{"fd1", 1}, {"fd2", -1}};
582 ret = appSpawnClient.SetExtMsgFds(reqHandle, fds);
583 EXPECT_NE(ret, ERR_OK);
584 }
585
586 // Scenario3: Test when AppSpawnReqMsgAddFd returns ERR_INVALID_ARGS for some items in fds.
587 HWTEST_F(AppSpawnClientSecondTest,
588 SetExtMsgFds_ShouldReturnErrInvalidArgs_WhenSomeItemsInFdsReturnInvalidArgs, TestSize.Level2)
589 {
590 AppSpawnClient appSpawnClient;
591 AppSpawnReqMsgHandle reqHandle = nullptr;
592 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
593 EXPECT_EQ(ret, ERR_OK);
594 std::map<std::string, int32_t> fds = {{"fd1", -1}, {"fd2", -1}};
595 ret = appSpawnClient.SetExtMsgFds(reqHandle, fds);
596 EXPECT_NE(ret, ERR_OK);
597 }
598
599 #ifdef SUPPORT_CHILD_PROCESS
600 // Scenario1: Test when childProcessType is CHILD_PROCESS_TYPE_NOT_CHILD then AppSpawnReqMsgSetAppFlag is called.
601 HWTEST_F(AppSpawnClientSecondTest, SetChildProcessTypeStartFlag_001, TestSize.Level2)
602 {
603 auto asc = std::make_shared<AppSpawnClient>(true);
604 AppSpawnReqMsgHandle reqHandle = nullptr;
605 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
606 EXPECT_EQ(ret, ERR_OK);
607 int32_t childProcessType = CHILD_PROCESS_TYPE_NOT_CHILD;
608 ret = asc->SetChildProcessTypeStartFlag(reqHandle, childProcessType);
609 EXPECT_EQ(ret, ERR_OK);
610 }
611
612 // Scenario2: Test when childProcessType is CHILD_PROCESS_TYPE_JS then AppSpawnReqMsgSetAppFlag is called.
613 HWTEST_F(AppSpawnClientSecondTest, SetChildProcessTypeStartFlag_002, TestSize.Level2)
614 {
615 auto asc = std::make_shared<AppSpawnClient>(true);
616 AppSpawnReqMsgHandle reqHandle = nullptr;
617 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
618 EXPECT_EQ(ret, ERR_OK);
619 int32_t childProcessType = CHILD_PROCESS_TYPE_JS;
620 ret = asc->SetChildProcessTypeStartFlag(reqHandle, childProcessType);
621 EXPECT_EQ(ret, ERR_OK);
622 }
623 #endif // SUPPORT_CHILD_PROCESS
624
625 // Scenario1: Test when startMsg.procName is null return ERR_INVALID_VALUE.
626 HWTEST_F(AppSpawnClientSecondTest, GetRenderProcessTerminationStatus_001, TestSize.Level2)
627 {
628 auto asc = std::make_shared<AppSpawnClient>(true);
629 AppSpawnStartMsg startMsg;
630 int status;
631 startMsg.uid = 0;
632 startMsg.gids.push_back(1);
633 startMsg.procName = "";
634 int ret = asc->GetRenderProcessTerminationStatus(startMsg, status);
635 EXPECT_EQ(ret, ERR_INVALID_VALUE);
636 }
637
638 // Scenario2: Test when startMsg.procName is not null return ERR_OK.
639 HWTEST_F(AppSpawnClientSecondTest, GetRenderProcessTerminationStatus_002, TestSize.Level2)
640 {
641 auto asc = std::make_shared<AppSpawnClient>(true);
642 AppSpawnStartMsg startMsg;
643 int status;
644 startMsg.uid = 0;
645 startMsg.gids.push_back(1);
646 startMsg.procName = "testProcName";
647 int ret = asc->GetRenderProcessTerminationStatus(startMsg, status);
648 EXPECT_EQ(ret, ERR_OK);
649 }
650 } // namespace AppExecFwk
651 } // namespace OHOS
652