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