• 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 "gtest/gtest.h"
17 #include <gmock/gmock.h>
18 #include "cJSON.h"
19 #define private public
20 #include "sandbox_core.h"
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <fstream>
25 #include "sandbox_core.h"
26 #include "lib_func_mock.h"
27 
28 // mock sandbox_core.cpp func
29 #include "lib_func_define.h"
30 #include "sandbox_core.cpp"
31 #include "lib_func_undef.h"
32 
33 #undef private
34 
35 using namespace std;
36 using namespace testing;
37 using namespace testing::ext;
38 using namespace OHOS::AppSpawn;
39 
40 class DebugSandboxTest : public ::testing::Test {
41 public:
42     static void SetUpTestCase(void);
43     static void TearDownTestCase();
SetUp()44     void SetUp() override {};
TearDown()45     void TearDown() override {};
46     static inline shared_ptr<LibraryFuncMock> funcMock = nullptr;
47 };
48 
SetUpTestCase(void)49 void DebugSandboxTest::SetUpTestCase(void)
50 {
51     GTEST_LOG_(INFO) << "SetUpTestCase enter";
52     funcMock = make_shared<LibraryFuncMock>();
53     LibraryFuncMock::libraryFunc_ = funcMock;
54 }
55 
TearDownTestCase()56 void DebugSandboxTest::TearDownTestCase()
57 {
58     GTEST_LOG_(INFO) << "TearDownTestCase enter";
59     LibraryFuncMock::libraryFunc_ = nullptr;
60     funcMock = nullptr;
61 }
62 
63 /**
64  * @tc.name  : DoUninstallDebugSandbox_ShouldRemoveSandbox_WhenConfigIsValid
65  * @tc.number: DoUninstallDebugSandboxTest_001
66  * @tc.desc  : 测试当配置有效时,DoUninstallDebugSandbox 函数能够正确卸载沙箱
67  */
68 HWTEST_F(DebugSandboxTest, ATC_DoUninstallDebugSandbox_ShouldRemoveSandbox_WhenConfigIsValid, TestSize.Level0)
69 {
70     const char testDebugMountPathsJson[] = R"({
71             "mount-paths": [
72                 {
73                     "src-path": "/data/app/el1/<currentUserId>/base/<variablePackageName>",
74                     "sandbox-path": "/data/storage/el1/base",
75                     "sandbox-flags": [ "bind", "rec" ],
76                     "mount-shared-flag": "true",
77                     "check-action-status": "false"
78                 },
79                 {
80                     "src-path": "/data/app/el1/<currentUserId>/database/<variablePackageName>",
81                     "sandbox-path": "/data/storage/el1/database",
82                     "sandbox-flags": [ "bind", "rec" ],
83                     "mount-shared-flag": "true",
84                     "check-action-status": "false"
85                 }
86             ]
87         })";
88 
89     cJSON *config = cJSON_Parse(testDebugMountPathsJson);
90     ASSERT_NE(config, nullptr);
91     std::vector<std::string> bundleList = {"com.example.app"};
92 
93     // 模拟 access 返回 0,表示路径存在
94     EXPECT_CALL(*funcMock, access(_, _)).WillOnce(Return(0)).WillOnce(Return(0));
95 
96     // 模拟 umount2 返回 0,表示卸载成功
97     EXPECT_CALL(*funcMock, umount2(_, _)).WillOnce(Return(0)).WillOnce(Return(0));
98 
99     // 模拟 rmdir 返回 0,表示删除目录成功
100     EXPECT_CALL(*funcMock, rmdir(_)).WillOnce(Return(0)).WillOnce(Return(0));
101 
102     SandboxCore::DoUninstallDebugSandbox(bundleList, config);
103 
104     cJSON_Delete(config);
105 }
106 
107 /**
108  * @tc.name  : DoUninstallDebugSandbox_ShouldNotRemoveSandbox_WhenConfigIsInvalid
109  * @tc.number: DoUninstallDebugSandboxTest_002
110  * @tc.desc  : 测试当配置无效时,DoUninstallDebugSandbox 函数不会卸载沙箱
111  */
112 HWTEST_F(DebugSandboxTest, ATC_DoUninstallDebugSandbox_ShouldNotRemoveSandbox_WhenConfigIsInvalid, TestSize.Level0)
113 {
114     std::vector<std::string> bundleList = {"com.example.app"};
115     cJSON *config = nullptr;
116 
117     SandboxCore::DoUninstallDebugSandbox(bundleList, config);
118 }
119 
120 /**
121  * @tc.name  : DoUninstallDebugSandbox_ShouldNotRemoveSandbox_WhenMountPointIsNotArray
122  * @tc.number: DoUninstallDebugSandboxTest_003
123  * @tc.desc  : 测试当 mountPoints 不是数组时,DoUninstallDebugSandbox 函数不会卸载沙箱
124  */
125 HWTEST_F(DebugSandboxTest, ATC_DoUninstallDebugSandbox_ShouldNotRemoveSandbox_WhenMountPointIsNotArray, TestSize.Level0)
126 {
127     const char testDebugMountPathsJson[] = R"({
128             "mount-paths": {
129                 "src-path": "/data/app/el1/<currentUserId>/base/<variablePackageName>",
130                 "sandbox-path": "/data/storage/el1/base",
131                 "sandbox-flags": [ "bind", "rec" ],
132                 "mount-shared-flag": "true",
133                 "check-action-status": "false"
134             }
135         })";
136 
137     cJSON *config = cJSON_Parse(testDebugMountPathsJson);
138     ASSERT_NE(config, nullptr);
139     std::vector<std::string> bundleList = {"com.example.app"};
140 
141     SandboxCore::DoUninstallDebugSandbox(bundleList, config);
142 
143     cJSON_Delete(config);
144 }
145 
146 /**
147  * @tc.name  : UninstallDebugSandbox_ShouldReturnInvalidArg_WhenPropertyIsNull
148  * @tc.number: UninstallDebugSandboxTest_001
149  * @tc.desc  : 测试当传入的 property 为 nullptr 时,函数应返回 APPSPAWN_ARG_INVALID
150  */
151 HWTEST_F(DebugSandboxTest, ATC_UninstallDebugSandbox_ShouldReturnInvalidArg_WhenPropertyIsNull, TestSize.Level0)
152 {
153     AppSpawningCtx *property = nullptr;
154     AppSpawnMgr content;
155 
156     int32_t result = SandboxCore::UninstallDebugSandbox(&content, property);
157     EXPECT_EQ(result, APPSPAWN_ARG_INVALID);
158 }
159 
160 /**
161  * @tc.name  : UninstallDebugSandbox_ShouldReturnInvalidArg_WhenContentIsNull
162  * @tc.number: UninstallDebugSandboxTest_002
163  * @tc.desc  : 测试当传入的 content 为 nullptr 时,函数应返回 APPSPAWN_ARG_INVALID
164  */
165 HWTEST_F(DebugSandboxTest, ATC_UninstallDebugSandbox_ShouldReturnInvalidArg_WhenContentIsNull, TestSize.Level0)
166 {
167     AppSpawningCtx property;
168     AppSpawnMgr *content = nullptr;
169 
170     int32_t result = SandboxCore::UninstallDebugSandbox(content, &property);
171     EXPECT_EQ(result, APPSPAWN_ARG_INVALID);
172 }