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 <cerrno>
17 #include <cstdbool>
18 #include <gtest/gtest.h>
19 #include <memory>
20 #include <string>
21 #include <sys/mount.h>
22 #include <sys/stat.h>
23 #include <sys/syscall.h>
24 #include <sys/types.h>
25
26 #include "appspawn_manager.h"
27 #include "appspawn_permission.h"
28 #include "appspawn_sandbox.h"
29 #include "appspawn_server.h"
30 #include "appspawn_utils.h"
31 #include "cJSON.h"
32 #include "json_utils.h"
33 #include "securec.h"
34
35 #include "app_spawn_stub.h"
36 #include "app_spawn_test_helper.h"
37
38 using namespace testing;
39 using namespace testing::ext;
40
41 namespace OHOS {
42 static const std::string g_commonConfig = "{ \
43 \"global\": { \
44 \"sandbox-root\": \"/mnt/sandbox/<currentUserId>/app-root\", \
45 \"sandbox-ns-flags\": [ \"pid\", \"net\" ], \
46 \"top-sandbox-switch\": \"ON\" \
47 }, \
48 \"required\":{ \
49 \"system-const\":{ \
50 \"mount-paths\" : [{ \
51 \"src-path\" : \"/lib\", \
52 \"sandbox-path\" : \"/lib\", \
53 \"check-action-status\": \"false\", \
54 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
55 \"category\": \"shared\", \
56 \"app-apl-name\" : \"system\" \
57 }, { \
58 \"src-path\" : \"/lib1\", \
59 \"sandbox-path\" : \"/lib1\", \
60 \"check-action-status\": \"false\", \
61 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
62 \"category\": \"rdonly\", \
63 \"app-apl-name\" : \"system\" \
64 }, { \
65 \"src-path\" : \"/none\", \
66 \"sandbox-path\" : \"/storage/cloud/epfs\", \
67 \"check-action-status\": \"false\", \
68 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
69 \"category\": \"epfs\", \
70 \"app-apl-name\" : \"system\" \
71 }, { \
72 \"src-path\" : \"/storage/Users/<currentUserId>/appdata/el1\", \
73 \"sandbox-path\" : \"/storage/Users/<currentUserId>/appdata/el1\", \
74 \"check-action-status\": \"false\", \
75 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
76 \"category\": \"dac_override\", \
77 \"app-apl-name\" : \"system\" \
78 }, { \
79 \"src-path\" : \"/dev/fuse\", \
80 \"sandbox-path\" : \"/mnt/data/fuse\", \
81 \"category\": \"fuse\", \
82 \"check-action-status\": \"true\" \
83 }], \
84 \"symbol-links\" : [], \
85 \"mount-groups\": [\"test-always\"] \
86 }, \
87 \"app-variable\":{ \
88 \"mount-paths\" : [{ \
89 \"src-path\" : \"/config\", \
90 \"sandbox-path\" : \"/config\", \
91 \"check-action-status\": \"false\", \
92 \"app-apl-name\" : \"system\", \
93 \"category\": \"shared\" \
94 }], \
95 \"symbol-links\" : [], \
96 \"mount-groups\": [\"el5\"] \
97 } \
98 }, \
99 \"name-groups\": [ \
100 { \
101 \"name\": \"user-public\", \
102 \"type\": \"system-const\", \
103 \"mount-paths\" : [{ \
104 \"src-path\" : \"/data/app/el2/<currentUserId>/base/<PackageName>\", \
105 \"sandbox-path\" : \"/data/storage/el2/base\", \
106 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
107 \"category\": \"shared\" \
108 }] \
109 }, { \
110 \"name\": \"el5\", \
111 \"type\": \"app-variable\", \
112 \"deps-mode\": \"not-exists\", \
113 \"mount-paths-deps\": { \
114 \"sandbox-path\": \"/data/storage/el5\", \
115 \"src-path\": \"/data/app/el5/<currentUserId>/base\", \
116 \"category\": \"shared\" \
117 }, \
118 \"mount-paths\" : [{ \
119 \"src-path\" : \"/data/app/el5/<currentUserId>/base/<PackageName>\", \
120 \"sandbox-path\" : \"<deps-path>/base\" \
121 }] \
122 }, { \
123 \"name\": \"el6\", \
124 \"type\": \"app-variable\", \
125 \"deps-mode\": \"not-exists\", \
126 \"mount-paths-deps\": { \
127 \"sandbox-path\": \"/data/storage/el6\", \
128 \"src-path\": \"/data/app/el6/<currentUserId>/base\", \
129 \"category\": \"shared\" \
130 }, \
131 \"mount-paths\" : [{ \
132 \"src-path\" : \"/data/app/el6/<currentUserId>/base/<PackageName>\", \
133 \"sandbox-path\" : \"<deps-path>/base\" \
134 }] \
135 },{ \
136 \"name\": \"test-always\", \
137 \"type\": \"system-const\", \
138 \"deps-mode\": \"always\", \
139 \"mount-paths-deps\": { \
140 \"sandbox-path\": \"/data/storage/e20\", \
141 \"src-path\": \"/data/app/e20/<currentUserId>/base\", \
142 \"category\": \"shared\" \
143 }, \
144 \"mount-paths\" : [{ \
145 \"src-path\" : \"/data/app/e20/<currentUserId>/base/<PackageName>\", \
146 \"sandbox-path\" : \"<deps-path>/base\" \
147 }] \
148 } \
149 ] \
150 }";
151
152 static const std::string g_packageNameConfig = "{ \
153 \"global\": { \
154 \"sandbox-root\": \"/mnt/sandbox/<currentUserId>/app-root\", \
155 \"sandbox-ns-flags\": [ \"pid\", \"net\" ], \
156 \"top-sandbox-switch\": \"OFF\" \
157 }, \
158 \"conditional\":{ \
159 \"package-name\": [{ \
160 \"name\": \"test.example.ohos.com\", \
161 \"sandbox-switch\": \"ON\", \
162 \"sandbox-shared\" : \"true\", \
163 \"sandbox-ns-flags\" : [ \"pid\", \"net\" ], \
164 \"mount-paths\" : [{ \
165 \"src-path\" : \"/config\", \
166 \"sandbox-path\" : \"/config\", \
167 \"check-action-status\": \"false\", \
168 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
169 \"category\": \"shared\", \
170 \"app-apl-name\" : \"system\" \
171 }], \
172 \"symbol-links\" : [{ \
173 \"target-name\" : \"/system/etc\", \
174 \"link-name\" : \"/etc\", \
175 \"check-action-status\": \"false\", \
176 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \" \
177 }] \
178 }, \
179 { \
180 \"name\": \"com.example.myapplication\", \
181 \"sandbox-switch\": \"ON\", \
182 \"sandbox-shared\" : \"true\", \
183 \"mount-paths\" : [{ \
184 \"src-path\" : \"/mnt/data/<currentUserId>\", \
185 \"sandbox-path\" : \"/mnt/data\", \
186 \"category\": \"shared\", \
187 \"check-action-status\": \"true\" \
188 }, { \
189 \"src-path\" : \"/dev/fuse\", \
190 \"sandbox-path\" : \"/mnt/data/fuse\", \
191 \"category\": \"fuse\", \
192 \"check-action-status\": \"true\" \
193 }],\
194 \"symbol-links\" : [] \
195 }]\
196 } \
197 }";
198
199 static const std::string g_permissionConfig = "{ \
200 \"global\": { \
201 \"sandbox-root\": \"/mnt/sandbox/<currentUserId>/app-root\", \
202 \"sandbox-ns-flags\": [ \"pid\", \"net\" ] \
203 }, \
204 \"conditional\":{ \
205 \"permission\": [{ \
206 \"name\": \"ohos.permission.FILE_ACCESS_MANAGER\", \
207 \"sandbox-switch\": \"ON\", \
208 \"gids\": [\"file_manager\", \"user_data_rw\"], \
209 \"sandbox-ns-flags\" : [ \"pid\", \"net\" ], \
210 \"mount-paths\" : [{ \
211 \"src-path\" : \"/config--1\", \
212 \"sandbox-path\" : \"/data/app/el1/<currentUserId>/database/<PackageName_index>\", \
213 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
214 \"category\": \"shared\", \
215 \"app-apl-name\" : \"system\", \
216 \"check-action-status\": \"true\" \
217 }], \
218 \"symbol-links\" : [{ \
219 \"target-name\" : \"/system/etc\", \
220 \"link-name\" : \"/etc\", \
221 \"check-action-status\": \"false\", \
222 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \" \
223 } \
224 ] \
225 }, \
226 { \
227 \"name\": \"ohos.permission.ACTIVATE_THEME_PACKAGE\", \
228 \"sandbox-switch\": \"ON\", \
229 \"gids\": [1006, 1008], \
230 \"sandbox-ns-flags\" : [ \"pid\", \"net\" ], \
231 \"mount-paths\" : [{ \
232 \"src-path\" : \"/config--2\", \
233 \"sandbox-path\" : \"/data/app/el1/<currentUserId>/database/<PackageName_index>\", \
234 \"check-action-status\": \"false\" \
235 }], \
236 \"symbol-links\" : [] \
237 }] \
238 } \
239 }";
240
241 static const std::string g_spawnFlagsConfig = "{ \
242 \"global\": { \
243 \"sandbox-root\": \"/mnt/sandbox/<currentUserId>/app-root\", \
244 \"sandbox-ns-flags\": [ \"pid\", \"net\" ], \
245 \"top-sandbox-switch\": \"OFF\" \
246 }, \
247 \"conditional\":{ \
248 \"spawn-flag\": [{ \
249 \"name\": \"START_FLAGS_BACKUP\", \
250 \"mount-paths\": [{ \
251 \"src-path\" : \"/data/app/el1/bundle/public/\", \
252 \"sandbox-path\" : \"/data/bundles/\", \
253 \"check-action-status\": \"true\", \
254 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
255 \"category\": \"shared\", \
256 \"app-apl-name\" : \"system\" \
257 }, { \
258 \"sandbox-path\": \"/data/storage/el1/backup\", \
259 \"src-path\": \"/data/service/el1/<currentUserId>/backup/bundles/<PackageName>\" \
260 }], \
261 \"mount-groups\": [] \
262 }, { \
263 \"name\": \"DLP_MANAGER\", \
264 \"mount-paths\": [{ \
265 \"src-path\" : \"/data/app/el1/bundle/public/\", \
266 \"sandbox-path\" : \"/data/bundles/\", \
267 \"check-action-status\": \"true\" \
268 }], \
269 \"mount-groups\": [] \
270 }] \
271 }\
272 }";
273
GetFirstSandboxMountPathNode(const SandboxSection * section)274 static inline SandboxMountNode *GetFirstSandboxMountPathNode(const SandboxSection *section)
275 {
276 APPSPAWN_CHECK_ONLY_EXPER(section != nullptr, return nullptr);
277 ListNode *node = section->front.next;
278 if (node == §ion->front) {
279 return nullptr;
280 }
281 return reinterpret_cast<SandboxMountNode *>(ListEntry(node, SandboxMountNode, node));
282 }
283
GetNextSandboxMountPathNode(const SandboxSection * section,SandboxMountNode * pathNode)284 static inline SandboxMountNode *GetNextSandboxMountPathNode(const SandboxSection *section, SandboxMountNode *pathNode)
285 {
286 APPSPAWN_CHECK_ONLY_EXPER(section != nullptr && pathNode != nullptr, return nullptr);
287 if (pathNode->node.next == §ion->front) {
288 return nullptr;
289 }
290 return reinterpret_cast<SandboxMountNode *>(ListEntry(pathNode->node.next, SandboxMountNode, node));
291 }
292
293 AppSpawnTestHelper g_testHelper;
294 class AppSpawnSandboxTest : public testing::Test {
295 public:
SetUpTestCase()296 static void SetUpTestCase() {}
TearDownTestCase()297 static void TearDownTestCase()
298 {
299 StubNode *stub = GetStubNode(STUB_MOUNT);
300 if (stub) {
301 stub->flags &= ~STUB_NEED_CHECK;
302 }
303 }
SetUp()304 void SetUp() {}
TearDown()305 void TearDown() {}
306 };
307
TestCreateAppSpawningCtx()308 static AppSpawningCtx *TestCreateAppSpawningCtx()
309 {
310 AppSpawnClientHandle clientHandle = nullptr;
311 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
312 APPSPAWN_CHECK(ret == 0, return nullptr, "Failed to create reqMgr");
313 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
314 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, return nullptr, "Failed to create req");
315 return g_testHelper.GetAppProperty(clientHandle, reqHandle);
316 }
317
TestGetSandboxContext(const AppSpawningCtx * property,int nwebspawn)318 static SandboxContext *TestGetSandboxContext(const AppSpawningCtx *property, int nwebspawn)
319 {
320 AppSpawnMsgFlags *msgFlags = (AppSpawnMsgFlags *)GetAppProperty(property, TLV_MSG_FLAGS);
321 APPSPAWN_CHECK(msgFlags != nullptr, return nullptr, "No msg flags in msg %{public}s", GetProcessName(property));
322
323 SandboxContext *context = GetSandboxContext();
324 APPSPAWN_CHECK(context != nullptr, return nullptr, "Failed to get context");
325
326 context->nwebspawn = nwebspawn;
327 context->bundleName = GetBundleName(property);
328 context->bundleHasWps = strstr(context->bundleName, "wps") != NULL;
329 context->dlpBundle = strcmp(GetProcessName(property), "com.ohos.dlpmanager") == 0;
330 context->appFullMountEnable = 0;
331
332 context->sandboxSwitch = 1;
333 context->sandboxShared = false;
334 context->message = property->message;
335 context->rootPath = nullptr;
336 context->rootPath = nullptr;
337 return context;
338 }
339
TestParseAppSandboxConfig(AppSpawnSandboxCfg * sandbox,const char * buffer)340 static int TestParseAppSandboxConfig(AppSpawnSandboxCfg *sandbox, const char *buffer)
341 {
342 cJSON *config = cJSON_Parse(buffer);
343 if (config == nullptr) {
344 APPSPAWN_LOGE("TestParseAppSandboxConfig config %{public}s", buffer);
345 return -1;
346 }
347 int ret = 0;
348 do {
349 ret = ParseAppSandboxConfig(config, sandbox);
350 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
351
352 uint32_t depNodeCount = sandbox->depNodeCount;
353 APPSPAWN_CHECK_ONLY_EXPER(depNodeCount > 0, break);
354
355 sandbox->depGroupNodes = (SandboxNameGroupNode **)calloc(1, sizeof(SandboxNameGroupNode *) * depNodeCount);
356 APPSPAWN_CHECK(sandbox->depGroupNodes != nullptr, break, "Failed alloc memory ");
357 sandbox->depNodeCount = 0;
358 ListNode *node = sandbox->nameGroupsQueue.front.next;
359 while (node != &sandbox->nameGroupsQueue.front) {
360 SandboxNameGroupNode *groupNode = (SandboxNameGroupNode *)ListEntry(node, SandboxMountNode, node);
361 if (groupNode->depNode) {
362 sandbox->depGroupNodes[sandbox->depNodeCount++] = groupNode;
363 }
364 node = node->next;
365 }
366 APPSPAWN_LOGI("LoadAppSandboxConfig depNodeCount %{public}d", sandbox->depNodeCount);
367 } while (0);
368 cJSON_Delete(config);
369 return ret;
370 }
371
HandleSplitString(const char * str,void * context)372 static int HandleSplitString(const char *str, void *context)
373 {
374 std::vector<std::string> *results = reinterpret_cast<std::vector<std::string> *>(context);
375 results->push_back(std::string(str));
376 return 0;
377 }
378
TestJsonUtilSplit(const char * args[],uint32_t argc,const std::string & input,const std::string & pattern)379 static int TestJsonUtilSplit(const char *args[], uint32_t argc, const std::string &input, const std::string &pattern)
380 {
381 std::vector<std::string> results;
382 StringSplit(input.c_str(), pattern.c_str(), reinterpret_cast<void *>(&results), HandleSplitString);
383 if (argc != results.size()) {
384 return -1;
385 }
386 for (size_t i = 0; i < argc; i++) {
387 if (strcmp(args[i], results[i].c_str()) != 0) {
388 return -1;
389 }
390 }
391 return 0;
392 }
393
394 HWTEST_F(AppSpawnSandboxTest, App_Spawn_JsonUtil_001, TestSize.Level0)
395 {
396 const char *args[] = {"S_IRUSR", "S_IWOTH", "S_IRWXU"};
397 std::string cmd = " S_IRUSR S_IWOTH S_IRWXU ";
398 size_t size = sizeof(args) / sizeof(args[0]);
399 ASSERT_EQ(TestJsonUtilSplit(args, size, cmd, " "), 0);
400 }
401
402 HWTEST_F(AppSpawnSandboxTest, App_Spawn_JsonUtil_002, TestSize.Level0)
403 {
404 const char *args[] = {"S_IRUSR", "S_IWOTH", "S_IRWXU"};
405 std::string cmd = "S_IRUSR S_IWOTH S_IRWXU";
406 size_t size = sizeof(args) / sizeof(args[0]);
407 ASSERT_EQ(TestJsonUtilSplit(args, size, cmd, " "), 0);
408 }
409
410 HWTEST_F(AppSpawnSandboxTest, App_Spawn_JsonUtil_003, TestSize.Level0)
411 {
412 const char *args[] = {"S_IRUSR", "S_IWOTH", "S_IRWXU"};
413 std::string cmd = " S_IRUSR S_IWOTH S_IRWXU";
414 size_t size = sizeof(args) / sizeof(args[0]);
415 ASSERT_EQ(TestJsonUtilSplit(args, size, cmd, " "), 0);
416 }
417
418 HWTEST_F(AppSpawnSandboxTest, App_Spawn_JsonUtil_004, TestSize.Level0)
419 {
420 const char *args[] = {"S_IRUSR", "S_IWOTH", "S_IRWXU"};
421 std::string cmd = "S_IRUSR S_IWOTH S_IRWXU ";
422 size_t size = sizeof(args) / sizeof(args[0]);
423 ASSERT_EQ(TestJsonUtilSplit(args, size, cmd, " "), 0);
424 }
425
426 HWTEST_F(AppSpawnSandboxTest, App_Spawn_JsonUtil_005, TestSize.Level0)
427 {
428 const char *args[] = {"S_IRUSR"};
429 std::string cmd = " S_IRUSR ";
430 size_t size = sizeof(args) / sizeof(args[0]);
431 ASSERT_EQ(TestJsonUtilSplit(args, size, cmd, " "), 0);
432 }
433
434 HWTEST_F(AppSpawnSandboxTest, App_Spawn_JsonUtil_006, TestSize.Level0)
435 {
436 const char *args[] = {"S_IRUSR", "S_IWOTH", "S_IRWXU"};
437 std::string cmd = " S_IRUSR | S_IWOTH | S_IRWXU ";
438 size_t size = sizeof(args) / sizeof(args[0]);
439 ASSERT_EQ(TestJsonUtilSplit(args, size, cmd, "|"), 0);
440 }
441
442 HWTEST_F(AppSpawnSandboxTest, App_Spawn_JsonUtil_007, TestSize.Level0)
443 {
444 const char *args[] = {"send", "--type", "2"};
445 std::string cmd = "send --type 2 ";
446 size_t size = sizeof(args) / sizeof(args[0]);
447 ASSERT_EQ(TestJsonUtilSplit(args, size, cmd, " "), 0);
448 }
449
450 /**
451 * @brief 测试Variable 变量替换 <currentUserId> <PackageName_index>
452 *
453 */
454 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_001, TestSize.Level0)
455 {
456 AddDefaultVariable();
457 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
458 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
459 ASSERT_EQ(context != nullptr, 1);
460
461 const char *real = "/data/app/el2/100/log/100_com.example.myapplication";
462 const char *value = GetSandboxRealVar(context, 0,
463 "/data/app/el2/<currentUserId>/log/<PackageName_index>", nullptr, nullptr);
464 APPSPAWN_LOGV("value %{public}s", value);
465 APPSPAWN_LOGV("real %{public}s", real);
466 ASSERT_EQ(value != nullptr, 1);
467 ASSERT_EQ(strcmp(value, real) == 0, 1);
468 DeleteSandboxContext(&context);
469 DeleteAppSpawningCtx(spawningCtx);
470 }
471
472 /**
473 * @brief 测试变量<lib>
474 *
475 */
476 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_002, TestSize.Level0)
477 {
478 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
479 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
480 ASSERT_EQ(context != nullptr, 1);
481
482 const char *value = GetSandboxRealVar(context, 0, "/system/<lib>/module", nullptr, nullptr);
483 APPSPAWN_LOGV("value %{public}s", value);
484 ASSERT_EQ(value != nullptr, 1);
485 #ifdef APPSPAWN_64
486 ASSERT_EQ(strcmp(value, "/system/lib64/module") == 0, 1);
487 #else
488 ASSERT_EQ(strcmp(value, "/system/lib/module") == 0, 1);
489 #endif
490 DeleteSandboxContext(&context);
491 DeleteAppSpawningCtx(spawningCtx);
492 }
493
494 /**
495 * @brief 测试系统参数变量<param:test.variable.001>
496 *
497 */
498 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_003, TestSize.Level0)
499 {
500 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
501 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
502 ASSERT_EQ(context != nullptr, 1);
503
504 const char *real = "/system/test.variable.001/test001";
505 const char *value = GetSandboxRealVar(context, 0, "/system/<param:test.variable.001>/test001", nullptr, nullptr);
506 APPSPAWN_LOGV("value %{public}s", value);
507 ASSERT_EQ(value != nullptr, 1);
508 ASSERT_EQ(strcmp(value, real) == 0, 1);
509 DeleteSandboxContext(&context);
510 DeleteAppSpawningCtx(spawningCtx);
511 }
512
TestVariableReplace(const SandboxContext * context,const char * buffer,uint32_t bufferLen,uint32_t * realLen,const VarExtraData * extraData)513 static int TestVariableReplace(const SandboxContext *context,
514 const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData)
515 {
516 int len = sprintf_s((char *)buffer, bufferLen, "%s", "Test-value-001.Test-value-002.Test-value-003.Test-value-004");
517 APPSPAWN_CHECK(len > 0 && ((uint32_t)len < bufferLen),
518 return -1, "Failed to format path app: %{public}s", context->bundleName);
519 *realLen = (uint32_t)len;
520 return 0;
521 }
522
523 /**
524 * @brief 测试注册变量,和替换
525 *
526 */
527 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_004, TestSize.Level0)
528 {
529 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
530 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
531 ASSERT_EQ(context != nullptr, 1);
532
533 AddVariableReplaceHandler("<Test-Var-001>", TestVariableReplace);
534
535 const char *real = "/system/Test-value-001.Test-value-002.Test-value-003.Test-value-004/test001";
536 const char *value = GetSandboxRealVar(context, 0, "/system/<Test-Var-001>/test001", nullptr, nullptr);
537 APPSPAWN_LOGV("value %{public}s", value);
538 ASSERT_EQ(value != nullptr, 1);
539 ASSERT_EQ(strcmp(value, real) == 0, 1);
540 DeleteSandboxContext(&context);
541 DeleteAppSpawningCtx(spawningCtx);
542 }
543
544 /**
545 * @brief 测试dep-path 变量替换
546 *
547 */
TestGetVarExtraData(const SandboxContext * context,uint32_t sandboxTag,const PathMountNode * depNode)548 static VarExtraData *TestGetVarExtraData(const SandboxContext *context, uint32_t sandboxTag,
549 const PathMountNode *depNode)
550 {
551 static VarExtraData extraData;
552 (void)memset_s(&extraData, sizeof(extraData), 0, sizeof(extraData));
553 extraData.sandboxTag = sandboxTag;
554 if (sandboxTag == SANDBOX_TAG_NAME_GROUP) {
555 extraData.data.depNode = (PathMountNode *)depNode;
556 }
557 return &extraData;
558 }
559
TestSetMountPathOperation(uint32_t * operation,uint32_t index)560 static inline void TestSetMountPathOperation(uint32_t *operation, uint32_t index)
561 {
562 *operation |= (1 << index);
563 }
564
565 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_005, TestSize.Level0)
566 {
567 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
568 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
569 ASSERT_EQ(context != nullptr, 1);
570
571 PathMountNode pathNode;
572 pathNode.source = const_cast<char *>("/data/app/el2/<currentUserId>/base");
573 pathNode.target = const_cast<char *>("/data/storage/el2");
574 pathNode.category = MOUNT_TMP_SHRED;
575 VarExtraData *extraData = TestGetVarExtraData(context, SANDBOX_TAG_NAME_GROUP, &pathNode);
576 const char *real = "/data/storage/el2/base";
577 TestSetMountPathOperation(&extraData->operation, MOUNT_PATH_OP_REPLACE_BY_SANDBOX);
578 const char *value = GetSandboxRealVar(context, 0, "<deps-path>/base", nullptr, extraData);
579 APPSPAWN_LOGV("value %{public}s", value);
580 ASSERT_EQ(value != nullptr, 1);
581 ASSERT_EQ(strcmp(value, real) == 0, 1);
582 DeleteSandboxContext(&context);
583 DeleteAppSpawningCtx(spawningCtx);
584 }
585
586 /**
587 * @brief 测试dep-path 变量替换
588 *
589 */
590 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_006, TestSize.Level0)
591 {
592 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
593 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
594 ASSERT_EQ(context != nullptr, 1);
595
596 PathMountNode pathNode;
597 pathNode.source = const_cast<char *>("/data/app/el2/<currentUserId>/base");
598 pathNode.target = const_cast<char *>("/data/storage/el2");
599 pathNode.category = MOUNT_TMP_SHRED;
600 VarExtraData *extraData = TestGetVarExtraData(context, SANDBOX_TAG_NAME_GROUP, &pathNode);
601 const char *real = "/data/app/el2/100/base/base";
602 const char *value = GetSandboxRealVar(context, 0, "<deps-path>/base", nullptr, extraData);
603 APPSPAWN_LOGV("value %{public}s", value);
604 ASSERT_EQ(value != nullptr, 1);
605 ASSERT_EQ(strcmp(value, real) == 0, 1);
606 DeleteSandboxContext(&context);
607 DeleteAppSpawningCtx(spawningCtx);
608 }
609
610 /**
611 * @brief 测试dep-src-path 变量替换
612 *
613 */
614 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_007, TestSize.Level0)
615 {
616 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
617 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
618 ASSERT_EQ(context != nullptr, 1);
619
620 PathMountNode pathNode;
621 pathNode.source = const_cast<char *>("/data/app/el2/<currentUserId>/base");
622 pathNode.target = const_cast<char *>("/data/storage/el2");
623 pathNode.category = MOUNT_TMP_SHRED;
624 VarExtraData *extraData = TestGetVarExtraData(context, SANDBOX_TAG_NAME_GROUP, &pathNode);
625 const char *real = "/data/app/el2/100/base/base";
626 const char *value = GetSandboxRealVar(context, 0, "<deps-src-path>/base", nullptr, extraData);
627 APPSPAWN_LOGV("value %{public}s", value);
628 ASSERT_EQ(value != nullptr, 1);
629 ASSERT_EQ(strcmp(value, real) == 0, 1);
630 DeleteSandboxContext(&context);
631 DeleteAppSpawningCtx(spawningCtx);
632 }
633
634 /**
635 * @brief 测试dep-sandbox-path 变量替换
636 *
637 */
638 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_008, TestSize.Level0)
639 {
640 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
641 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
642 ASSERT_EQ(context != nullptr, 1);
643
644 PathMountNode pathNode;
645 pathNode.source = const_cast<char *>("/data/app/el2/<currentUserId>/base");
646 pathNode.target = const_cast<char *>("/data/storage/el2");
647 pathNode.category = MOUNT_TMP_SHRED;
648 VarExtraData *extraData = TestGetVarExtraData(context, SANDBOX_TAG_NAME_GROUP, &pathNode);
649 const char *real = "/data/storage/el2/base";
650 const char *value = GetSandboxRealVar(context, 0, "<deps-sandbox-path>/base", nullptr, extraData);
651 APPSPAWN_LOGV("value %{public}s", value);
652 ASSERT_EQ(value != nullptr, 1);
653 ASSERT_EQ(strcmp(value, real) == 0, 1);
654 DeleteSandboxContext(&context);
655 DeleteAppSpawningCtx(spawningCtx);
656 }
657
658 /**
659 * @brief 测试不存在的变量替换
660 *
661 */
662 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Variable_009, TestSize.Level0)
663 {
664 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
665 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
666 ASSERT_EQ(context != nullptr, 1);
667
668 const char *real = "<deps-test-path>/base";
669 const char *value = GetSandboxRealVar(context, 0, "<deps-test-path>/base", nullptr, nullptr);
670 APPSPAWN_LOGV("value %{public}s", value);
671 ASSERT_EQ(value != nullptr, 1);
672 ASSERT_EQ(strcmp(value, real) == 0, 1);
673 DeleteSandboxContext(&context);
674 DeleteAppSpawningCtx(spawningCtx);
675 }
676
677 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Permission_01, TestSize.Level0)
678 {
679 AppSpawnSandboxCfg *sandbox = nullptr;
680 int ret = -1;
681 do {
682 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
683 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
684 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
685 sandbox->extData.dumpNode(&sandbox->extData);
686
687 AppSpawnTestHelper testHelper;
688 const std::vector<const char *> &permissions = testHelper.GetPermissions();
689 for (auto permission : permissions) {
690 const SandboxPermissionNode *node = GetPermissionNodeInQueue(&sandbox->permissionQueue, permission);
691 APPSPAWN_CHECK(node != nullptr && strcmp(node->section.name, permission) == 0,
692 break, "Failed to permission %{public}s", permission);
693 const SandboxPermissionNode *node2 =
694 GetPermissionNodeInQueueByIndex(&sandbox->permissionQueue, node->permissionIndex);
695 APPSPAWN_CHECK(node2 != nullptr && strcmp(node->section.name, node2->section.name) == 0,
696 break, "Failed to permission %{public}s", permission);
697 }
698 const char *permission = "ohos.permission.XXXXX";
699 const SandboxPermissionNode *node = GetPermissionNodeInQueue(&sandbox->permissionQueue, permission);
700 APPSPAWN_CHECK_ONLY_EXPER(node == nullptr, break);
701 node = GetPermissionNodeInQueue(nullptr, permission);
702 APPSPAWN_CHECK_ONLY_EXPER(node == nullptr, break);
703 ret = 0;
704 } while (0);
705 if (sandbox != nullptr) {
706 sandbox->extData.freeNode(&sandbox->extData);
707 }
708 ASSERT_EQ(ret, 0);
709 }
710
ProcessTestExpandConfig(const SandboxContext * context,const AppSpawnSandboxCfg * appSandbox,const char * name)711 static int ProcessTestExpandConfig(const SandboxContext *context,
712 const AppSpawnSandboxCfg *appSandbox, const char *name)
713 {
714 uint32_t size = 0;
715 char *extInfo = (char *)GetAppSpawnMsgExtInfo(context->message, name, &size);
716 if (size == 0 || extInfo == nullptr) {
717 return 0;
718 }
719 return 0;
720 }
721
722 HWTEST_F(AppSpawnSandboxTest, App_Spawn_ExpandCfg_01, TestSize.Level0)
723 {
724 AppSpawnClientHandle clientHandle = nullptr;
725 AppSpawnReqMsgHandle reqHandle = 0;
726 AppSpawningCtx *property = nullptr;
727 AppSpawnSandboxCfg *sandbox = nullptr;
728 int ret = -1;
729 do {
730 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
731 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
732 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
733 // add default
734 AddDefaultExpandAppSandboxConfigHandle();
735 // create msg
736 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
737 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
738 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
739 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
740 // add expand info to msg
741 const char hspListStr[] = "{ \
742 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
743 \"modules\":[\"module1\", \"module2\"], \
744 \"versions\":[\"v10001\", \"v10002\"] \
745 }";
746 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "HspList",
747 reinterpret_cast<uint8_t *>(const_cast<char *>(hspListStr)), strlen(hspListStr) + 1);
748 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", hspListStr);
749
750 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
751 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
752 ret = MountSandboxConfigs(sandbox, property, 0);
753 } while (0);
754 if (sandbox != nullptr) {
755 sandbox->extData.freeNode(&sandbox->extData);
756 }
757 DeleteAppSpawningCtx(property);
758 AppSpawnClientDestroy(clientHandle);
759 ASSERT_EQ(ret, 0);
760 }
761
762 HWTEST_F(AppSpawnSandboxTest, App_Spawn_ExpandCfg_02, TestSize.Level0)
763 {
764 AppSpawnClientHandle clientHandle = nullptr;
765 AppSpawnReqMsgHandle reqHandle = 0;
766 AppSpawningCtx *property = nullptr;
767 AppSpawnSandboxCfg *sandbox = nullptr;
768 int ret = -1;
769 do {
770 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
771 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
772 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
773
774 // add default
775 AddDefaultExpandAppSandboxConfigHandle();
776 // create msg
777 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
778 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
779 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
780 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
781 // add expand info to msg
782 const char dataGroupInfoListStr[] = R"([
783 {
784 "gid":"2010001",
785 "dir":"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975",
786 "dataGroupId":"43200",
787 "uuid":"091a68a9-2cc9-4279-8849-28631b598975"
788 }, {
789 "gid":"2010001",
790 "dir":"/data/app/el2/100/group/49c016e6-065a-abd1-5867-b1f91114f840",
791 "dataGroupId":"43200",
792 "uuid":"49c016e6-065a-abd1-5867-b1f91114f840"
793 }
794 ])";
795 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "DataGroup",
796 reinterpret_cast<uint8_t *>(const_cast<char *>(dataGroupInfoListStr)), strlen(dataGroupInfoListStr) + 1);
797 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", dataGroupInfoListStr);
798
799 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
800 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
801 ret = MountSandboxConfigs(sandbox, property, 0);
802 } while (0);
803 if (sandbox != nullptr) {
804 sandbox->extData.freeNode(&sandbox->extData);
805 }
806 DeleteAppSpawningCtx(property);
807 AppSpawnClientDestroy(clientHandle);
808 ASSERT_EQ(ret, 0);
809 }
810
811 HWTEST_F(AppSpawnSandboxTest, App_Spawn_ExpandCfg_03, TestSize.Level0)
812 {
813 AppSpawnClientHandle clientHandle = nullptr;
814 AppSpawnReqMsgHandle reqHandle = 0;
815 AppSpawningCtx *property = nullptr;
816 AppSpawnSandboxCfg *sandbox = nullptr;
817 int ret = -1;
818 do {
819 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
820 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
821 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
822
823 // add default
824 AddDefaultVariable();
825 AddDefaultExpandAppSandboxConfigHandle();
826
827 // create msg
828 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
829 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
830 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
831 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
832 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_OVERLAY);
833 // add expand info to msg
834 const char *overlayInfo = "/data/app/el1/bundle/public/com.ohos.demo/feature.hsp| "
835 "/data/app/el1/bundle/public/com.ohos.demo/feature.hsp";
836
837 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "Overlay",
838 reinterpret_cast<uint8_t *>(const_cast<char *>(overlayInfo)), strlen(overlayInfo) + 1);
839 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", overlayInfo);
840
841 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
842 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
843 ret = MountSandboxConfigs(sandbox, property, 0);
844 } while (0);
845 if (sandbox != nullptr) {
846 sandbox->extData.freeNode(&sandbox->extData);
847 }
848 DeleteAppSpawningCtx(property);
849 AppSpawnClientDestroy(clientHandle);
850 ASSERT_EQ(ret, 0);
851 }
852
853 HWTEST_F(AppSpawnSandboxTest, App_Spawn_ExpandCfg_04, TestSize.Level0)
854 {
855 AppSpawnClientHandle clientHandle = nullptr;
856 AppSpawnReqMsgHandle reqHandle = 0;
857 AppSpawningCtx *property = nullptr;
858 AppSpawnSandboxCfg *sandbox = nullptr;
859 int ret = -1;
860 do {
861 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
862 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
863 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
864
865 // add test
866 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
867 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
868 ret = RegisterExpandSandboxCfgHandler("test-cfg", EXPAND_CFG_HANDLER_PRIO_START, ProcessTestExpandConfig);
869 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
870 ret = RegisterExpandSandboxCfgHandler("test-cfg", EXPAND_CFG_HANDLER_PRIO_START, ProcessTestExpandConfig);
871 APPSPAWN_CHECK_ONLY_EXPER(ret == APPSPAWN_NODE_EXIST, break);
872
873 // create msg
874 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
875 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
876 // add expand info to msg
877 const char *testInfo = "\"app-base\":[{ \
878 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
879 \"mount-paths\" : [{ \
880 \"src-path\" : \"/config\", \
881 \"sandbox-path\" : \"/config\", \
882 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
883 \"check-action-status\": \"false\", \
884 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
885 \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \
886 \"dac-override-sensitive\": \"true\", \
887 \"mount-shared-flag\" : \"true\", \
888 \"app-apl-name\" : \"system\", \
889 \"fs-type\": \"sharefs\", \
890 \"options\": \"support_overwrite=1\" \
891 }], \
892 \"symbol-links\" : [] \
893 }]";
894
895 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "test-cfg",
896 reinterpret_cast<uint8_t *>(const_cast<char *>(testInfo)), strlen(testInfo) + 1);
897 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", testInfo);
898
899 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
900 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
901 ret = MountSandboxConfigs(sandbox, property, 0);
902 } while (0);
903 if (sandbox != nullptr) {
904 sandbox->extData.freeNode(&sandbox->extData);
905 }
906 DeleteAppSpawningCtx(property);
907 AppSpawnClientDestroy(clientHandle);
908 ASSERT_EQ(ret, 0);
909 }
910
911 /**
912 * @brief 加载系统的sandbox文件
913 *
914 */
915 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_001, TestSize.Level0)
916 {
917 AppSpawnSandboxCfg *sandbox = nullptr;
918 int ret = -1;
919 do {
920 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
921 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
922 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
923 sandbox->extData.dumpNode(&sandbox->extData);
924 ret = 0;
925 } while (0);
926 if (sandbox != nullptr) {
927 sandbox->extData.freeNode(&sandbox->extData);
928 }
929 ASSERT_EQ(ret, 0);
930 }
931
932 /**
933 * @brief 加载基础的sandbox配置,并检查结果
934 *
935 */
936 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_002, TestSize.Level0)
937 {
938 AppSpawnSandboxCfg *sandbox = nullptr;
939 int ret = -1;
940 do {
941 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
942 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
943 TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
944 APPSPAWN_LOGV("sandbox->rootPath: %{public}s", sandbox->rootPath);
945
946 ret = UnmountDepPaths(sandbox, 1);
947 ASSERT_EQ(ret, 0);
948
949 ASSERT_EQ(sandbox->topSandboxSwitch == 1, 1);
950 ASSERT_EQ((sandbox->sandboxNsFlags & (CLONE_NEWPID | CLONE_NEWNET)) == (CLONE_NEWPID | CLONE_NEWNET), 1);
951 ASSERT_EQ(strcmp(sandbox->rootPath, "/mnt/sandbox/<currentUserId>/app-root"), 0);
952
953 SandboxSection *section = GetSandboxSection(&sandbox->requiredQueue, "system-const");
954 ASSERT_EQ(section != nullptr, 1);
955 // check mount path
956 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(GetFirstSandboxMountPathNode(section));
957 ASSERT_EQ(pathNode != nullptr, 1);
958 ASSERT_EQ(pathNode->checkErrorFlag == 0, 1);
959 ASSERT_EQ((pathNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
960 ASSERT_EQ(pathNode->category, MOUNT_TMP_SHRED);
961 pathNode = reinterpret_cast<PathMountNode *>(GetNextSandboxMountPathNode(section, &pathNode->sandboxNode));
962 ASSERT_EQ(pathNode != nullptr, 1);
963 ASSERT_EQ(pathNode->category, MOUNT_TMP_RDONLY);
964 pathNode = reinterpret_cast<PathMountNode *>(GetNextSandboxMountPathNode(section, &pathNode->sandboxNode));
965 ASSERT_EQ(pathNode != nullptr, 1);
966 ASSERT_EQ(pathNode->category, MOUNT_TMP_EPFS);
967 pathNode = reinterpret_cast<PathMountNode *>(GetNextSandboxMountPathNode(section, &pathNode->sandboxNode));
968 ASSERT_EQ(pathNode != nullptr, 1);
969 ASSERT_EQ(pathNode->category, MOUNT_TMP_DAC_OVERRIDE);
970 pathNode = reinterpret_cast<PathMountNode *>(GetNextSandboxMountPathNode(section, &pathNode->sandboxNode));
971 ASSERT_EQ(pathNode != nullptr, 1);
972 ASSERT_EQ(pathNode->category, MOUNT_TMP_FUSE);
973 ret = 0;
974 } while (0);
975 if (sandbox) {
976 DeleteAppSpawnSandbox(sandbox);
977 }
978 ASSERT_EQ(ret, 0);
979 }
980
981 /**
982 * @brief 加载包名sandbox配置,并检查结果
983 *
984 */
985 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_003, TestSize.Level0)
986 {
987 AppSpawnSandboxCfg *sandbox = nullptr;
988 int ret = -1;
989 do {
990 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
991 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
992
993 TestParseAppSandboxConfig(sandbox, g_packageNameConfig.c_str());
994 APPSPAWN_LOGV("sandbox->rootPath: %{public}s", sandbox->rootPath);
995
996 // top check
997 ASSERT_EQ(sandbox->topSandboxSwitch == 0, 1); // not set
998 ASSERT_EQ((sandbox->sandboxNsFlags & (CLONE_NEWPID | CLONE_NEWNET)) == (CLONE_NEWPID | CLONE_NEWNET), 1);
999
1000 // check private section
1001 SandboxPackageNameNode *sandboxNode = reinterpret_cast<SandboxPackageNameNode *>(
1002 GetSandboxSection(&sandbox->packageNameQueue, "test.example.ohos.com"));
1003 ASSERT_EQ(sandboxNode != nullptr, 1);
1004 ASSERT_EQ(strcmp(sandboxNode->section.name, "test.example.ohos.com"), 0);
1005 ASSERT_EQ((sandboxNode->section.sandboxShared == 1) && (sandboxNode->section.sandboxSwitch == 1), 1);
1006
1007 // check path node
1008 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(
1009 GetFirstSandboxMountPathNode(&sandboxNode->section));
1010 ASSERT_EQ(pathNode != nullptr, 1);
1011 ASSERT_EQ(pathNode->checkErrorFlag == 0, 1);
1012 ASSERT_EQ((pathNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1013
1014 ASSERT_EQ((pathNode->appAplName != nullptr) && (strcmp(pathNode->appAplName, "system") == 0), 1);
1015 // check symlink
1016 SymbolLinkNode *linkNode = reinterpret_cast<SymbolLinkNode *>(
1017 GetNextSandboxMountPathNode(&sandboxNode->section, &pathNode->sandboxNode));
1018 ASSERT_EQ(linkNode != nullptr, 1);
1019 ASSERT_EQ(linkNode->checkErrorFlag == 0, 1);
1020 ASSERT_EQ((linkNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1021 ret = 0;
1022 } while (0);
1023 if (sandbox) {
1024 DeleteAppSpawnSandbox(sandbox);
1025 }
1026 ASSERT_EQ(ret, 0);
1027 }
1028
1029 /**
1030 * @brief 加载一个permission sandbox 配置。并检查配置解析是否正确
1031 *
1032 */
1033 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_004, TestSize.Level0)
1034 {
1035 AppSpawnSandboxCfg *sandbox = nullptr;
1036 int ret = -1;
1037 do {
1038 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1039 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1040
1041 TestParseAppSandboxConfig(sandbox, g_permissionConfig.c_str());
1042 APPSPAWN_LOGV("sandbox->rootPath: %{public}s", sandbox->rootPath);
1043 // top check
1044 ASSERT_EQ(sandbox->topSandboxSwitch == 1, 1); // not set, default value 1
1045
1046 // check permission section
1047 SandboxPermissionNode *permissionNode = reinterpret_cast<SandboxPermissionNode *>(
1048 GetSandboxSection(&sandbox->permissionQueue, "ohos.permission.FILE_ACCESS_MANAGER"));
1049 ASSERT_EQ(permissionNode != nullptr, 1);
1050 ASSERT_EQ(permissionNode->section.gidTable != nullptr, 1);
1051 ASSERT_EQ(permissionNode->section.gidCount, 2);
1052 ASSERT_EQ(permissionNode->section.gidTable[0], 1006);
1053 ASSERT_EQ(permissionNode->section.gidTable[1], 1008);
1054 ASSERT_EQ(strcmp(permissionNode->section.name, "ohos.permission.FILE_ACCESS_MANAGER"), 0);
1055
1056 // check path node
1057 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(
1058 GetFirstSandboxMountPathNode(&permissionNode->section));
1059 ASSERT_EQ(pathNode != nullptr, 1);
1060 ASSERT_EQ(pathNode->checkErrorFlag == 1, 1);
1061 ASSERT_EQ((pathNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1062 ASSERT_EQ((pathNode->appAplName != nullptr) && (strcmp(pathNode->appAplName, "system") == 0), 1);
1063
1064 // check symlink
1065 SymbolLinkNode *linkNode = reinterpret_cast<SymbolLinkNode *>(
1066 GetNextSandboxMountPathNode(&permissionNode->section, &pathNode->sandboxNode));
1067 ASSERT_EQ(linkNode != nullptr, 1);
1068 ASSERT_EQ(linkNode->checkErrorFlag == 0, 1);
1069 ASSERT_EQ((linkNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1070 ret = 0;
1071 } while (0);
1072 if (sandbox) {
1073 DeleteAppSpawnSandbox(sandbox);
1074 }
1075 ASSERT_EQ(ret, 0);
1076 }
1077
1078 /**
1079 * @brief 加载一个spawn-flags sandbox 配置。并检查配置解析是否正确
1080 *
1081 */
1082 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_005, TestSize.Level0)
1083 {
1084 AppSpawnSandboxCfg *sandbox = nullptr;
1085 int ret = -1;
1086 do {
1087 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1088 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1089 TestParseAppSandboxConfig(sandbox, g_spawnFlagsConfig.c_str());
1090
1091 // top check
1092 ASSERT_EQ(sandbox->topSandboxSwitch == 0, 1); // not set
1093 ASSERT_EQ(sandbox->sandboxNsFlags == (CLONE_NEWPID | CLONE_NEWNET), 1);
1094
1095 // check private section
1096 SandboxFlagsNode *sandboxNode = reinterpret_cast<SandboxFlagsNode *>(
1097 GetSandboxSection(&sandbox->spawnFlagsQueue, "START_FLAGS_BACKUP"));
1098 ASSERT_EQ(sandboxNode != nullptr, 1);
1099 ASSERT_EQ(strcmp(sandboxNode->section.name, "START_FLAGS_BACKUP"), 0);
1100 // no set, check default
1101 ASSERT_EQ((sandboxNode->section.sandboxShared == 0) && (sandboxNode->section.sandboxSwitch == 1), 1);
1102 ASSERT_EQ(sandboxNode->flagIndex == APP_FLAGS_BACKUP_EXTENSION, 1);
1103
1104 // check path node
1105 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(
1106 GetFirstSandboxMountPathNode(&sandboxNode->section));
1107 ASSERT_EQ(pathNode != nullptr, 1);
1108 ASSERT_EQ(pathNode->checkErrorFlag == 1, 1); // set
1109 ASSERT_EQ((pathNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1110 ASSERT_EQ((pathNode->appAplName != nullptr) && (strcmp(pathNode->appAplName, "system") == 0), 1);
1111 ret = 0;
1112 } while (0);
1113 if (sandbox) {
1114 DeleteAppSpawnSandbox(sandbox);
1115 }
1116 ASSERT_EQ(ret, 0);
1117 }
1118
1119 /**
1120 * @brief 加载一个name-group sandbox 配置。并检查配置解析是否正确
1121 *
1122 */
1123 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_006, TestSize.Level0)
1124 {
1125 AppSpawnSandboxCfg *sandbox = nullptr;
1126 int ret = -1;
1127 do {
1128 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1129 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1130 TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1131
1132 // check private section
1133 SandboxNameGroupNode *sandboxNode = reinterpret_cast<SandboxNameGroupNode *>(
1134 GetSandboxSection(&sandbox->nameGroupsQueue, "el5"));
1135 ASSERT_EQ(sandboxNode != nullptr, 1);
1136 ASSERT_EQ(strcmp(sandboxNode->section.name, "el5"), 0);
1137 // no set, check default
1138 ASSERT_EQ((sandboxNode->section.sandboxShared == 0) && (sandboxNode->section.sandboxSwitch == 1), 1);
1139 ASSERT_EQ(sandboxNode->depMode == MOUNT_MODE_NOT_EXIST, 1);
1140 ASSERT_EQ(sandboxNode->destType == SANDBOX_TAG_APP_VARIABLE, 1);
1141 ASSERT_EQ(sandboxNode->depNode != nullptr, 1);
1142 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(sandboxNode->depNode);
1143 ASSERT_EQ(pathNode->category, MOUNT_TMP_SHRED);
1144 ASSERT_EQ(strcmp(pathNode->target, "/data/storage/el5") == 0, 1);
1145
1146 // check path node
1147 pathNode = reinterpret_cast<PathMountNode *>(GetFirstSandboxMountPathNode(&sandboxNode->section));
1148 ASSERT_EQ(pathNode != nullptr, 1);
1149 ASSERT_EQ(strcmp(pathNode->source, "/data/app/el5/<currentUserId>/base/<PackageName>") == 0, 1);
1150 ASSERT_EQ(strcmp(pathNode->target, "<deps-path>/base") == 0, 1);
1151 ret = 0;
1152 } while (0);
1153 if (sandbox) {
1154 DeleteAppSpawnSandbox(sandbox);
1155 }
1156 ASSERT_EQ(ret, 0);
1157 }
1158 /**
1159 * @brief 沙盒执行,能执行到对应的检查项,并且检查通过
1160 *
1161 */
1162 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_001, TestSize.Level0)
1163 {
1164 AppSpawnSandboxCfg *sandbox = nullptr;
1165 AppSpawnClientHandle clientHandle = nullptr;
1166 AppSpawnReqMsgHandle reqHandle = 0;
1167 AppSpawningCtx *property = nullptr;
1168 StubNode *stub = GetStubNode(STUB_MOUNT);
1169 ASSERT_NE(stub != nullptr, 0);
1170 int ret = -1;
1171 do {
1172 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1173 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1174 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1175 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1176
1177 ret = APPSPAWN_ARG_INVALID;
1178 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1179 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1180
1181 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1182 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1183 sandbox->appFullMountEnable = 1;
1184 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1185 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1186
1187 // set check point
1188 MountArg args = {};
1189 args.originPath = "/storage/Users/100/appdata/el1";
1190 args.destinationPath = "/mnt/sandbox/100/app-root/storage/Users/100/appdata/el1";
1191 args.fsType = "sharefs";
1192 args.options = "support_overwrite=1";
1193 args.mountFlags = MS_NODEV;
1194 args.mountSharedFlag = MS_SLAVE;
1195 stub->flags = STUB_NEED_CHECK;
1196 stub->arg = reinterpret_cast<void *>(&args);
1197
1198 ret = StagedMountSystemConst(sandbox, property, 0);
1199 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1200 ASSERT_EQ(stub->result, 0);
1201 } while (0);
1202 if (sandbox) {
1203 DeleteAppSpawnSandbox(sandbox);
1204 }
1205 stub->flags &= ~STUB_NEED_CHECK;
1206 DeleteAppSpawningCtx(property);
1207 AppSpawnClientDestroy(clientHandle);
1208 ASSERT_EQ(ret, 0);
1209 }
1210
1211 /**
1212 * @brief app-variable部分执行。让mount执行失败,但是不需要返回错误结果
1213 *
1214 */
1215 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_002, TestSize.Level0)
1216 {
1217 AppSpawnSandboxCfg *sandbox = nullptr;
1218 AppSpawnClientHandle clientHandle = nullptr;
1219 AppSpawnReqMsgHandle reqHandle = 0;
1220 AppSpawningCtx *property = nullptr;
1221 StubNode *stub = GetStubNode(STUB_MOUNT);
1222 ASSERT_NE(stub != nullptr, 0);
1223 int ret = -1;
1224 do {
1225 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1226 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1227 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1228 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1229
1230 ret = APPSPAWN_ARG_INVALID;
1231 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1232 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1233
1234 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1235 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1236 sandbox->appFullMountEnable = 1;
1237 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1238 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1239
1240 // set check point
1241 MountArg args = {};
1242 args.originPath = "/config";
1243 args.destinationPath = "/mnt/sandbox/100/com.example.myapplication/config";
1244 args.fsType = "sharefs";
1245 args.mountFlags = MS_NODEV | MS_RDONLY; // 当前条件走customizedFlags,这里设置为customizedFlags
1246 stub->flags = STUB_NEED_CHECK;
1247 stub->arg = reinterpret_cast<void *>(&args);
1248 // 执行失败, 但是不返回
1249 args.mountFlags = MS_NODEV;
1250 ret = MountSandboxConfigs(sandbox, property, 0);
1251 ASSERT_EQ(ret, 0);
1252 ASSERT_NE(stub->result, 0);
1253 } while (0);
1254 if (sandbox) {
1255 DeleteAppSpawnSandbox(sandbox);
1256 }
1257 stub->flags &= ~STUB_NEED_CHECK;
1258 DeleteAppSpawningCtx(property);
1259 AppSpawnClientDestroy(clientHandle);
1260 ASSERT_EQ(ret, 0);
1261 }
1262
1263 /**
1264 * @brief app-variable部分执行。让mount执行失败,失败返回错误结果
1265 *
1266 */
1267 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_003, TestSize.Level0)
1268 {
1269 AppSpawnSandboxCfg *sandbox = nullptr;
1270 AppSpawnClientHandle clientHandle = nullptr;
1271 AppSpawnReqMsgHandle reqHandle = 0;
1272 AppSpawningCtx *property = nullptr;
1273 StubNode *stub = GetStubNode(STUB_MOUNT);
1274 ASSERT_NE(stub != nullptr, 0);
1275 int ret = -1;
1276 do {
1277 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1278 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1279 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1280 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1281
1282 ret = APPSPAWN_ARG_INVALID;
1283 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1284 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1285
1286 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1287 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1288 sandbox->appFullMountEnable = 1;
1289
1290 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1291 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1292 SandboxSection *section = GetSandboxSection(&sandbox->requiredQueue, "app-variable");
1293 ASSERT_EQ(section != nullptr, 1);
1294
1295 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(GetFirstSandboxMountPathNode(section));
1296 pathNode->checkErrorFlag = 1; // 设置错误检查
1297 APPSPAWN_LOGV("pathNode %{public}s => %{public}s \n", pathNode->source, pathNode->target);
1298 // set check point
1299 MountArg args = {};
1300 args.originPath = "/config";
1301 args.destinationPath = "/mnt/sandbox/100/com.example.myapplication/config";
1302 args.fsType = "sharefs";
1303 args.mountFlags = MS_NODEV | MS_RDONLY; // 当前条件走customizedFlags,这里设置为customizedFlags
1304 stub->flags = STUB_NEED_CHECK;
1305 stub->arg = reinterpret_cast<void *>(&args);
1306
1307 // 执行失败, 返回错误
1308 args.mountFlags = MS_NODEV;
1309 ret = MountSandboxConfigs(sandbox, property, 0);
1310 ASSERT_NE(ret, 0);
1311 ASSERT_NE(stub->result, 0);
1312 ret = 0;
1313 } while (0);
1314 ASSERT_EQ(ret, 0);
1315 if (sandbox) {
1316 DeleteAppSpawnSandbox(sandbox);
1317 }
1318 stub->flags &= ~STUB_NEED_CHECK;
1319 DeleteAppSpawningCtx(property);
1320 AppSpawnClientDestroy(clientHandle);
1321 }
1322
1323 /**
1324 * @brief package name 执行
1325 *
1326 */
1327 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_004, TestSize.Level0)
1328 {
1329 AppSpawnSandboxCfg *sandbox = nullptr;
1330 AppSpawnClientHandle clientHandle = nullptr;
1331 AppSpawnReqMsgHandle reqHandle = 0;
1332 AppSpawningCtx *property = nullptr;
1333 StubNode *stub = GetStubNode(STUB_MOUNT);
1334 ASSERT_NE(stub != nullptr, 0);
1335 int ret = -1;
1336 do {
1337 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1338 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1339 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1340 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1341
1342 ret = APPSPAWN_ARG_INVALID;
1343 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1344 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1345
1346 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1347 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1348 sandbox->appFullMountEnable = 1;
1349 ret = TestParseAppSandboxConfig(sandbox, g_packageNameConfig.c_str());
1350 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1351
1352 // set check point
1353 MountArg args = {};
1354 args.originPath = "/dev/fuse";
1355 args.destinationPath = "/mnt/sandbox/100/app-root/mnt/data/fuse";
1356 args.fsType = "fuse";
1357 args.mountFlags = MS_LAZYTIME | MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID;
1358 stub->flags = STUB_NEED_CHECK;
1359 stub->arg = reinterpret_cast<void *>(&args);
1360
1361 ret = MountSandboxConfigs(sandbox, property, 0);
1362 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1363 ASSERT_EQ(stub->result, 0);
1364 } while (0);
1365 if (sandbox) {
1366 DeleteAppSpawnSandbox(sandbox);
1367 }
1368 stub->flags &= ~STUB_NEED_CHECK;
1369 DeleteAppSpawningCtx(property);
1370 AppSpawnClientDestroy(clientHandle);
1371 ASSERT_EQ(ret, 0);
1372 }
1373
1374 /**
1375 * @brief 测试package-name执行,执行失败
1376 *
1377 */
1378 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_005, TestSize.Level0)
1379 {
1380 AppSpawnSandboxCfg *sandbox = nullptr;
1381 AppSpawnClientHandle clientHandle = nullptr;
1382 AppSpawnReqMsgHandle reqHandle = 0;
1383 AppSpawningCtx *property = nullptr;
1384 StubNode *stub = GetStubNode(STUB_MOUNT);
1385 ASSERT_NE(stub != nullptr, 0);
1386 int ret = -1;
1387 do {
1388 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1389 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1390 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1391 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1392
1393 ret = APPSPAWN_ARG_INVALID;
1394 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1395 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1396
1397 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1398 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1399 sandbox->appFullMountEnable = 1;
1400
1401 ret = TestParseAppSandboxConfig(sandbox, g_packageNameConfig.c_str());
1402 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1403
1404 // set check point
1405 MountArg args = {};
1406 args.originPath = "/dev/fuse";
1407 args.destinationPath = "/home/axw/appspawn_ut/mnt/sandbox/100/com.example.myapplication/mnt/data/fuse";
1408 args.fsType = "fuse";
1409 args.mountFlags = MS_LAZYTIME | MS_NOATIME | MS_NODEV | MS_NOEXEC;
1410 stub->flags = STUB_NEED_CHECK;
1411 stub->arg = reinterpret_cast<void *>(&args);
1412 ret = MountSandboxConfigs(sandbox, property, 0);
1413 ASSERT_NE(ret, 0);
1414 ASSERT_NE(stub->result, 0);
1415 ret = 0;
1416 } while (0);
1417 if (sandbox) {
1418 DeleteAppSpawnSandbox(sandbox);
1419 }
1420 stub->flags &= ~STUB_NEED_CHECK;
1421 DeleteAppSpawningCtx(property);
1422 AppSpawnClientDestroy(clientHandle);
1423 ASSERT_EQ(ret, 0);
1424 }
1425
1426 /**
1427 * @brief 测试permission 添加下appFullMountEnable 打开
1428 *
1429 */
1430 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_006, TestSize.Level0)
1431 {
1432 AppSpawnSandboxCfg *sandbox = nullptr;
1433 AppSpawnClientHandle clientHandle = nullptr;
1434 AppSpawnReqMsgHandle reqHandle = 0;
1435 AppSpawningCtx *property = nullptr;
1436 StubNode *stub = GetStubNode(STUB_MOUNT);
1437 ASSERT_NE(stub != nullptr, 0);
1438 int ret = -1;
1439 do {
1440 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1441 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1442 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1443 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1444
1445 ret = APPSPAWN_ARG_INVALID;
1446 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1447 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1448
1449 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1450 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1451 sandbox->appFullMountEnable = 1;
1452
1453 ret = TestParseAppSandboxConfig(sandbox, g_permissionConfig.c_str());
1454 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1455 // set permission flags
1456 int index = GetPermissionIndexInQueue(&sandbox->permissionQueue, "ohos.permission.FILE_ACCESS_MANAGER");
1457 SetAppPermissionFlags(property, index);
1458
1459 // set check point
1460 MountArg args = {};
1461 args.originPath = "/config--1";
1462 args.destinationPath = "/mnt/sandbox/100/app-root/data/app/el1/currentUser/"
1463 "database/com.example.myapplication_100";
1464 // permission 下,fstype使用default
1465 // "sharefs"
1466 args.mountFlags = MS_BIND | MS_REC;
1467 stub->flags = STUB_NEED_CHECK;
1468 stub->arg = reinterpret_cast<void *>(&args);
1469 stub->result = 0;
1470 ret = MountSandboxConfigs(sandbox, property, 0);
1471 ASSERT_NE(ret, 0); // do not check result
1472 ASSERT_NE(stub->result, 0);
1473 ret = 0;
1474 } while (0);
1475 if (sandbox) {
1476 DeleteAppSpawnSandbox(sandbox);
1477 }
1478 stub->flags &= ~STUB_NEED_CHECK;
1479 DeleteAppSpawningCtx(property);
1480 AppSpawnClientDestroy(clientHandle);
1481 ASSERT_EQ(ret, 0);
1482 }
1483
1484 /**
1485 * @brief 测试permission 添加下appFullMountEnable 打开,执行失败
1486 *
1487 */
1488 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_007, TestSize.Level0)
1489 {
1490 AppSpawnSandboxCfg *sandbox = nullptr;
1491 AppSpawnClientHandle clientHandle = nullptr;
1492 AppSpawnReqMsgHandle reqHandle = 0;
1493 AppSpawningCtx *property = nullptr;
1494 StubNode *stub = GetStubNode(STUB_MOUNT);
1495 ASSERT_NE(stub != nullptr, 0);
1496 int ret = -1;
1497 do {
1498 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1499 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1500 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1501 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1502
1503 ret = APPSPAWN_ARG_INVALID;
1504 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1505 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1506
1507 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1508 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1509 sandbox->appFullMountEnable = 1;
1510
1511 ret = TestParseAppSandboxConfig(sandbox, g_permissionConfig.c_str());
1512 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1513 // set permission flags
1514 int index = GetPermissionIndexInQueue(&sandbox->permissionQueue, "ohos.permission.FILE_ACCESS_MANAGER");
1515 SetAppPermissionFlags(property, index);
1516
1517 // set check point
1518 MountArg args = {};
1519 args.originPath = "/config--1";
1520 args.destinationPath = "/mnt/sandbox/100/app-root/data/app/el1/currentUser/"
1521 "database/com.example.myapplication_100";
1522 args.fsType = "sharefs";
1523 args.mountFlags = MS_RDONLY;
1524 stub->flags = STUB_NEED_CHECK;
1525 stub->arg = reinterpret_cast<void *>(&args);
1526 ret = MountSandboxConfigs(sandbox, property, 0);
1527 ASSERT_NE(ret, 0);
1528 ASSERT_NE(stub->result, 0);
1529 ret = 0;
1530 } while (0);
1531 if (sandbox) {
1532 DeleteAppSpawnSandbox(sandbox);
1533 }
1534 stub->flags &= ~STUB_NEED_CHECK;
1535 DeleteAppSpawningCtx(property);
1536 AppSpawnClientDestroy(clientHandle);
1537 ASSERT_EQ(ret, 0);
1538 }
1539
1540 /**
1541 * @brief system-config部分执行,测试每一种模版结果是否正确
1542 * 测试 shared
1543 *
1544 */
1545 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_001, TestSize.Level0)
1546 {
1547 AppSpawnSandboxCfg *sandbox = nullptr;
1548 AppSpawnClientHandle clientHandle = nullptr;
1549 AppSpawnReqMsgHandle reqHandle = 0;
1550 AppSpawningCtx *property = nullptr;
1551 StubNode *stub = GetStubNode(STUB_MOUNT);
1552 ASSERT_NE(stub != nullptr, 0);
1553 int ret = -1;
1554 do {
1555 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1556 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1557 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1558 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1559
1560 ret = APPSPAWN_ARG_INVALID;
1561 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1562 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1563
1564 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1565 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1566 sandbox->appFullMountEnable = 1;
1567 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1568 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1569
1570 // set check point
1571 MountArg args = {};
1572 args.originPath = "/lib";
1573 args.destinationPath = "/mnt/sandbox/100/app-root/lib";
1574 args.fsType = nullptr;
1575 args.mountFlags = MS_BIND | MS_REC;
1576 args.mountSharedFlag = MS_SHARED;
1577 stub->flags = STUB_NEED_CHECK;
1578 stub->arg = reinterpret_cast<void *>(&args);
1579
1580 ret = StagedMountSystemConst(sandbox, property, 0);
1581 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1582 ASSERT_EQ(stub->result, 0);
1583 } while (0);
1584 if (sandbox) {
1585 DeleteAppSpawnSandbox(sandbox);
1586 }
1587 stub->flags &= ~STUB_NEED_CHECK;
1588 DeleteAppSpawningCtx(property);
1589 AppSpawnClientDestroy(clientHandle);
1590 ASSERT_EQ(ret, 0);
1591 }
1592
1593 /**
1594 * @brief system-config部分执行,测试每一种模版结果是否正确
1595 * 测试 rdonly
1596 *
1597 */
1598 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_002, TestSize.Level0)
1599 {
1600 AppSpawnSandboxCfg *sandbox = nullptr;
1601 AppSpawnClientHandle clientHandle = nullptr;
1602 AppSpawnReqMsgHandle reqHandle = 0;
1603 AppSpawningCtx *property = nullptr;
1604 StubNode *stub = GetStubNode(STUB_MOUNT);
1605 ASSERT_NE(stub != nullptr, 0);
1606 int ret = -1;
1607 do {
1608 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1609 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1610 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1611 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1612
1613 ret = APPSPAWN_ARG_INVALID;
1614 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1615 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1616
1617 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1618 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1619 sandbox->appFullMountEnable = 1;
1620 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1621 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1622
1623 // set check point
1624 MountArg args = {};
1625 args.originPath = "/lib1";
1626 args.destinationPath = "/mnt/sandbox/100/app-root/lib1";
1627 args.fsType = nullptr;
1628 args.mountFlags = MS_NODEV | MS_RDONLY;
1629 args.mountSharedFlag = MS_SLAVE;
1630 stub->flags = STUB_NEED_CHECK;
1631 stub->arg = reinterpret_cast<void *>(&args);
1632
1633 ret = StagedMountSystemConst(sandbox, property, 0);
1634 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1635 ASSERT_EQ(stub->result, 0);
1636 } while (0);
1637 if (sandbox) {
1638 DeleteAppSpawnSandbox(sandbox);
1639 }
1640 stub->flags &= ~STUB_NEED_CHECK;
1641 DeleteAppSpawningCtx(property);
1642 AppSpawnClientDestroy(clientHandle);
1643 ASSERT_EQ(ret, 0);
1644 }
1645
1646 /**
1647 * @brief system-config部分执行,测试每一种模版结果是否正确
1648 * 测试 epfs
1649 *
1650 */
1651 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_003, TestSize.Level0)
1652 {
1653 AppSpawnSandboxCfg *sandbox = nullptr;
1654 AppSpawnClientHandle clientHandle = nullptr;
1655 AppSpawnReqMsgHandle reqHandle = 0;
1656 AppSpawningCtx *property = nullptr;
1657 StubNode *stub = GetStubNode(STUB_MOUNT);
1658 ASSERT_NE(stub != nullptr, 0);
1659 int ret = -1;
1660 do {
1661 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1662 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1663 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1664 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1665
1666 ret = APPSPAWN_ARG_INVALID;
1667 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1668 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1669
1670 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1671 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1672 sandbox->appFullMountEnable = 1;
1673 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1674 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1675
1676 // set check point
1677 MountArg args = {};
1678 args.originPath = "none";
1679 args.destinationPath = "/mnt/sandbox/100/app-root/storage/cloud/epfs";
1680 args.fsType = "epfs";
1681 args.mountFlags = MS_NODEV;
1682 args.mountSharedFlag = MS_SLAVE;
1683 stub->flags = STUB_NEED_CHECK;
1684 stub->arg = reinterpret_cast<void *>(&args);
1685
1686 ret = StagedMountSystemConst(sandbox, property, 0);
1687 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1688 ASSERT_EQ(stub->result, 0);
1689 } while (0);
1690 if (sandbox) {
1691 DeleteAppSpawnSandbox(sandbox);
1692 }
1693 stub->flags &= ~STUB_NEED_CHECK;
1694 DeleteAppSpawningCtx(property);
1695 AppSpawnClientDestroy(clientHandle);
1696 ASSERT_EQ(ret, 0);
1697 }
1698
1699 /**
1700 * @brief system-config部分执行,测试每一种模版结果是否正确
1701 * 测试 fuse
1702 *
1703 */
1704 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_004, TestSize.Level0)
1705 {
1706 AppSpawnSandboxCfg *sandbox = nullptr;
1707 AppSpawnClientHandle clientHandle = nullptr;
1708 AppSpawnReqMsgHandle reqHandle = 0;
1709 AppSpawningCtx *property = nullptr;
1710 StubNode *stub = GetStubNode(STUB_MOUNT);
1711 ASSERT_NE(stub != nullptr, 0);
1712 int ret = -1;
1713 do {
1714 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1715 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1716 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1717 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1718
1719 ret = APPSPAWN_ARG_INVALID;
1720 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1721 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1722
1723 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1724 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1725 sandbox->appFullMountEnable = 1;
1726 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1727 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1728
1729 // set check point
1730 MountArg args = {};
1731 args.originPath = "/dev/fuse";
1732 args.destinationPath = "/mnt/sandbox/100/app-root/mnt/data/fuse";
1733 args.fsType = "fuse";
1734 args.mountFlags = MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_LAZYTIME;
1735 args.mountSharedFlag = MS_SLAVE;
1736 stub->flags = STUB_NEED_CHECK;
1737 stub->arg = reinterpret_cast<void *>(&args);
1738
1739 ret = StagedMountSystemConst(sandbox, property, 0);
1740 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1741 ASSERT_EQ(stub->result, 0);
1742 } while (0);
1743 if (sandbox) {
1744 DeleteAppSpawnSandbox(sandbox);
1745 }
1746 stub->flags &= ~STUB_NEED_CHECK;
1747 DeleteAppSpawningCtx(property);
1748 AppSpawnClientDestroy(clientHandle);
1749 ASSERT_EQ(ret, 0);
1750 }
1751
1752 /**
1753 * @brief 测试unshare前的执行,not-exist时,节点不存在,执行dep的挂载
1754 *
1755 */
1756 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_001, TestSize.Level0)
1757 {
1758 AppSpawnSandboxCfg *sandbox = nullptr;
1759 AppSpawnClientHandle clientHandle = nullptr;
1760 AppSpawnReqMsgHandle reqHandle = 0;
1761 AppSpawningCtx *property = nullptr;
1762 StubNode *stub = GetStubNode(STUB_MOUNT);
1763 ASSERT_NE(stub != nullptr, 0);
1764 int ret = -1;
1765 do {
1766 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1767 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1768 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1769 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1770
1771 ret = APPSPAWN_ARG_INVALID;
1772 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1773 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1774
1775 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1776 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1777 sandbox->appFullMountEnable = 1;
1778 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1779 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1780
1781 // set check point
1782 MountArg args = {};
1783 args.originPath = "/data/app/el5/100/base";
1784 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/el5";
1785 args.mountFlags = MS_BIND | MS_REC;
1786 args.mountSharedFlag = MS_SHARED;
1787 stub->flags = STUB_NEED_CHECK;
1788 stub->arg = reinterpret_cast<void *>(&args);
1789 stub->result = -1;
1790
1791 ret = MountSandboxConfigs(sandbox, property, 0);
1792 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1793 ASSERT_EQ(stub->result, 0);
1794 } while (0);
1795 if (sandbox) {
1796 DeleteAppSpawnSandbox(sandbox);
1797 }
1798 stub->flags &= ~STUB_NEED_CHECK;
1799 DeleteAppSpawningCtx(property);
1800 AppSpawnClientDestroy(clientHandle);
1801 ASSERT_EQ(ret, 0);
1802 }
1803
1804 /**
1805 * @brief 测试unshare前的执行,not-exist时,节点存在,不执行dep的挂载
1806 *
1807 */
1808 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_002, TestSize.Level0)
1809 {
1810 AppSpawnSandboxCfg *sandbox = nullptr;
1811 AppSpawnClientHandle clientHandle = nullptr;
1812 AppSpawnReqMsgHandle reqHandle = 0;
1813 AppSpawningCtx *property = nullptr;
1814 StubNode *stub = GetStubNode(STUB_MOUNT);
1815 ASSERT_NE(stub != nullptr, 0);
1816 int ret = -1;
1817 do {
1818 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1819 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1820 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1821 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1822
1823 ret = APPSPAWN_ARG_INVALID;
1824 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1825 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1826
1827 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1828 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1829 sandbox->appFullMountEnable = 1;
1830 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1831 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1832
1833 // set check point
1834 MountArg args = {};
1835 args.originPath = "/data/app/el6/100/base";
1836 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/el6";
1837 args.mountFlags = MS_BIND | MS_REC;
1838 args.mountSharedFlag = MS_SHARED;
1839 stub->flags = STUB_NEED_CHECK;
1840 stub->arg = reinterpret_cast<void *>(&args);
1841
1842 ret = MountSandboxConfigs(sandbox, property, 0);
1843 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1844 ASSERT_EQ(stub->result, 0);
1845 } while (0);
1846 if (sandbox) {
1847 DeleteAppSpawnSandbox(sandbox);
1848 }
1849 stub->flags &= ~STUB_NEED_CHECK;
1850 DeleteAppSpawningCtx(property);
1851 AppSpawnClientDestroy(clientHandle);
1852 ASSERT_EQ(ret, 0);
1853 }
1854
1855 /**
1856 * @brief 测试unshare前的执行,always时,执行dep的挂载
1857 *
1858 */
1859 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_003, TestSize.Level0)
1860 {
1861 AppSpawnSandboxCfg *sandbox = nullptr;
1862 AppSpawnClientHandle clientHandle = nullptr;
1863 AppSpawnReqMsgHandle reqHandle = 0;
1864 AppSpawningCtx *property = nullptr;
1865 StubNode *stub = GetStubNode(STUB_MOUNT);
1866 ASSERT_NE(stub != nullptr, 0);
1867 int ret = -1;
1868 do {
1869 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1870 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1871 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1872 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1873
1874 ret = APPSPAWN_ARG_INVALID;
1875 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1876 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1877
1878 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1879 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1880 sandbox->appFullMountEnable = 1;
1881 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1882 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1883
1884 // set check point
1885 MountArg args = {};
1886 args.originPath = "/data/app/e20/100/base";
1887 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/e20";
1888 args.mountFlags = MS_BIND | MS_REC;
1889 args.mountSharedFlag = MS_SHARED;
1890 stub->flags = STUB_NEED_CHECK;
1891 stub->arg = reinterpret_cast<void *>(&args);
1892 stub->result = 0;
1893
1894 ret = MountSandboxConfigs(sandbox, property, 0);
1895 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1896 ASSERT_EQ(stub->result, 0);
1897 } while (0);
1898 if (sandbox) {
1899 DeleteAppSpawnSandbox(sandbox);
1900 }
1901 stub->flags &= ~STUB_NEED_CHECK;
1902 DeleteAppSpawningCtx(property);
1903 AppSpawnClientDestroy(clientHandle);
1904 ASSERT_EQ(ret, 0);
1905 }
1906
1907 /**
1908 * @brief 测试unshare后执行,一次挂载时,使用sandbox-path
1909 *
1910 */
1911 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_004, TestSize.Level0)
1912 {
1913 AppSpawnSandboxCfg *sandbox = nullptr;
1914 AppSpawnClientHandle clientHandle = nullptr;
1915 AppSpawnReqMsgHandle reqHandle = 0;
1916 AppSpawningCtx *property = nullptr;
1917 StubNode *stub = GetStubNode(STUB_MOUNT);
1918 ASSERT_NE(stub != nullptr, 0);
1919 int ret = -1;
1920 do {
1921 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1922 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1923 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1924 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1925
1926 ret = APPSPAWN_ARG_INVALID;
1927 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1928 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1929
1930 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1931 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1932 sandbox->appFullMountEnable = 1;
1933 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1934 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1935
1936 // set check point
1937 MountArg args = {};
1938 args.originPath = "/data/app/e15/100/base/com.example.myapplication";
1939 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/e15/base";
1940 args.mountFlags = MS_BIND | MS_REC;
1941 args.mountSharedFlag = MS_SLAVE;
1942 stub->flags = STUB_NEED_CHECK;
1943 stub->arg = reinterpret_cast<void *>(&args);
1944 stub->result = 0;
1945
1946 ret = MountSandboxConfigs(sandbox, property, 0);
1947 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1948 ASSERT_EQ(stub->result, 0);
1949 } while (0);
1950 if (sandbox) {
1951 DeleteAppSpawnSandbox(sandbox);
1952 }
1953 stub->flags &= ~STUB_NEED_CHECK;
1954 DeleteAppSpawningCtx(property);
1955 AppSpawnClientDestroy(clientHandle);
1956 ASSERT_EQ(ret, 0);
1957 }
1958
1959 /**
1960 * @brief system-const,一次挂载时,使用sandbox-path
1961 *
1962 */
1963 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_005, TestSize.Level0)
1964 {
1965 AppSpawnSandboxCfg *sandbox = nullptr;
1966 AppSpawnClientHandle clientHandle = nullptr;
1967 AppSpawnReqMsgHandle reqHandle = 0;
1968 AppSpawningCtx *property = nullptr;
1969 StubNode *stub = GetStubNode(STUB_MOUNT);
1970 ASSERT_NE(stub != nullptr, 0);
1971 int ret = -1;
1972 do {
1973 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1974 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1975 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1976 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1977
1978 ret = APPSPAWN_ARG_INVALID;
1979 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1980 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1981
1982 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
1983 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1984 sandbox->appFullMountEnable = 1;
1985 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1986 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1987
1988 // set check point
1989 MountArg args = {};
1990 args.originPath = "/data/app/e20/100/base/com.example.myapplication";
1991 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/e20/base";
1992 args.mountFlags = MS_BIND | MS_REC;
1993 args.mountSharedFlag = MS_SLAVE;
1994 stub->flags = STUB_NEED_CHECK;
1995 stub->arg = reinterpret_cast<void *>(&args);
1996 stub->result = -1;
1997
1998 ret = StagedMountSystemConst(sandbox, property, 0);
1999 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2000 ASSERT_EQ(stub->result, 0);
2001 } while (0);
2002 if (sandbox) {
2003 DeleteAppSpawnSandbox(sandbox);
2004 }
2005 stub->flags &= ~STUB_NEED_CHECK;
2006 DeleteAppSpawningCtx(property);
2007 AppSpawnClientDestroy(clientHandle);
2008 ASSERT_EQ(ret, 0);
2009 }
2010
2011 /**
2012 * @brief 测试app extension
2013 *
2014 */
2015 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_001, TestSize.Level0)
2016 {
2017 AppSpawnSandboxCfg *sandbox = nullptr;
2018 AppSpawnClientHandle clientHandle = nullptr;
2019 AppSpawnReqMsgHandle reqHandle = 0;
2020 AppSpawningCtx *property = nullptr;
2021 int ret = -1;
2022 do {
2023 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2024 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
2025 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
2026 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
2027
2028 // set APP_FLAGS_ISOLATED_SANDBOX
2029 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX);
2030 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2031
2032 ret = APPSPAWN_ARG_INVALID;
2033 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2034 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
2035
2036 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
2037 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
2038 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
2039 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2040
2041 ret = MountSandboxConfigs(sandbox, property, 0);
2042 } while (0);
2043 if (sandbox) {
2044 DeleteAppSpawnSandbox(sandbox);
2045 }
2046 DeleteAppSpawningCtx(property);
2047 AppSpawnClientDestroy(clientHandle);
2048 ASSERT_EQ(ret, 0);
2049 }
2050
2051 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_002, TestSize.Level0)
2052 {
2053 AppSpawnSandboxCfg *sandbox = nullptr;
2054 AppSpawnClientHandle clientHandle = nullptr;
2055 AppSpawnReqMsgHandle reqHandle = 0;
2056 AppSpawningCtx *property = nullptr;
2057 int ret = -1;
2058 do {
2059 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2060 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
2061 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
2062 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
2063
2064 // set APP_FLAGS_ISOLATED_SANDBOX
2065 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX);
2066 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2067
2068 ret = APPSPAWN_ARG_INVALID;
2069 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2070 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
2071
2072 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
2073 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
2074 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
2075 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2076
2077 sandbox->sandboxNsFlags = CLONE_NEWPID; // only pid
2078 ret = MountSandboxConfigs(sandbox, property, 0);
2079 } while (0);
2080 if (sandbox) {
2081 DeleteAppSpawnSandbox(sandbox);
2082 }
2083 DeleteAppSpawningCtx(property);
2084 AppSpawnClientDestroy(clientHandle);
2085 ASSERT_EQ(ret, 0);
2086 }
2087
2088 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_003, TestSize.Level0)
2089 {
2090 AppSpawnSandboxCfg *sandbox = nullptr;
2091 AppSpawnClientHandle clientHandle = nullptr;
2092 AppSpawnReqMsgHandle reqHandle = 0;
2093 AppSpawningCtx *property = nullptr;
2094 int ret = -1;
2095 do {
2096 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2097 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
2098 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
2099 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
2100
2101 // set APP_FLAGS_ISOLATED_SANDBOX
2102 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX);
2103 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2104 // add expand info to msg
2105 const char dataGroupInfoListStr[] = R"([
2106 {
2107 "gid":"2010001",
2108 "dir":"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975",
2109 "dataGroupId":"43200",
2110 "uuid":"091a68a9-2cc9-4279-8849-28631b598975"
2111 }, {
2112 "gid":"2010001",
2113 "dir":"/data/app/el2/100/group/49c016e6-065a-abd1-5867-b1f91114f840",
2114 "dataGroupId":"43200",
2115 "uuid":"49c016e6-065a-abd1-5867-b1f91114f840"
2116 }
2117 ])";
2118 ret = AppSpawnReqMsgAddStringInfo(reqHandle, "DataGroup", dataGroupInfoListStr);
2119 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", dataGroupInfoListStr);
2120
2121 ret = APPSPAWN_ARG_INVALID;
2122 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2123 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
2124
2125 sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
2126 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
2127 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
2128 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2129 ret = MountSandboxConfigs(sandbox, property, 0);
2130 } while (0);
2131 if (sandbox) {
2132 DeleteAppSpawnSandbox(sandbox);
2133 }
2134 DeleteAppSpawningCtx(property);
2135 AppSpawnClientDestroy(clientHandle);
2136 ASSERT_EQ(ret, 0);
2137 }
2138
2139 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_004, TestSize.Level0)
2140 {
2141 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
2142 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2143 ASSERT_EQ(context != nullptr, 1);
2144
2145 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2146 APPSPAWN_LOGV("value %{public}s", value);
2147 ASSERT_EQ(value != nullptr, 1);
2148 ASSERT_EQ(strcmp(value, "/system/com.example.myapplication/module") == 0, 1);
2149 DeleteSandboxContext(&context);
2150 DeleteAppSpawningCtx(spawningCtx);
2151 }
2152
2153 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_005, TestSize.Level0)
2154 {
2155 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
2156 ASSERT_EQ(spawningCtx != nullptr, 1);
2157 int ret = SetAppSpawnMsgFlag(spawningCtx->message, TLV_MSG_FLAGS, APP_FLAGS_CLONE_ENABLE);
2158 ASSERT_EQ(ret, 0);
2159 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2160 ASSERT_EQ(context != nullptr, 1);
2161
2162 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2163 APPSPAWN_LOGV("value %{public}s", value);
2164 ASSERT_EQ(value != nullptr, 1); // +clone-bundleIndex+packageName
2165 ASSERT_EQ(strcmp(value, "/system/+clone-100+com.example.myapplication/module") == 0, 1);
2166 DeleteSandboxContext(&context);
2167 DeleteAppSpawningCtx(spawningCtx);
2168 }
2169
2170 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_006, TestSize.Level0)
2171 {
2172 AppSpawnClientHandle clientHandle = nullptr;
2173 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2174 ASSERT_EQ(ret, 0);
2175 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
2176 ASSERT_EQ(reqHandle != nullptr, 1);
2177 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_EXTENSION, "test001");
2178 ASSERT_EQ(ret, 0);
2179 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
2180 ASSERT_EQ(ret, 0);
2181 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2182 ASSERT_EQ(spawningCtx != nullptr, 1);
2183
2184 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2185 ASSERT_EQ(context != nullptr, 1);
2186
2187 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2188 APPSPAWN_LOGV("value %{public}s", value);
2189 ASSERT_EQ(value != nullptr, 1); // +extension-<extensionType>+packageName
2190 ASSERT_EQ(strcmp(value, "/system/+extension-test001+com.example.myapplication/module") == 0, 1);
2191 DeleteSandboxContext(&context);
2192 DeleteAppSpawningCtx(spawningCtx);
2193 AppSpawnClientDestroy(clientHandle);
2194 }
2195
2196 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_007, TestSize.Level0)
2197 {
2198 AppSpawnClientHandle clientHandle = nullptr;
2199 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2200 ASSERT_EQ(ret, 0);
2201 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
2202 ASSERT_EQ(reqHandle != nullptr, 1);
2203 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_EXTENSION, "test001");
2204 ASSERT_EQ(ret, 0);
2205 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
2206 ASSERT_EQ(ret, 0);
2207 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_CLONE_ENABLE);
2208 ASSERT_EQ(ret, 0);
2209 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2210 ASSERT_EQ(spawningCtx != nullptr, 1);
2211
2212 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2213 ASSERT_EQ(context != nullptr, 1);
2214
2215 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2216 APPSPAWN_LOGV("value %{public}s", value);
2217 ASSERT_EQ(value != nullptr, 1); // +clone-bundleIndex+extension-<extensionType>+packageName
2218 ASSERT_EQ(strcmp(value, "/system/+clone-100+extension-test001+com.example.myapplication/module") == 0, 1);
2219 DeleteSandboxContext(&context);
2220 DeleteAppSpawningCtx(spawningCtx);
2221 AppSpawnClientDestroy(clientHandle);
2222 }
2223
2224 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_008, TestSize.Level0)
2225 {
2226 AppSpawnClientHandle clientHandle = nullptr;
2227 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2228 ASSERT_EQ(ret, 0);
2229 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
2230 ASSERT_EQ(reqHandle != nullptr, 1);
2231 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
2232 ASSERT_EQ(ret, 0);
2233 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_CLONE_ENABLE);
2234 ASSERT_EQ(ret, 0);
2235 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2236 ASSERT_EQ(spawningCtx != nullptr, 1);
2237
2238 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2239 ASSERT_EQ(context != nullptr, 1);
2240
2241 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2242 ASSERT_EQ(value == nullptr, 1);
2243
2244 DeleteSandboxContext(&context);
2245 DeleteAppSpawningCtx(spawningCtx);
2246 AppSpawnClientDestroy(clientHandle);
2247 }
2248
2249 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_009, TestSize.Level0)
2250 {
2251 AppSpawnClientHandle clientHandle = nullptr;
2252 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2253 ASSERT_EQ(ret, 0);
2254 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
2255 ASSERT_EQ(reqHandle != nullptr, 1);
2256 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
2257 ASSERT_EQ(ret, 0);
2258 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2259 ASSERT_EQ(spawningCtx != nullptr, 1);
2260
2261 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2262 ASSERT_EQ(context != nullptr, 1);
2263
2264 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2265 ASSERT_EQ(value == nullptr, 1);
2266
2267 DeleteSandboxContext(&context);
2268 DeleteAppSpawningCtx(spawningCtx);
2269 AppSpawnClientDestroy(clientHandle);
2270 }
2271
2272 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_sandbox, TestSize.Level0)
2273 {
2274 const char *path = "/test1/xxxx/xxxx";
2275 bool ret = CheckDirRecursive(path);
2276 ASSERT_TRUE(ret);
2277
2278 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
2279 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2280 ASSERT_EQ(context != nullptr, 1);
2281 const SandboxContext *contextTest = const_cast<SandboxContext*>(context);
2282 SandboxMountNode node = {};
2283 char source[] = {"/data/app/el5/<currentUserId>/database/<PackageName>"};
2284 char target[] = {"/data/storage/el1/database"};
2285 const PathMountNode sandboxNode = {node, nullptr, nullptr, 0, false, 1, 1, 1, 0, nullptr, {}};
2286 const MountArg args = {"/data/xxx/xxx", nullptr, nullptr, 0, nullptr, 0};
2287 CreateDemandSrc(contextTest, &sandboxNode, &args);
2288 char apl[] = "apl";
2289 const PathMountNode sandboxNode1 = {node, source, nullptr, 0, false, 1, 1, 1, 0, nullptr, {}};
2290 const PathMountNode sandboxNode2 = {node, nullptr, target, 0, false, 1, 1, 1, 0, nullptr, {}};
2291 const PathMountNode sandboxNode3 = {node, source, target, 0, false, 1, 1, 1, 0, apl, {}};
2292 struct ListNode front;
2293 char name[] = {"test"};
2294 const SandboxSection section = {node, front, name, 16, 16, nullptr, 1, 1, nullptr};
2295 char rootPath[] = "/test/xxxx";
2296 SandboxContext context1 = {{{}}, "test.example.ohos.com", nullptr, 1, 1, 1, 1, 1, 1, 1, rootPath};
2297 int res = CheckSandboxMountNode(nullptr, §ion, &sandboxNode, 0);
2298 ASSERT_EQ(res, 0);
2299 res = CheckSandboxMountNode(nullptr, §ion, &sandboxNode1, 0);
2300 ASSERT_EQ(res, 0);
2301 res = CheckSandboxMountNode(nullptr, §ion, &sandboxNode2, 0);
2302 ASSERT_EQ(res, 0);
2303 res = CheckSandboxMountNode(&context1, §ion, &sandboxNode3, MOUNT_PATH_OP_NONE);
2304 EXPECT_EQ(res, 0);
2305 }
2306
2307 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_sandbox_001, TestSize.Level0)
2308 {
2309 char target[] = {"/data/storage/el1/database"};
2310 SandboxMountNode node = {};
2311 struct ListNode front;
2312 char name[] = "test";
2313 char apl[] = "apl";
2314 const SandboxSection section = {node, front, name, 16, 16, nullptr, 1, 1, nullptr};
2315 char rootPath[] = "/test/xxxx";
2316 SandboxContext context1 = {{{}}, "test.example.ohos.com", nullptr, 1, 1, 1, 1, 1, 1, 1, rootPath};
2317 char source1[] = {"/xxx/xxx/xxx"};
2318 uint32_t operation = 0x1 << SANDBOX_TAG_SPAWN_FLAGS;
2319 const PathMountNode sandboxNode4 = {node, source1, target, 0, false, 1, 1, 1, 0, apl, {}};
2320 context1.bundleHasWps = 1;
2321 int res = CheckSandboxMountNode(&context1, §ion, &sandboxNode4, operation);
2322 EXPECT_EQ(res, 1);
2323 context1.bundleHasWps = 0;
2324 res = CheckSandboxMountNode(&context1, §ion, &sandboxNode4, operation);
2325 EXPECT_EQ(res, 1);
2326
2327 char source2[] = "/data/app/xxx";
2328 const PathMountNode sandboxNode5 = {node, source2, target, 0, false, 1, 1, 1, 0, apl, {}};
2329 res = CheckSandboxMountNode(&context1, §ion, &sandboxNode5, operation);
2330 EXPECT_EQ(res, 1);
2331
2332 char source3[] = "/data/app/base";
2333 const PathMountNode sandboxNode6 = {node, source3, target, 0, false, 1, 1, 1, 0, apl, {}};
2334 res = CheckSandboxMountNode(&context1, §ion, &sandboxNode6, operation);
2335 EXPECT_EQ(res, 1);
2336
2337 char source4[] = "/data/app/<PackageName>";
2338 const PathMountNode sandboxNode7 = {node, source4, target, 0, false, 1, 1, 1, 0, apl, {}};
2339 res = CheckSandboxMountNode(&context1, §ion, &sandboxNode7, operation);
2340 EXPECT_EQ(res, 1);
2341 }
2342
2343 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_template, TestSize.Level0)
2344 {
2345 uint32_t ret = GetMountCategory("test");
2346 ASSERT_EQ(ret, MOUNT_TMP_DEFAULT);
2347 const MountArgTemplate *arg = GetMountArgTemplate(10);
2348 ASSERT_EQ(arg, nullptr);
2349 arg = GetMountArgTemplate(8);
2350 ASSERT_EQ(arg, nullptr);
2351 const SandboxFlagInfo info[2] = {{"test1", 0}, {"test2", 0}};
2352 const SandboxFlagInfo *info1 = GetSandboxFlagInfo("test", info, 2);
2353 ASSERT_EQ(info1, nullptr);
2354 ret = GetPathMode("test");
2355 ASSERT_EQ(ret, 0);
2356 }
2357
2358 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_permission, TestSize.Level0)
2359 {
2360 AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
2361 ASSERT_NE(sandbox, nullptr);
2362 int ret = AddSandboxPermissionNode(FILE_CROSS_APP_MODE, nullptr);
2363 ASSERT_EQ(ret, APPSPAWN_ARG_INVALID);
2364 ret = AddSandboxPermissionNode(nullptr, &sandbox->permissionQueue);
2365 ASSERT_EQ(ret, APPSPAWN_ARG_INVALID);
2366 ret = AddSandboxPermissionNode(nullptr, nullptr);
2367 ASSERT_EQ(ret, APPSPAWN_ARG_INVALID);
2368 ret = AddSandboxPermissionNode(FILE_CROSS_APP_MODE, &sandbox->permissionQueue);
2369 ASSERT_EQ(ret, 0);
2370 ret = AddSandboxPermissionNode(FILE_CROSS_APP_MODE, &sandbox->permissionQueue);
2371 ASSERT_EQ(ret, 0);
2372
2373 const SandboxPermissionNode *node = GetPermissionNodeInQueue(nullptr, nullptr);
2374 ASSERT_EQ(node, nullptr);
2375 node = GetPermissionNodeInQueue(&sandbox->permissionQueue, nullptr);
2376 ASSERT_EQ(node, nullptr);
2377 node = GetPermissionNodeInQueue(nullptr, FILE_CROSS_APP_MODE);
2378 ASSERT_EQ(node, nullptr);
2379 node = GetPermissionNodeInQueueByIndex(nullptr, 0);
2380 ASSERT_EQ(node, nullptr);
2381
2382 ret = PermissionRenumber(nullptr);
2383 ASSERT_EQ(ret, -1);
2384 }
2385 } // namespace OHOS