• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 AppSpawnDebugSandboxTest : 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 
AppSpawnDebugSandboxTestCreateAppSpawningCtx(int base)64 static AppSpawningCtx *AppSpawnDebugSandboxTestCreateAppSpawningCtx(int base)
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     if (base == 0) {
72         return g_testHelper.GetAppProperty(clientHandle, reqHandle);
73     } else if (base == 1) {
74         // save provision type to req
75         const char *provision = "debug";
76         ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_PROVISION_TYPE, provision);
77         APPSPAWN_CHECK(ret == 0, return nullptr, "Failed to add provision %{public}s", APPSPAWN_SERVER_NAME);
78         return g_testHelper.GetAppProperty(clientHandle, reqHandle);
79     }
80     return nullptr;
81 }
82 
AppSpawnDebugSandboxTestGetSandboxContext(const AppSpawningCtx * property,int nwebspawn)83 static SandboxContext *AppSpawnDebugSandboxTestGetSandboxContext(const AppSpawningCtx *property, int nwebspawn)
84 {
85     AppSpawnMsgFlags *msgFlags = (AppSpawnMsgFlags *)GetAppProperty(property, TLV_MSG_FLAGS);
86     APPSPAWN_CHECK(msgFlags != nullptr, return nullptr, "No msg flags in msg %{public}s", GetProcessName(property));
87 
88     SandboxContext *context = GetSandboxContext();
89     APPSPAWN_CHECK(context != nullptr, return nullptr, "Failed to get context");
90 
91     context->nwebspawn = nwebspawn;
92     context->bundleName = GetBundleName(property);
93     context->bundleHasWps = strstr(context->bundleName, "wps") != nullptr;
94     context->dlpBundle = strcmp(GetProcessName(property), "com.ohos.dlpmanager") == 0;
95     context->appFullMountEnable = 0;
96 
97     context->sandboxSwitch = 1;
98     context->sandboxShared = false;
99     context->message = property->message;
100     context->rootPath = nullptr;
101     context->rootPath = nullptr;
102     return context;
103 }
104 
105 /**
106  * @tc.name: InstallDebugSandbox_ShouldReturnInvalidArg_WhenPropertyIsNull
107  * @tc.desc: 测试当 property 为 NULL 时,函数应返回 APPSPAWN_ARG_INVALID.
108  * @tc.number: InstallDebugSandboxTest_001
109  */
110 HWTEST_F(AppSpawnDebugSandboxTest, ATC_InstallDebugSandbox_ShouldReturnInvalidArg_WhenPropertyIsNull, TestSize.Level0)
111 {
112     AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN);
113     AppSpawningCtx *property = nullptr;
114     int ret = InstallDebugSandbox(mgr, property);
115     EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
116     // delete
117     DeleteAppSpawnMgr(mgr);
118 }
119 
120 /**
121  * @tc.name: InstallDebugSandbox_ShouldReturnInvalidArg_WhenContentIsNull
122  * @tc.desc: 测试当 content 为 NULL 时,函数应返回 APPSPAWN_ARG_INVALID.
123  * @tc.number: InstallDebugSandboxTest_002
124  */
125 HWTEST_F(AppSpawnDebugSandboxTest, ATC_InstallDebugSandbox_ShouldReturnInvalidArg_WhenContentIsNull, TestSize.Level0)
126 {
127     AppSpawnMgr *mgr = nullptr;
128     AppSpawningCtx *property = AppSpawnDebugSandboxTestCreateAppSpawningCtx(0);
129     int ret = InstallDebugSandbox(mgr, property);
130     EXPECT_EQ(ret, APPSPAWN_ARG_INVALID);
131     // delete
132     DeleteAppSpawningCtx(property);
133 }
134 
135 /**
136  * @tc.name: InstallDebugSandbox_ShouldReturnZero_WhenNotDeveloperMode
137  * @tc.desc: 测试当不在开发者模式时,函数应返回 0.
138  * @tc.number: InstallDebugSandboxTest_003
139  */
140 HWTEST_F(AppSpawnDebugSandboxTest, InstallDebugSandbox_ShouldReturnZero_WhenNotDeveloperMode, TestSize.Level0)
141 {
142     AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN);
143     AppSpawningCtx *property = AppSpawnDebugSandboxTestCreateAppSpawningCtx(0);
144     int ret = InstallDebugSandbox(mgr, property);
145     EXPECT_EQ(ret, 0);
146     // delete
147     DeleteAppSpawningCtx(property);
148     DeleteAppSpawnMgr(mgr);
149 }
150 
151 /**
152  * @tc.name: InstallDebugSandbox_ShouldReturnZero_WhenProvisionTypeInvalid
153  * @tc.desc: 测试当 provisionType 无效时,函数应返回 0.
154  * @tc.number: InstallDebugSandboxTest_004
155  */
156 HWTEST_F(AppSpawnDebugSandboxTest, ATC_InstallDebugSandbox_ShouldReturnZero_WhenProvisionTypeInvalid, TestSize.Level0)
157 {
158     AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN);
159     AppSpawningCtx *property = AppSpawnDebugSandboxTestCreateAppSpawningCtx(0);
160     property->client.flags = APP_DEVELOPER_MODE;
161     int ret = InstallDebugSandbox(mgr, property);
162     EXPECT_EQ(ret, 0);
163     // delete
164     DeleteAppSpawningCtx(property);
165     DeleteAppSpawnMgr(mgr);
166 }
167 
168 /**
169  * @tc.name: InstallDebugSandbox_ShouldReturnSandboxInvalid_WhenSandboxConfigNotFound
170  * @tc.desc: 测试当找不到沙箱配置时,函数应返回 APPSPAWN_SANDBOX_INVALID.
171  * @tc.number: InstallDebugSandboxTest_005
172  */
173 HWTEST_F(AppSpawnDebugSandboxTest,
174          InstallDebugSandbox_ShouldReturnSandboxInvalid_WhenSandboxConfigNotFound, TestSize.Level0)
175 {
176     AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN);
177     AppSpawningCtx *property = AppSpawnDebugSandboxTestCreateAppSpawningCtx(1);
178     property->client.flags = APP_DEVELOPER_MODE;
179     int ret = InstallDebugSandbox(mgr, property);
180     EXPECT_EQ(ret, APPSPAWN_SANDBOX_INVALID);
181     // delete
182     DeleteAppSpawningCtx(property);
183     DeleteAppSpawnMgr(mgr);
184 }
185 
186 /**
187  * @tc.name: InstallDebugSandbox_ShouldReturnSystemError_WhenSandboxContextIsNull
188  * @tc.desc: 测试当沙箱上下文获取失败时,函数应返回 APPSPAWN_SYSTEM_ERROR.
189  * @tc.number: InstallDebugSandboxTest_006
190  */
191 HWTEST_F(AppSpawnDebugSandboxTest,
192          InstallDebugSandbox_ShouldReturnSystemError_WhenSandboxContextIsNull, TestSize.Level0)
193 {
194     AppSpawnMgr *mgr = CreateAppSpawnMgr(MODE_FOR_APP_SPAWN);
195     AppSpawningCtx *property = AppSpawnDebugSandboxTestCreateAppSpawningCtx(1);
196     property->client.flags = APP_DEVELOPER_MODE;
197     int ret = PreLoadDebugSandboxCfg(mgr);
198     EXPECT_EQ(ret, 0);
199     ret = InstallDebugSandbox(mgr, property);
200     EXPECT_EQ(ret, 0);
201     // delete
202     DeleteAppSpawningCtx(property);
203     DeleteAppSpawnMgr(mgr);
204 }
205 }