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 #include <cerrno>
19 #include <memory>
20 #include <string>
21
22 #include "appspawn_server.h"
23 #include "appspawn_service.h"
24 #include "json_utils.h"
25 #include "parameter.h"
26 #include "sandbox_utils.h"
27 #include "securec.h"
28
29 #include "app_spawn_stub.h"
30 #include "app_spawn_test_helper.h"
31
32 using namespace testing;
33 using namespace testing::ext;
34 using namespace OHOS::AppSpawn;
35 using nlohmann::json;
36
37 namespace OHOS {
38 AppSpawnTestHelper g_testHelper;
39
40 class AppSpawnSandboxTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 };
47
SetUpTestCase()48 void AppSpawnSandboxTest::SetUpTestCase() {}
49
TearDownTestCase()50 void AppSpawnSandboxTest::TearDownTestCase() {}
51
SetUp()52 void AppSpawnSandboxTest::SetUp() {}
53
TearDown()54 void AppSpawnSandboxTest::TearDown() {}
55
GetTestAppProperty()56 static AppSpawningCtx *GetTestAppProperty()
57 {
58 AppSpawnClientHandle clientHandle = nullptr;
59 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
60 APPSPAWN_CHECK(ret == 0, return nullptr, "Failed to create reqMgr");
61 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
62 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, return nullptr, "Failed to create req");
63 return g_testHelper.GetAppProperty(clientHandle, reqHandle);
64 }
65
GetTestAppPropertyWithExtInfo(const char * name,const char * value)66 static AppSpawningCtx *GetTestAppPropertyWithExtInfo(const char *name, const char *value)
67 {
68 AppSpawnClientHandle clientHandle = nullptr;
69 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
70 APPSPAWN_CHECK(ret == 0, return nullptr, "Failed to create reqMgr");
71 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
72 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, return nullptr, "Failed to create req");
73 AppSpawnReqMsgAddStringInfo(reqHandle, name, value);
74 return g_testHelper.GetAppProperty(clientHandle, reqHandle);
75 }
76
77 /**
78 * @tc.name: App_Spawn_Sandbox_005
79 * @tc.desc: load system config SetAppSandboxProperty by App ohos.samples.ecg.
80 * @tc.type: FUNC
81 * @tc.require:issueI5NTX6
82 * @tc.author:
83 */
84 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_08, TestSize.Level0)
85 {
86 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_08 start";
87 g_testHelper.SetTestUid(1000); // 1000 test
88 g_testHelper.SetTestGid(1000); // 1000 test
89 g_testHelper.SetProcessName("ohos.samples.ecg");
90 AppSpawningCtx *appProperty = GetTestAppProperty();
91 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
92 DeleteAppSpawningCtx(appProperty);
93 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_08 end";
94 }
95
96 /**
97 * @tc.name: App_Spawn_Sandbox_09
98 * @tc.desc: load system config SetAppSandboxProperty by App com.ohos.dlpmanager.
99 * @tc.type: FUNC
100 * @tc.require:issueI5NTX6
101 * @tc.author:
102 */
103 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_09, TestSize.Level0)
104 {
105 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09 start";
106 g_testHelper.SetProcessName("com.ohos.dlpmanager");
107 g_testHelper.SetTestApl("normal");
108 AppSpawningCtx *appProperty = GetTestAppProperty();
109 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
110 DeleteAppSpawningCtx(appProperty);
111 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09 end";
112 }
113
114 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_09_1, TestSize.Level0)
115 {
116 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09_1 start";
117 g_testHelper.SetTestUid(1000); // 1000 test
118 g_testHelper.SetTestGid(1000); // 1000 test
119 g_testHelper.SetProcessName("com.ohos.dlpmanager");
120 g_testHelper.SetTestApl("normal");
121 AppSpawningCtx *appProperty = GetTestAppProperty();
122 int ret = OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(nullptr);
123 EXPECT_NE(ret, 0);
124 DeleteAppSpawningCtx(appProperty);
125 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09_1 end";
126 }
127
128 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_09_2, TestSize.Level0)
129 {
130 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09_1 start";
131 g_testHelper.SetTestUid(1000); // 1000 test
132 g_testHelper.SetTestGid(1000); // 1000 test
133 g_testHelper.SetProcessName("");
134 g_testHelper.SetTestApl("normal");
135 AppSpawningCtx *appProperty = GetTestAppProperty();
136
137 int ret = OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
138 EXPECT_NE(ret, 0);
139 DeleteAppSpawningCtx(appProperty);
140 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09_1 end";
141 }
142
143 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_09_3, TestSize.Level0)
144 {
145 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09_1 start";
146 g_testHelper.SetTestUid(1000); // 1000 test
147 g_testHelper.SetTestGid(1000); // 1000 test
148 g_testHelper.SetProcessName("com.\\ohos.dlpmanager");
149 g_testHelper.SetTestApl("normal");
150 AppSpawningCtx *appProperty = GetTestAppProperty();
151 int ret = OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
152 EXPECT_NE(ret, 0);
153 DeleteAppSpawningCtx(appProperty);
154 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09_1 end";
155 }
156
157 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_09_4, TestSize.Level0)
158 {
159 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09_1 start";
160 g_testHelper.SetTestUid(1000); // 1000 test
161 g_testHelper.SetTestGid(1000); // 1000 test
162 g_testHelper.SetProcessName("com./ohos.dlpmanager");
163 g_testHelper.SetTestApl("normal");
164 AppSpawningCtx *appProperty = GetTestAppProperty();
165 int ret = OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
166 EXPECT_NE(ret, 0);
167 DeleteAppSpawningCtx(appProperty);
168 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09_1 end";
169 }
170
171 /**
172 * @tc.name: App_Spawn_Sandbox_10
173 * @tc.desc: parse config define by self Set App Sandbox Property.
174 * @tc.type: FUNC
175 * @tc.require:issueI5NTX6
176 * @tc.author:
177 */
178 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_10, TestSize.Level0)
179 {
180 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_10 start";
181 std::string mJsconfig = "{ \
182 \"common\":[{ \
183 \"top-sandbox-switch\": \"ON\", \
184 \"app-base\":[{ \
185 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
186 \"mount-paths\" : [{ \
187 \"src-path\" : \"/config\", \
188 \"sandbox-path\" : \"/config\", \
189 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
190 \"check-action-status\": \"false\", \
191 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \" \
192 }], \
193 \"symbol-links\" : [] \
194 }] \
195 }], \
196 \"individual\": [] \
197 }";
198 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
199
200 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
201 GTEST_LOG_(INFO) << "SetAppSandboxProperty start" << std::endl;
202 g_testHelper.SetTestUid(1000); // 1000 test
203 g_testHelper.SetTestGid(1000); // 1000 test
204 g_testHelper.SetProcessName("test.appspawn");
205 g_testHelper.SetTestApl("normal");
206 AppSpawningCtx *appProperty = GetTestAppProperty();
207 GTEST_LOG_(INFO) << "SetAppSandboxProperty section 2" << std::endl;
208 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
209 DeleteAppSpawningCtx(appProperty);
210 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_10 end";
211 }
212
213 /**
214 * @tc.name: App_Spawn_Sandbox_13
215 * @tc.desc: parse config define by self Set App Sandbox Property.
216 * @tc.type: FUNC
217 * @tc.require:issueI5NTX6
218 * @tc.author:
219 */
220 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_13, TestSize.Level0)
221 {
222 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_13 start";
223 std::string mJsconfig = "{ \
224 \"common\":[{ \
225 \"app-base\":[{ \
226 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
227 \"mount-paths\" : [{ \
228 \"src-path\" : \"/config\", \
229 \"sandbox-path\" : \"\", \
230 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
231 \"check-action-status\": \"false\", \
232 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \" \
233 }], \
234 \"symbol-links\" : [] \
235 }] \
236 }], \
237 \"individual\": [] \
238 }";
239 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
240
241 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
242
243 GTEST_LOG_(INFO) << "SetAppSandboxProperty start" << std::endl;
244 g_testHelper.SetTestUid(1000); // 1000 test
245 g_testHelper.SetTestGid(1000); // 1000 test
246 g_testHelper.SetProcessName("test.appspawn");
247 g_testHelper.SetTestApl("normal");
248 AppSpawningCtx *appProperty = GetTestAppProperty();
249 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
250 DeleteAppSpawningCtx(appProperty);
251 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_13 end";
252 }
253
254 /**
255 * @tc.name: App_Spawn_Sandbox_14
256 * @tc.desc: parse sandbox config without sandbox-root label
257 * @tc.type: FUNC
258 * @tc.require:issueI5NTX6
259 * @tc.author:
260 */
261 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_14, TestSize.Level0)
262 {
263 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_14 start";
264 std::string mJsconfig = "{ \
265 \"common\":[{ \
266 \"app-base\":[{ \
267 \"mount-paths\" : [{ \
268 \"src-path\" : \"/config\", \
269 \"sandbox-path\" : \"\", \
270 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
271 \"check-action-status\": \"false\", \
272 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \" \
273 }], \
274 \"symbol-links\" : [] \
275 }] \
276 }], \
277 \"individual\": [] \
278 }";
279 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
280
281 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
282
283 GTEST_LOG_(INFO) << "SetAppSandboxProperty start" << std::endl;
284
285 g_testHelper.SetTestUid(1000); // 1000 test
286 g_testHelper.SetTestGid(1000); // 1000 test
287 g_testHelper.SetProcessName("test.appspawn");
288 g_testHelper.SetTestApl("normal");
289 AppSpawningCtx *appProperty = GetTestAppProperty();
290
291 GTEST_LOG_(INFO) << "SetAppSandboxProperty section 2" << std::endl;
292 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
293 DeleteAppSpawningCtx(appProperty);
294 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_14 end";
295 }
296
297 /**
298 * @tc.name: App_Spawn_Sandbox_15
299 * @tc.desc: parse app sandbox config with PackageName_index label
300 * @tc.type: FUNC
301 * @tc.require:issueI5NTX6
302 * @tc.author:
303 */
304 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_15, TestSize.Level0)
305 {
306 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_15 start";
307 std::string mJsconfig = "{ \
308 \"common\":[{ \
309 \"app-base\":[{ \
310 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName_index>\", \
311 \"mount-paths\" : [{ \
312 \"src-path\" : \"/config\", \
313 \"sandbox-path\" : \"\", \
314 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
315 \"check-action-status\": \"false\", \
316 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \" \
317 }], \
318 \"symbol-links\" : [] \
319 }] \
320 }], \
321 \"individual\": [] \
322 }";
323 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
324
325 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
326
327 GTEST_LOG_(INFO) << "SetAppSandboxProperty start" << std::endl;
328 g_testHelper.SetTestUid(1000); // 1000 test
329 g_testHelper.SetTestGid(1000); // 1000 test
330 g_testHelper.SetProcessName("test.appspawn");
331 g_testHelper.SetTestApl("normal");
332 AppSpawningCtx *appProperty = GetTestAppProperty();
333 GTEST_LOG_(INFO) << "SetAppSandboxProperty section 2" << std::endl;
334 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
335 DeleteAppSpawningCtx(appProperty);
336 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_15 end";
337 }
338
339 /**
340 * @tc.name: App_Spawn_Sandbox_16
341 * @tc.desc: parse config define by self Set App Sandbox Property.
342 * @tc.type: FUNC
343 * @tc.require:issueI5NTX6
344 * @tc.author:
345 */
346 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_16, TestSize.Level0)
347 {
348 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_16 start";
349 std::string mJsconfig = "{ \
350 \"common\":[{ \
351 \"app-base\":[{ \
352 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
353 \"symbol-links\" : [] \
354 }] \
355 }], \
356 \"individual\": [] \
357 }";
358 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
359
360 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
361
362 GTEST_LOG_(INFO) << "SetAppSandboxProperty start" << std::endl;
363 g_testHelper.SetTestUid(1000); // 1000 test
364 g_testHelper.SetTestGid(1000); // 1000 test
365 g_testHelper.SetProcessName("test.appspawn");
366 g_testHelper.SetTestApl("normal");
367 AppSpawningCtx *appProperty = GetTestAppProperty();
368 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
369 DeleteAppSpawningCtx(appProperty);
370 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_16 end";
371 }
372
373 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_17, TestSize.Level0)
374 {
375 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09 start";
376 nlohmann::json appSandboxConfig;
377 bool rc = JsonUtils::GetJsonObjFromJson(appSandboxConfig, "");
378 EXPECT_FALSE(rc);
379 std::string path(256, 'w'); // 256 test
380 rc = JsonUtils::GetJsonObjFromJson(appSandboxConfig, path);
381 EXPECT_FALSE(rc);
382
383 std::string mJsconfig = "{ \
384 \"common\":[{ \
385 \"app-base\":[{ \
386 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
387 \"symbol-links\" : [] \
388 }] \
389 }], \
390 \"individual\": [] \
391 }";
392 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
393 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
394
395 std::string value;
396 rc = JsonUtils::GetStringFromJson(j_config, "common", value);
397 EXPECT_FALSE(rc);
398 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_09 end";
399 }
400
401 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_18, TestSize.Level0)
402 {
403 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_18 start";
404 std::string mJsconfig1 = "{ \
405 \"sandbox-switch\": \"ON\", \
406 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\" \
407 }";
408 nlohmann::json j_config1 = nlohmann::json::parse(mJsconfig1.c_str());
409 bool ret = OHOS::AppSpawn::SandboxUtils::GetSbxSwitchStatusByConfig(j_config1);
410 EXPECT_TRUE(ret);
411
412 std::string mJsconfig2 = "{ \
413 \"sandbox-switch\": \"OFF\", \
414 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\" \
415 }";
416 nlohmann::json j_config2 = nlohmann::json::parse(mJsconfig2.c_str());
417 ret = OHOS::AppSpawn::SandboxUtils::GetSbxSwitchStatusByConfig(j_config2);
418 EXPECT_FALSE(ret);
419
420 std::string mJsconfig3 = "{ \
421 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\" \
422 }";
423 nlohmann::json j_config3 = nlohmann::json::parse(mJsconfig3.c_str());
424 ret = OHOS::AppSpawn::SandboxUtils::GetSbxSwitchStatusByConfig(j_config3);
425 EXPECT_TRUE(ret);
426 }
427
428 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_20, TestSize.Level0)
429 {
430 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_20 start";
431 std::string mJsconfig = "{ \
432 \"common\":[{ \
433 \"top-sandbox-switch\": \"OFF\", \
434 \"app-base\":[{ \
435 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
436 \"symbol-links\" : [] \
437 }] \
438 }], \
439 \"individual\": [] \
440 }";
441 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
442 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
443
444 g_testHelper.SetTestUid(1000); // 1000 test
445 g_testHelper.SetTestGid(1000); // 1000 test
446 g_testHelper.SetProcessName("test.appspawn");
447 g_testHelper.SetTestApl("normal");
448 AppSpawningCtx *appProperty = GetTestAppProperty();
449
450 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
451
452 std::string mJsconfig1 = "{ \
453 \"common\":[{ \
454 \"app-base\":[{ \
455 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
456 \"symbol-links\" : [] \
457 }] \
458 }], \
459 \"individual\": [{ \
460 \"test.bundle.name\" : [{ \
461 \"sandbox-switch\": \"OFF\", \
462 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\" \
463 }] \
464 }] \
465 }";
466 nlohmann::json j_config1 = nlohmann::json::parse(mJsconfig1.c_str());
467 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config1, SANBOX_APP_JSON_CONFIG);
468 OHOS::AppSpawn::SandboxUtils::SetAppSandboxProperty(appProperty);
469 DeleteAppSpawningCtx(appProperty);
470 }
471
472 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_21, TestSize.Level0)
473 {
474 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_21 start";
475
476 bool ret = OHOS::AppSpawn::SandboxUtils::CheckBundleNameForPrivate(std::string("__internal__"));
477 EXPECT_FALSE(ret);
478 }
479
480 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_22, TestSize.Level0)
481 {
482 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_22 start";
483 std::string mJsconfig1 = "{ \
484 \"common\":[], \
485 \"individual\": [] \
486 }";
487 nlohmann::json j_config1 = nlohmann::json::parse(mJsconfig1.c_str());
488 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config1, SANBOX_APP_JSON_CONFIG);
489
490 g_testHelper.SetTestUid(1000); // 1000 test
491 g_testHelper.SetTestGid(1000); // 1000 test
492 g_testHelper.SetProcessName("test.appspawn");
493 g_testHelper.SetTestApl("system_basic");
494 AppSpawningCtx *appProperty = GetTestAppProperty();
495
496 const char *strl1 = "/mnt/sandbox/100/test.bundle1";
497 std::string testBundle = strl1;
498 int ret = OHOS::AppSpawn::SandboxUtils::SetCommonAppSandboxProperty(appProperty, testBundle);
499 DeleteAppSpawningCtx(appProperty);
500 EXPECT_EQ(ret, 0);
501 }
502
503 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_23, TestSize.Level0)
504 {
505 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_23 start";
506 const char *strl1 = "/mnt/sandbox/100/test.bundle1";
507 std::string testBundle = strl1;
508 int ret = OHOS::AppSpawn::SandboxUtils::SetRenderSandboxProperty(nullptr, testBundle);
509 EXPECT_EQ(ret, 0);
510 }
511
512 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_24, TestSize.Level0)
513 {
514 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_24 start";
515
516 g_testHelper.SetTestUid(1000); // 1000 test
517 g_testHelper.SetTestGid(1000); // 1000 test
518 g_testHelper.SetProcessName("test.bundle.name1");
519 g_testHelper.SetTestApl("system_basic");
520 AppSpawningCtx *appProperty = GetTestAppProperty();
521
522 std::string mJsconfig1 = "{ \
523 \"common\":[{ \
524 \"app-base\":[{ \
525 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
526 \"symbol-links\" : [{ \
527 \"target-name\" : \"/system/etc\", \
528 \"link-name\" : \"/etc\", \
529 \"check-action-status\": \"false\" \
530 }, { \
531 \"target\" : \"/system/etc\", \
532 \"link\" : \"/etc\", \
533 \"check\": \"false\" \
534 }] \
535 }], \
536 \"app-resources\" : [{ \
537 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
538 \"mount-paths\" : [], \
539 \"symbol-links\" : [] \
540 }] \
541 }] \
542 }";
543 nlohmann::json j_config1 = nlohmann::json::parse(mJsconfig1.c_str());
544 int ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFileCommonSymlink(appProperty, j_config1);
545 EXPECT_EQ(ret, 0);
546
547 std::string mJsconfig2 = "{ \
548 \"common\":[{ \
549 \"app-base\":[{ \
550 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
551 \"symbol-links\" : [{ \
552 \"target-name\" : \"/data/test123\", \
553 \"link-name\" : \"/test123\", \
554 \"check-action-status\": \"true\" \
555 }] \
556 }] \
557 }] \
558 }";
559 nlohmann::json j_config2 = nlohmann::json::parse(mJsconfig2.c_str());
560 ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFileCommonSymlink(appProperty, j_config2);
561 DeleteAppSpawningCtx(appProperty);
562 EXPECT_EQ(ret, 0);
563 }
564
565 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_25, TestSize.Level0)
566 {
567 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_25 start";
568 g_testHelper.SetTestUid(1000); // 1000 test
569 g_testHelper.SetTestGid(1000); // 1000 test
570 g_testHelper.SetProcessName("test.bundle.wps");
571 g_testHelper.SetTestApl("system_basic");
572 g_testHelper.SetTestMsgFlags(4); // 4 is test parameter
573 AppSpawningCtx *appProperty = GetTestAppProperty();
574
575 std::string mJsconfig1 = "{ \
576 \"common\":[{ \
577 \"app-resources\" : [{ \
578 \"flags\": \"DLP_MANAGER\", \
579 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
580 \"mount-paths\" : [{ \
581 \"src-path\" : \"/data/app/el2/<currentUserId>/base/<PackageName_index>\", \
582 \"sandbox-path\" : \"/data/storage/el2/base\", \
583 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
584 \"check-action-status\": \"false\", \
585 \"app-apl-name\": \"test123\", \
586 \"fs-type\": \"ext4\" \
587 }, { \
588 \"src-path\" : \"/data/app/el1/<currentUserId>/database/<PackageName_index>\", \
589 \"sandbox-path\" : \"/data/storage/el1/database\", \
590 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
591 \"check-action-status\": \"false\" \
592 }], \
593 \"symbol-links\" : [] \
594 }] \
595 }] \
596 }";
597 nlohmann::json j_config1 = nlohmann::json::parse(mJsconfig1.c_str());
598 int ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFileCommonBind(appProperty, j_config1);
599 EXPECT_EQ(ret, 0);
600
601 ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFileCommonFlagsPointHandle(appProperty, j_config1);
602 DeleteAppSpawningCtx(appProperty);
603 EXPECT_EQ(ret, 0);
604 }
605
606 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_26, TestSize.Level0)
607 {
608 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_26 start";
609 g_testHelper.SetTestUid(1000); // 1000 test
610 g_testHelper.SetTestGid(1000); // 1000 test
611 g_testHelper.SetProcessName("com.ohos.dlpmanager");
612 g_testHelper.SetTestApl("system_basic");
613 AppSpawningCtx *appProperty = GetTestAppProperty();
614 std::string mJsconfig2 = "{ \
615 \"common\":[{ \
616 \"app-base\" : [{ \
617 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
618 \"mount-paths\" : [{ \
619 \"src-path\" : \"/data/app/el2/<currentUserId>/base/<PackageName_index>\", \
620 \"sandbox-path\" : \"/data/storage/el2/base\", \
621 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
622 \"fs-type\" : \"ext4\", \
623 \"check-action-status\": \"true\" \
624 }] \
625 }] \
626 }] \
627 }";
628 nlohmann::json j_config2 = nlohmann::json::parse(mJsconfig2.c_str());
629 int ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFileCommonBind(appProperty, j_config2);
630 DeleteAppSpawningCtx(appProperty);
631 EXPECT_NE(ret, 0);
632 }
633
634 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_27, TestSize.Level0)
635 {
636 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_27 start";
637 g_testHelper.SetTestUid(1000); // 1000 test
638 g_testHelper.SetTestGid(1000); // 1000 test
639 g_testHelper.SetProcessName("test.bundle.name");
640 g_testHelper.SetTestApl("system_basic");
641 AppSpawningCtx *appProperty = GetTestAppProperty();
642
643 std::string mJsconfig1 = "{ \
644 \"individual\": [{ \
645 \"test.bundle.name\" : [{ \
646 \"sandbox-switch\": \"OFF\", \
647 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\" \
648 }] \
649 }] \
650 }";
651 nlohmann::json j_config1 = nlohmann::json::parse(mJsconfig1.c_str());
652 int ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFilePrivateFlagsPointHandle(appProperty, j_config1);
653 EXPECT_EQ(ret, 0);
654
655 std::string mJsconfig2 = "{ \
656 \"individual\": [{ \
657 \"test.bundle.name\" : [{ \
658 \"flags-point\" : [{ \
659 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
660 \"mount-paths\" : [{ \
661 \"src-path\" : \"/data/app/el2/<currentUserId>/base/<PackageName_index>\", \
662 \"sandbox-path\" : \"/data/storage/el2/base\", \
663 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
664 \"check-action-status\": \"false\" \
665 }]\
666 }], \
667 \"sandbox-switch\": \"OFF\", \
668 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\" \
669 }] \
670 }] \
671 }";
672 nlohmann::json j_config2 = nlohmann::json::parse(mJsconfig2.c_str());
673 ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFilePrivateFlagsPointHandle(appProperty, j_config2);
674 DeleteAppSpawningCtx(appProperty);
675 EXPECT_EQ(ret, 0);
676 }
677
678 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_28, TestSize.Level0)
679 {
680 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_28 start";
681 g_testHelper.SetTestUid(1000); // 1000 test
682 g_testHelper.SetTestGid(1000); // 1000 test
683 g_testHelper.SetProcessName("test.bundle.name");
684 g_testHelper.SetTestApl("system_basic");
685 g_testHelper.SetTestMsgFlags(4); // 4 is test parameter
686 AppSpawningCtx *appProperty = GetTestAppProperty();
687
688 std::string mJsconfig3 = "{ \
689 \"individual\": [{ \
690 \"test.bundle.name\" : [{ \
691 \"flags-point\" : [{ \
692 \"flags\": \"DLP_MANAGER\", \
693 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
694 \"mount-paths\" : [{ \
695 \"src-path\" : \"/data/app/el2/<currentUserId>/base/<PackageName_index>\", \
696 \"sandbox-path\" : \"/data/storage/el2/base\", \
697 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
698 \"check-action-status\": \"false\" \
699 }] \
700 }], \
701 \"sandbox-switch\": \"OFF\", \
702 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\" \
703 }] \
704 }] \
705 }";
706 nlohmann::json j_config3 = nlohmann::json::parse(mJsconfig3.c_str());
707 int ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFilePrivateFlagsPointHandle(appProperty, j_config3);
708 DeleteAppSpawningCtx(appProperty);
709 EXPECT_EQ(ret, 0);
710 }
711
712 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_29, TestSize.Level0)
713 {
714 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_29 start";
715 g_testHelper.SetTestUid(1000); // 1000 test
716 g_testHelper.SetTestGid(1000); // 1000 test
717 g_testHelper.SetProcessName("test.bundle.name");
718 g_testHelper.SetTestApl("system_basic");
719 g_testHelper.SetTestMsgFlags(4); // 4 is test parameter
720 AppSpawningCtx *appProperty = GetTestAppProperty();
721
722 std::string mJsconfig3 = "{ \
723 \"individual\": [{ \
724 \"test.bundle.name\" : [{ \
725 \"flags-point\" : [{ \
726 \"flags\": \"DLP_MANAGER\", \
727 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
728 \"mount-paths\" : [{ \
729 \"src-path\" : \"/data/app/el2/<currentUserId>/base/<PackageName_index>\", \
730 \"sandbox-path\" : \"/data/storage/el2/base\", \
731 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
732 \"check-action-status\": \"false\" \
733 }] \
734 }], \
735 \"sandbox-switch\": \"OFF\", \
736 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\" \
737 }] \
738 }] \
739 }";
740 nlohmann::json j_config3 = nlohmann::json::parse(mJsconfig3.c_str());
741 int ret = OHOS::AppSpawn::SandboxUtils::DoSandboxFilePrivateFlagsPointHandle(appProperty, j_config3);
742 DeleteAppSpawningCtx(appProperty);
743 EXPECT_EQ(ret, 0);
744 }
745
746 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_30, TestSize.Level0)
747 {
748 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_30 start";
749 g_testHelper.SetTestUid(1000); // 1000 test
750 g_testHelper.SetTestGid(1000); // 1000 test
751 g_testHelper.SetProcessName("com.ohos.wps");
752 g_testHelper.SetTestApl("apl123");
753 g_testHelper.SetTestMsgFlags(4); // 4 is test parameter
754 AppSpawningCtx *appProperty = GetTestAppProperty();
755
756 std::string mJsconfig3 = "{ \
757 \"flags\": \"DLP_MANAGER\", \
758 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
759 \"mount-paths\" : [{ \
760 \"src-path\" : \"/data/app/el2/<currentUserId>/base/database/<PackageName>\", \
761 \"sandbox-path\" : \"/data/storage/el2/base\", \
762 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
763 \"check-action-status\": \"false\" \
764 }] \
765 }";
766 nlohmann::json j_config3 = nlohmann::json::parse(mJsconfig3.c_str());
767 int ret = OHOS::AppSpawn::SandboxUtils::DoAllMntPointsMount(appProperty, j_config3, nullptr);
768 EXPECT_EQ(ret, 0);
769
770 std::string mJsconfig4 = "{ \
771 \"flags\": \"DLP_MANAGER\", \
772 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
773 \"mount-paths\" : [{ \
774 \"src-path\" : \"/data/app/el2/<currentUserId>/database/<PackageName>\", \
775 \"sandbox-path\" : \"/data/storage/el2/base\", \
776 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
777 \"check-action-status\": \"false\" \
778 }] \
779 }";
780 nlohmann::json j_config4 = nlohmann::json::parse(mJsconfig4.c_str());
781 ret = OHOS::AppSpawn::SandboxUtils::DoAllMntPointsMount(appProperty, j_config4, nullptr);
782 DeleteAppSpawningCtx(appProperty);
783 EXPECT_EQ(ret, 0);
784 }
785
786 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_31, TestSize.Level0)
787 {
788 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_31 start";
789 g_testHelper.SetTestUid(1000); // 1000 test
790 g_testHelper.SetTestGid(1000); // 1000 test
791 g_testHelper.SetProcessName("com.ohos.dlpmanager");
792 g_testHelper.SetTestApl("apl123");
793 AppSpawningCtx *appProperty = GetTestAppProperty();
794
795 std::string mJsconfig1 = "{ \
796 \"flags\": \"DLP_TEST\", \
797 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
798 \"mount-paths\" : [{ \
799 \"src-path\" : \"/data/app/el2/<currentUserId>/base/<PackageName_index>\", \
800 \"sandbox-path\" : \"/data/storage/el2/base\", \
801 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
802 \"check-action-status\": \"false\" \
803 }] \
804 }";
805 nlohmann::json j_config1 = nlohmann::json::parse(mJsconfig1.c_str());
806 int ret = OHOS::AppSpawn::SandboxUtils::DoAllMntPointsMount(appProperty, j_config1, nullptr);
807 EXPECT_EQ(ret, 0);
808
809 std::string mJsconfig2 = "{ \
810 \"flags\": \"DLP_TEST\", \
811 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
812 \"mount-paths\" : [{ \
813 \"src-path\" : \"/data/app/el2/<currentUserId>/base/<PackageName_index>\", \
814 \"sandbox-path\" : \"/data/storage/el2/base\", \
815 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
816 \"fs-type\" : \"ext4\", \
817 \"check-action-status\": \"false\", \
818 \"app-apl-name\" : \"apl123\" \
819 }, { \
820 \"src-path\" : \"/data/app/el2/<currentUserId>/database/<PackageName_index>\", \
821 \"sandbox-path\" : \"/data/storage/el2/base\", \
822 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
823 \"fs-type\" : \"ext4\", \
824 \"check-action-status\": \"true\" \
825 }] \
826 }";
827
828 nlohmann::json j_config2 = nlohmann::json::parse(mJsconfig2.c_str());
829 ret = OHOS::AppSpawn::SandboxUtils::DoAllMntPointsMount(appProperty, j_config2, nullptr);
830 DeleteAppSpawningCtx(appProperty);
831 EXPECT_TRUE(ret != 0);
832 }
833
834 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_32, TestSize.Level0)
835 {
836 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_32 start";
837 int ret = OHOS::AppSpawn::SandboxUtils::DoAppSandboxMountOnce(nullptr, "", nullptr, 0, nullptr);
838 EXPECT_EQ(ret, 0);
839
840 std::string mJsconfig1 = "{ \
841 \"dest-mode\" : \"S_IRUSR|S_IWUSR|S_IXUSR\" \
842 }";
843 nlohmann::json j_config1 = nlohmann::json::parse(mJsconfig1.c_str());
844 std::string sandboxRoot;
845 const char *str = "/data/test11122";
846 sandboxRoot = str;
847 OHOS::AppSpawn::SandboxUtils::DoSandboxChmod(j_config1, sandboxRoot);
848 }
849
850 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_34, TestSize.Level0)
851 {
852 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_34 start";
853 g_testHelper.SetTestUid(1000); // 1000 test
854 g_testHelper.SetTestGid(1000); // 1000 test
855 g_testHelper.SetProcessName("com.ohos.dlpmanager");
856 g_testHelper.SetTestApl("apl123");
857
858 const char *strl1 = "/mnt/sandbox/100/test.bundle1";
859 std::string testBundle = strl1;
860 char hspListStr[] = "{ \
861 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
862 \"modules\":[\"module1\", \"module2\"], \
863 \"versions\":[\"v10001\", \"v10002\"] \
864 }";
865 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
866 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
867 DeleteAppSpawningCtx(appProperty);
868 EXPECT_EQ(0, ret);
869 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_34 end";
870 }
871
InvalidJsonTest(std::string & testBundle)872 static void InvalidJsonTest(std::string &testBundle)
873 {
874 char hspListStr[] = "{";
875 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
876 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
877 DeleteAppSpawningCtx(appProperty);
878 EXPECT_NE(0, ret);
879 }
880
NoBundleTest(std::string & testBundle)881 static void NoBundleTest(std::string &testBundle)
882 {
883 char hspListStr[] = "{ \
884 \"modules\":[\"module1\", \"module2\"], \
885 \"versions\":[\"v10001\", \"v10002\"] \
886 }";
887 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
888 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
889 DeleteAppSpawningCtx(appProperty);
890 EXPECT_NE(0, ret);
891 }
892
NoModulesTest(std::string & testBundle)893 static void NoModulesTest(std::string &testBundle)
894 {
895 char hspListStr[] = "{ \
896 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
897 \"versions\":[\"v10001\", \"v10002\"] \
898 }";
899
900 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
901 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
902 DeleteAppSpawningCtx(appProperty);
903 EXPECT_NE(0, ret);
904 }
905
NoVersionsTest(std::string & testBundle)906 static void NoVersionsTest(std::string &testBundle)
907 {
908 char hspListStr[] = "{ \
909 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
910 \"modules\":[\"module1\", \"module2\"] \
911 }";
912 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
913 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
914 DeleteAppSpawningCtx(appProperty);
915 EXPECT_NE(0, ret);
916 }
917
ListSizeNotSameTest(std::string & testBundle)918 static void ListSizeNotSameTest(std::string &testBundle)
919 {
920 char hspListStr[] = "{ \
921 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
922 \"modules\":[\"module1\"], \
923 \"versions\":[\"v10001\", \"v10002\"] \
924 }";
925 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
926 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
927 DeleteAppSpawningCtx(appProperty);
928 EXPECT_NE(0, ret);
929 }
930
ValueTypeIsNotArraryTest(std::string & testBundle)931 static void ValueTypeIsNotArraryTest(std::string &testBundle)
932 {
933 char hspListStr[] = "{ \
934 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
935 \"modules\":[\"module1\", \"module2\"], \
936 \"versions\": 1001 \
937 }";
938 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
939 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
940 DeleteAppSpawningCtx(appProperty);
941 EXPECT_NE(0, ret);
942 }
943
ElementTypeIsNotStringTest(std::string & testBundle)944 static void ElementTypeIsNotStringTest(std::string &testBundle)
945 {
946 char hspListStr[] = "{ \
947 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
948 \"modules\":[\"module1\", \"module2\"], \
949 \"versions\": [1001, 1002] \
950 }";
951 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
952 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
953 DeleteAppSpawningCtx(appProperty);
954 EXPECT_NE(0, ret);
955 }
956
ElementTypeIsNotSameTestSN(std::string & testBundle)957 static void ElementTypeIsNotSameTestSN(std::string &testBundle)
958 {
959 // element type is not same, string + number
960 char hspListStr[] = "{ \
961 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
962 \"modules\":[\"module1\", \"module2\"], \
963 \"versions\": [\"v10001\", 1002] \
964 }";
965 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
966 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
967 DeleteAppSpawningCtx(appProperty);
968 EXPECT_NE(0, ret);
969 }
970
ElementTypeIsNotSameTestNS(std::string & testBundle)971 static void ElementTypeIsNotSameTestNS(std::string &testBundle)
972 {
973 // element type is not same, number + string
974 char hspListStr[] = "{ \
975 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
976 \"modules\":[\"module1\", \"module2\"], \
977 \"versions\": [1001, \"v10002\"] \
978 }";
979 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
980 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
981 DeleteAppSpawningCtx(appProperty);
982 EXPECT_NE(0, ret);
983 }
984
985 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_35, TestSize.Level0)
986 {
987 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_35 start";
988 const char *strl1 = "/mnt/sandbox/100/test.bundle1";
989 std::string testBundle = strl1;
990 InvalidJsonTest(testBundle);
991 NoBundleTest(testBundle);
992 NoModulesTest(testBundle);
993 NoVersionsTest(testBundle);
994 ListSizeNotSameTest(testBundle);
995 ValueTypeIsNotArraryTest(testBundle);
996 ElementTypeIsNotStringTest(testBundle);
997 ElementTypeIsNotSameTestSN(testBundle);
998 ElementTypeIsNotSameTestNS(testBundle);
999 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_35 end";
1000 }
1001
1002 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_36, TestSize.Level0)
1003 {
1004 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_36 start";
1005 const char *strl1 = "/mnt/sandbox/100/test.bundle1";
1006 std::string testBundle = strl1;
1007
1008 { // empty name
1009 char hspListStr[] = "{ \
1010 \"bundles\":[\"\", \"test.bundle2\"], \
1011 \"modules\":[\"module1\", \"module2\"], \
1012 \"versions\":[\"v10001\", \"v10002\"] \
1013 }";
1014 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
1015 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
1016 DeleteAppSpawningCtx(appProperty);
1017 EXPECT_NE(0, ret);
1018 }
1019 { // name is .
1020 char hspListStr[] = "{ \
1021 \"bundles\":[\".\", \"test.bundle2\"], \
1022 \"modules\":[\"module1\", \"module2\"], \
1023 \"versions\":[\"v10001\", \"v10002\"] \
1024 }";
1025 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
1026 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
1027 DeleteAppSpawningCtx(appProperty);
1028 EXPECT_NE(0, ret);
1029 }
1030 { // name is ..
1031 char hspListStr[] = "{ \
1032 \"bundles\":[\"..\", \"test.bundle2\"], \
1033 \"modules\":[\"module1\", \"module2\"], \
1034 \"versions\":[\"v10001\", \"v10002\"] \
1035 }";
1036 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
1037 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
1038 DeleteAppSpawningCtx(appProperty);
1039 EXPECT_NE(0, ret);
1040 }
1041 { // name contains /
1042 char hspListStr[] = "{ \
1043 \"bundles\":[\"test/bundle1\", \"test.bundle2\"], \
1044 \"modules\":[\"module1\", \"module2\"], \
1045 \"versions\":[\"v10001\", \"v10002\"] \
1046 }";
1047 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("HspList", hspListStr);
1048 int ret = OHOS::AppSpawn::SandboxUtils::MountAllHsp(appProperty, testBundle);
1049 DeleteAppSpawningCtx(appProperty);
1050 EXPECT_NE(0, ret);
1051 }
1052 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_36 end";
1053 }
1054
1055 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_37, TestSize.Level0)
1056 {
1057 APPSPAWN_LOGI("App_Spawn_Sandbox_37 start");
1058 g_testHelper.SetTestUid(1000); // 1000 test
1059 g_testHelper.SetTestGid(1000); // 1000 test
1060 g_testHelper.SetProcessName("ohos.samples.xxx");
1061 g_testHelper.SetTestApl("system_basic");
1062 AppSpawningCtx *appProperty = GetTestAppProperty();
1063
1064 AppSpawnMgr content;
1065 LoadAppSandboxConfig(&content);
1066 std::string sandboxPackagePath = "/mnt/sandbox/100/";
1067 const std::string bundleName = GetBundleName(appProperty);
1068 sandboxPackagePath += bundleName;
1069
1070 int ret = SandboxUtils::SetPrivateAppSandboxProperty(appProperty);
1071 EXPECT_EQ(0, ret);
1072 ret = SandboxUtils::SetCommonAppSandboxProperty(appProperty, sandboxPackagePath);
1073 DeleteAppSpawningCtx(appProperty);
1074 EXPECT_EQ(0, ret);
1075 APPSPAWN_LOGI("App_Spawn_Sandbox_37 end");
1076 }
1077
1078 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_38, TestSize.Level0)
1079 {
1080 APPSPAWN_LOGI("App_Spawn_Sandbox_38 start");
1081 g_testHelper.SetTestUid(1000); // 1000 test
1082 g_testHelper.SetTestGid(1000); // 1000 test
1083 g_testHelper.SetProcessName("com.example.deviceinfo");
1084 g_testHelper.SetTestApl("system_basic");
1085 AppSpawningCtx *appProperty = GetTestAppProperty();
1086
1087 std::string pJsconfig1 = "{ \
1088 \"common\":[], \
1089 \"individual\": [ { \
1090 \"com.example.deviceinfo\" : [{ \
1091 \"sandbox-switch\": \"ON\", \
1092 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
1093 \"mount-paths\" : [{ \
1094 \"src-path\" : \"/data/app/el1/bundle/public/\", \
1095 \"sandbox-path\" : \"/data/accounts/account_0/applications/2222222\", \
1096 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
1097 \"check-action-status\": \"true\" \
1098 }, { \
1099 \"src-path\" : \"/data/app/el1/bundle/public/\", \
1100 \"sandbox-path\" : \"/data/bundles/aaaaaa\", \
1101 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
1102 \"check-action-status\": \"true\" \
1103 }\
1104 ],\
1105 \"symbol-links\" : [] \
1106 }] \
1107 }] \
1108 }";
1109
1110 nlohmann::json p_config1 = nlohmann::json::parse(pJsconfig1.c_str());
1111 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(p_config1, SANBOX_APP_JSON_CONFIG);
1112
1113 std::string sandboxPackagePath = "/mnt/sandbox/100/";
1114 const std::string bundleName = GetBundleName(appProperty);
1115 sandboxPackagePath += bundleName;
1116 int ret = SandboxUtils::SetPrivateAppSandboxProperty(appProperty);
1117 EXPECT_EQ(0, ret);
1118 ret = SandboxUtils::SetCommonAppSandboxProperty(appProperty, sandboxPackagePath);
1119 DeleteAppSpawningCtx(appProperty);
1120 EXPECT_EQ(0, ret);
1121 APPSPAWN_LOGI("App_Spawn_Sandbox_38 end");
1122 }
1123
1124 /**
1125 * @tc.name: App_Spawn_Sandbox_39
1126 * @tc.desc: load overlay config SetAppSandboxProperty by App com.ohos.demo.
1127 * @tc.type: FUNC
1128 * @tc.require:issueI7D0H9
1129 * @tc.author:
1130 */
1131 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_39, TestSize.Level0)
1132 {
1133 APPSPAWN_LOGI("App_Spawn_Sandbox_39 start");
1134 g_testHelper.SetTestUid(1000); // 1000 test
1135 g_testHelper.SetTestGid(1000); // 1000 test
1136 g_testHelper.SetProcessName("com.ohos.demo");
1137 g_testHelper.SetTestApl("normal");
1138 g_testHelper.SetTestMsgFlags(0x100); // 0x100 is test parameter
1139
1140 std::string overlayInfo = "/data/app/el1/bundle/public/com.ohos.demo/feature.hsp|";
1141 overlayInfo += "/data/app/el1/bundle/public/com.ohos.demo/feature.hsp|";
1142 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("Overlay", overlayInfo.c_str());
1143 std::string sandBoxRootDir = "/mnt/sandbox/100/com.ohos.demo";
1144 int32_t ret = OHOS::AppSpawn::SandboxUtils::SetOverlayAppSandboxProperty(appProperty, sandBoxRootDir);
1145 DeleteAppSpawningCtx(appProperty);
1146 EXPECT_EQ(0, ret);
1147 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_39 end";
1148 }
1149 /**
1150 * @tc.name: App_Spawn_Sandbox_40
1151 * @tc.desc: load group info config SetAppSandboxProperty
1152 * @tc.type: FUNC
1153 * @tc.require:issueI7FUPV
1154 * @tc.author:
1155 */
1156 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_40, TestSize.Level0)
1157 {
1158 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_40 start";
1159 g_testHelper.SetTestUid(1000); // 1000 test
1160 g_testHelper.SetTestGid(1000); // 1000 test
1161 g_testHelper.SetProcessName("com.ohos.demo");
1162 g_testHelper.SetTestApl("normal");
1163 g_testHelper.SetTestMsgFlags(0x100);
1164 std::string sandboxPrefix = "/mnt/sandbox/100/testBundle";
1165 char dataGroupInfoListStr[] = "{ \
1166 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
1167 \"dir\":[\"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975\", \
1168 \"/data/app/el2/100/group/ce876162-fe69-45d3-aa8e-411a047af564\"], \
1169 \"gid\":[\"20100001\", \"20100002\"] \
1170 }";
1171 AppSpawningCtx *appProperty = GetTestAppPropertyWithExtInfo("DataGroup", dataGroupInfoListStr);
1172 int ret = OHOS::AppSpawn::SandboxUtils::MountAllGroup(appProperty, sandboxPrefix);
1173 DeleteAppSpawningCtx(appProperty);
1174 EXPECT_EQ(0, ret);
1175 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_40 end";
1176 }
1177
1178 /**
1179 * @tc.name: App_Spawn_Sandbox_41
1180 * @tc.desc: parse namespace config.
1181 * @tc.type: FUNC
1182 * @tc.require:issueI8B63M
1183 * @tc.author:
1184 */
1185 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_41, TestSize.Level0)
1186 {
1187 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_41 start";
1188 std::string mJsconfig = "{ \
1189 \"common\":[{ \
1190 \"top-sandbox-switch\": \"ON\", \
1191 \"app-base\":[{ \
1192 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
1193 \"sandbox-ns-flags\": [ \"pid\" ], \
1194 \"mount-paths\" : [], \
1195 \"symbol-links\" : [] \
1196 }] \
1197 }], \
1198 \"individual\":[{ \
1199 \"__internal__.com.ohos.render\":[{ \
1200 \"sandbox-root\" : \"/mnt/sandbox/com.ohos.render/<PackageName>\", \
1201 \"sandbox-ns-flags\": [ \"pid\", \"net\" ], \
1202 \"mount-paths\" : [], \
1203 \"symbol-links\" : [] \
1204 }] \
1205 }] \
1206 }";
1207 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
1208
1209 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
1210
1211 uint32_t cloneFlags = OHOS::AppSpawn::SandboxUtils::GetSandboxNsFlags(false);
1212 EXPECT_EQ(!!(cloneFlags & CLONE_NEWPID), true);
1213
1214 cloneFlags = OHOS::AppSpawn::SandboxUtils::GetSandboxNsFlags(true);
1215 EXPECT_EQ(!!(cloneFlags & (CLONE_NEWPID | CLONE_NEWNET)), true);
1216
1217 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_41 end";
1218 }
1219
1220 /**
1221 * @tc.name: App_Spawn_Sandbox_42
1222 * @tc.desc: parse config file for fstype .
1223 * @tc.type: FUNC
1224 * @tc.require: https://gitee.com/openharmony/startup_appspawn/issues/I8OF9K
1225 * @tc.author:
1226 */
1227 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_42, TestSize.Level0)
1228 {
1229 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_42 start";
1230 std::string mJsconfig = "{ \
1231 \"mount-paths\": [{ \
1232 \"src-path\": \"/data/app/el1/<currentUserId>/base\", \
1233 \"sandbox-path\": \"/storage/Users/<currentUserId>/appdata/el1\", \
1234 \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \
1235 \"dac-override-sensitive\": \"true\", \
1236 \"fs-type\": \"sharefs\", \
1237 \"options\": \"support_overwrite=1\" \
1238 }] \
1239 }";
1240 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
1241 const char *mountPath = "mount-paths";
1242 nlohmann::json j_secondConfig = j_config[mountPath][0];
1243 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
1244
1245 std::string fsType = OHOS::AppSpawn::SandboxUtils::GetSandboxFsType(j_secondConfig);
1246 int ret = strcmp(fsType.c_str(), "sharefs");
1247 EXPECT_EQ(ret, 0);
1248
1249 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_42 end";
1250 }
1251
1252 /**
1253 * @tc.name: App_Spawn_Sandbox_43
1254 * @tc.desc: get sandbox mount config when section is common.
1255 * @tc.type: FUNC
1256 * @tc.require: https://gitee.com/openharmony/startup_appspawn/issues/I8OF9K
1257 * @tc.author:
1258 */
1259 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_43, TestSize.Level0)
1260 {
1261 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_43 start";
1262 std::string mJsconfig = "{ \
1263 \"mount-paths\": [{ \
1264 \"src-path\": \"/data/app/el1/<currentUserId>/base\", \
1265 \"sandbox-path\": \"/storage/Users/<currentUserId>/appdata/el1\", \
1266 \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \
1267 \"dac-override-sensitive\": \"true\", \
1268 \"fs-type\": \"sharefs\", \
1269 \"options\": \"support_overwrite=1\" \
1270 }] \
1271 }";
1272 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
1273 const char *mountPath = "mount-paths";
1274 nlohmann::json j_secondConfig = j_config[mountPath][0];
1275
1276 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
1277 OHOS::AppSpawn::SandboxUtils::SandboxMountConfig mountConfig;
1278 std::string section = "common";
1279 AppSpawningCtx *appProperty = GetTestAppProperty();
1280 OHOS::AppSpawn::SandboxUtils::GetSandboxMountConfig(appProperty, section, j_secondConfig, mountConfig);
1281 int ret = strcmp(mountConfig.fsType.c_str(), "sharefs");
1282 EXPECT_EQ(ret, 0);
1283 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_43 end";
1284 }
1285
1286 /**
1287 * @tc.name: App_Spawn_Sandbox_44
1288 * @tc.desc: get sandbox mount config when section is permission.
1289 * @tc.type: FUNC
1290 * @tc.require: https://gitee.com/openharmony/startup_appspawn/issues/I8OF9K
1291 * @tc.author:
1292 */
1293 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_44, TestSize.Level0)
1294 {
1295 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_44 start";
1296 std::string mJsconfig = "{ \
1297 \"mount-paths\": [{ \
1298 \"src-path\": \"/data/app/el1/<currentUserId>/base\", \
1299 \"sandbox-path\": \"/storage/Users/<currentUserId>/appdata/el1\", \
1300 \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \
1301 \"dac-override-sensitive\": \"true\", \
1302 \"fs-type\": \"sharefs\", \
1303 \"options\": \"support_overwrite=1\" \
1304 }] \
1305 }";
1306 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
1307 const char *mountPath = "mount-paths";
1308 nlohmann::json j_secondConfig = j_config[mountPath][0];
1309
1310 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
1311 OHOS::AppSpawn::SandboxUtils::SandboxMountConfig mountConfig;
1312 std::string section = "permission";
1313 AppSpawningCtx *appProperty = GetTestAppProperty();
1314 OHOS::AppSpawn::SandboxUtils::GetSandboxMountConfig(appProperty, section, j_secondConfig, mountConfig);
1315 int ret = strcmp(mountConfig.fsType.c_str(), "sharefs");
1316 EXPECT_EQ(ret, 0);
1317 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_44 end";
1318 }
1319
1320 /**
1321 * @tc.name: App_Spawn_Sandbox_45
1322 * @tc.desc: parse config file for options.
1323 * @tc.type: FUNC
1324 * @tc.require: https://gitee.com/openharmony/startup_appspawn/issues/I8OF9K
1325 * @tc.author:
1326 */
1327 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_45, TestSize.Level0)
1328 {
1329 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_45 start";
1330 std::string mJsconfig = "{ \
1331 \"mount-paths\": [{ \
1332 \"src-path\": \"/data/app/el1/<currentUserId>/base\", \
1333 \"sandbox-path\": \"/storage/Users/<currentUserId>/appdata/el1\", \
1334 \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \
1335 \"dac-override-sensitive\": \"true\", \
1336 \"fs-type\": \"sharefs\", \
1337 \"options\": \"support_overwrite=1\" \
1338 }] \
1339 }";
1340 nlohmann::json j_config = nlohmann::json::parse(mJsconfig.c_str());
1341 const char *mountPath = "mount-paths";
1342 nlohmann::json j_secondConfig = j_config[mountPath][0];
1343
1344 OHOS::AppSpawn::SandboxUtils::StoreJsonConfig(j_config, SANBOX_APP_JSON_CONFIG);
1345 AppSpawningCtx *appProperty = GetTestAppProperty();
1346 std::string options = OHOS::AppSpawn::SandboxUtils::GetSandboxOptions(appProperty, j_secondConfig);
1347 int ret = strcmp(options.c_str(), "support_overwrite=1,user_id=100");
1348 EXPECT_EQ(ret, 0);
1349 GTEST_LOG_(INFO) << "App_Spawn_Sandbox_45 end";
1350 }
1351 /**
1352 * @brief 测试app extension
1353 *
1354 */
1355 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_004, TestSize.Level0)
1356 {
1357 AppSpawningCtx *spawningCtx = GetTestAppProperty();
1358 std::string path = SandboxUtils::ConvertToRealPath(spawningCtx, "/system/<variablePackageName>/module");
1359 APPSPAWN_LOGV("path %{public}s", path.c_str());
1360 ASSERT_EQ(path.c_str() != nullptr, 1);
1361 ASSERT_EQ(strcmp(path.c_str(), "/system/com.example.myapplication/module") == 0, 1);
1362 DeleteAppSpawningCtx(spawningCtx);
1363 }
1364
1365 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_005, TestSize.Level0)
1366 {
1367 AppSpawningCtx *spawningCtx = GetTestAppProperty();
1368 ASSERT_EQ(spawningCtx != nullptr, 1);
1369 int ret = SetAppSpawnMsgFlag(spawningCtx->message, TLV_MSG_FLAGS, APP_FLAGS_CLONE_ENABLE);
1370 ASSERT_EQ(ret, 0);
1371
1372 std::string path = SandboxUtils::ConvertToRealPath(spawningCtx, "/system/<variablePackageName>/module");
1373 APPSPAWN_LOGV("path %{public}s", path.c_str());
1374 ASSERT_EQ(path.c_str() != nullptr, 1); // +clone-bundleIndex+packageName
1375 ASSERT_EQ(strcmp(path.c_str(), "/system/+clone-100+com.example.myapplication/module") == 0, 1);
1376 DeleteAppSpawningCtx(spawningCtx);
1377 }
1378
1379 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_006, TestSize.Level0)
1380 {
1381 AppSpawnClientHandle clientHandle = nullptr;
1382 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1383 ASSERT_EQ(ret, 0);
1384 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1385 ASSERT_EQ(reqHandle != nullptr, 1);
1386 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_EXTENSION, "test001");
1387 ASSERT_EQ(ret, 0);
1388 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
1389 ASSERT_EQ(ret, 0);
1390 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1391 ASSERT_EQ(spawningCtx != nullptr, 1);
1392
1393 std::string path = SandboxUtils::ConvertToRealPath(spawningCtx, "/system/<variablePackageName>/module");
1394 APPSPAWN_LOGV("path %{public}s", path.c_str());
1395 ASSERT_EQ(path.c_str() != nullptr, 1); // +extension-<extensionType>+packageName
1396 ASSERT_EQ(strcmp(path.c_str(), "/system/+extension-test001+com.example.myapplication/module") == 0, 1);
1397 DeleteAppSpawningCtx(spawningCtx);
1398 AppSpawnClientDestroy(clientHandle);
1399 }
1400
1401 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_007, TestSize.Level0)
1402 {
1403 AppSpawnClientHandle clientHandle = nullptr;
1404 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1405 ASSERT_EQ(ret, 0);
1406 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1407 ASSERT_EQ(reqHandle != nullptr, 1);
1408 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_EXTENSION, "test001");
1409 ASSERT_EQ(ret, 0);
1410 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
1411 ASSERT_EQ(ret, 0);
1412 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_CLONE_ENABLE);
1413 ASSERT_EQ(ret, 0);
1414 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1415 ASSERT_EQ(spawningCtx != nullptr, 1);
1416
1417 std::string path = SandboxUtils::ConvertToRealPath(spawningCtx, "/system/<variablePackageName>/module");
1418 APPSPAWN_LOGV("path %{public}s", path.c_str());
1419 ASSERT_EQ(path.c_str() != nullptr, 1); // +clone-bundleIndex+extension-<extensionType>+packageName
1420
1421 ASSERT_EQ(strcmp(path.c_str(), "/system/+clone-100+extension-test001+com.example.myapplication/module") == 0, 1);
1422 DeleteAppSpawningCtx(spawningCtx);
1423 AppSpawnClientDestroy(clientHandle);
1424 }
1425
1426 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_008, TestSize.Level0)
1427 {
1428 AppSpawnClientHandle clientHandle = nullptr;
1429 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1430 ASSERT_EQ(ret, 0);
1431 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1432 ASSERT_EQ(reqHandle != nullptr, 1);
1433 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
1434 ASSERT_EQ(ret, 0);
1435 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_CLONE_ENABLE);
1436 ASSERT_EQ(ret, 0);
1437 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1438 ASSERT_EQ(spawningCtx != nullptr, 1);
1439
1440 std::string path = SandboxUtils::ConvertToRealPath(spawningCtx, "/system/<variablePackageName>/module");
1441 APPSPAWN_LOGV("path %{public}s", path.c_str());
1442 ASSERT_STREQ(path.c_str(), "");
1443
1444 DeleteAppSpawningCtx(spawningCtx);
1445 AppSpawnClientDestroy(clientHandle);
1446 }
1447
1448 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_009, TestSize.Level0)
1449 {
1450 AppSpawnClientHandle clientHandle = nullptr;
1451 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1452 ASSERT_EQ(ret, 0);
1453 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
1454 ASSERT_EQ(reqHandle != nullptr, 1);
1455 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
1456 ASSERT_EQ(ret, 0);
1457 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1458 ASSERT_EQ(spawningCtx != nullptr, 1);
1459
1460 std::string path = SandboxUtils::ConvertToRealPath(spawningCtx, "/system/<variablePackageName>/module");
1461 APPSPAWN_LOGV("path %{public}s", path.c_str());
1462 ASSERT_STREQ(path.c_str(), "");
1463
1464 DeleteAppSpawningCtx(spawningCtx);
1465 AppSpawnClientDestroy(clientHandle);
1466 }
1467 } // namespace OHOS
1468