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 "dlp_utils_test.h"
17 #include <cstdio>
18 #include <cstring>
19 #include <fcntl.h>
20 #include <iostream>
21 #include <fstream>
22 #include <thread>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include "dlp_file.h"
26 #include "dlp_file_manager.h"
27 #include "dlp_permission.h"
28 #include "dlp_permission_log.h"
29 #include "dlp_utils.h"
30 #include "c_mock_common.h"
31
32 using namespace testing::ext;
33 using namespace OHOS::Security::DlpPermission;
34 using namespace std;
35
36 namespace {
37 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpUtilsTest"};
38 }
39
SetUpTestCase()40 void DlpUtilsTest::SetUpTestCase() {}
41
TearDownTestCase()42 void DlpUtilsTest::TearDownTestCase() {}
43
SetUp()44 void DlpUtilsTest::SetUp() {}
45
TearDown()46 void DlpUtilsTest::TearDown() {}
47
48 /**
49 * @tc.name: GetBundleMgrProxy001
50 * @tc.desc: test GetBundleMgrProxy
51 * @tc.type: FUNC
52 * @tc.require:
53 */
54 HWTEST_F(DlpUtilsTest, GetBundleMgrProxy, TestSize.Level0)
55 {
56 DLP_LOG_INFO(LABEL, "GetBundleMgrProxy");
57 auto bundleMgrProxy = DlpUtils::GetBundleMgrProxy();
58 if (bundleMgrProxy == nullptr) {
59 ASSERT_EQ(bundleMgrProxy, nullptr);
60 } else {
61 ASSERT_NE(bundleMgrProxy, nullptr);
62 }
63 }
64
65 /**
66 * @tc.name: GetAuthPolicyWithType001
67 * @tc.desc: test GetAuthPolicyWithType
68 * @tc.type: FUNC
69 * @tc.require:
70 */
71 HWTEST_F(DlpUtilsTest, GetAuthPolicyWithType, TestSize.Level0)
72 {
73 DLP_LOG_INFO(LABEL, "GetAuthPolicyWithType");
74 std::vector<std::string> authPolicy;
75 std::string fileType = "txt";
76 DlpUtils::GetAuthPolicyWithType(DLP_AUTH_POLICY, fileType, authPolicy);
77 fileType = "dlp";
78 DlpUtils::GetAuthPolicyWithType(DLP_AUTH_POLICY, fileType, authPolicy);
79 fileType = "pdf";
80 DlpUtils::GetAuthPolicyWithType(DLP_AUTH_POLICY, fileType, authPolicy);
81 fileType = "wer";
82 bool ret = DlpUtils::GetAuthPolicyWithType(DLP_AUTH_POLICY, fileType, authPolicy);
83 if (ret == false) {
84 ASSERT_EQ(ret, false);
85 } else {
86 ASSERT_EQ(ret, true);
87 }
88 }
89
90 /**
91 * @tc.name: GetFileTypeBySuffix001
92 * @tc.desc: test GetFileTypeBySuffix
93 * @tc.type: FUNC
94 * @tc.require:
95 */
96 HWTEST_F(DlpUtilsTest, GetFileTypeBySuffix, TestSize.Level0)
97 {
98 DLP_LOG_INFO(LABEL, "GetFileTypeBySuffix");
99 std::string fileType = "txt";
100 DlpUtils::GetFileTypeBySuffix(fileType, true);
101 fileType = "pdf";
102 DlpUtils::GetFileTypeBySuffix(fileType, true);
103 fileType = "pic";
104 DlpUtils::GetFileTypeBySuffix(fileType, true);
105 fileType = "svg";
106 DlpUtils::GetFileTypeBySuffix(fileType, true);
107 fileType = "txt";
108 DlpUtils::GetFileTypeBySuffix(fileType, false);
109 fileType = "pdf";
110 DlpUtils::GetFileTypeBySuffix(fileType, false);
111 fileType = "pic";
112 DlpUtils::GetFileTypeBySuffix(fileType, false);
113 fileType = "svg";
114 DlpUtils::GetFileTypeBySuffix(fileType, false);
115 fileType = "aaaa";
116 ASSERT_EQ(DlpUtils::GetFileTypeBySuffix(fileType, false), "");
117 }
118
119 /**
120 * @tc.name: GetDlpFileRealSuffix001
121 * @tc.desc: test GetDlpFileRealSuffix
122 * @tc.type: FUNC
123 * @tc.require:
124 */
125 HWTEST_F(DlpUtilsTest, GetDlpFileRealSuffix, TestSize.Level0)
126 {
127 DLP_LOG_INFO(LABEL, "GetDlpFileRealSuffix");
128 bool isFromUriName = true;
129 std::string fileType = "txt";
130 DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName);
131 fileType = "pdf";
132 DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName);
133 fileType = "pic";
134 DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName);
135 fileType = "svg.dlp";
136 DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName);
137 isFromUriName = false;
138 fileType = "txt";
139 DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName);
140 fileType = "pdf.dlp";
141 DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName);
142 fileType = "pic";
143 DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName);
144 fileType = "svg";
145 DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName);
146 fileType = "aaaa";
147 ASSERT_EQ(DlpUtils::GetDlpFileRealSuffix(fileType, isFromUriName), "");
148 }
149
150 /**
151 * @tc.name: GetFileNameWithFd001
152 * @tc.desc: test GetFileNameWithFd
153 * @tc.type: FUNC
154 * @tc.require:
155 */
156 HWTEST_F(DlpUtilsTest, GetFileNameWithFd, TestSize.Level0)
157 {
158 DLP_LOG_INFO(LABEL, "GetFileNameWithFd");
159 std::string fileName;
160 int32_t fd = 0;
161 DlpUtils::GetFileNameWithFd(fd, fileName);
162 fd = 1;
163 DlpUtils::GetFileNameWithFd(fd, fileName);
164 fd = 2;
165 DlpUtils::GetFileNameWithFd(fd, fileName);
166 fd = -1;
167 DlpUtils::GetFileNameWithFd(fd, fileName);
168 fd = 3;
169 DlpUtils::GetFileNameWithFd(fd, fileName);
170 fd = 0;
171 ASSERT_NE(DlpUtils::GetFileNameWithFd(fd, fileName), DLP_PARSE_ERROR_CIPHER_PARAMS_INVALID);
172 }
173
174 /**
175 * @tc.name: GetFileNameWithDlpFd001
176 * @tc.desc: test GetFileNameWithDlpFd
177 * @tc.type: FUNC
178 * @tc.require:
179 */
180 HWTEST_F(DlpUtilsTest, GetFileNameWithDlpFd, TestSize.Level0)
181 {
182 DLP_LOG_INFO(LABEL, "GetFileNameWithDlpFd");
183 std::string fileName;
184 int32_t fd = 0;
185 DlpUtils::GetFileNameWithDlpFd(fd, fileName);
186 fd = 1;
187 DlpUtils::GetFileNameWithDlpFd(fd, fileName);
188 fd = 2;
189 DlpUtils::GetFileNameWithDlpFd(fd, fileName);
190 fd = -1;
191 DlpUtils::GetFileNameWithDlpFd(fd, fileName);
192 fd = 3;
193 DlpUtils::GetFileNameWithDlpFd(fd, fileName);
194 fd = 0;
195 ASSERT_NE(DlpUtils::GetFileNameWithDlpFd(fd, fileName), DLP_PARSE_ERROR_CIPHER_PARAMS_INVALID);
196 }
197
198 /**
199 * @tc.name: GetRealTypeWithRawFile001
200 * @tc.desc: test GetRealTypeWithRawFile
201 * @tc.type: FUNC
202 * @tc.require:
203 */
204 HWTEST_F(DlpUtilsTest, GetRealTypeWithRawFile, TestSize.Level0)
205 {
206 DLP_LOG_INFO(LABEL, "GetRealTypeWithRawFile");
207 int32_t fd = 0;
208 DlpUtils::GetRealTypeWithRawFile(fd);
209 fd = 1;
210 DlpUtils::GetRealTypeWithRawFile(fd);
211 fd = 2;
212 DlpUtils::GetRealTypeWithRawFile(fd);
213 fd = -1;
214 DlpUtils::GetRealTypeWithRawFile(fd);
215 fd = 3;
216 DlpUtils::GetRealTypeWithRawFile(fd);
217 fd = 0;
218 ASSERT_NE(DlpUtils::GetRealTypeWithRawFile(fd), "txt");
219 }
220
221 /**
222 * @tc.name: GetFilePathWithFd001
223 * @tc.desc: test GetFilePathWithFd
224 * @tc.type: FUNC
225 * @tc.require:
226 */
227 HWTEST_F(DlpUtilsTest, GetFilePathWithFd, TestSize.Level0)
228 {
229 DLP_LOG_INFO(LABEL, "GetFilePathWithFd");
230 std::string srcFilePath;
231 int32_t fd = 0;
232 DlpUtils::GetFilePathWithFd(fd, srcFilePath);
233 fd = 1;
234 DlpUtils::GetFilePathWithFd(fd, srcFilePath);
235 fd = 2;
236 DlpUtils::GetFilePathWithFd(fd, srcFilePath);
237 fd = -1;
238 ASSERT_NE(DlpUtils::GetFilePathWithFd(fd, srcFilePath), DLP_PARSE_ERROR_CIPHER_PARAMS_INVALID);
239 }
240
241 /**
242 * @tc.name: GetRealTypeForEnterpriseWithFd001
243 * @tc.desc: test GetRealTypeForEnterpriseWithFd
244 * @tc.type: FUNC
245 * @tc.require:
246 */
247 HWTEST_F(DlpUtilsTest, GetRealTypeForEnterpriseWithFd001, TestSize.Level0)
248 {
249 DLP_LOG_INFO(LABEL, "GetRealTypeForEnterpriseWithFd001");
250
251 int fd = open("/data/fuse_test.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
252 ASSERT_NE(fd, -1);
253 struct DlpHeader header = {
254 .magic = DLP_FILE_MAGIC,
255 .certSize = 20,
256 .contactAccountSize = 20,
257 .fileType = 1,
258 };
259 uint8_t buffer[8] = {0};
260 write(fd, buffer, 8);
261 write(fd, &header, sizeof(header));
262 lseek(fd, 0, SEEK_SET);
263 bool isFromUriName = true;
264 DlpUtils::GetRealTypeForEnterpriseWithFd(fd, isFromUriName);
265 close(fd);
266 unlink("/data/fuse_test.txt.dlp");
267 }
268
269 /**
270 * @tc.name: GetRealTypeForEnterpriseWithFd002
271 * @tc.desc: test GetRealTypeForEnterpriseWithFd
272 * @tc.type: FUNC
273 * @tc.require:
274 */
275 HWTEST_F(DlpUtilsTest, GetRealTypeForEnterpriseWithFd002, TestSize.Level0)
276 {
277 DLP_LOG_INFO(LABEL, "GetRealTypeForEnterpriseWithFd002");
278 int fd = open("/data/fuse_test.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
279 ASSERT_NE(fd, -1);
280 struct DlpHeader header = {
281 .magic = DLP_FILE_MAGIC,
282 .certSize = 20,
283 .contactAccountSize = 20,
284 .fileType = 5,
285 };
286 uint8_t buffer[8] = {0};
287 write(fd, buffer, 8);
288 write(fd, &header, sizeof(header));
289 lseek(fd, 0, SEEK_SET);
290 bool isFromUriName = true;
291 DlpUtils::GetRealTypeForEnterpriseWithFd(fd, isFromUriName);
292 close(fd);
293 unlink("/data/fuse_test.txt.dlp");
294 }
295
296 /**
297 * @tc.name: GetRealTypeForEnterpriseWithFd003
298 * @tc.desc: test GetRealTypeForEnterpriseWithFd
299 * @tc.type: FUNC
300 * @tc.require:
301 */
302 HWTEST_F(DlpUtilsTest, GetRealTypeForEnterpriseWithFd003, TestSize.Level0)
303 {
304 DLP_LOG_INFO(LABEL, "GetRealTypeForEnterpriseWithFd003");
305 int fd = open("/data/fuse_test.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
306 ASSERT_NE(fd, -1);
307 struct DlpHeader header = {
308 .magic = DLP_FILE_MAGIC,
309 .certSize = 20,
310 .contactAccountSize = 20,
311 .fileType = 1000,
312 };
313 uint8_t buffer[8] = {0};
314 write(fd, buffer, 8);
315 write(fd, &header, sizeof(header));
316 lseek(fd, 0, SEEK_SET);
317 bool isFromUriName = true;
318 DlpUtils::GetRealTypeForEnterpriseWithFd(fd, isFromUriName);
319 close(fd);
320 unlink("/data/fuse_test.txt.dlp");
321 }
322
323 /**
324 * @tc.name: GetRealTypeForEnterpriseWithFd004
325 * @tc.desc: test GetRealTypeForEnterpriseWithFd
326 * @tc.type: FUNC
327 * @tc.require:
328 */
329 HWTEST_F(DlpUtilsTest, GetRealTypeForEnterpriseWithFd004, TestSize.Level0)
330 {
331 DLP_LOG_INFO(LABEL, "GetRealTypeForEnterpriseWithFd004");
332 int fd = open("/data/fuse_test.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
333 ASSERT_NE(fd, -1);
334 struct DlpHeader header = {
335 .magic = DLP_FILE_MAGIC,
336 .certSize = 20,
337 .contactAccountSize = 20,
338 .fileType = 1000,
339 };
340 uint8_t buffer[8] = {0};
341 write(fd, buffer, 8);
342 write(fd, &header, sizeof(header) - 1);
343 lseek(fd, 0, SEEK_SET);
344 bool isFromUriName = true;
345 DlpUtils::GetRealTypeForEnterpriseWithFd(fd, isFromUriName);
346 close(fd);
347 unlink("/data/fuse_test.txt.dlp");
348 }