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, nullptr);
249 ASSERT_NE(ret, 0);
250
251 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
252 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
253 ASSERT_EQ(context != nullptr, 1);
254 AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
255 ASSERT_EQ(sandbox != nullptr, 1);
256 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
257
258 const char testDataGroupStr1[] = R"([
259 {
260 "gid":"2010001",
261 "dir":"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975",
262 "dataGroupId":"43200",
263 "uuid":"091a68a9-2cc9-4279-8849-28631b598975"
264 }, {
265 "gid":"2010001",
266 "dir":"/data/app/el2/100/group/49c016e6-065a-abd1-5867-b1f91114f840",
267 "dataGroupId":"43200",
268 "uuid":"49c016e6-065a-abd1-5867-b1f91114f840"
269 }
270 ])";
271 cJSON *config1 = cJSON_Parse(testDataGroupStr1);
272 ASSERT_NE(config1, nullptr);
273 ret = MountAllGroup(context, sandbox, nullptr);
274 ASSERT_NE(ret, 0);
275 ret = MountAllGroup(context, nullptr, config1);
276 ASSERT_NE(ret, 0);
277 ret = MountAllGroup(nullptr, sandbox, config1);
278 ASSERT_NE(ret, 0);
279 context->rootPath = strdup("/mnt/sandbox/100/app-root");
280 ASSERT_EQ(context->rootPath != nullptr, 1);
281 ret = MountAllGroup(context, sandbox, config1);
282 ASSERT_EQ(ret, 0);
283 cJSON_Delete(config1);
284 free(context->rootPath);
285 context->rootPath = NULL;
286
287 const char testDataGroupStr2[] = R"([
288 {
289 "gid":"2010001",
290 "dir":"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975",
291 "dataGroupId":"43200",
292 "uuid":"091a68a9-2cc9-4279-8849-28631b598975"
293 }, {
294 "gid":"2010001",
295 "dir":"/data/app/el2/100/group/49c016e6-065a-abd1-5867-b1f91114f840",
296 "uuid":"49c016e6-065a-abd1-5867-b1f91114f840"
297 }
298 ])";
299 cJSON *config2 = cJSON_Parse(testDataGroupStr2);
300 ASSERT_NE(config2, nullptr);
301 ret = MountAllGroup(context, sandbox, config2); // gid count != dataGroupId
302 ASSERT_NE(ret, 0);
303 cJSON_Delete(config2);
304
305 const char testDataGroupStr3[] = R"([
306 {
307 "gid":"2010001",
308 "dir":"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975",
309 "dataGroupId":"43200",
310 "uuid":"091a68a9-2cc9-4279-8849-28631b598975"
311 }, {
312 "gid":"2010001",
313 "dataGroupId":"43200",
314 "uuid":"49c016e6-065a-abd1-5867-b1f91114f840"
315 }
316 ])";
317 cJSON *config3 = cJSON_Parse(testDataGroupStr3);
318 ASSERT_NE(config3, nullptr);
319 ret = MountAllGroup(context, sandbox, config3); // dir count != dataGroupId
320 ASSERT_NE(ret, -1);
321 cJSON_Delete(config3);
322 if (sandbox != nullptr) {
323 sandbox->extData.freeNode(&sandbox->extData);
324 }
325 }
326
327 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllGroup_001, TestSize.Level0)
328 {
329 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
330 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
331 ASSERT_EQ(context != nullptr, 1);
332 AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
333 ASSERT_EQ(sandbox != nullptr, 1);
334 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
335
336 const char testDataGroupStr1[] = R"([
337 {
338 "gid":"2010001",
339 "dir":"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975",
340 "uuid":"091a68a9-2cc9-4279-8849-28631b598975"
341 }
342 ])";
343 cJSON *config1 = cJSON_Parse(testDataGroupStr1);
344 ASSERT_NE(config1, nullptr);
345 int ret = MountAllGroup(context, sandbox, config1); // dataGroupIds is null
346 ASSERT_NE(ret, 0);
347 cJSON_Delete(config1);
348 if (sandbox != nullptr) {
349 sandbox->extData.freeNode(&sandbox->extData);
350 }
351 }
352
353 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_MountAllGroup_002, TestSize.Level0)
354 {
355 AppSpawningCtx *spawningCtx = TestCreateAppSpawningCtx();
356 SandboxContext *context = TestGetSandboxContext(spawningCtx, 0);
357 ASSERT_EQ(context != nullptr, 1);
358 AppSpawnSandboxCfg *sandbox = CreateAppSpawnSandbox(EXT_DATA_APP_SANDBOX);
359 ASSERT_EQ(sandbox != nullptr, 1);
360 LoadAppSandboxConfig(sandbox, EXT_DATA_APP_SANDBOX);
361
362 const char testDataGroupStr1[] = R"([
363 {
364 "test":"2010001",
365 "dir":"/data/app/el2/100/group/091a68a9-2cc9-4279-8849-28631b598975",
366 "dataGroupId":"43200",
367 "uuid":"091a68a9-2cc9-4279-8849-28631b598975"
368 }
369 ])";
370 cJSON *config1 = cJSON_Parse(testDataGroupStr1);
371 ASSERT_NE(config1, nullptr);
372 int ret = MountAllGroup(context, sandbox, config1); // gid is null
373 ASSERT_NE(ret, 0);
374 cJSON_Delete(config1);
375
376 const char testDataGroupStr2[] = R"([
377 {
378 "gid":"2010001",
379 "dataGroupId":"43200",
380 "uuid":"091a68a9-2cc9-4279-8849-28631b598975"
381 }
382 ])";
383 cJSON *config2 = cJSON_Parse(testDataGroupStr2);
384 ASSERT_NE(config2, nullptr);
385 ret = MountAllGroup(context, sandbox, config2); // dir
386 ASSERT_NE(ret, 0);
387 cJSON_Delete(config2);
388 }
389
390 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_cfgvar, TestSize.Level0)
391 {
392 SandboxContext context = {};
393 context.bundleName = "com.xxx.xxx.xxx";
394 char buffer[MAX_BUFF] = {};
395 uint32_t realLen = 0;
396 int ret = VarPackageNameReplace(&context, buffer, MAX_BUFF, &realLen, nullptr);
397 EXPECT_EQ(ret, 0);
398
399 context.bundleName = "com.xxxxxxx.xxxxxxx.xxxxxxx";
400 ret = VarPackageNameReplace(&context, buffer, MAX_BUFF, &realLen, nullptr);
401 EXPECT_EQ(ret, -1);
402
403 VarExtraData extraData = {};
404 ret = ReplaceVariableForDepSandboxPath(nullptr, buffer, MAX_BUFF, &realLen, nullptr);
405 EXPECT_EQ(ret, -1);
406 ret = ReplaceVariableForDepSrcPath(nullptr, buffer, MAX_BUFF, &realLen, nullptr);
407 EXPECT_EQ(ret, -1);
408 ret = ReplaceVariableForDepSandboxPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
409 EXPECT_EQ(ret, -1);
410 ret = ReplaceVariableForDepSrcPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
411 EXPECT_EQ(ret, -1);
412 extraData.data.depNode = (PathMountNode *)malloc(sizeof(PathMountNode));
413 ASSERT_EQ(extraData.data.depNode != nullptr, 1);
414
415 extraData.data.depNode->target = (char*)malloc(sizeof(char) * TEST_STR_LEN);
416 ASSERT_EQ(extraData.data.depNode->target != nullptr, 1);
417 ret = strcpy_s(extraData.data.depNode->target, TEST_STR_LEN, "/xxxx/xxxx/xxxxxxx/xxxxxx/xxx");
418 EXPECT_EQ(ret, 0);
419 ret = ReplaceVariableForDepSandboxPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
420 EXPECT_EQ(ret, -1);
421 ret = strcpy_s(extraData.data.depNode->target, TEST_STR_LEN, "/xxxx/xxxx/xxxxxx");
422 EXPECT_EQ(ret, 0);
423 ret = ReplaceVariableForDepSandboxPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
424 EXPECT_EQ(ret, 0);
425
426 extraData.data.depNode->source = (char*)malloc(sizeof(char) * TEST_STR_LEN);
427 ASSERT_EQ(extraData.data.depNode->source != nullptr, 1);
428 ret = strcpy_s(extraData.data.depNode->source, TEST_STR_LEN, "/xxxx/xxxx/xxxxxxx/xxxxxx/xxx");
429 EXPECT_EQ(ret, 0);
430 ret = ReplaceVariableForDepSrcPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
431 EXPECT_EQ(ret, -1);
432 ret = strcpy_s(extraData.data.depNode->source, TEST_STR_LEN, "/xxxx/xxxx/xxxxxx");
433 EXPECT_EQ(ret, 0);
434 ret = ReplaceVariableForDepSrcPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
435 EXPECT_EQ(ret, 0);
436 free(extraData.data.depNode->target);
437 free(extraData.data.depNode->source);
438 free(extraData.data.depNode);
439 }
440
441 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_cfgvar_001, TestSize.Level0)
442 {
443 char buffer[MAX_BUFF] = {};
444 uint32_t realLen = 0;
445 VarExtraData extraData = {};
446 int ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, nullptr);
447 EXPECT_EQ(ret, -1);
448 ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
449 EXPECT_EQ(ret, -1);
450 extraData.data.depNode = (PathMountNode *)malloc(sizeof(PathMountNode));
451 ASSERT_EQ(extraData.data.depNode != nullptr, 1);
452 extraData.data.depNode->source = (char*)malloc(sizeof(char) * TEST_STR_LEN);
453 EXPECT_EQ(extraData.data.depNode->source != nullptr, 1);
454
455 ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
456 EXPECT_EQ(ret, 0);
457
458 ret = strcpy_s(extraData.data.depNode->source, TEST_STR_LEN, "/xxxx/xxxx/xxxxxxx/xxxxxx/xxx");
459 EXPECT_EQ(ret, 0);
460 ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
461 EXPECT_EQ(ret, -1);
462
463 ret = strcpy_s(extraData.data.depNode->source, TEST_STR_LEN, "/xxxx/xxxx/xxxxxx");
464 EXPECT_EQ(ret, 0);
465 ret = ReplaceVariableForDepPath(nullptr, buffer, MAX_BUFF, &realLen, &extraData);
466 EXPECT_EQ(ret, 0);
467 free(extraData.data.depNode->source);
468 free(extraData.data.depNode);
469 }
470
471 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_cfgvar_002, TestSize.Level0)
472 {
473 const char *realVar = GetSandboxRealVar(nullptr, 0, nullptr, nullptr, nullptr);
474 EXPECT_EQ(realVar == nullptr, 1);
475
476 SandboxContext context = {};
477 realVar = GetSandboxRealVar(&context, 0, nullptr, nullptr, nullptr);
478 EXPECT_EQ(realVar == nullptr, 1);
479
480 context.buffer[0].buffer = (char*)malloc(sizeof(char) * MAX_BUFF);
481 ASSERT_EQ(context.buffer[0].buffer != nullptr, 1);
482 int ret = strcpy_s(context.buffer[0].buffer, MAX_BUFF, "xxxxxxxx");
483 EXPECT_EQ(ret, 0);
484 context.buffer[0].bufferLen = MAX_BUFF;
485 context.buffer[0].current = 0;
486 realVar = GetSandboxRealVar(&context, 0, nullptr, nullptr, nullptr);
487 EXPECT_EQ(realVar != nullptr, 1);
488 realVar = GetSandboxRealVar(&context, 4, nullptr, nullptr, nullptr);
489 EXPECT_EQ(realVar == nullptr, 1);
490 realVar = GetSandboxRealVar(&context, 0, nullptr, "test/xxxx", nullptr);
491 EXPECT_EQ(realVar != nullptr, 1);
492 realVar = GetSandboxRealVar(&context, 0, "xxxx/xxx", nullptr, nullptr);
493 EXPECT_EQ(realVar != nullptr, 1);
494 realVar = GetSandboxRealVar(&context, 0, "xxxx/xxx", "test/xxxx", nullptr);
495 EXPECT_EQ(realVar != nullptr, 1);
496 GetSandboxRealVar(&context, 1, nullptr, nullptr, nullptr);
497 EXPECT_EQ(realVar != nullptr, 1);
498 free(context.buffer[0].buffer);
499 }
500
501 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_manager, TestSize.Level0)
502 {
503 int ret = SpawnPrepareSandboxCfg(nullptr, nullptr);
504 EXPECT_EQ(ret, -1);
505
506 AppSpawnMgr content = {};
507 ret = SpawnPrepareSandboxCfg(&content, nullptr);
508 EXPECT_EQ(ret, -1);
509 AppSpawningCtx property = {};
510 property.message = (AppSpawnMsgNode *)malloc(sizeof(AppSpawnMsgNode));
511 memset_s(property.message, sizeof(AppSpawnMsgNode), 0, sizeof(AppSpawnMsgNode));
512 ASSERT_EQ(property.message != nullptr, 1);
513 ret = strcpy_s(property.message->msgHeader.processName, APP_LEN_PROC_NAME, "com.xxx.xxx.xxx");
514 EXPECT_EQ(ret, 0);
515 ret = SpawnPrepareSandboxCfg(&content, &property);
516 EXPECT_EQ(ret, -1);
517 free(property.message);
518
519 PathMountNode mountNode = {};
520 mountNode.checkErrorFlag = true;
521 mountNode.createDemand = 0;
522 DumpSandboxMountNode(nullptr, 0);
523 mountNode.sandboxNode.type = SANDBOX_TAG_MOUNT_PATH;
524 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
525 mountNode.sandboxNode.type = SANDBOX_TAG_SYMLINK;
526 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
527 mountNode.source = (char*)malloc(sizeof(char) * TEST_STR_LEN);
528 ASSERT_EQ(mountNode.source!= nullptr, 1);
529 mountNode.target = (char*)malloc(sizeof(char) * TEST_STR_LEN);
530 ASSERT_EQ(mountNode.target!= nullptr, 1);
531 mountNode.appAplName = (char*)malloc(sizeof(char) * TEST_STR_LEN);
532 ASSERT_EQ(mountNode.appAplName!= nullptr, 1);
533 ret = strcpy_s(mountNode.source, TEST_STR_LEN, "/xxxxx/xxx");
534 EXPECT_EQ(ret, 0);
535 ret = strcpy_s(mountNode.target, TEST_STR_LEN, "/test/xxxx");
536 EXPECT_EQ(ret, 0);
537 ret = strcpy_s(mountNode.appAplName, TEST_STR_LEN, "apl");
538 EXPECT_EQ(ret, 0);
539 mountNode.sandboxNode.type = SANDBOX_TAG_MOUNT_FILE;
540 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
541 mountNode.checkErrorFlag = false;
542 mountNode.createDemand = 1;
543 mountNode.sandboxNode.type = SANDBOX_TAG_SYMLINK;
544 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
545 mountNode.sandboxNode.type = SANDBOX_TAG_REQUIRED;
546 DumpSandboxMountNode(&mountNode.sandboxNode, 0);
547 free(mountNode.source);
548 free(mountNode.target);
549 free(mountNode.appAplName);
550 }
551
552 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_001, TestSize.Level0)
553 {
554 unsigned long ret = GetMountModeFromConfig(nullptr, nullptr, 1);
555 EXPECT_EQ(ret, 1);
556 const char testStr1[] = "{ \
557 \"test\":\"xxxxxxxxx\", \
558 }";
559 cJSON *config1 = cJSON_Parse(testStr1);
560 ASSERT_EQ(config1, nullptr);
561 ret = GetMountModeFromConfig(config1, "test", 1);
562 EXPECT_EQ(ret, 1);
563
564 ret = GetFlagIndexFromJson(config1);
565 EXPECT_EQ(ret, 0);
566 const char testStr2[] = "{ \
567 \"name\":\"xxxxxxxxx\", \
568 }";
569 cJSON *config2 = cJSON_Parse(testStr2);
570 ASSERT_EQ(config2, nullptr);
571 ret = GetFlagIndexFromJson(config2);
572 EXPECT_EQ(ret, 0);
573 cJSON_Delete(config1);
574 cJSON_Delete(config2);
575 }
576
577 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_002, TestSize.Level0)
578 {
579 const char testStr1[] = "{ \
580 \"mount-paths\":[\"/xxxx/xxx\", \"/xxx/xxx\"] \
581 }";
582 cJSON *config1 = cJSON_Parse(testStr1);
583 ASSERT_NE(config1, nullptr);
584 cJSON *mountPaths = cJSON_GetObjectItemCaseSensitive(config1, "mount-paths");
585 int result = ParseMountPathsConfig(nullptr, mountPaths, nullptr, 1);
586 EXPECT_EQ(result, 0);
587 cJSON_Delete(config1);
588
589 const char testStr2[] = "{ \
590 \"mount-paths\":[{\
591 \"src-path\": \"/config\", \
592 \"test\": \"/test/xxx\" \
593 }] \
594 }";
595 cJSON *config2 = cJSON_Parse(testStr2);
596 ASSERT_NE(config2, nullptr);
597 mountPaths = cJSON_GetObjectItemCaseSensitive(config2, "mount-paths");
598 result = ParseMountPathsConfig(nullptr, mountPaths, nullptr, 1);
599 EXPECT_EQ(result, 0);
600 cJSON_Delete(config2);
601 }
602
603 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_003, TestSize.Level0)
604 {
605 const char testStr1[] = "{ \
606 \"mount-paths\":[{ \
607 \"sandbox-path\": \"/config\", \
608 \"test\": \"/test/xxx\"}] \
609 }";
610 cJSON *config1 = cJSON_Parse(testStr1);
611 ASSERT_NE(config1, nullptr);
612 cJSON *mountPaths = cJSON_GetObjectItemCaseSensitive(config1, "mount-paths");
613 int result = ParseMountPathsConfig(nullptr, mountPaths, nullptr, 1);
614 EXPECT_EQ(result, 0);
615 cJSON_Delete(config1);
616
617 const char testStr2[] = "{ \
618 \"mount-paths\":[{ \
619 \"src-path\": \"/xxx/xxx\", \
620 \"sandbox-path\": \"/test/xxx\"}] \
621 }";
622 cJSON *config2 = cJSON_Parse(testStr2);
623 ASSERT_NE(config2, nullptr);
624 mountPaths = cJSON_GetObjectItemCaseSensitive(config2, "mount-paths");
625 result = ParseMountPathsConfig(nullptr, mountPaths, nullptr, 1);
626 EXPECT_EQ(result, 0);
627 cJSON_Delete(config2);
628 }
629
630 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_004, TestSize.Level0)
631 {
632 int ret = ParseSymbolLinksConfig(nullptr, nullptr, nullptr);
633 EXPECT_EQ(ret, -1);
634
635 const char testStr1[] = "{ \
636 \"test\":\"xxxxxxxxx\" \
637 }";
638 cJSON *config1 = cJSON_Parse(testStr1);
639 ASSERT_NE(config1, nullptr);
640 ret = ParseSymbolLinksConfig(nullptr, config1, nullptr);
641 EXPECT_EQ(ret, -1);
642 cJSON_Delete(config1);
643
644 const char testStr2[] = "{ \
645 \"symbol-links\":[\"/xxxx/xxx\", \"/xxx/xxx\"] \
646 }";
647 cJSON *config2 = cJSON_Parse(testStr2);
648 ASSERT_NE(config2, nullptr);
649 cJSON *symbolLinks = cJSON_GetObjectItemCaseSensitive(config2, "symbol-links");
650 ret = ParseSymbolLinksConfig(nullptr, symbolLinks, nullptr);
651 EXPECT_EQ(ret, -1);
652 cJSON_Delete(config2);
653 }
654
655 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_005, TestSize.Level0)
656 {
657 const char testStr1[] = "{ \
658 \"symbol-links\":[{\
659 \"test\":\"/xxxx/xxx\", \
660 \"target-name\":\"/xxx/xxx\"}] \
661 }";
662 cJSON *config1 = cJSON_Parse(testStr1);
663 ASSERT_NE(config1, nullptr);
664 cJSON *symbolLinks = cJSON_GetObjectItemCaseSensitive(config1, "symbol-links");
665 int ret = ParseSymbolLinksConfig(nullptr, symbolLinks, nullptr);
666 EXPECT_EQ(ret, -1);
667 cJSON_Delete(config1);
668
669 const char testStr2[] = "{ \
670 \"symbol-links\":[{\
671 \"test\":\"/xxxx/xxx\", \
672 \"link-name\":\"/xxx/xxx\"}] \
673 }";
674 cJSON *config2 = cJSON_Parse(testStr2);
675 ASSERT_NE(config2, nullptr);
676 symbolLinks = cJSON_GetObjectItemCaseSensitive(config2, "symbol-links");
677 ret = ParseSymbolLinksConfig(nullptr, symbolLinks, nullptr);
678 EXPECT_EQ(ret, -1);
679 cJSON_Delete(config2);
680
681 const char testStr3[] = "{ \
682 \"symbol-links\":[{\
683 \"target-name\":\"/xxxx/xxx\", \
684 \"link-name\":\"/xxx/xxx\"}] \
685 }";
686 cJSON *config3 = cJSON_Parse(testStr3);
687 ASSERT_NE(config3, nullptr);
688 symbolLinks = cJSON_GetObjectItemCaseSensitive(config3, "symbol-links");
689 ret = ParseSymbolLinksConfig(nullptr, symbolLinks, nullptr);
690 EXPECT_EQ(ret, 0);
691 cJSON_Delete(config3);
692 }
693
694 HWTEST_F(AppSpawnSandboxCoverageTest, App_Spawn_Sandbox_load_006, TestSize.Level0)
695 {
696 const char testStr1[] = "{ \
697 \"test\":\"xxxxxxxxx\" \
698 }";
699 cJSON *config1 = cJSON_Parse(testStr1);
700 ASSERT_NE(config1, nullptr);
701 SandboxSection section = {};
702 int ret = ParseGidTableConfig(nullptr, config1, §ion);
703 EXPECT_EQ(ret, 0);
704 cJSON_Delete(config1);
705
706 const char testStr2[] = "{ \
707 \"test\":[] \
708 }";
709 cJSON *config2 = cJSON_Parse(testStr2);
710 ASSERT_NE(config2, nullptr);
711 cJSON *testCfg = cJSON_GetObjectItemCaseSensitive(config2, "test");
712 ret = ParseGidTableConfig(nullptr, testCfg, §ion);
713 EXPECT_EQ(ret, 0);
714 cJSON_Delete(config2);
715
716 const char testStr3[] = "{ \
717 \"gids\":[\"202400\", \"202500\", \"202600\"] \
718 }";
719 cJSON *config3 = cJSON_Parse(testStr3);
720 ASSERT_NE(config3, nullptr);
721 testCfg = cJSON_GetObjectItemCaseSensitive(config3, "gids");
722 ret = ParseGidTableConfig(nullptr, testCfg, §ion);
723 EXPECT_EQ(ret, 0);
724 cJSON_Delete(config3);
725
726 if (section.gidTable != nullptr) {
727 free(section.gidTable);
728 }
729 }
730 }