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
17 #include "access_core.h"
18
19 #include <gtest/gtest.h>
20 #include <sys/xattr.h>
21
22 namespace OHOS::FileManagement::ModuleFileIO::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26 const mode_t DIR_PERMISSIONS = 0755;
27
28 class AccessCoreTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 const string CLOUDDISK_FILE_PREFIX = "/data/storage/el2/cloud";
35 const string DISTRIBUTED_FILE_PREFIX = "/data/storage/el2/distributedfiles";
36 const string CLOUD_FILE_LOCATION = "user.cloud.location";
37 const string POSITION_LOCAL = "1";
38 const string POSITION_BOTH = "2";
39 };
40
SetUpTestCase(void)41 void AccessCoreTest::SetUpTestCase(void)
42 {
43 GTEST_LOG_(INFO) << "SetUpTestCase";
44 }
45
TearDownTestCase(void)46 void AccessCoreTest::TearDownTestCase(void)
47 {
48 GTEST_LOG_(INFO) << "TearDownTestCase";
49 }
50
SetUp(void)51 void AccessCoreTest::SetUp(void)
52 {
53 GTEST_LOG_(INFO) << "SetUp";
54 }
55
TearDown(void)56 void AccessCoreTest::TearDown(void)
57 {
58 GTEST_LOG_(INFO) << "TearDown";
59 }
60
CreateDirectoryRecursive(const std::string & path)61 bool CreateDirectoryRecursive(const std::string& path) {
62 if (path.empty()) {
63 return false;
64 }
65
66 size_t pos = 0;
67 std::string dir;
68 if (path[0] == '/') {
69 dir += '/';
70 pos++;
71 }
72
73 while ((pos = path.find('/', pos)) != std::string::npos) {
74 dir = path.substr(0, pos++);
75 if (dir.empty()) {
76 continue;
77 }
78 if (mkdir(dir.c_str(), DIR_PERMISSIONS) == -1) {
79 if (errno != EEXIST) {
80 return false;
81 }
82 }
83 }
84
85 if (mkdir(path.c_str(), DIR_PERMISSIONS) == -1 && errno != EEXIST) {
86 return false;
87 }
88 return true;
89 }
90
91 /**
92 * @tc.name: AccessCoreTest_DoAccess_001
93 * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR.
94 * @tc.size: MEDIUM
95 * @tc.type: FUNC
96 * @tc.level Level 1
97 */
98 HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_001, testing::ext::TestSize.Level1)
99 {
100 GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_001";
101
102 std::string path;
103 std::optional<AccessModeType> mode;
104
105 auto res = AccessCore::DoAccess(path, mode);
106 EXPECT_EQ(res.IsSuccess(), false);
107
108 GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_001";
109 }
110
111 /**
112 * @tc.name: AccessCoreTest_DoAccess_002
113 * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR.
114 * @tc.size: MEDIUM
115 * @tc.type: FUNC
116 * @tc.level Level 1
117 */
118 HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_002, testing::ext::TestSize.Level1)
119 {
120 GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_002";
121
122 std::string path = "";
123 AccessModeType mode = AccessModeType::EXIST;
124 AccessFlag flag = DEFAULT_FLAG;
125
126 auto res = AccessCore::DoAccess(path, mode, flag);
127 EXPECT_EQ(res.IsSuccess(), false);
128
129 GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_002";
130 }
131
132 /**
133 * @tc.name: AccessCoreTest_DoAccess_003
134 * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR.
135 * @tc.size: MEDIUM
136 * @tc.type: FUNC
137 * @tc.level Level 1
138 */
139 HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_003, testing::ext::TestSize.Level1)
140 {
141 GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_003";
142
143 std::string path = "test";
144 std::optional<AccessModeType> mode = std::make_optional<AccessModeType>(AccessModeType::ERROR);
145
146 auto res = AccessCore::DoAccess(path, mode);
147 EXPECT_EQ(res.IsSuccess(), false);
148
149 GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_003";
150 }
151
152 /**
153 * @tc.name: AccessCoreTest_DoAccess_004
154 * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS.
155 * @tc.size: MEDIUM
156 * @tc.type: FUNC
157 * @tc.level Level 1
158 */
159 HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_004, testing::ext::TestSize.Level1)
160 {
161 GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_004";
162
163 std::string path = CLOUDDISK_FILE_PREFIX;
164 AccessModeType mode = AccessModeType::EXIST;
165 AccessFlag flag = LOCAL_FLAG;
166
167 auto res = AccessCore::DoAccess(path, mode, flag);
168 EXPECT_EQ(res.IsSuccess(), true);
169
170 GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_004";
171 }
172
173 /**
174 * @tc.name: AccessCoreTest_DoAccess_005
175 * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR.
176 * @tc.size: MEDIUM
177 * @tc.type: FUNC
178 * @tc.level Level 1
179 */
180 HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_005, testing::ext::TestSize.Level1)
181 {
182 GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_005";
183
184 std::string path = CLOUDDISK_FILE_PREFIX;
185 AccessModeType mode = AccessModeType::EXIST;
186 AccessFlag flag = LOCAL_FLAG;
187
188 ASSERT_TRUE(CreateDirectoryRecursive(path));
189 auto re = setxattr(path.c_str(), CLOUD_FILE_LOCATION.c_str(), POSITION_LOCAL.c_str(), POSITION_LOCAL.size(), 0);
190 ASSERT_NE(re, -1);
191
192 auto res = AccessCore::DoAccess(path, mode, flag);
193 EXPECT_EQ(res.IsSuccess(), true);
194
195 GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_005";
196 }
197
198 /**
199 * @tc.name: AccessCoreTest_DoAccess_006
200 * @tc.desc: Test function of AccessCore::DoAccess interface for SUCCESS.
201 * @tc.size: MEDIUM
202 * @tc.type: FUNC
203 * @tc.level Level 1
204 */
205 HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_006, testing::ext::TestSize.Level1)
206 {
207 GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_006";
208
209 std::string path = "AccessCoreTest";
210 AccessModeType mode = AccessModeType::EXIST;
211 AccessFlag flag = LOCAL_FLAG;
212
213 auto res = AccessCore::DoAccess(path, mode, flag);
214 EXPECT_EQ(res.IsSuccess(), true);
215
216 GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_006";
217 }
218
219 /**
220 * @tc.name: AccessCoreTest_DoAccess_007
221 * @tc.desc: Test function of AccessCore::DoAccess interface for ERROR.
222 * @tc.size: MEDIUM
223 * @tc.type: FUNC
224 * @tc.level Level 1
225 */
226 HWTEST_F(AccessCoreTest, AccessCoreTest_DoAccess_007, testing::ext::TestSize.Level1)
227 {
228 GTEST_LOG_(INFO) << "AccessCoreTest-begin AccessCoreTest_DoAccess_007";
229
230 std::string path = CLOUDDISK_FILE_PREFIX;
231 AccessModeType mode = AccessModeType::EXIST;
232 AccessFlag flag = LOCAL_FLAG;
233
234 ASSERT_TRUE(CreateDirectoryRecursive(path));
235 auto re = setxattr(path.c_str(), CLOUD_FILE_LOCATION.c_str(), POSITION_BOTH.c_str(), POSITION_BOTH.size(), 0);
236 ASSERT_NE(re, -1);
237
238 auto res = AccessCore::DoAccess(path, mode, flag);
239 EXPECT_EQ(res.IsSuccess(), true);
240
241 GTEST_LOG_(INFO) << "AccessCoreTest-end AccessCoreTest_DoAccess_007";
242 }
243
244 } // OHOS::FileManagement::ModuleFileIO::Test