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 #define MAX_BUFF 20
42 #define TEST_STR_LEN 30
43
44 extern "C" {
45 void DumpSandboxMountNode(const SandboxMountNode *sandboxNode, uint32_t index);
46 }
47
48 namespace OHOS {
49 static AppSpawnTestHelper g_testHelper;
50 class AppSpawnSandboxCoverageTest : public testing::Test {
51 public:
SetUpTestCase()52 static void SetUpTestCase() {}
TearDownTestCase()53 static void TearDownTestCase()
54 {
55 StubNode *stub = GetStubNode(STUB_MOUNT);
56 if (stub) {
57 stub->flags &= ~STUB_NEED_CHECK;
58 }
59 }
SetUp()60 void SetUp() {}
TearDown()61 void TearDown() {}
62 };
63
TestCreateAppSpawningCtx()64 static AppSpawningCtx *TestCreateAppSpawningCtx()
65 {
66 AppSpawnClientHandle clientHandle = nullptr;
67 int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
68 APPSPAWN_CHECK(ret == 0, return nullptr, "Failed to create reqMgr");
69 AppSpawnReqMsgHandle reqHandle = g_testHelper.CreateMsg(clientHandle, MSG_APP_SPAWN, 0);
70 APPSPAWN_CHECK(reqHandle != INVALID_REQ_HANDLE, return nullptr, "Failed to create req");
71 return g_testHelper.GetAppProperty(clientHandle, reqHandle);
72 }
73
TestGetSandboxContext(const AppSpawningCtx * property,int nwebspawn)74 static SandboxContext *TestGetSandboxContext(const AppSpawningCtx *property, int nwebspawn)
75 {
76 AppSpawnMsgFlags *msgFlags = (AppSpawnMsgFlags *)GetAppProperty(property, TLV_MSG_FLAGS);
77 APPSPAWN_CHECK(msgFlags != nullptr, return nullptr, "No msg flags in msg %{public}s", GetProcessName(property));
78
79 SandboxContext *context = GetSandboxContext();
80 APPSPAWN_CHECK(context != nullptr, return nullptr, "Failed to get context");
81
82 context->nwebspawn = nwebspawn;
83 context->bundleName = GetBundleName(property);
84 context->bundleHasWps = strstr(context->bundleName, "wps") != nullptr;
85 context->dlpBundle = strcmp(GetProcessName(property), "com.ohos.dlpmanager") == 0;
86 context->appFullMountEnable = 0;
87
88 context->sandboxSwitch = 1;
89 context->sandboxShared = false;
90 context->message = property->message;
91 context->rootPath = nullptr;
92 context->rootPath = nullptr;
93 return context;
94 }
95
96 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_ProcessExpandAppSandboxConfig, TestSize.Level0)
97 {
98 int ret = ProcessExpandAppSandboxConfig(nullptr, nullptr, nullptr);
99 ASSERT_NE(ret, 0);
100 const SandboxContext context = {};
101 const AppSpawnSandboxCfg sandboxCfg = {};
102 ret = ProcessExpandAppSandboxConfig(&context, nullptr, nullptr);
103 ASSERT_NE(ret, 0);
104 ret = ProcessExpandAppSandboxConfig(nullptr, &sandboxCfg, nullptr);
105 ASSERT_NE(ret, 0);
106 ret = ProcessExpandAppSandboxConfig(&context, &sandboxCfg, nullptr);
107 ASSERT_NE(ret, 0);
108 }
109
110 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllHsp_001, TestSize.Level0)
111 {
112 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
113 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
114 char testJson[] = "{ \
115 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
116 \"modules\":[\"module1\", \"module2\"], \
117 \"versions\":[\"v10001\", \"v10002\"] \
118 }";
119 cJSON *config = cJSON_Parse(testJson);
120 ASSERT_NE(config, nullptr);
121 int ret = MountAllHsp(nullptr, nullptr);
122 ASSERT_EQ(ret, -1);
123 ret = MountAllHsp(context, nullptr);
124 ASSERT_EQ(ret, -1);
125 ret = MountAllHsp(nullptr, config);
126 ASSERT_EQ(ret, -1);
127 ret = MountAllHsp(context, config);
128 ASSERT_EQ(ret, 0);
129 cJSON_Delete(config);
130 }
131
132 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllHsp_002, TestSize.Level0)
133 {
134 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
135 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
136 char testJson1[] = "{ \
137 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
138 \"modules\":[\"module1\"], \
139 \"versions\":[\"v10001\"] \
140 }";
141 cJSON *config1 = cJSON_Parse(testJson1);
142 ASSERT_NE(config1, nullptr);
143 int ret = MountAllHsp(context, config1); // bundles count != modules
144 cJSON_Delete(config1);
145 ASSERT_EQ(ret, -1);
146
147 char testJson2[] = "{ \
148 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
149 \"modules\":[\"module1\", \"module2\"], \
150 \"versions\":[\"v10001\"] \
151 }";
152 cJSON *config2 = cJSON_Parse(testJson2);
153 ASSERT_NE(config2, nullptr);
154 ret = MountAllHsp(context, config2); // bundles count != versions
155 cJSON_Delete(config2);
156 ASSERT_EQ(ret, -1);
157
158 char testJson3[] = "{ \
159 \"bundles\":[\".\", \"test.bundle2\"], \
160 \"modules\":[\"module1\", \"module2\"], \
161 \"versions\":[\"v10001\", \"v10002\"] \
162 }";
163 cJSON *config3 = cJSON_Parse(testJson3);
164 ASSERT_NE(config3, nullptr);
165 ret = MountAllHsp(context, config3);
166 cJSON_Delete(config3);
167 ASSERT_EQ(ret, -1);
168
169 char testJson4[] = "{ \
170 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
171 \"modules\":[\"..\", \"module2\"], \
172 \"versions\":[\"v10001\", \"v10002\"] \
173 }";
174 cJSON *config4 = cJSON_Parse(testJson4);
175 ASSERT_NE(config4, nullptr);
176 ret = MountAllHsp(context, config4);
177 cJSON_Delete(config4);
178 ASSERT_EQ(ret, -1);
179 }
180
181 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllHsp_003, TestSize.Level0)
182 {
183 const SandboxContext context = {};
184 char testJson1[] = "{ \
185 \"text\":\"test.bundle1\"\
186 }";
187 char testJson2[] = "{ \
188 \"bundles\":\"test.bundle1\"\
189 }";
190 cJSON *config1 = cJSON_Parse(testJson1);
191 ASSERT_NE(config1, nullptr);
192 cJSON *config2 = cJSON_Parse(testJson2);
193 ASSERT_NE(config2, nullptr);
194 int ret = MountAllHsp(&context, config1); // bundles is null
195 ASSERT_EQ(ret, -1);
196 ret = MountAllHsp(&context, config2); // bundles not Array
197 ASSERT_EQ(ret, -1);
198 cJSON_Delete(config1);
199 cJSON_Delete(config2);
200 }
201
202 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllHsp_004, TestSize.Level0)
203 {
204 const SandboxContext context = {};
205 char testJson1[] = "{ \
206 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
207 \"test\":\"module1\" \
208 }";
209 char testJson2[] = "{ \
210 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
211 \"modules\":\"module1\" \
212 }";
213 cJSON *config1 = cJSON_Parse(testJson1);
214 ASSERT_NE(config1, nullptr);
215 cJSON *config2 = cJSON_Parse(testJson2);
216 ASSERT_NE(config2, nullptr);
217 int ret = MountAllHsp(&context, config1); // modules is null
218 ASSERT_EQ(ret, -1);
219 ret = MountAllHsp(&context, config2); // bundles not Array
220 ASSERT_EQ(ret, -1);
221 cJSON_Delete(config1);
222 cJSON_Delete(config2);
223
224 char testJson3[] = "{ \
225 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
226 \"modules\":[\"module1\", \"module2\"], \
227 \"test\":\"v10001\" \
228 }";
229 char testJson4[] = "{ \
230 \"bundles\":[\"test.bundle1\", \"test.bundle2\"], \
231 \"modules\":[\"module1\", \"module2\"], \
232 \"versions\":\"v10001\" \
233 }";
234 cJSON *config3 = cJSON_Parse(testJson3);
235 ASSERT_NE(config3, nullptr);
236 cJSON *config4 = cJSON_Parse(testJson4);
237 ASSERT_NE(config4, nullptr);
238 ret = MountAllHsp(&context, config3); // versions is null
239 ASSERT_EQ(ret, -1);
240 ret = MountAllHsp(&context, config4); // versions not Array
241 ASSERT_EQ(ret, -1);
242 cJSON_Delete(config3);
243 cJSON_Delete(config4);
244 }
245
246 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllGroup, TestSize.Level0)
247 {
248 int ret = MountAllGroup(nullptr, nullptr);
249 ASSERT_EQ(ret, -1);
250
251 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
252 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
253 ASSERT_EQ(context != nullptr, 1);
254
255 const char testDataGroupStr1[] = "{ \
256 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
257 \"dir\":[\"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975\", \
258 \"/data/app/el2/100/group/ce876162-fe69-45d3-aa8e-411a047af564\"], \
259 \"gid\":[\"20100001\", \"20100002\"] \
260 }";
261 cJSON *config1 = cJSON_Parse(testDataGroupStr1);
262 ASSERT_NE(config1, nullptr);
263 ret = MountAllGroup(context, nullptr);
264 ASSERT_EQ(ret, -1);
265 ret = MountAllGroup(nullptr, config1);
266 ASSERT_EQ(ret, -1);
267 ret = MountAllGroup(context, config1);
268 ASSERT_EQ(ret, 0);
269 cJSON_Delete(config1);
270
271 const char testDataGroupStr2[] = "{ \
272 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
273 \"dir\":[\"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975\"], \
274 \"gid\":[\"20100001\"] \
275 }";
276 cJSON *config2 = cJSON_Parse(testDataGroupStr2);
277 ASSERT_NE(config2, nullptr);
278 ret = MountAllGroup(context, config2); // gid count != dataGroupId
279 ASSERT_EQ(ret, -1);
280 cJSON_Delete(config2);
281
282 const char testDataGroupStr3[] = "{ \
283 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
284 \"dir\":[\"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975\"], \
285 \"gid\":[\"20100001\", \"20100002\"] \
286 }";
287 cJSON *config3 = cJSON_Parse(testDataGroupStr3);
288 ASSERT_NE(config3, nullptr);
289 ret = MountAllGroup(context, config3); // dir count != dataGroupId
290 ASSERT_EQ(ret, -1);
291 cJSON_Delete(config3);
292 }
293
294 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllGroup_001, TestSize.Level0)
295 {
296 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
297 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
298 ASSERT_EQ(context != nullptr, 1);
299
300 const char testDataGroupStr1[] = "{ \
301 \"test\":[\"1234abcd5678efgh\", \"abcduiop1234\"] \
302 }";
303 cJSON *config1 = cJSON_Parse(testDataGroupStr1);
304 ASSERT_NE(config1, nullptr);
305 int ret = MountAllGroup(context, config1); // dataGroupIds is null
306 ASSERT_EQ(ret, -1);
307 cJSON_Delete(config1);
308
309 const char testDataGroupStr2[] = "{ \
310 \"dataGroupId\":\"1234abcd5678efgh\" \
311 }";
312 cJSON *config2 = cJSON_Parse(testDataGroupStr2);
313 ASSERT_NE(config2, nullptr);
314 ret = MountAllGroup(context, config2); // dataGroupIds is not Array
315 ASSERT_EQ(ret, -1);
316 cJSON_Delete(config2);
317 }
318
319 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllGroup_002, TestSize.Level0)
320 {
321 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
322 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
323 ASSERT_EQ(context != nullptr, 1);
324
325 const char testDataGroupStr1[] = "{ \
326 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
327 \"test\":[\"20100001\", \"20100002\"] \
328 }";
329 cJSON *config1 = cJSON_Parse(testDataGroupStr1);
330 ASSERT_NE(config1, nullptr);
331 int ret = MountAllGroup(context, config1); // gid is null
332 ASSERT_EQ(ret, -1);
333 cJSON_Delete(config1);
334
335 const char testDataGroupStr2[] = "{ \
336 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
337 \"gid\":\"20100001\" \
338 }";
339 cJSON *config2 = cJSON_Parse(testDataGroupStr2);
340 ASSERT_NE(config2, nullptr);
341 ret = MountAllGroup(context, config2); // gid is not Array
342 ASSERT_EQ(ret, -1);
343 cJSON_Delete(config2);
344
345 const char testDataGroupStr3[] = "{ \
346 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
347 \"test\":[\"/data/app/el2/100/group/ce876162-fe69-45d3-aa8e-411a047af564\"], \
348 \"gid\":[\"20100001\", \"20100002\"] \
349 }";
350 cJSON *config3 = cJSON_Parse(testDataGroupStr3);
351 ASSERT_NE(config3, nullptr);
352 ret = MountAllGroup(context, config3); // dir is null
353 ASSERT_EQ(ret, -1);
354 cJSON_Delete(config3);
355
356 const char testDataGroupStr4[] = "{ \
357 \"dataGroupId\":[\"1234abcd5678efgh\", \"abcduiop1234\"], \
358 \"dir\":\"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975\", \
359 \"gid\":[\"20100001\", \"20100002\"] \
360 }";
361 cJSON *config4 = cJSON_Parse(testDataGroupStr4);
362 ASSERT_NE(config4, nullptr);
363 ret = MountAllGroup(context, config4); // dir is not Array
364 ASSERT_EQ(ret, -1);
365 cJSON_Delete(config4);
366 }
367
368 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_cfgvar, TestSize.Level0)
369 {
370 SandboxContext context = {};
371 context.bundleName = "com.xxx.xxx.xxx";
372 char buffer[MAX_BUFF] = {};
373 uint32_t realLen = 0;
374 int ret = VarPackageNameReplace(&context, buffer, MAX_BUFF, &realLen, nullptr);
375 EXPECT_EQ(ret, 0);
376
377 context.bundleName = "com.xxxxxxx.xxxxxxx.xxxxxxx";
378 ret = VarPackageNameReplace(&context, buffer, MAX_BUFF, &realLen, nullptr);
379 EXPECT_EQ(ret, -1);
380
381 VarExtraData extraData = {};
382 ret = ReplaceVariableForDepSandboxPath(nullptr, buffer, MAX_BUFF, &realLen, nullptr);
383 EXPECT_EQ(ret, -1);
384 ret = ReplaceVariableForDepSrcPath(nullptr, buffer, MAX_BUFF, &realLen, nullptr);
385 EXPECT_EQ(ret, -1);
386 ret = ReplaceVariableForDepSandboxPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
387 EXPECT_EQ(ret, -1);
388 ret = ReplaceVariableForDepSrcPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
389 EXPECT_EQ(ret, -1);
390 extraData.data.depNode = (PathMountNode *)malloc(sizeof(PathMountNode));
391 ASSERT_EQ(extraData.data.depNode != nullptr, 1);
392
393 extraData.data.depNode->target = (char*)malloc(sizeof(char) * TEST_STR_LEN);
394 ASSERT_EQ(extraData.data.depNode->target != nullptr, 1);
395 ret = strcpy_s(extraData.data.depNode->target, TEST_STR_LEN, "/xxxx/xxxx/xxxxxxx/xxxxxx/xxx");
396 EXPECT_EQ(ret, 0);
397 ret = ReplaceVariableForDepSandboxPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
398 EXPECT_EQ(ret, -1);
399 ret = strcpy_s(extraData.data.depNode->target, TEST_STR_LEN, "/xxxx/xxxx/xxxxxx");
400 EXPECT_EQ(ret, 0);
401 ret = ReplaceVariableForDepSandboxPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
402 EXPECT_EQ(ret, 0);
403
404 extraData.data.depNode->source = (char*)malloc(sizeof(char) * TEST_STR_LEN);
405 ASSERT_EQ(extraData.data.depNode->source != nullptr, 1);
406 ret = strcpy_s(extraData.data.depNode->source, TEST_STR_LEN, "/xxxx/xxxx/xxxxxxx/xxxxxx/xxx");
407 EXPECT_EQ(ret, 0);
408 ret = ReplaceVariableForDepSrcPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
409 EXPECT_EQ(ret, -1);
410 ret = strcpy_s(extraData.data.depNode->source, TEST_STR_LEN, "/xxxx/xxxx/xxxxxx");
411 EXPECT_EQ(ret, 0);
412 ret = ReplaceVariableForDepSrcPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
413 EXPECT_EQ(ret, 0);
414 free(extraData.data.depNode->target);
415 free(extraData.data.depNode->source);
416 free(extraData.data.depNode);
417 }
418
419 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_cfgvar_001, TestSize.Level0)
420 {
421 char buffer[MAX_BUFF] = {};
422 uint32_t realLen = 0;
423 VarExtraData extraData = {};
424 int ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, nullptr);
425 EXPECT_EQ(ret, -1);
426 ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
427 EXPECT_EQ(ret, -1);
428 extraData.data.depNode = (PathMountNode *)malloc(sizeof(PathMountNode));
429 ASSERT_EQ(extraData.data.depNode != nullptr, 1);
430 extraData.data.depNode->source = (char*)malloc(sizeof(char) * TEST_STR_LEN);
431 EXPECT_EQ(extraData.data.depNode->source != nullptr, 1);
432
433 ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
434 EXPECT_EQ(ret, 0);
435
436 ret = strcpy_s(extraData.data.depNode->source, TEST_STR_LEN, "/xxxx/xxxx/xxxxxxx/xxxxxx/xxx");
437 EXPECT_EQ(ret, 0);
438 ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
439 EXPECT_EQ(ret, -1);
440
441 ret = strcpy_s(extraData.data.depNode->source, TEST_STR_LEN, "/xxxx/xxxx/xxxxxx");
442 EXPECT_EQ(ret, 0);
443 ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
444 EXPECT_EQ(ret, 0);
445 free(extraData.data.depNode->source);
446 free(extraData.data.depNode);
447 }
448
449 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_cfgvar_002, TestSize.Level0)
450 {
451 const char *realVar = GetSandboxRealVar(nullptr, 0, nullptr, nullptr, nullptr);
452 EXPECT_EQ(realVar == nullptr, 1);
453
454 SandboxContext context = {};
455 realVar = GetSandboxRealVar(&context, 0, nullptr, nullptr, nullptr);
456 EXPECT_EQ(realVar == nullptr, 1);
457
458 context.buffer[0].buffer = (char*)malloc(sizeof(char) * MAX_BUFF);
459 ASSERT_EQ(context.buffer[0].buffer != nullptr, 1);
460 int ret = strcpy_s(context.buffer[0].buffer, MAX_BUFF, "xxxxxxxx");
461 EXPECT_EQ(ret, 0);
462 context.buffer[0].bufferLen = MAX_BUFF;
463 context.buffer[0].current = 0;
464 realVar = GetSandboxRealVar(&context, 0, nullptr, nullptr, nullptr);
465 EXPECT_EQ(realVar != nullptr, 1);
466 realVar = GetSandboxRealVar(&context, 4, nullptr, nullptr, nullptr);
467 EXPECT_EQ(realVar == nullptr, 1);
468 realVar = GetSandboxRealVar(&context, 0, nullptr, "test/xxxx", nullptr);
469 EXPECT_EQ(realVar != nullptr, 1);
470 realVar = GetSandboxRealVar(&context, 0, "xxxx/xxx", nullptr, nullptr);
471 EXPECT_EQ(realVar != nullptr, 1);
472 realVar = GetSandboxRealVar(&context, 0, "xxxx/xxx", "test/xxxx", nullptr);
473 EXPECT_EQ(realVar != nullptr, 1);
474 GetSandboxRealVar(&context, 1, nullptr, nullptr, nullptr);
475 EXPECT_EQ(realVar != nullptr, 1);
476 free(context.buffer[0].buffer);
477 }
478
479 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_manager, TestSize.Level0)
480 {
481 int ret = SpawnPrepareSandboxCfg(nullptr, nullptr);
482 EXPECT_EQ(ret, -1);
483
484 AppSpawnMgr content = {};
485 ret = SpawnPrepareSandboxCfg(&content, nullptr);
486 EXPECT_EQ(ret, -1);
487 AppSpawningCtx property = {};
488 property.message = (AppSpawnMsgNode *)malloc(sizeof(AppSpawnMsgNode));
489 ASSERT_EQ(property.message != nullptr, 1);
490 ret = strcpy_s(property.message->msgHeader.processName, APP_LEN_PROC_NAME, "com.xxx.xxx.xxx");
491 EXPECT_EQ(ret, 0);
492 ret = SpawnPrepareSandboxCfg(&content, &property);
493 EXPECT_EQ(ret, -1);
494 free(property.message);
495
496 PathMountNode mountNode = {};
497 mountNode.checkErrorFlag = true;
498 mountNode.createDemand = 0;
499 DumpSandboxMountNode(nullptr, 0);
500 mountNode.sandboxNode.type = SANDBOX_TAG_MOUNT_PATH;
501 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
502 mountNode.sandboxNode.type = SANDBOX_TAG_SYMLINK;
503 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
504 mountNode.source = (char*)malloc(sizeof(char) * TEST_STR_LEN);
505 ASSERT_EQ(mountNode.source!= nullptr, 1);
506 mountNode.target = (char*)malloc(sizeof(char) * TEST_STR_LEN);
507 ASSERT_EQ(mountNode.target!= nullptr, 1);
508 mountNode.appAplName = (char*)malloc(sizeof(char) * TEST_STR_LEN);
509 ASSERT_EQ(mountNode.appAplName!= nullptr, 1);
510 ret = strcpy_s(mountNode.source, TEST_STR_LEN, "/xxxxx/xxx");
511 EXPECT_EQ(ret, 0);
512 ret = strcpy_s(mountNode.target, TEST_STR_LEN, "/test/xxxx");
513 EXPECT_EQ(ret, 0);
514 ret = strcpy_s(mountNode.appAplName, TEST_STR_LEN, "apl");
515 EXPECT_EQ(ret, 0);
516 mountNode.sandboxNode.type = SANDBOX_TAG_MOUNT_FILE;
517 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
518 mountNode.checkErrorFlag = false;
519 mountNode.createDemand = 1;
520 mountNode.sandboxNode.type = SANDBOX_TAG_SYMLINK;
521 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
522 mountNode.sandboxNode.type = SANDBOX_TAG_REQUIRED;
523 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
524 free(mountNode.source);
525 free(mountNode.target);
526 free(mountNode.appAplName);
527 }
528
529 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_001, TestSize.Level0)
530 {
531 unsigned long ret = GetMountModeFromConfig(nullptr, nullptr, 1);
532 EXPECT_EQ(ret, 1);
533 const char testStr1[] = "{ \
534 \"test\":\"xxxxxxxxx\", \
535 }";
536 cJSON *config1 = cJSON_Parse(testStr1);
537 ASSERT_EQ(config1, nullptr);
538 ret = GetMountModeFromConfig(config1, "test", 1);
539 EXPECT_EQ(ret, 1);
540
541 ret = GetFlagIndexFromJson(config1);
542 EXPECT_EQ(ret, 0);
543 const char testStr2[] = "{ \
544 \"name\":\"xxxxxxxxx\", \
545 }";
546 cJSON *config2 = cJSON_Parse(testStr2);
547 ASSERT_EQ(config2, nullptr);
548 ret = GetFlagIndexFromJson(config2);
549 EXPECT_EQ(ret, 0);
550 cJSON_Delete(config1);
551 cJSON_Delete(config2);
552 }
553
554 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_002, TestSize.Level0)
555 {
556 const char testStr1[] = "{ \
557 \"mount-paths\":[\"/xxxx/xxx\", \"/xxx/xxx\"] \
558 }";
559 cJSON *config1 = cJSON_Parse(testStr1);
560 ASSERT_NE(config1, nullptr);
561 cJSON *mountPaths = cJSON_GetObjectItemCaseSensitive(config1, "mount-paths");
562 int result = ParseMountPathsConfig(nullptr, mountPaths, nullptr, 1);
563 EXPECT_EQ(result, 0);
564 cJSON_Delete(config1);
565
566 const char testStr2[] = "{ \
567 \"mount-paths\":[{\
568 \"src-path\": \"/config\", \
569 \"test\": \"/test/xxx\" \
570 }] \
571 }";
572 cJSON *config2 = cJSON_Parse(testStr2);
573 ASSERT_NE(config2, nullptr);
574 mountPaths = cJSON_GetObjectItemCaseSensitive(config2, "mount-paths");
575 result = ParseMountPathsConfig(nullptr, mountPaths, nullptr, 1);
576 EXPECT_EQ(result, 0);
577 cJSON_Delete(config2);
578 }
579
580 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_003, TestSize.Level0)
581 {
582 const char testStr1[] = "{ \
583 \"mount-paths\":[{ \
584 \"sandbox-path\": \"/config\", \
585 \"test\": \"/test/xxx\"}] \
586 }";
587 cJSON *config1 = cJSON_Parse(testStr1);
588 ASSERT_NE(config1, nullptr);
589 cJSON *mountPaths = cJSON_GetObjectItemCaseSensitive(config1, "mount-paths");
590 int result = ParseMountPathsConfig(nullptr, mountPaths, nullptr, 1);
591 EXPECT_EQ(result, 0);
592 cJSON_Delete(config1);
593
594 const char testStr2[] = "{ \
595 \"mount-paths\":[{ \
596 \"src-path\": \"/xxx/xxx\", \
597 \"sandbox-path\": \"/test/xxx\"}] \
598 }";
599 cJSON *config2 = cJSON_Parse(testStr2);
600 ASSERT_NE(config2, nullptr);
601 mountPaths = cJSON_GetObjectItemCaseSensitive(config2, "mount-paths");
602 result = ParseMountPathsConfig(nullptr, mountPaths, nullptr, 1);
603 EXPECT_EQ(result, 0);
604 cJSON_Delete(config2);
605 }
606
607 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_004, TestSize.Level0)
608 {
609 int ret = ParseSymbolLinksConfig(nullptr, nullptr, nullptr);
610 EXPECT_EQ(ret, -1);
611
612 const char testStr1[] = "{ \
613 \"test\":\"xxxxxxxxx\" \
614 }";
615 cJSON *config1 = cJSON_Parse(testStr1);
616 ASSERT_NE(config1, nullptr);
617 ret = ParseSymbolLinksConfig(nullptr, config1, nullptr);
618 EXPECT_EQ(ret, -1);
619 cJSON_Delete(config1);
620
621 const char testStr2[] = "{ \
622 \"symbol-links\":[\"/xxxx/xxx\", \"/xxx/xxx\"] \
623 }";
624 cJSON *config2 = cJSON_Parse(testStr2);
625 ASSERT_NE(config2, nullptr);
626 cJSON *symbolLinks = cJSON_GetObjectItemCaseSensitive(config2, "symbol-links");
627 ret = ParseSymbolLinksConfig(nullptr, symbolLinks, nullptr);
628 EXPECT_EQ(ret, -1);
629 cJSON_Delete(config2);
630 }
631
632 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_005, TestSize.Level0)
633 {
634 const char testStr1[] = "{ \
635 \"symbol-links\":[{\
636 \"test\":\"/xxxx/xxx\", \
637 \"target-name\":\"/xxx/xxx\"}] \
638 }";
639 cJSON *config1 = cJSON_Parse(testStr1);
640 ASSERT_NE(config1, nullptr);
641 cJSON *symbolLinks = cJSON_GetObjectItemCaseSensitive(config1, "symbol-links");
642 int ret = ParseSymbolLinksConfig(nullptr, symbolLinks, nullptr);
643 EXPECT_EQ(ret, -1);
644 cJSON_Delete(config1);
645
646 const char testStr2[] = "{ \
647 \"symbol-links\":[{\
648 \"test\":\"/xxxx/xxx\", \
649 \"link-name\":\"/xxx/xxx\"}] \
650 }";
651 cJSON *config2 = cJSON_Parse(testStr2);
652 ASSERT_NE(config2, nullptr);
653 symbolLinks = cJSON_GetObjectItemCaseSensitive(config2, "symbol-links");
654 ret = ParseSymbolLinksConfig(nullptr, symbolLinks, nullptr);
655 EXPECT_EQ(ret, -1);
656 cJSON_Delete(config2);
657
658 const char testStr3[] = "{ \
659 \"symbol-links\":[{\
660 \"target-name\":\"/xxxx/xxx\", \
661 \"link-name\":\"/xxx/xxx\"}] \
662 }";
663 cJSON *config3 = cJSON_Parse(testStr3);
664 ASSERT_NE(config3, nullptr);
665 symbolLinks = cJSON_GetObjectItemCaseSensitive(config3, "symbol-links");
666 ret = ParseSymbolLinksConfig(nullptr, symbolLinks, nullptr);
667 EXPECT_EQ(ret, 0);
668 cJSON_Delete(config3);
669 }
670
671 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_006, TestSize.Level0)
672 {
673 const char testStr1[] = "{ \
674 \"test\":\"xxxxxxxxx\" \
675 }";
676 cJSON *config1 = cJSON_Parse(testStr1);
677 ASSERT_NE(config1, nullptr);
678 int ret = ParseGidTableConfig(nullptr, config1, nullptr);
679 EXPECT_EQ(ret, 0);
680 cJSON_Delete(config1);
681
682 const char testStr2[] = "{ \
683 \"test\":[] \
684 }";
685 cJSON *config2 = cJSON_Parse(testStr2);
686 ASSERT_NE(config2, nullptr);
687 cJSON *testCfg = cJSON_GetObjectItemCaseSensitive(config2, "test");
688 ret = ParseGidTableConfig(nullptr, testCfg, nullptr);
689 EXPECT_EQ(ret, 0);
690 cJSON_Delete(config2);
691
692 SandboxSection section = {};
693 const char testStr3[] = "{ \
694 \"gids\":[\"202400\", \"202500\", \"202600\"] \
695 }";
696 cJSON *config3 = cJSON_Parse(testStr3);
697 ASSERT_NE(config3, nullptr);
698 testCfg = cJSON_GetObjectItemCaseSensitive(config3, "gids");
699 ret = ParseGidTableConfig(nullptr, testCfg, §ion);
700 EXPECT_EQ(ret, 0);
701 cJSON_Delete(config3);
702
703 section.gidTable = (gid_t *)malloc(sizeof(gid_t) * 10);
704 ASSERT_EQ(section.gidTable != nullptr, 1);
705 ret = ParseGidTableConfig(nullptr, testCfg, §ion);
706 EXPECT_EQ(ret, 0);
707 if (section.gidTable != nullptr) {
708 free(section.gidTable);
709 }
710 }
711 }