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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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 AppSpawnReqMsgHandle reqHandle = nullptr;
362 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
363 EXPECT_EQ(ret, ERR_OK);
364 ret = asc->AppspawnSetExtMsg(startMsg, reqHandle);
365 EXPECT_EQ(ret, ERR_OK);
366 }
367
368 // Scenario1: Test when reqHandle is nullptr.
369 HWTEST_F(AppSpawnClientSecondTest, AppspawnCreateDefaultMsg_001, TestSize.Level0)
370 {
371 auto asc = std::make_shared<AppSpawnClient>(true);
372 AppSpawnReqMsgHandle reqHandle = nullptr;
373 AppSpawnStartMsg startMsg;
374 int ret = asc->AppspawnCreateDefaultMsg(startMsg, reqHandle);
375 EXPECT_NE(ret, ERR_OK);
376 }
377
378 // Scenario1: Test when reqHandle is not nullptr.
379 HWTEST_F(AppSpawnClientSecondTest, AppspawnCreateDefaultMsg_002, TestSize.Level0)
380 {
381 auto asc = std::make_shared<AppSpawnClient>(true);
382 AppSpawnStartMsg startMsg;
383 startMsg.uid = 1;
384 startMsg.gid = 1;
385 startMsg.gids.push_back(1);
386 DataGroupInfo dgi;
387 startMsg.dataGroupInfoList.push_back(dgi);
388 startMsg.code = MSG_APP_SPAWN;
389 startMsg.procName = "testProcName";
390 AppSpawnReqMsgHandle reqHandle = nullptr;
391 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
392 EXPECT_EQ(ret, ERR_OK);
393 ret = asc->AppspawnCreateDefaultMsg(startMsg, reqHandle);
394 EXPECT_NE(ret, ERR_OK);
395 }
396
397 // Scenario1: Test when startMsg.code is MSG_APP_SPAWN and uid is negative.
398 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenUidIsNegative, TestSize.Level0)
399 {
400 AppSpawnClient appSpawnClient;
401 AppSpawnStartMsg startMsg;
402 startMsg.code = MSG_APP_SPAWN;
403 startMsg.uid = -1;
404 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
405 }
406
407 // Scenario2: Test when startMsg.code is MSG_APP_SPAWN and gid is negative.
408 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenGidIsNegative, TestSize.Level0)
409 {
410 AppSpawnClient appSpawnClient;
411 AppSpawnStartMsg startMsg;
412 startMsg.code = MSG_APP_SPAWN;
413 startMsg.gid = -1;
414 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
415 }
416
417 // Scenario3: Test when startMsg.code is MSG_APP_SPAWN and gids size is more than APP_MAX_GIDS.
418 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenGidsSizeIsMoreThanMax, TestSize.Level0)
419 {
420 AppSpawnClient appSpawnClient;
421 AppSpawnStartMsg startMsg;
422 startMsg.code = MSG_APP_SPAWN;
423 startMsg.gids.push_back(1);
424 startMsg.gids.push_back(2);
425 startMsg.gids.push_back(3);
426 startMsg.gids.push_back(4);
427 startMsg.gids.push_back(5);
428 startMsg.gids.push_back(6);
429 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
430 }
431
432 // Scenario4: Test when startMsg.code is MSG_APP_SPAWN and gids array contains negative value.
433 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenGidsArrayContainsNegative, TestSize.Level0)
434 {
435 AppSpawnClient appSpawnClient;
436 AppSpawnStartMsg startMsg;
437 startMsg.code = MSG_APP_SPAWN;
438 startMsg.gids.push_back(1);
439 startMsg.gids.push_back(-2);
440 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
441 }
442
443 // Scenario5: Test when startMsg.code is MSG_APP_SPAWN and procName is empty.
444 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenProcNameIsEmpty, TestSize.Level0)
445 {
446 AppSpawnClient appSpawnClient;
447 AppSpawnStartMsg startMsg;
448 startMsg.code = MSG_APP_SPAWN;
449 startMsg.procName = "";
450 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
451 }
452
453 // Scenario6: Test when startMsg.code is MSG_APP_SPAWN and procName size is more than MAX_PROC_NAME_LEN.
454 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenProcNameSizeIsMoreThanMax, TestSize.Level0)
455 {
456 AppSpawnClient appSpawnClient;
457 AppSpawnStartMsg startMsg;
458 startMsg.code = MSG_APP_SPAWN;
459 startMsg.procName = "a";
460 startMsg.procName.append(MAX_PROC_NAME_LEN, 'a');
461 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
462 }
463
464 // Scenario7: Test when startMsg.code is MSG_GET_RENDER_TERMINATION_STATUS and pid is negative.
465 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenPidIsNegative, TestSize.Level0)
466 {
467 AppSpawnClient appSpawnClient;
468 AppSpawnStartMsg startMsg;
469 startMsg.code = MSG_GET_RENDER_TERMINATION_STATUS;
470 startMsg.pid = -1;
471 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
472 }
473
474 // Scenario8: Test when startMsg.code is not MSG_APP_SPAWN and not MSG_GET_RENDER_TERMINATION_STATUS.
475 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnFalse_WhenCodeIsInvalid, TestSize.Level0)
476 {
477 AppSpawnClient appSpawnClient;
478 AppSpawnStartMsg startMsg;
479 startMsg.code = 9999;
480 EXPECT_FALSE(appSpawnClient.VerifyMsg(startMsg));
481 }
482
483 // Scenario9: Test when startMsg.code is MSG_APP_SPAWN and all conditions are valid.
484 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnTrue_WhenAllConditionsAreValid, TestSize.Level0)
485 {
486 AppSpawnClient appSpawnClient;
487 AppSpawnStartMsg startMsg;
488 startMsg.code = MSG_APP_SPAWN;
489 startMsg.uid = 1;
490 startMsg.gid = 1;
491 startMsg.procName = "test";
492 EXPECT_TRUE(appSpawnClient.VerifyMsg(startMsg));
493 }
494
495 // Scenario10: Test when startMsg.code is MSG_GET_RENDER_TERMINATION_STATUS and pid is valid.
496 HWTEST_F(AppSpawnClientSecondTest, VerifyMsg_ShouldReturnTrue_WhenPidIsValid, TestSize.Level0)
497 {
498 AppSpawnClient appSpawnClient;
499 AppSpawnStartMsg startMsg;
500 startMsg.code = MSG_GET_RENDER_TERMINATION_STATUS;
501 startMsg.pid = 1;
502 EXPECT_TRUE(appSpawnClient.VerifyMsg(startMsg));
503 }
504
505
506 // Scenario1: Test when VerifyMsg returns false then StartProcess returns ERR_INVALID_VALUE.
507 HWTEST_F(AppSpawnClientSecondTest, StartProcess_001, TestSize.Level0)
508 {
509 AppSpawnClient appSpawnClient;
510 AppSpawnStartMsg startMsg;
511 pid_t pid;
512 EXPECT_EQ(appSpawnClient.StartProcess(startMsg, pid), ERR_INVALID_VALUE);
513 }
514
515 // Scenario2: Test when startMsg.procName is null returns false.
516 HWTEST_F(AppSpawnClientSecondTest, StartProcess_002, TestSize.Level0)
517 {
518 auto asc = std::make_shared<AppSpawnClient>(true);
519 AppSpawnStartMsg startMsg;
520 pid_t pid;
521 startMsg.uid = 0;
522 startMsg.gids.push_back(1);
523 startMsg.procName = "";
524 int ret = asc->StartProcess(startMsg, pid);
525 EXPECT_EQ(ret, ERR_INVALID_VALUE);
526
527 startMsg.uid = 0;
528 startMsg.gids.push_back(1);
529 startMsg.procName = "testProcName";
530 ret = asc->StartProcess(startMsg, pid);
531 EXPECT_NE(ret, ERR_OK);
532 }
533
534 // Scenario1: Test when reqHandle is nullptr returns ERR_OK
535 HWTEST_F(AppSpawnClientSecondTest, SetIsolationModeFlag_001, TestSize.Level0)
536 {
537 auto asc = std::make_shared<AppSpawnClient>(true);
538 AppSpawnReqMsgHandle reqHandle = nullptr;
539 AppSpawnStartMsg startMsg;
540 startMsg.isolationMode = false;
541 int ret = asc->SetIsolationModeFlag(startMsg, reqHandle);
542 EXPECT_EQ(ret, ERR_OK);
543 }
544
545 // Scenario2: Test when startMsg.isolationMode is true returns ERR_OK
546 HWTEST_F(AppSpawnClientSecondTest, SetIsolationModeFlag_002, TestSize.Level0)
547 {
548 auto asc = std::make_shared<AppSpawnClient>(true);
549 AppSpawnStartMsg startMsg;
550 startMsg.isolationMode = true;
551 startMsg.code = MSG_APP_SPAWN;
552 startMsg.procName = "testProcName";
553 AppSpawnReqMsgHandle reqHandle = nullptr;
554 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(startMsg.code), startMsg.procName.c_str(), &reqHandle);
555 EXPECT_EQ(ret, ERR_OK);
556 ret = asc->SetIsolationModeFlag(startMsg, reqHandle);
557 EXPECT_EQ(ret, ERR_OK);
558 }
559
560 // Scenario1: Test when AppSpawnReqMsgAddFd returns ERR_OK for all items in fds.
561 HWTEST_F(AppSpawnClientSecondTest, SetExtMsgFds_ShouldReturnErrOk_WhenAllItemsInFdsReturnOk, TestSize.Level0)
562 {
563 AppSpawnClient appSpawnClient;
564 AppSpawnReqMsgHandle reqHandle = nullptr;
565 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
566 EXPECT_EQ(ret, ERR_OK);
567 std::map<std::string, int32_t> fds = {{"fd1", 1}, {"fd2", 2}};
568 ret = appSpawnClient.SetExtMsgFds(reqHandle, fds);
569 ASSERT_EQ(ret, ERR_OK);
570 }
571
572 // Scenario2: Test when AppSpawnReqMsgAddFd returns ERR_NO_PERMISSION for some items in fds.
573 HWTEST_F(AppSpawnClientSecondTest,
574 SetExtMsgFds_ShouldReturnErrNoPermission_WhenSomeItemsInFdsReturnNoPermission, TestSize.Level0)
575 {
576 AppSpawnClient appSpawnClient;
577 AppSpawnReqMsgHandle reqHandle = nullptr;
578 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
579 EXPECT_EQ(ret, ERR_OK);
580 std::map<std::string, int32_t> fds = {{"fd1", 1}, {"fd2", -1}};
581 ret = appSpawnClient.SetExtMsgFds(reqHandle, fds);
582 EXPECT_NE(ret, ERR_OK);
583 }
584
585 // Scenario3: Test when AppSpawnReqMsgAddFd returns ERR_INVALID_ARGS for some items in fds.
586 HWTEST_F(AppSpawnClientSecondTest,
587 SetExtMsgFds_ShouldReturnErrInvalidArgs_WhenSomeItemsInFdsReturnInvalidArgs, TestSize.Level0)
588 {
589 AppSpawnClient appSpawnClient;
590 AppSpawnReqMsgHandle reqHandle = nullptr;
591 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
592 EXPECT_EQ(ret, ERR_OK);
593 std::map<std::string, int32_t> fds = {{"fd1", -1}, {"fd2", -1}};
594 ret = appSpawnClient.SetExtMsgFds(reqHandle, fds);
595 EXPECT_NE(ret, ERR_OK);
596 }
597
598 #ifdef SUPPORT_CHILD_PROCESS
599 // Scenario1: Test when childProcessType is CHILD_PROCESS_TYPE_NOT_CHILD then AppSpawnReqMsgSetAppFlag is called.
600 HWTEST_F(AppSpawnClientSecondTest, SetChildProcessTypeStartFlag_001, TestSize.Level0)
601 {
602 auto asc = std::make_shared<AppSpawnClient>(true);
603 AppSpawnReqMsgHandle reqHandle = nullptr;
604 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
605 EXPECT_EQ(ret, ERR_OK);
606 int32_t childProcessType = CHILD_PROCESS_TYPE_NOT_CHILD;
607 ret = asc->SetChildProcessTypeStartFlag(reqHandle, childProcessType);
608 EXPECT_EQ(ret, ERR_OK);
609 }
610
611 // Scenario2: Test when childProcessType is CHILD_PROCESS_TYPE_JS then AppSpawnReqMsgSetAppFlag is called.
612 HWTEST_F(AppSpawnClientSecondTest, SetChildProcessTypeStartFlag_002, TestSize.Level0)
613 {
614 auto asc = std::make_shared<AppSpawnClient>(true);
615 AppSpawnReqMsgHandle reqHandle = nullptr;
616 int ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(MSG_APP_SPAWN), "testProcName", &reqHandle);
617 EXPECT_EQ(ret, ERR_OK);
618 int32_t childProcessType = CHILD_PROCESS_TYPE_JS;
619 ret = asc->SetChildProcessTypeStartFlag(reqHandle, childProcessType);
620 EXPECT_EQ(ret, ERR_OK);
621 }
622 #endif // SUPPORT_CHILD_PROCESS
623
624 // Scenario1: Test when startMsg.procName is null return ERR_INVALID_VALUE.
625 HWTEST_F(AppSpawnClientSecondTest, GetRenderProcessTerminationStatus_001, TestSize.Level0)
626 {
627 auto asc = std::make_shared<AppSpawnClient>(true);
628 AppSpawnStartMsg startMsg;
629 int status;
630 startMsg.uid = 0;
631 startMsg.gids.push_back(1);
632 startMsg.procName = "";
633 int ret = asc->GetRenderProcessTerminationStatus(startMsg, status);
634 EXPECT_EQ(ret, ERR_INVALID_VALUE);
635 }
636
637 // Scenario2: Test when startMsg.procName is not null return ERR_OK.
638 HWTEST_F(AppSpawnClientSecondTest, GetRenderProcessTerminationStatus_002, TestSize.Level0)
639 {
640 auto asc = std::make_shared<AppSpawnClient>(true);
641 AppSpawnStartMsg startMsg;
642 int status;
643 startMsg.uid = 0;
644 startMsg.gids.push_back(1);
645 startMsg.procName = "testProcName";
646 int ret = asc->GetRenderProcessTerminationStatus(startMsg, status);
647 EXPECT_EQ(ret, ERR_OK);
648 }
649 } // namespace AppExecFwk
650 } // namespace OHOS
651