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/com.example.myapplication_100";
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();
683 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
684 LoadAppSandboxConfig(sandbox, 0);
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();
731 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
732 LoadAppSandboxConfig(sandbox, 0);
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();
771 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
772 LoadAppSandboxConfig(sandbox, 0);
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[] = "{ \
783 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
784 \"dir\":[\"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975\", \
785 \"/data/app/el2/100/group/ce876162-fe69-45d3-aa8e-411a047af564\"], \
786 \"gid\":[\"20100001\", \"20100002\"] \
787 }";
788 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "DataGroup",
789 reinterpret_cast<uint8_t *>(const_cast<char *>(dataGroupInfoListStr)), strlen(dataGroupInfoListStr) + 1);
790 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", dataGroupInfoListStr);
791
792 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
793 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
794 ret = MountSandboxConfigs(sandbox, property, 0);
795 } while (0);
796 if (sandbox != nullptr) {
797 sandbox->extData.freeNode(&sandbox->extData);
798 }
799 DeleteAppSpawningCtx(property);
800 AppSpawnClientDestroy(clientHandle);
801 ASSERT_EQ(ret, 0);
802 }
803
804 HWTEST_F(AppSpawnSandboxTest, App_Spawn_ExpandCfg_03, TestSize.Level0)
805 {
806 AppSpawnClientHandle clientHandle = nullptr;
807 AppSpawnReqMsgHandle reqHandle = 0;
808 AppSpawningCtx *property = nullptr;
809 AppSpawnSandboxCfg *sandbox = nullptr;
810 int ret = -1;
811 do {
812 sandbox = CreateAppSpawnSandbox();
813 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
814 LoadAppSandboxConfig(sandbox, 0);
815
816 // add default
817 AddDefaultVariable();
818 AddDefaultExpandAppSandboxConfigHandle();
819
820 // create msg
821 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
822 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
823 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
824 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
825 AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_OVERLAY);
826 // add expand info to msg
827 const char *overlayInfo = "/data/app/el1/bundle/public/com.ohos.demo/feature.hsp| "
828 "/data/app/el1/bundle/public/com.ohos.demo/feature.hsp";
829
830 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "Overlay",
831 reinterpret_cast<uint8_t *>(const_cast<char *>(overlayInfo)), strlen(overlayInfo) + 1);
832 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", overlayInfo);
833
834 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
835 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
836 ret = MountSandboxConfigs(sandbox, property, 0);
837 } while (0);
838 if (sandbox != nullptr) {
839 sandbox->extData.freeNode(&sandbox->extData);
840 }
841 DeleteAppSpawningCtx(property);
842 AppSpawnClientDestroy(clientHandle);
843 ASSERT_EQ(ret, 0);
844 }
845
846 HWTEST_F(AppSpawnSandboxTest, App_Spawn_ExpandCfg_04, TestSize.Level0)
847 {
848 AppSpawnClientHandle clientHandle = nullptr;
849 AppSpawnReqMsgHandle reqHandle = 0;
850 AppSpawningCtx *property = nullptr;
851 AppSpawnSandboxCfg *sandbox = nullptr;
852 int ret = -1;
853 do {
854 sandbox = CreateAppSpawnSandbox();
855 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
856 LoadAppSandboxConfig(sandbox, 0);
857
858 // add test
859 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
860 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
861 ret = RegisterExpandSandboxCfgHandler("test-cfg", EXPAND_CFG_HANDLER_PRIO_START, ProcessTestExpandConfig);
862 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
863 ret = RegisterExpandSandboxCfgHandler("test-cfg", EXPAND_CFG_HANDLER_PRIO_START, ProcessTestExpandConfig);
864 APPSPAWN_CHECK_ONLY_EXPER(ret == APPSPAWN_NODE_EXIST, break);
865
866 // create msg
867 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_SPAWN_NATIVE_PROCESS, 0);
868 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
869 // add expand info to msg
870 const char *testInfo = "\"app-base\":[{ \
871 \"sandbox-root\" : \"/mnt/sandbox/<currentUserId>/<PackageName>\", \
872 \"mount-paths\" : [{ \
873 \"src-path\" : \"/config\", \
874 \"sandbox-path\" : \"/config\", \
875 \"sandbox-flags\" : [ \"bind\", \"rec\" ], \
876 \"check-action-status\": \"false\", \
877 \"dest-mode\": \"S_IRUSR | S_IWOTH | S_IRWXU \", \
878 \"sandbox-flags-customized\": [ \"MS_NODEV\", \"MS_RDONLY\" ], \
879 \"dac-override-sensitive\": \"true\", \
880 \"mount-shared-flag\" : \"true\", \
881 \"app-apl-name\" : \"system\", \
882 \"fs-type\": \"sharefs\", \
883 \"options\": \"support_overwrite=1\" \
884 }], \
885 \"symbol-links\" : [] \
886 }]";
887
888 ret = AppSpawnReqMsgAddExtInfo(reqHandle, "test-cfg",
889 reinterpret_cast<uint8_t *>(const_cast<char *>(testInfo)), strlen(testInfo) + 1);
890 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", testInfo);
891
892 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
893 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
894 ret = MountSandboxConfigs(sandbox, property, 0);
895 } while (0);
896 if (sandbox != nullptr) {
897 sandbox->extData.freeNode(&sandbox->extData);
898 }
899 DeleteAppSpawningCtx(property);
900 AppSpawnClientDestroy(clientHandle);
901 ASSERT_EQ(ret, 0);
902 }
903
904 /**
905 * @brief 加载系统的sandbox文件
906 *
907 */
908 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_001, TestSize.Level0)
909 {
910 AppSpawnSandboxCfg *sandbox = nullptr;
911 int ret = -1;
912 do {
913 sandbox = CreateAppSpawnSandbox();
914 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
915 LoadAppSandboxConfig(sandbox, 0);
916 sandbox->extData.dumpNode(&sandbox->extData);
917 ret = 0;
918 } while (0);
919 if (sandbox != nullptr) {
920 sandbox->extData.freeNode(&sandbox->extData);
921 }
922 ASSERT_EQ(ret, 0);
923 }
924
925 /**
926 * @brief 加载基础的sandbox配置,并检查结果
927 *
928 */
929 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_002, TestSize.Level0)
930 {
931 AppSpawnSandboxCfg *sandbox = nullptr;
932 int ret = -1;
933 do {
934 sandbox = CreateAppSpawnSandbox();
935 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
936 TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
937 APPSPAWN_LOGV("sandbox->rootPath: %{public}s", sandbox->rootPath);
938
939 ASSERT_EQ(sandbox->topSandboxSwitch == 1, 1);
940 ASSERT_EQ((sandbox->sandboxNsFlags & (CLONE_NEWPID | CLONE_NEWNET)) == (CLONE_NEWPID | CLONE_NEWNET), 1);
941 ASSERT_EQ(strcmp(sandbox->rootPath, "/mnt/sandbox/<currentUserId>/app-root"), 0);
942
943 SandboxSection *section = GetSandboxSection(&sandbox->requiredQueue, "system-const");
944 ASSERT_EQ(section != nullptr, 1);
945 // check mount path
946 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(GetFirstSandboxMountPathNode(section));
947 ASSERT_EQ(pathNode != nullptr, 1);
948 ASSERT_EQ(pathNode->checkErrorFlag == 0, 1);
949 ASSERT_EQ((pathNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
950 ASSERT_EQ(pathNode->category, MOUNT_TMP_SHRED);
951 pathNode = reinterpret_cast<PathMountNode *>(GetNextSandboxMountPathNode(section, &pathNode->sandboxNode));
952 ASSERT_EQ(pathNode != nullptr, 1);
953 ASSERT_EQ(pathNode->category, MOUNT_TMP_RDONLY);
954 pathNode = reinterpret_cast<PathMountNode *>(GetNextSandboxMountPathNode(section, &pathNode->sandboxNode));
955 ASSERT_EQ(pathNode != nullptr, 1);
956 ASSERT_EQ(pathNode->category, MOUNT_TMP_EPFS);
957 pathNode = reinterpret_cast<PathMountNode *>(GetNextSandboxMountPathNode(section, &pathNode->sandboxNode));
958 ASSERT_EQ(pathNode != nullptr, 1);
959 ASSERT_EQ(pathNode->category, MOUNT_TMP_DAC_OVERRIDE);
960 pathNode = reinterpret_cast<PathMountNode *>(GetNextSandboxMountPathNode(section, &pathNode->sandboxNode));
961 ASSERT_EQ(pathNode != nullptr, 1);
962 ASSERT_EQ(pathNode->category, MOUNT_TMP_FUSE);
963 ret = 0;
964 } while (0);
965 if (sandbox) {
966 DeleteAppSpawnSandbox(sandbox);
967 }
968 ASSERT_EQ(ret, 0);
969 }
970
971 /**
972 * @brief 加载包名sandbox配置,并检查结果
973 *
974 */
975 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_003, TestSize.Level0)
976 {
977 AppSpawnSandboxCfg *sandbox = nullptr;
978 int ret = -1;
979 do {
980 sandbox = CreateAppSpawnSandbox();
981 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
982
983 TestParseAppSandboxConfig(sandbox, g_packageNameConfig.c_str());
984 APPSPAWN_LOGV("sandbox->rootPath: %{public}s", sandbox->rootPath);
985
986 // top check
987 ASSERT_EQ(sandbox->topSandboxSwitch == 0, 1); // not set
988 ASSERT_EQ((sandbox->sandboxNsFlags & (CLONE_NEWPID | CLONE_NEWNET)) == (CLONE_NEWPID | CLONE_NEWNET), 1);
989
990 // check private section
991 SandboxPackageNameNode *sandboxNode = reinterpret_cast<SandboxPackageNameNode *>(
992 GetSandboxSection(&sandbox->packageNameQueue, "test.example.ohos.com"));
993 ASSERT_EQ(sandboxNode != nullptr, 1);
994 ASSERT_EQ(strcmp(sandboxNode->section.name, "test.example.ohos.com"), 0);
995 ASSERT_EQ((sandboxNode->section.sandboxShared == 1) && (sandboxNode->section.sandboxSwitch == 1), 1);
996
997 // check path node
998 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(
999 GetFirstSandboxMountPathNode(&sandboxNode->section));
1000 ASSERT_EQ(pathNode != nullptr, 1);
1001 ASSERT_EQ(pathNode->checkErrorFlag == 0, 1);
1002 ASSERT_EQ((pathNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1003
1004 ASSERT_EQ((pathNode->appAplName != nullptr) && (strcmp(pathNode->appAplName, "system") == 0), 1);
1005 // check symlink
1006 SymbolLinkNode *linkNode = reinterpret_cast<SymbolLinkNode *>(
1007 GetNextSandboxMountPathNode(&sandboxNode->section, &pathNode->sandboxNode));
1008 ASSERT_EQ(linkNode != nullptr, 1);
1009 ASSERT_EQ(linkNode->checkErrorFlag == 0, 1);
1010 ASSERT_EQ((linkNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1011 ret = 0;
1012 } while (0);
1013 if (sandbox) {
1014 DeleteAppSpawnSandbox(sandbox);
1015 }
1016 ASSERT_EQ(ret, 0);
1017 }
1018
1019 /**
1020 * @brief 加载一个permission sandbox 配置。并检查配置解析是否正确
1021 *
1022 */
1023 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_004, TestSize.Level0)
1024 {
1025 AppSpawnSandboxCfg *sandbox = nullptr;
1026 int ret = -1;
1027 do {
1028 sandbox = CreateAppSpawnSandbox();
1029 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1030
1031 TestParseAppSandboxConfig(sandbox, g_permissionConfig.c_str());
1032 APPSPAWN_LOGV("sandbox->rootPath: %{public}s", sandbox->rootPath);
1033 // top check
1034 ASSERT_EQ(sandbox->topSandboxSwitch == 1, 1); // not set, default value 1
1035
1036 // check permission section
1037 SandboxPermissionNode *permissionNode = reinterpret_cast<SandboxPermissionNode *>(
1038 GetSandboxSection(&sandbox->permissionQueue, "ohos.permission.FILE_ACCESS_MANAGER"));
1039 ASSERT_EQ(permissionNode != nullptr, 1);
1040 ASSERT_EQ(permissionNode->section.gidTable != nullptr, 1);
1041 ASSERT_EQ(permissionNode->section.gidCount, 2);
1042 ASSERT_EQ(permissionNode->section.gidTable[0], 1006);
1043 ASSERT_EQ(permissionNode->section.gidTable[1], 1008);
1044 ASSERT_EQ(strcmp(permissionNode->section.name, "ohos.permission.FILE_ACCESS_MANAGER"), 0);
1045
1046 // check path node
1047 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(
1048 GetFirstSandboxMountPathNode(&permissionNode->section));
1049 ASSERT_EQ(pathNode != nullptr, 1);
1050 ASSERT_EQ(pathNode->checkErrorFlag == 1, 1);
1051 ASSERT_EQ((pathNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1052 ASSERT_EQ((pathNode->appAplName != nullptr) && (strcmp(pathNode->appAplName, "system") == 0), 1);
1053
1054 // check symlink
1055 SymbolLinkNode *linkNode = reinterpret_cast<SymbolLinkNode *>(
1056 GetNextSandboxMountPathNode(&permissionNode->section, &pathNode->sandboxNode));
1057 ASSERT_EQ(linkNode != nullptr, 1);
1058 ASSERT_EQ(linkNode->checkErrorFlag == 0, 1);
1059 ASSERT_EQ((linkNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1060 ret = 0;
1061 } while (0);
1062 if (sandbox) {
1063 DeleteAppSpawnSandbox(sandbox);
1064 }
1065 ASSERT_EQ(ret, 0);
1066 }
1067
1068 /**
1069 * @brief 加载一个spawn-flags sandbox 配置。并检查配置解析是否正确
1070 *
1071 */
1072 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_005, TestSize.Level0)
1073 {
1074 AppSpawnSandboxCfg *sandbox = nullptr;
1075 int ret = -1;
1076 do {
1077 sandbox = CreateAppSpawnSandbox();
1078 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1079 TestParseAppSandboxConfig(sandbox, g_spawnFlagsConfig.c_str());
1080
1081 // top check
1082 ASSERT_EQ(sandbox->topSandboxSwitch == 0, 1); // not set
1083 ASSERT_EQ(sandbox->sandboxNsFlags == (CLONE_NEWPID | CLONE_NEWNET), 1);
1084
1085 // check private section
1086 SandboxFlagsNode *sandboxNode = reinterpret_cast<SandboxFlagsNode *>(
1087 GetSandboxSection(&sandbox->spawnFlagsQueue, "START_FLAGS_BACKUP"));
1088 ASSERT_EQ(sandboxNode != nullptr, 1);
1089 ASSERT_EQ(strcmp(sandboxNode->section.name, "START_FLAGS_BACKUP"), 0);
1090 // no set, check default
1091 ASSERT_EQ((sandboxNode->section.sandboxShared == 0) && (sandboxNode->section.sandboxSwitch == 1), 1);
1092 ASSERT_EQ(sandboxNode->flagIndex == APP_FLAGS_BACKUP_EXTENSION, 1);
1093
1094 // check path node
1095 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(
1096 GetFirstSandboxMountPathNode(&sandboxNode->section));
1097 ASSERT_EQ(pathNode != nullptr, 1);
1098 ASSERT_EQ(pathNode->checkErrorFlag == 1, 1); // set
1099 ASSERT_EQ((pathNode->destMode & (S_IRUSR | S_IWOTH | S_IRWXU)) == (S_IRUSR | S_IWOTH | S_IRWXU), 1);
1100 ASSERT_EQ((pathNode->appAplName != nullptr) && (strcmp(pathNode->appAplName, "system") == 0), 1);
1101 ret = 0;
1102 } while (0);
1103 if (sandbox) {
1104 DeleteAppSpawnSandbox(sandbox);
1105 }
1106 ASSERT_EQ(ret, 0);
1107 }
1108
1109 /**
1110 * @brief 加载一个name-group sandbox 配置。并检查配置解析是否正确
1111 *
1112 */
1113 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_cfg_006, TestSize.Level0)
1114 {
1115 AppSpawnSandboxCfg *sandbox = nullptr;
1116 int ret = -1;
1117 do {
1118 sandbox = CreateAppSpawnSandbox();
1119 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1120 TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1121
1122 // check private section
1123 SandboxNameGroupNode *sandboxNode = reinterpret_cast<SandboxNameGroupNode *>(
1124 GetSandboxSection(&sandbox->nameGroupsQueue, "el5"));
1125 ASSERT_EQ(sandboxNode != nullptr, 1);
1126 ASSERT_EQ(strcmp(sandboxNode->section.name, "el5"), 0);
1127 // no set, check default
1128 ASSERT_EQ((sandboxNode->section.sandboxShared == 0) && (sandboxNode->section.sandboxSwitch == 1), 1);
1129 ASSERT_EQ(sandboxNode->depMode == MOUNT_MODE_NOT_EXIST, 1);
1130 ASSERT_EQ(sandboxNode->destType == SANDBOX_TAG_APP_VARIABLE, 1);
1131 ASSERT_EQ(sandboxNode->depNode != nullptr, 1);
1132 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(sandboxNode->depNode);
1133 ASSERT_EQ(pathNode->category, MOUNT_TMP_SHRED);
1134 ASSERT_EQ(strcmp(pathNode->target, "/data/storage/el5") == 0, 1);
1135
1136 // check path node
1137 pathNode = reinterpret_cast<PathMountNode *>(GetFirstSandboxMountPathNode(&sandboxNode->section));
1138 ASSERT_EQ(pathNode != nullptr, 1);
1139 ASSERT_EQ(strcmp(pathNode->source, "/data/app/el5/<currentUserId>/base/<PackageName>") == 0, 1);
1140 ASSERT_EQ(strcmp(pathNode->target, "<deps-path>/base") == 0, 1);
1141 ret = 0;
1142 } while (0);
1143 if (sandbox) {
1144 DeleteAppSpawnSandbox(sandbox);
1145 }
1146 ASSERT_EQ(ret, 0);
1147 }
1148 /**
1149 * @brief 沙盒执行,能执行到对应的检查项,并且检查通过
1150 *
1151 */
1152 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_001, TestSize.Level0)
1153 {
1154 AppSpawnSandboxCfg *sandbox = nullptr;
1155 AppSpawnClientHandle clientHandle = nullptr;
1156 AppSpawnReqMsgHandle reqHandle = 0;
1157 AppSpawningCtx *property = nullptr;
1158 StubNode *stub = GetStubNode(STUB_MOUNT);
1159 ASSERT_NE(stub != nullptr, 0);
1160 int ret = -1;
1161 do {
1162 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1163 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1164 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1165 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1166
1167 ret = APPSPAWN_ARG_INVALID;
1168 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1169 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1170
1171 sandbox = CreateAppSpawnSandbox();
1172 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1173 sandbox->appFullMountEnable = 1;
1174 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1175 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1176
1177 // set check point
1178 MountArg args = {};
1179 args.originPath = "/storage/Users/100/appdata/el1";
1180 args.destinationPath = "/mnt/sandbox/100/app-root/storage/Users/100/appdata/el1";
1181 args.fsType = "sharefs";
1182 args.options = "support_overwrite=1";
1183 args.mountFlags = MS_NODEV | MS_RDONLY;
1184 args.mountSharedFlag = MS_SLAVE;
1185 stub->flags = STUB_NEED_CHECK;
1186 stub->arg = reinterpret_cast<void *>(&args);
1187
1188 ret = StagedMountSystemConst(sandbox, property, 0);
1189 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1190 ASSERT_EQ(stub->result, 0);
1191 } while (0);
1192 if (sandbox) {
1193 DeleteAppSpawnSandbox(sandbox);
1194 }
1195 stub->flags &= ~STUB_NEED_CHECK;
1196 DeleteAppSpawningCtx(property);
1197 AppSpawnClientDestroy(clientHandle);
1198 ASSERT_EQ(ret, 0);
1199 }
1200
1201 /**
1202 * @brief app-variable部分执行。让mount执行失败,但是不需要返回错误结果
1203 *
1204 */
1205 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_002, TestSize.Level0)
1206 {
1207 AppSpawnSandboxCfg *sandbox = nullptr;
1208 AppSpawnClientHandle clientHandle = nullptr;
1209 AppSpawnReqMsgHandle reqHandle = 0;
1210 AppSpawningCtx *property = nullptr;
1211 StubNode *stub = GetStubNode(STUB_MOUNT);
1212 ASSERT_NE(stub != nullptr, 0);
1213 int ret = -1;
1214 do {
1215 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1216 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1217 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1218 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1219
1220 ret = APPSPAWN_ARG_INVALID;
1221 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1222 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1223
1224 sandbox = CreateAppSpawnSandbox();
1225 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1226 sandbox->appFullMountEnable = 1;
1227 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1228 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1229
1230 // set check point
1231 MountArg args = {};
1232 args.originPath = "/config";
1233 args.destinationPath = "/mnt/sandbox/100/com.example.myapplication/config";
1234 args.fsType = "sharefs";
1235 args.mountFlags = MS_NODEV | MS_RDONLY; // 当前条件走customizedFlags,这里设置为customizedFlags
1236 stub->flags = STUB_NEED_CHECK;
1237 stub->arg = reinterpret_cast<void *>(&args);
1238 // 执行失败, 但是不返回
1239 args.mountFlags = MS_NODEV;
1240 ret = MountSandboxConfigs(sandbox, property, 0);
1241 ASSERT_EQ(ret, 0);
1242 ASSERT_NE(stub->result, 0);
1243 } while (0);
1244 if (sandbox) {
1245 DeleteAppSpawnSandbox(sandbox);
1246 }
1247 stub->flags &= ~STUB_NEED_CHECK;
1248 DeleteAppSpawningCtx(property);
1249 AppSpawnClientDestroy(clientHandle);
1250 ASSERT_EQ(ret, 0);
1251 }
1252
1253 /**
1254 * @brief app-variable部分执行。让mount执行失败,失败返回错误结果
1255 *
1256 */
1257 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_003, TestSize.Level0)
1258 {
1259 AppSpawnSandboxCfg *sandbox = nullptr;
1260 AppSpawnClientHandle clientHandle = nullptr;
1261 AppSpawnReqMsgHandle reqHandle = 0;
1262 AppSpawningCtx *property = nullptr;
1263 StubNode *stub = GetStubNode(STUB_MOUNT);
1264 ASSERT_NE(stub != nullptr, 0);
1265 int ret = -1;
1266 do {
1267 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1268 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1269 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1270 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1271
1272 ret = APPSPAWN_ARG_INVALID;
1273 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1274 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1275
1276 sandbox = CreateAppSpawnSandbox();
1277 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1278 sandbox->appFullMountEnable = 1;
1279
1280 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1281 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1282 SandboxSection *section = GetSandboxSection(&sandbox->requiredQueue, "app-variable");
1283 ASSERT_EQ(section != nullptr, 1);
1284
1285 PathMountNode *pathNode = reinterpret_cast<PathMountNode *>(GetFirstSandboxMountPathNode(section));
1286 pathNode->checkErrorFlag = 1; // 设置错误检查
1287 APPSPAWN_LOGV("pathNode %{public}s => %{public}s \n", pathNode->source, pathNode->target);
1288 // set check point
1289 MountArg args = {};
1290 args.originPath = "/config";
1291 args.destinationPath = "/mnt/sandbox/100/com.example.myapplication/config";
1292 args.fsType = "sharefs";
1293 args.mountFlags = MS_NODEV | MS_RDONLY; // 当前条件走customizedFlags,这里设置为customizedFlags
1294 stub->flags = STUB_NEED_CHECK;
1295 stub->arg = reinterpret_cast<void *>(&args);
1296
1297 // 执行失败, 返回错误
1298 args.mountFlags = MS_NODEV;
1299 ret = MountSandboxConfigs(sandbox, property, 0);
1300 ASSERT_NE(ret, 0);
1301 ASSERT_NE(stub->result, 0);
1302 ret = 0;
1303 } while (0);
1304 ASSERT_EQ(ret, 0);
1305 if (sandbox) {
1306 DeleteAppSpawnSandbox(sandbox);
1307 }
1308 stub->flags &= ~STUB_NEED_CHECK;
1309 DeleteAppSpawningCtx(property);
1310 AppSpawnClientDestroy(clientHandle);
1311 }
1312
1313 /**
1314 * @brief package name 执行
1315 *
1316 */
1317 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_004, TestSize.Level0)
1318 {
1319 AppSpawnSandboxCfg *sandbox = nullptr;
1320 AppSpawnClientHandle clientHandle = nullptr;
1321 AppSpawnReqMsgHandle reqHandle = 0;
1322 AppSpawningCtx *property = nullptr;
1323 StubNode *stub = GetStubNode(STUB_MOUNT);
1324 ASSERT_NE(stub != nullptr, 0);
1325 int ret = -1;
1326 do {
1327 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1328 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1329 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1330 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1331
1332 ret = APPSPAWN_ARG_INVALID;
1333 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1334 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1335
1336 sandbox = CreateAppSpawnSandbox();
1337 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1338 sandbox->appFullMountEnable = 1;
1339 ret = TestParseAppSandboxConfig(sandbox, g_packageNameConfig.c_str());
1340 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1341
1342 // set check point
1343 MountArg args = {};
1344 args.originPath = "/dev/fuse";
1345 args.destinationPath = "/mnt/sandbox/100/app-root/mnt/data/fuse";
1346 args.fsType = "fuse";
1347 args.mountFlags = MS_LAZYTIME | MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID;
1348 stub->flags = STUB_NEED_CHECK;
1349 stub->arg = reinterpret_cast<void *>(&args);
1350
1351 ret = MountSandboxConfigs(sandbox, property, 0);
1352 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1353 ASSERT_EQ(stub->result, 0);
1354 } while (0);
1355 if (sandbox) {
1356 DeleteAppSpawnSandbox(sandbox);
1357 }
1358 stub->flags &= ~STUB_NEED_CHECK;
1359 DeleteAppSpawningCtx(property);
1360 AppSpawnClientDestroy(clientHandle);
1361 ASSERT_EQ(ret, 0);
1362 }
1363
1364 /**
1365 * @brief 测试package-name执行,执行失败
1366 *
1367 */
1368 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_005, TestSize.Level0)
1369 {
1370 AppSpawnSandboxCfg *sandbox = nullptr;
1371 AppSpawnClientHandle clientHandle = nullptr;
1372 AppSpawnReqMsgHandle reqHandle = 0;
1373 AppSpawningCtx *property = nullptr;
1374 StubNode *stub = GetStubNode(STUB_MOUNT);
1375 ASSERT_NE(stub != nullptr, 0);
1376 int ret = -1;
1377 do {
1378 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1379 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1380 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1381 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1382
1383 ret = APPSPAWN_ARG_INVALID;
1384 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1385 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1386
1387 sandbox = CreateAppSpawnSandbox();
1388 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1389 sandbox->appFullMountEnable = 1;
1390
1391 ret = TestParseAppSandboxConfig(sandbox, g_packageNameConfig.c_str());
1392 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1393
1394 // set check point
1395 MountArg args = {};
1396 args.originPath = "/dev/fuse";
1397 args.destinationPath = "/home/axw/appspawn_ut/mnt/sandbox/100/com.example.myapplication/mnt/data/fuse";
1398 args.fsType = "fuse";
1399 args.mountFlags = MS_LAZYTIME | MS_NOATIME | MS_NODEV | MS_NOEXEC;
1400 stub->flags = STUB_NEED_CHECK;
1401 stub->arg = reinterpret_cast<void *>(&args);
1402 ret = MountSandboxConfigs(sandbox, property, 0);
1403 ASSERT_NE(ret, 0);
1404 ASSERT_NE(stub->result, 0);
1405 ret = 0;
1406 } while (0);
1407 if (sandbox) {
1408 DeleteAppSpawnSandbox(sandbox);
1409 }
1410 stub->flags &= ~STUB_NEED_CHECK;
1411 DeleteAppSpawningCtx(property);
1412 AppSpawnClientDestroy(clientHandle);
1413 ASSERT_EQ(ret, 0);
1414 }
1415
1416 /**
1417 * @brief 测试permission 添加下appFullMountEnable 打开
1418 *
1419 */
1420 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_006, TestSize.Level0)
1421 {
1422 AppSpawnSandboxCfg *sandbox = nullptr;
1423 AppSpawnClientHandle clientHandle = nullptr;
1424 AppSpawnReqMsgHandle reqHandle = 0;
1425 AppSpawningCtx *property = nullptr;
1426 StubNode *stub = GetStubNode(STUB_MOUNT);
1427 ASSERT_NE(stub != nullptr, 0);
1428 int ret = -1;
1429 do {
1430 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1431 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1432 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1433 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1434
1435 ret = APPSPAWN_ARG_INVALID;
1436 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1437 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1438
1439 sandbox = CreateAppSpawnSandbox();
1440 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1441 sandbox->appFullMountEnable = 1;
1442
1443 ret = TestParseAppSandboxConfig(sandbox, g_permissionConfig.c_str());
1444 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1445 // set permission flags
1446 int index = GetPermissionIndexInQueue(&sandbox->permissionQueue, "ohos.permission.FILE_ACCESS_MANAGER");
1447 SetAppPermissionFlags(property, index);
1448
1449 // set check point
1450 MountArg args = {};
1451 args.originPath = "/config--1";
1452 args.destinationPath = "/mnt/sandbox/100/app-root/data/app/el1/currentUser/"
1453 "database/com.example.myapplication_100";
1454 // permission 下,fstype使用default
1455 // "sharefs"
1456 args.mountFlags = MS_BIND | MS_REC;
1457 stub->flags = STUB_NEED_CHECK;
1458 stub->arg = reinterpret_cast<void *>(&args);
1459 stub->result = 0;
1460 ret = MountSandboxConfigs(sandbox, property, 0);
1461 ASSERT_EQ(ret, 0); // do not check result
1462 ASSERT_EQ(stub->result, 0);
1463 ret = 0;
1464 } while (0);
1465 if (sandbox) {
1466 DeleteAppSpawnSandbox(sandbox);
1467 }
1468 stub->flags &= ~STUB_NEED_CHECK;
1469 DeleteAppSpawningCtx(property);
1470 AppSpawnClientDestroy(clientHandle);
1471 ASSERT_EQ(ret, 0);
1472 }
1473
1474 /**
1475 * @brief 测试permission 添加下appFullMountEnable 打开,执行失败
1476 *
1477 */
1478 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_007, TestSize.Level0)
1479 {
1480 AppSpawnSandboxCfg *sandbox = nullptr;
1481 AppSpawnClientHandle clientHandle = nullptr;
1482 AppSpawnReqMsgHandle reqHandle = 0;
1483 AppSpawningCtx *property = nullptr;
1484 StubNode *stub = GetStubNode(STUB_MOUNT);
1485 ASSERT_NE(stub != nullptr, 0);
1486 int ret = -1;
1487 do {
1488 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1489 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1490 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1491 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1492
1493 ret = APPSPAWN_ARG_INVALID;
1494 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1495 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1496
1497 sandbox = CreateAppSpawnSandbox();
1498 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1499 sandbox->appFullMountEnable = 1;
1500
1501 ret = TestParseAppSandboxConfig(sandbox, g_permissionConfig.c_str());
1502 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1503 // set permission flags
1504 int index = GetPermissionIndexInQueue(&sandbox->permissionQueue, "ohos.permission.FILE_ACCESS_MANAGER");
1505 SetAppPermissionFlags(property, index);
1506
1507 // set check point
1508 MountArg args = {};
1509 args.originPath = "/config--1";
1510 args.destinationPath = "/mnt/sandbox/100/app-root/data/app/el1/currentUser/"
1511 "database/com.example.myapplication_100";
1512 args.fsType = "sharefs";
1513 args.mountFlags = MS_RDONLY;
1514 stub->flags = STUB_NEED_CHECK;
1515 stub->arg = reinterpret_cast<void *>(&args);
1516 ret = MountSandboxConfigs(sandbox, property, 0);
1517 ASSERT_NE(ret, 0);
1518 ASSERT_NE(stub->result, 0);
1519 ret = 0;
1520 } while (0);
1521 if (sandbox) {
1522 DeleteAppSpawnSandbox(sandbox);
1523 }
1524 stub->flags &= ~STUB_NEED_CHECK;
1525 DeleteAppSpawningCtx(property);
1526 AppSpawnClientDestroy(clientHandle);
1527 ASSERT_EQ(ret, 0);
1528 }
1529
1530 /**
1531 * @brief system-config部分执行,测试每一种模版结果是否正确
1532 * 测试 shared
1533 *
1534 */
1535 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_001, TestSize.Level0)
1536 {
1537 AppSpawnSandboxCfg *sandbox = nullptr;
1538 AppSpawnClientHandle clientHandle = nullptr;
1539 AppSpawnReqMsgHandle reqHandle = 0;
1540 AppSpawningCtx *property = nullptr;
1541 StubNode *stub = GetStubNode(STUB_MOUNT);
1542 ASSERT_NE(stub != nullptr, 0);
1543 int ret = -1;
1544 do {
1545 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1546 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1547 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1548 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1549
1550 ret = APPSPAWN_ARG_INVALID;
1551 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1552 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1553
1554 sandbox = CreateAppSpawnSandbox();
1555 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1556 sandbox->appFullMountEnable = 1;
1557 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1558 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1559
1560 // set check point
1561 MountArg args = {};
1562 args.originPath = "/lib";
1563 args.destinationPath = "/mnt/sandbox/100/app-root/lib";
1564 args.fsType = nullptr;
1565 args.mountFlags = MS_BIND | MS_REC;
1566 args.mountSharedFlag = MS_SHARED;
1567 stub->flags = STUB_NEED_CHECK;
1568 stub->arg = reinterpret_cast<void *>(&args);
1569
1570 ret = StagedMountSystemConst(sandbox, property, 0);
1571 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1572 ASSERT_EQ(stub->result, 0);
1573 } while (0);
1574 if (sandbox) {
1575 DeleteAppSpawnSandbox(sandbox);
1576 }
1577 stub->flags &= ~STUB_NEED_CHECK;
1578 DeleteAppSpawningCtx(property);
1579 AppSpawnClientDestroy(clientHandle);
1580 ASSERT_EQ(ret, 0);
1581 }
1582
1583 /**
1584 * @brief system-config部分执行,测试每一种模版结果是否正确
1585 * 测试 rdonly
1586 *
1587 */
1588 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_002, TestSize.Level0)
1589 {
1590 AppSpawnSandboxCfg *sandbox = nullptr;
1591 AppSpawnClientHandle clientHandle = nullptr;
1592 AppSpawnReqMsgHandle reqHandle = 0;
1593 AppSpawningCtx *property = nullptr;
1594 StubNode *stub = GetStubNode(STUB_MOUNT);
1595 ASSERT_NE(stub != nullptr, 0);
1596 int ret = -1;
1597 do {
1598 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1599 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1600 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1601 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1602
1603 ret = APPSPAWN_ARG_INVALID;
1604 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1605 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1606
1607 sandbox = CreateAppSpawnSandbox();
1608 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1609 sandbox->appFullMountEnable = 1;
1610 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1611 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1612
1613 // set check point
1614 MountArg args = {};
1615 args.originPath = "/lib1";
1616 args.destinationPath = "/mnt/sandbox/100/app-root/lib1";
1617 args.fsType = nullptr;
1618 args.mountFlags = MS_NODEV | MS_RDONLY;
1619 args.mountSharedFlag = MS_SLAVE;
1620 stub->flags = STUB_NEED_CHECK;
1621 stub->arg = reinterpret_cast<void *>(&args);
1622
1623 ret = StagedMountSystemConst(sandbox, property, 0);
1624 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1625 ASSERT_EQ(stub->result, 0);
1626 } while (0);
1627 if (sandbox) {
1628 DeleteAppSpawnSandbox(sandbox);
1629 }
1630 stub->flags &= ~STUB_NEED_CHECK;
1631 DeleteAppSpawningCtx(property);
1632 AppSpawnClientDestroy(clientHandle);
1633 ASSERT_EQ(ret, 0);
1634 }
1635
1636 /**
1637 * @brief system-config部分执行,测试每一种模版结果是否正确
1638 * 测试 epfs
1639 *
1640 */
1641 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_003, TestSize.Level0)
1642 {
1643 AppSpawnSandboxCfg *sandbox = nullptr;
1644 AppSpawnClientHandle clientHandle = nullptr;
1645 AppSpawnReqMsgHandle reqHandle = 0;
1646 AppSpawningCtx *property = nullptr;
1647 StubNode *stub = GetStubNode(STUB_MOUNT);
1648 ASSERT_NE(stub != nullptr, 0);
1649 int ret = -1;
1650 do {
1651 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1652 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1653 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1654 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1655
1656 ret = APPSPAWN_ARG_INVALID;
1657 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1658 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1659
1660 sandbox = CreateAppSpawnSandbox();
1661 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1662 sandbox->appFullMountEnable = 1;
1663 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1664 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1665
1666 // set check point
1667 MountArg args = {};
1668 args.originPath = "none";
1669 args.destinationPath = "/mnt/sandbox/100/app-root/storage/cloud/epfs";
1670 args.fsType = "epfs";
1671 args.mountFlags = MS_NODEV;
1672 args.mountSharedFlag = MS_SLAVE;
1673 stub->flags = STUB_NEED_CHECK;
1674 stub->arg = reinterpret_cast<void *>(&args);
1675
1676 ret = StagedMountSystemConst(sandbox, property, 0);
1677 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1678 ASSERT_EQ(stub->result, 0);
1679 } while (0);
1680 if (sandbox) {
1681 DeleteAppSpawnSandbox(sandbox);
1682 }
1683 stub->flags &= ~STUB_NEED_CHECK;
1684 DeleteAppSpawningCtx(property);
1685 AppSpawnClientDestroy(clientHandle);
1686 ASSERT_EQ(ret, 0);
1687 }
1688
1689 /**
1690 * @brief system-config部分执行,测试每一种模版结果是否正确
1691 * 测试 fuse
1692 *
1693 */
1694 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Category_004, TestSize.Level0)
1695 {
1696 AppSpawnSandboxCfg *sandbox = nullptr;
1697 AppSpawnClientHandle clientHandle = nullptr;
1698 AppSpawnReqMsgHandle reqHandle = 0;
1699 AppSpawningCtx *property = nullptr;
1700 StubNode *stub = GetStubNode(STUB_MOUNT);
1701 ASSERT_NE(stub != nullptr, 0);
1702 int ret = -1;
1703 do {
1704 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1705 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1706 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1707 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1708
1709 ret = APPSPAWN_ARG_INVALID;
1710 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1711 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1712
1713 sandbox = CreateAppSpawnSandbox();
1714 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1715 sandbox->appFullMountEnable = 1;
1716 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1717 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1718
1719 // set check point
1720 MountArg args = {};
1721 args.originPath = "/dev/fuse";
1722 args.destinationPath = "/mnt/sandbox/100/app-root/mnt/data/fuse";
1723 args.fsType = "fuse";
1724 args.mountFlags = MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_LAZYTIME;
1725 args.mountSharedFlag = MS_SLAVE;
1726 stub->flags = STUB_NEED_CHECK;
1727 stub->arg = reinterpret_cast<void *>(&args);
1728
1729 ret = StagedMountSystemConst(sandbox, property, 0);
1730 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1731 ASSERT_EQ(stub->result, 0);
1732 } while (0);
1733 if (sandbox) {
1734 DeleteAppSpawnSandbox(sandbox);
1735 }
1736 stub->flags &= ~STUB_NEED_CHECK;
1737 DeleteAppSpawningCtx(property);
1738 AppSpawnClientDestroy(clientHandle);
1739 ASSERT_EQ(ret, 0);
1740 }
1741
1742 /**
1743 * @brief 测试unshare前的执行,not-exist时,节点不存在,执行dep的挂载
1744 *
1745 */
1746 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_001, TestSize.Level0)
1747 {
1748 AppSpawnSandboxCfg *sandbox = nullptr;
1749 AppSpawnClientHandle clientHandle = nullptr;
1750 AppSpawnReqMsgHandle reqHandle = 0;
1751 AppSpawningCtx *property = nullptr;
1752 StubNode *stub = GetStubNode(STUB_MOUNT);
1753 ASSERT_NE(stub != nullptr, 0);
1754 int ret = -1;
1755 do {
1756 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1757 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1758 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1759 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1760
1761 ret = APPSPAWN_ARG_INVALID;
1762 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1763 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1764
1765 sandbox = CreateAppSpawnSandbox();
1766 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1767 sandbox->appFullMountEnable = 1;
1768 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1769 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1770
1771 // set check point
1772 MountArg args = {};
1773 args.originPath = "/data/app/el5/100/base";
1774 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/el5";
1775 args.mountFlags = MS_BIND | MS_REC;
1776 args.mountSharedFlag = MS_SHARED;
1777 stub->flags = STUB_NEED_CHECK;
1778 stub->arg = reinterpret_cast<void *>(&args);
1779 stub->result = -1;
1780
1781 ret = MountSandboxConfigs(sandbox, property, 0);
1782 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1783 ASSERT_EQ(stub->result, 0);
1784 } while (0);
1785 if (sandbox) {
1786 DeleteAppSpawnSandbox(sandbox);
1787 }
1788 stub->flags &= ~STUB_NEED_CHECK;
1789 DeleteAppSpawningCtx(property);
1790 AppSpawnClientDestroy(clientHandle);
1791 ASSERT_EQ(ret, 0);
1792 }
1793
1794 /**
1795 * @brief 测试unshare前的执行,not-exist时,节点存在,不执行dep的挂载
1796 *
1797 */
1798 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_002, TestSize.Level0)
1799 {
1800 AppSpawnSandboxCfg *sandbox = nullptr;
1801 AppSpawnClientHandle clientHandle = nullptr;
1802 AppSpawnReqMsgHandle reqHandle = 0;
1803 AppSpawningCtx *property = nullptr;
1804 StubNode *stub = GetStubNode(STUB_MOUNT);
1805 ASSERT_NE(stub != nullptr, 0);
1806 int ret = -1;
1807 do {
1808 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1809 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1810 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1811 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1812
1813 ret = APPSPAWN_ARG_INVALID;
1814 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1815 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1816
1817 sandbox = CreateAppSpawnSandbox();
1818 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1819 sandbox->appFullMountEnable = 1;
1820 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1821 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1822
1823 // set check point
1824 MountArg args = {};
1825 args.originPath = "/data/app/el6/100/base";
1826 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/el6";
1827 args.mountFlags = MS_BIND | MS_REC;
1828 args.mountSharedFlag = MS_SHARED;
1829 stub->flags = STUB_NEED_CHECK;
1830 stub->arg = reinterpret_cast<void *>(&args);
1831
1832 ret = MountSandboxConfigs(sandbox, property, 0);
1833 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1834 ASSERT_EQ(stub->result, 0);
1835 } while (0);
1836 if (sandbox) {
1837 DeleteAppSpawnSandbox(sandbox);
1838 }
1839 stub->flags &= ~STUB_NEED_CHECK;
1840 DeleteAppSpawningCtx(property);
1841 AppSpawnClientDestroy(clientHandle);
1842 ASSERT_EQ(ret, 0);
1843 }
1844
1845 /**
1846 * @brief 测试unshare前的执行,always时,执行dep的挂载
1847 *
1848 */
1849 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_003, TestSize.Level0)
1850 {
1851 AppSpawnSandboxCfg *sandbox = nullptr;
1852 AppSpawnClientHandle clientHandle = nullptr;
1853 AppSpawnReqMsgHandle reqHandle = 0;
1854 AppSpawningCtx *property = nullptr;
1855 StubNode *stub = GetStubNode(STUB_MOUNT);
1856 ASSERT_NE(stub != nullptr, 0);
1857 int ret = -1;
1858 do {
1859 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1860 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1861 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1862 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1863
1864 ret = APPSPAWN_ARG_INVALID;
1865 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1866 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1867
1868 sandbox = CreateAppSpawnSandbox();
1869 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1870 sandbox->appFullMountEnable = 1;
1871 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1872 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1873
1874 // set check point
1875 MountArg args = {};
1876 args.originPath = "/data/app/e20/100/base";
1877 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/e20";
1878 args.mountFlags = MS_BIND | MS_REC;
1879 args.mountSharedFlag = MS_SHARED;
1880 stub->flags = STUB_NEED_CHECK;
1881 stub->arg = reinterpret_cast<void *>(&args);
1882 stub->result = 0;
1883
1884 ret = MountSandboxConfigs(sandbox, property, 0);
1885 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1886 ASSERT_EQ(stub->result, 0);
1887 } while (0);
1888 if (sandbox) {
1889 DeleteAppSpawnSandbox(sandbox);
1890 }
1891 stub->flags &= ~STUB_NEED_CHECK;
1892 DeleteAppSpawningCtx(property);
1893 AppSpawnClientDestroy(clientHandle);
1894 ASSERT_EQ(ret, 0);
1895 }
1896
1897 /**
1898 * @brief 测试unshare后执行,一次挂载时,使用sandbox-path
1899 *
1900 */
1901 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_004, TestSize.Level0)
1902 {
1903 AppSpawnSandboxCfg *sandbox = nullptr;
1904 AppSpawnClientHandle clientHandle = nullptr;
1905 AppSpawnReqMsgHandle reqHandle = 0;
1906 AppSpawningCtx *property = nullptr;
1907 StubNode *stub = GetStubNode(STUB_MOUNT);
1908 ASSERT_NE(stub != nullptr, 0);
1909 int ret = -1;
1910 do {
1911 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1912 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1913 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1914 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1915
1916 ret = APPSPAWN_ARG_INVALID;
1917 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1918 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1919
1920 sandbox = CreateAppSpawnSandbox();
1921 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1922 sandbox->appFullMountEnable = 1;
1923 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1924 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1925
1926 // set check point
1927 MountArg args = {};
1928 args.originPath = "/data/app/e15/100/base/com.example.myapplication";
1929 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/e15/base";
1930 args.mountFlags = MS_BIND | MS_REC;
1931 args.mountSharedFlag = MS_SLAVE;
1932 stub->flags = STUB_NEED_CHECK;
1933 stub->arg = reinterpret_cast<void *>(&args);
1934 stub->result = 0;
1935
1936 ret = MountSandboxConfigs(sandbox, property, 0);
1937 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1938 ASSERT_EQ(stub->result, 0);
1939 } while (0);
1940 if (sandbox) {
1941 DeleteAppSpawnSandbox(sandbox);
1942 }
1943 stub->flags &= ~STUB_NEED_CHECK;
1944 DeleteAppSpawningCtx(property);
1945 AppSpawnClientDestroy(clientHandle);
1946 ASSERT_EQ(ret, 0);
1947 }
1948
1949 /**
1950 * @brief system-const,一次挂载时,使用sandbox-path
1951 *
1952 */
1953 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Deps_005, TestSize.Level0)
1954 {
1955 AppSpawnSandboxCfg *sandbox = nullptr;
1956 AppSpawnClientHandle clientHandle = nullptr;
1957 AppSpawnReqMsgHandle reqHandle = 0;
1958 AppSpawningCtx *property = nullptr;
1959 StubNode *stub = GetStubNode(STUB_MOUNT);
1960 ASSERT_NE(stub != nullptr, 0);
1961 int ret = -1;
1962 do {
1963 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
1964 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
1965 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
1966 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
1967
1968 ret = APPSPAWN_ARG_INVALID;
1969 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
1970 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
1971
1972 sandbox = CreateAppSpawnSandbox();
1973 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
1974 sandbox->appFullMountEnable = 1;
1975 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
1976 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1977
1978 // set check point
1979 MountArg args = {};
1980 args.originPath = "/data/app/e20/100/base/com.example.myapplication";
1981 args.destinationPath = "/mnt/sandbox/100/app-root/data/storage/e20/base";
1982 args.mountFlags = MS_BIND | MS_REC;
1983 args.mountSharedFlag = MS_SLAVE;
1984 stub->flags = STUB_NEED_CHECK;
1985 stub->arg = reinterpret_cast<void *>(&args);
1986 stub->result = -1;
1987
1988 ret = StagedMountSystemConst(sandbox, property, 0);
1989 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
1990 ASSERT_EQ(stub->result, 0);
1991 } while (0);
1992 if (sandbox) {
1993 DeleteAppSpawnSandbox(sandbox);
1994 }
1995 stub->flags &= ~STUB_NEED_CHECK;
1996 DeleteAppSpawningCtx(property);
1997 AppSpawnClientDestroy(clientHandle);
1998 ASSERT_EQ(ret, 0);
1999 }
2000
2001 /**
2002 * @brief 测试app extension
2003 *
2004 */
2005 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_001, TestSize.Level0)
2006 {
2007 AppSpawnSandboxCfg *sandbox = nullptr;
2008 AppSpawnClientHandle clientHandle = nullptr;
2009 AppSpawnReqMsgHandle reqHandle = 0;
2010 AppSpawningCtx *property = nullptr;
2011 int ret = -1;
2012 do {
2013 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2014 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
2015 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
2016 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
2017
2018 // set APP_FLAGS_ISOLATED_SANDBOX
2019 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX);
2020 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2021
2022 ret = APPSPAWN_ARG_INVALID;
2023 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2024 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
2025
2026 sandbox = CreateAppSpawnSandbox();
2027 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
2028 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
2029 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2030
2031 ret = MountSandboxConfigs(sandbox, property, 0);
2032 } while (0);
2033 if (sandbox) {
2034 DeleteAppSpawnSandbox(sandbox);
2035 }
2036 DeleteAppSpawningCtx(property);
2037 AppSpawnClientDestroy(clientHandle);
2038 ASSERT_EQ(ret, 0);
2039 }
2040
2041 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_002, TestSize.Level0)
2042 {
2043 AppSpawnSandboxCfg *sandbox = nullptr;
2044 AppSpawnClientHandle clientHandle = nullptr;
2045 AppSpawnReqMsgHandle reqHandle = 0;
2046 AppSpawningCtx *property = nullptr;
2047 int ret = -1;
2048 do {
2049 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2050 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
2051 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
2052 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
2053
2054 // set APP_FLAGS_ISOLATED_SANDBOX
2055 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX);
2056 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2057
2058 ret = APPSPAWN_ARG_INVALID;
2059 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2060 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
2061
2062 sandbox = CreateAppSpawnSandbox();
2063 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
2064 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
2065 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2066
2067 sandbox->sandboxNsFlags = CLONE_NEWPID; // only pid
2068 ret = MountSandboxConfigs(sandbox, property, 0);
2069 } while (0);
2070 if (sandbox) {
2071 DeleteAppSpawnSandbox(sandbox);
2072 }
2073 DeleteAppSpawningCtx(property);
2074 AppSpawnClientDestroy(clientHandle);
2075 ASSERT_EQ(ret, 0);
2076 }
2077
2078 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_003, TestSize.Level0)
2079 {
2080 AppSpawnSandboxCfg *sandbox = nullptr;
2081 AppSpawnClientHandle clientHandle = nullptr;
2082 AppSpawnReqMsgHandle reqHandle = 0;
2083 AppSpawningCtx *property = nullptr;
2084 int ret = -1;
2085 do {
2086 ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2087 APPSPAWN_CHECK(ret == 0, break, "Failed to create reqMgr %{public}s", APPSPAWN_SERVER_NAME);
2088 reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 1);
2089 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, break, "Failed to create req %{public}s", APPSPAWN_SERVER_NAME);
2090
2091 // set APP_FLAGS_ISOLATED_SANDBOX
2092 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_ISOLATED_SANDBOX);
2093 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2094 // add expand info to msg
2095 const char dataGroupInfoListStr[] = "{ \
2096 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
2097 \"dir\":[\"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975\", \
2098 \"/data/app/el2/100/group/ce876162-fe69-45d3-aa8e-411a047af564\"], \
2099 \"gid\":[\"20100001\", \"20100002\"] \
2100 }";
2101 ret = AppSpawnReqMsgAddStringInfo(reqHandle, "DataGroup", dataGroupInfoListStr);
2102 APPSPAWN_CHECK(ret == 0, break, "Failed to ext tlv %{public}s", dataGroupInfoListStr);
2103
2104 ret = APPSPAWN_ARG_INVALID;
2105 property = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2106 APPSPAWN_CHECK_ONLY_EXPER(property != nullptr, break);
2107
2108 sandbox = CreateAppSpawnSandbox();
2109 APPSPAWN_CHECK_ONLY_EXPER(sandbox != nullptr, break);
2110 ret = TestParseAppSandboxConfig(sandbox, g_commonConfig.c_str());
2111 APPSPAWN_CHECK_ONLY_EXPER(ret == 0, break);
2112 ret = MountSandboxConfigs(sandbox, property, 0);
2113 } while (0);
2114 if (sandbox) {
2115 DeleteAppSpawnSandbox(sandbox);
2116 }
2117 DeleteAppSpawningCtx(property);
2118 AppSpawnClientDestroy(clientHandle);
2119 ASSERT_EQ(ret, 0);
2120 }
2121
2122 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_004, TestSize.Level0)
2123 {
2124 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
2125 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2126 ASSERT_EQ(context != nullptr, 1);
2127
2128 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2129 APPSPAWN_LOGV("value %{public}s", value);
2130 ASSERT_EQ(value != nullptr, 1);
2131 ASSERT_EQ(strcmp(value, "/system/com.example.myapplication/module") == 0, 1);
2132 DeleteSandboxContext(context);
2133 DeleteAppSpawningCtx(spawningCtx);
2134 }
2135
2136 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_005, TestSize.Level0)
2137 {
2138 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
2139 ASSERT_EQ(spawningCtx != nullptr, 1);
2140 int ret = SetAppSpawnMsgFlag(spawningCtx->message, TLV_MSG_FLAGS, APP_FLAGS_CLONE_ENABLE);
2141 ASSERT_EQ(ret, 0);
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); // +clone-bundleIndex+packageName
2148 ASSERT_EQ(strcmp(value, "/system/+clone-100+com.example.myapplication/module") == 0, 1);
2149 DeleteSandboxContext(context);
2150 DeleteAppSpawningCtx(spawningCtx);
2151 }
2152
2153 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_006, TestSize.Level0)
2154 {
2155 AppSpawnClientHandle clientHandle = nullptr;
2156 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2157 ASSERT_EQ(ret, 0);
2158 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
2159 ASSERT_EQ(reqHandle != nullptr, 1);
2160 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_EXTENSION, "test001");
2161 ASSERT_EQ(ret, 0);
2162 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
2163 ASSERT_EQ(ret, 0);
2164 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2165 ASSERT_EQ(spawningCtx != nullptr, 1);
2166
2167 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2168 ASSERT_EQ(context != nullptr, 1);
2169
2170 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2171 APPSPAWN_LOGV("value %{public}s", value);
2172 ASSERT_EQ(value != nullptr, 1); // +extension-<extensionType>+packageName
2173 ASSERT_EQ(strcmp(value, "/system/+extension-test001+com.example.myapplication/module") == 0, 1);
2174 DeleteSandboxContext(context);
2175 DeleteAppSpawningCtx(spawningCtx);
2176 AppSpawnClientDestroy(clientHandle);
2177 }
2178
2179 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_007, TestSize.Level0)
2180 {
2181 AppSpawnClientHandle clientHandle = nullptr;
2182 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2183 ASSERT_EQ(ret, 0);
2184 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
2185 ASSERT_EQ(reqHandle != nullptr, 1);
2186 ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_EXTENSION, "test001");
2187 ASSERT_EQ(ret, 0);
2188 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
2189 ASSERT_EQ(ret, 0);
2190 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_CLONE_ENABLE);
2191 ASSERT_EQ(ret, 0);
2192 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2193 ASSERT_EQ(spawningCtx != nullptr, 1);
2194
2195 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2196 ASSERT_EQ(context != nullptr, 1);
2197
2198 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2199 APPSPAWN_LOGV("value %{public}s", value);
2200 ASSERT_EQ(value != nullptr, 1); // +clone-bundleIndex+extension-<extensionType>+packageName
2201 ASSERT_EQ(strcmp(value, "/system/+clone-100+extension-test001+com.example.myapplication/module") == 0, 1);
2202 DeleteSandboxContext(context);
2203 DeleteAppSpawningCtx(spawningCtx);
2204 AppSpawnClientDestroy(clientHandle);
2205 }
2206
2207 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_008, TestSize.Level0)
2208 {
2209 AppSpawnClientHandle clientHandle = nullptr;
2210 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2211 ASSERT_EQ(ret, 0);
2212 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
2213 ASSERT_EQ(reqHandle != nullptr, 1);
2214 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
2215 ASSERT_EQ(ret, 0);
2216 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_CLONE_ENABLE);
2217 ASSERT_EQ(ret, 0);
2218 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2219 ASSERT_EQ(spawningCtx != nullptr, 1);
2220
2221 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2222 ASSERT_EQ(context != nullptr, 1);
2223
2224 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2225 ASSERT_EQ(value == nullptr, 1);
2226
2227 DeleteSandboxContext(context);
2228 DeleteAppSpawningCtx(spawningCtx);
2229 AppSpawnClientDestroy(clientHandle);
2230 }
2231
2232 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_AppExtension_009, TestSize.Level0)
2233 {
2234 AppSpawnClientHandle clientHandle = nullptr;
2235 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
2236 ASSERT_EQ(ret, 0);
2237 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
2238 ASSERT_EQ(reqHandle != nullptr, 1);
2239 ret = AppSpawnReqMsgSetAppFlag(reqHandle, APP_FLAGS_EXTENSION_SANDBOX);
2240 ASSERT_EQ(ret, 0);
2241 AppSpawningCtx *spawningCtx = g_testHelper.GetAppProperty(clientHandle, reqHandle);
2242 ASSERT_EQ(spawningCtx != nullptr, 1);
2243
2244 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2245 ASSERT_EQ(context != nullptr, 1);
2246
2247 const char *value = GetSandboxRealVar(context, 0, "/system/<variablePackageName>/module", nullptr, nullptr);
2248 ASSERT_EQ(value == nullptr, 1);
2249
2250 DeleteSandboxContext(context);
2251 DeleteAppSpawningCtx(spawningCtx);
2252 AppSpawnClientDestroy(clientHandle);
2253 }
2254
2255 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_sandbox, TestSize.Level0)
2256 {
2257 const char *path = "/test1/xxxx/xxxx";
2258 bool ret = CheckDirRecursive(path);
2259 ASSERT_TRUE(ret);
2260
2261 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
2262 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
2263 ASSERT_EQ(context != nullptr, 1);
2264 const SandboxContext *contextTest = const_cast<SandboxContext*>(context);
2265 SandboxMountNode node = {};
2266 char source[] = {"/data/app/el5/<currentUserId>/database/<PackageName>"};
2267 char target[] = {"/data/storage/el1/database"};
2268 const PathMountNode sandboxNode = {node, nullptr, nullptr, 0, false, 1, 1, 0, nullptr, {}};
2269 const MountArg args = {"/data/xxx/xxx", nullptr, nullptr, 0, nullptr, 0};
2270 CreateDemandSrc(contextTest, &sandboxNode, &args);
2271
2272 char apl[] = "apl";
2273 const PathMountNode sandboxNode1 = {node, source, nullptr, 0, false, 1, 1, 0, nullptr, {}};
2274 const PathMountNode sandboxNode2 = {node, nullptr, target, 0, false, 1, 1, 0, nullptr, {}};
2275 const PathMountNode sandboxNode3 = {node, source, target, 0, false, 1, 1, 0, apl, {}};
2276 struct ListNode front;
2277 char name[] = {"test"};
2278 const SandboxSection section = {node, front, name, 16, 16, nullptr, 1, 1, nullptr};
2279 char rootPath[] = "/test/xxxx";
2280 SandboxContext context1 = {{{}}, "test.example.ohos.com", nullptr, 1, 1, 1, 1, 1, 1, 1, 1, rootPath};
2281 int res = CheckSandboxMountNode(nullptr, §ion, &sandboxNode, 0);
2282 ASSERT_EQ(res, 0);
2283 res = CheckSandboxMountNode(nullptr, §ion, &sandboxNode1, 0);
2284 ASSERT_EQ(res, 0);
2285 res = CheckSandboxMountNode(nullptr, §ion, &sandboxNode2, 0);
2286 ASSERT_EQ(res, 0);
2287 res = CheckSandboxMountNode(&context1, §ion, &sandboxNode3, MOUNT_PATH_OP_NONE);
2288 ASSERT_EQ(res, 0);
2289 }
2290
2291 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_mount_template, TestSize.Level0)
2292 {
2293 uint32_t ret = GetMountCategory("test");
2294 ASSERT_EQ(ret, MOUNT_TMP_DEFAULT);
2295 const MountArgTemplate *arg = GetMountArgTemplate(10);
2296 ASSERT_EQ(arg, nullptr);
2297 arg = GetMountArgTemplate(7);
2298 ASSERT_EQ(arg, nullptr);
2299 const SandboxFlagInfo info[2] = {{"test1", 0}, {"test2", 0}};
2300 const SandboxFlagInfo *info1 = GetSandboxFlagInfo("test", info, 2);
2301 ASSERT_EQ(info1, nullptr);
2302 ret = GetPathMode("test");
2303 ASSERT_EQ(ret, 0);
2304 }
2305
2306 HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_permission, TestSize.Level0)
2307 {
2308 AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox();
2309 ASSERT_NE(sandbox, nullptr);
2310 int ret = AddSandboxPermissionNode(FILE_CROSS_APP_MODE, nullptr);
2311 ASSERT_EQ(ret, APPSPAWN_ARG_INVALID);
2312 ret = AddSandboxPermissionNode(nullptr, &sandbox->permissionQueue);
2313 ASSERT_EQ(ret, APPSPAWN_ARG_INVALID);
2314 ret = AddSandboxPermissionNode(nullptr, nullptr);
2315 ASSERT_EQ(ret, APPSPAWN_ARG_INVALID);
2316 ret = AddSandboxPermissionNode(FILE_CROSS_APP_MODE, &sandbox->permissionQueue);
2317 ASSERT_EQ(ret, 0);
2318 ret = AddSandboxPermissionNode(FILE_CROSS_APP_MODE, &sandbox->permissionQueue);
2319 ASSERT_EQ(ret, 0);
2320
2321 const SandboxPermissionNode *node = GetPermissionNodeInQueue(nullptr, nullptr);
2322 ASSERT_EQ(node, nullptr);
2323 node = GetPermissionNodeInQueue(&sandbox->permissionQueue, nullptr);
2324 ASSERT_EQ(node, nullptr);
2325 node = GetPermissionNodeInQueue(nullptr, FILE_CROSS_APP_MODE);
2326 ASSERT_EQ(node, nullptr);
2327 node = GetPermissionNodeInQueueByIndex(nullptr, 0);
2328 ASSERT_EQ(node, nullptr);
2329
2330 ret = PermissionRenumber(nullptr);
2331 ASSERT_EQ(ret, -1);
2332 }
2333 } // namespace OHOS
2334