1 /*
2 * Copyright (c) 2023-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 "dlp_file_manager_test.h"
17 #include <cstddef>
18 #include <cstdio>
19 #include <cstring>
20 #include <fcntl.h>
21 #include "c_mock_common.h"
22 #define private public
23 #include "dlp_file_manager.h"
24 #undef private
25 #include "dlp_raw_file.h"
26 #include "dlp_zip_file.h"
27 #include "dlp_permission.h"
28 #include "dlp_permission_log.h"
29
30 namespace OHOS {
31 namespace Security {
32 namespace DlpPermission {
33 using namespace testing::ext;
34 using namespace std;
35 namespace {
36 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpFileManagerTest"};
37 static int g_fdDlp = -1;
38 static const std::string DLP_TEST_DIR = "/data/dlpTest/";
39 }
40
SetUpTestCase()41 void DlpFileManagerTest::SetUpTestCase()
42 {
43 struct stat fstat;
44 if (stat(DLP_TEST_DIR.c_str(), &fstat) != 0) {
45 if (errno == ENOENT) {
46 int32_t ret = mkdir(DLP_TEST_DIR.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
47 if (ret < 0) {
48 DLP_LOG_ERROR(LABEL, "mkdir mount point failed errno %{public}d", errno);
49 return;
50 }
51 } else {
52 DLP_LOG_ERROR(LABEL, "get mount point failed errno %{public}d", errno);
53 return;
54 }
55 }
56 }
57
TearDownTestCase()58 void DlpFileManagerTest::TearDownTestCase()
59 {
60 if (g_fdDlp != -1) {
61 close(g_fdDlp);
62 unlink("/data/fuse_test_dlp.txt");
63 g_fdDlp = -1;
64 }
65 rmdir(DLP_TEST_DIR.c_str());
66 }
67
SetUp()68 void DlpFileManagerTest::SetUp() {}
69
TearDown()70 void DlpFileManagerTest::TearDown() {}
71
72 /**
73 * @tc.name: OperDlpFileNode001
74 * @tc.desc: test add/remove/get dlp file node.
75 * @tc.type: FUNC
76 * @tc.require:
77 */
78 HWTEST_F(DlpFileManagerTest, OperDlpFileNode001, TestSize.Level0)
79 {
80 DLP_LOG_INFO(LABEL, "OperDlpFileNode001");
81
82 std::shared_ptr<DlpFile> filePtr;
83 EXPECT_EQ(filePtr, nullptr);
84 EXPECT_EQ(DlpFileManager::GetInstance().AddDlpFileNode(filePtr), DLP_PARSE_ERROR_VALUE_INVALID);
85 EXPECT_EQ(DlpFileManager::GetInstance().RemoveDlpFileNode(filePtr), DLP_PARSE_ERROR_VALUE_INVALID);
86 filePtr = std::make_shared<DlpZipFile>(1, DLP_TEST_DIR, 0, "txt");
87 ASSERT_NE(filePtr, nullptr);
88 EXPECT_EQ(DlpFileManager::GetInstance().AddDlpFileNode(filePtr), DLP_OK);
89 EXPECT_EQ(DlpFileManager::GetInstance().AddDlpFileNode(filePtr), DLP_PARSE_ERROR_FILE_ALREADY_OPENED);
90 EXPECT_NE(DlpFileManager::GetInstance().GetDlpFile(1), nullptr);
91 EXPECT_EQ(DlpFileManager::GetInstance().GetDlpFile(2), nullptr);
92 EXPECT_EQ(DlpFileManager::GetInstance().RemoveDlpFileNode(filePtr), DLP_OK);
93 EXPECT_EQ(DlpFileManager::GetInstance().GetDlpFile(1), nullptr);
94 DlpFileManager::GetInstance().g_DlpFileMap_[1] = filePtr;
95 EXPECT_EQ(DlpFileManager::GetInstance().GetDlpFile(1), filePtr);
96 DlpFileManager::GetInstance().g_DlpFileMap_.clear();
97 EXPECT_EQ(DlpFileManager::GetInstance().RemoveDlpFileNode(filePtr), DLP_PARSE_ERROR_FILE_NOT_OPENED);
98 }
99
100 /**
101 * @tc.name: OperDlpFileNode002
102 * @tc.desc: test add too many dlp file nodes.
103 * @tc.type: FUNC
104 * @tc.require:
105 */
106 HWTEST_F(DlpFileManagerTest, OperDlpFileNode002, TestSize.Level0)
107 {
108 DLP_LOG_INFO(LABEL, "OperDlpFileNode002");
109
110 std::shared_ptr<DlpFile> openDlpFiles[1000];
111
112 for (int i = 0; i < 1000; i++) {
113 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpZipFile>(i, DLP_TEST_DIR, i, "txt");
114 openDlpFiles[i] = filePtr;
115 EXPECT_EQ(DlpFileManager::GetInstance().AddDlpFileNode(filePtr), DLP_OK);
116 }
117
118 std::shared_ptr<DlpFile> filePtr1 = std::make_shared<DlpZipFile>(1001, DLP_TEST_DIR, 0, "txt");
119 EXPECT_EQ(DlpFileManager::GetInstance().AddDlpFileNode(filePtr1), DLP_PARSE_ERROR_TOO_MANY_OPEN_DLP_FILE);
120
121 for (int i = 0; i < 1000; i++) {
122 EXPECT_EQ(DlpFileManager::GetInstance().RemoveDlpFileNode(openDlpFiles[i]), DLP_OK);
123 }
124 }
125
126 /**
127 * @tc.name: GenerateCertData001
128 * @tc.desc: Generate cert data
129 * @tc.type: FUNC
130 * @tc.require:
131 */
132 HWTEST_F(DlpFileManagerTest, GenerateCertData001, TestSize.Level0)
133 {
134 DLP_LOG_INFO(LABEL, "GenerateCertData001");
135
136 PermissionPolicy policy;
137 struct DlpBlob certData;
138 EXPECT_EQ(DlpFileManager::GetInstance().GenerateCertData(policy, certData), DLP_SERVICE_ERROR_VALUE_INVALID);
139
140 policy.aeskey_ = new (std::nothrow) uint8_t[16];
141 ASSERT_NE(policy.aeskey_, nullptr);
142 policy.aeskeyLen_ = 16;
143 policy.iv_ = new (std::nothrow) uint8_t[16];
144 ASSERT_NE(policy.iv_, nullptr);
145 policy.ivLen_ = 16;
146 policy.hmacKey_ = new (std::nothrow) uint8_t[16];
147 ASSERT_NE(policy.hmacKey_, nullptr);
148 policy.hmacKeyLen_ = 16;
149
150 policy.ownerAccountType_ = CLOUD_ACCOUNT;
151 policy.ownerAccount_ = std::string(DLP_MAX_CERT_SIZE + 1, 'a');
152 policy.ownerAccountId_ = std::string(DLP_MAX_CERT_SIZE + 1, 'a');
153 EXPECT_EQ(DlpFileManager::GetInstance().GenerateCertData(policy, certData), DLP_PARSE_ERROR_VALUE_INVALID);
154
155 policy.ownerAccount_ = "test";
156 policy.ownerAccountId_ = "test";
157 DlpCMockCondition condition;
158 condition.mockSequence = { false, false, true, false, false };
159 SetMockConditions("memcpy_s", condition);
160 int res = DlpFileManager::GetInstance().GenerateCertData(policy, certData);
161 EXPECT_TRUE(res == DLP_OK || res == DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL);
162 DLP_LOG_INFO(LABEL, "GenerateCertData001 %{public}d", GetMockConditionCounts("memcpy_s"));
163 CleanMockConditions();
164 delete[] policy.aeskey_;
165 policy.aeskey_ = nullptr;
166 policy.aeskeyLen_ = 0;
167 delete[] policy.iv_;
168 policy.iv_ = nullptr;
169 policy.ivLen_ = 0;
170 delete[] policy.hmacKey_;
171 policy.hmacKey_ = nullptr;
172 policy.hmacKeyLen_ = 0;
173 }
174
175 /**
176 * @tc.name: PrepareDlpEncryptParms001
177 * @tc.desc: test prepare dlp encrypt params error
178 * @tc.type: FUNC
179 * @tc.require:
180 */
181 HWTEST_F(DlpFileManagerTest, PrepareDlpEncryptParms001, TestSize.Level0)
182 {
183 DLP_LOG_INFO(LABEL, "PrepareDlpEncryptParms001");
184
185 PermissionPolicy policy;
186 policy.aeskey_ = new (std::nothrow) uint8_t[16];
187 ASSERT_NE(policy.aeskey_, nullptr);
188 policy.aeskeyLen_ = 16;
189 policy.iv_ = new (std::nothrow) uint8_t[16];
190 ASSERT_NE(policy.iv_, nullptr);
191 policy.ivLen_ = 16;
192
193 policy.hmacKey_ = new (std::nothrow) uint8_t[16];
194 ASSERT_NE(policy.hmacKey_, nullptr);
195 policy.hmacKeyLen_ = 16;
196
197 policy.ownerAccountType_ = CLOUD_ACCOUNT;
198 policy.ownerAccount_ = "test";
199 policy.ownerAccountId_ = "test";
200 struct DlpBlob key;
201 struct DlpUsageSpec usage;
202 struct DlpBlob certData;
203 struct DlpBlob hmacKey;
204
205 // key create fail
206 DlpCMockCondition condition;
207 condition.mockSequence = { true };
208 SetMockConditions("RAND_bytes", condition);
209 EXPECT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR,
210 DlpFileManager::GetInstance().PrepareDlpEncryptParms(policy, key, usage, certData, hmacKey));
211 CleanMockConditions();
212
213 // iv create fail
214 condition.mockSequence = { false, true };
215 SetMockConditions("RAND_bytes", condition);
216 EXPECT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR,
217 DlpFileManager::GetInstance().PrepareDlpEncryptParms(policy, key, usage, certData, hmacKey));
218 CleanMockConditions();
219
220 // create cert data failed with memcpy_s fail
221 condition.mockSequence = { false, false, false, false, false, false, false, false, false, false, true };
222 SetMockConditions("memcpy_s", condition);
223 int res = DlpFileManager::GetInstance().PrepareDlpEncryptParms(policy, key, usage, certData, hmacKey);
224 EXPECT_TRUE(res == DLP_OK || res == DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL);
225 DLP_LOG_INFO(LABEL, "PrepareDlpEncryptParms001 %{public}d", GetMockConditionCounts("memcpy_s"));
226 CleanMockConditions();
227 delete[] policy.aeskey_;
228 policy.aeskey_ = nullptr;
229 policy.aeskeyLen_ = 0;
230 delete[] policy.iv_;
231 policy.iv_ = nullptr;
232 policy.ivLen_ = 0;
233 delete[] policy.hmacKey_;
234 policy.hmacKey_ = nullptr;
235 policy.hmacKeyLen_ = 0;
236 }
237
238 /**
239 * @tc.name: ParseDlpFileFormat001
240 * @tc.desc: test parse dlp file format error
241 * @tc.type: FUNC
242 * @tc.require:
243 */
244 HWTEST_F(DlpFileManagerTest, ParseDlpFileFormat001, TestSize.Level0)
245 {
246 DLP_LOG_INFO(LABEL, "UpdateDlpFileContentSize001");
247 g_fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
248 ASSERT_NE(g_fdDlp, -1);
249 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpRawFile>(g_fdDlp, "txt");
250 ASSERT_NE(filePtr, nullptr);
251 std::string appId = "test_appId_passed";
252
253 EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR, DlpFileManager::GetInstance().OpenRawDlpFile(-1, filePtr, appId, "txt"));
254
255 struct DlpHeader header = {
256 .magic = DLP_FILE_MAGIC,
257 .txtOffset = sizeof(struct DlpHeader) + 64,
258 .txtSize = 0,
259 .certOffset = sizeof(struct DlpHeader),
260 .certSize = 32,
261 .contactAccountOffset = sizeof(struct DlpHeader) + 32,
262 .contactAccountSize = 32
263 };
264
265 write(g_fdDlp, &header, sizeof(struct DlpHeader));
266 uint8_t buffer[64] = {0};
267 write(g_fdDlp, buffer, 64);
268 lseek(g_fdDlp, 0, SEEK_SET);
269 EXPECT_EQ(DLP_PARSE_ERROR_FILE_FORMAT_ERROR,
270 DlpFileManager::GetInstance().OpenRawDlpFile(g_fdDlp, filePtr, appId, "txt"));
271
272 close(g_fdDlp);
273 unlink("/data/fuse_test_dlp.txt");
274 g_fdDlp = -1;
275 }
276
277 /**
278 * @tc.name: ParseDlpFileFormat002
279 * @tc.desc: test parse dlp file formate error
280 * @tc.type: FUNC
281 * @tc.require:
282 */
283 HWTEST_F(DlpFileManagerTest, ParseDlpFileFormat002, TestSize.Level0)
284 {
285 DLP_LOG_INFO(LABEL, "ParseDlpFileFormat002");
286 g_fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
287 ASSERT_NE(g_fdDlp, -1);
288
289 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpRawFile>(g_fdDlp, "txt");
290 ASSERT_NE(filePtr, nullptr);
291
292 struct DlpHeader header = {
293 .magic = DLP_FILE_MAGIC,
294 .fileType = 10,
295 .offlineAccess = 0,
296 .algType = DLP_MODE_CTR,
297 .txtOffset = sizeof(struct DlpHeader) + 108,
298 .txtSize = 100,
299 .hmacOffset = sizeof(struct DlpHeader) + 208,
300 .hmacSize = 64,
301 .certOffset = sizeof(struct DlpHeader) + 272,
302 .certSize = 256,
303 .contactAccountOffset = sizeof(struct DlpHeader) + 8,
304 .contactAccountSize = 100,
305 .offlineCertOffset = sizeof(struct DlpHeader) + 272,
306 .offlineCertSize = 0
307 };
308 uint32_t version = 3;
309 uint32_t dlpHeaderSize = sizeof(struct DlpHeader);
310 write(g_fdDlp, &version, sizeof(struct DlpHeader));
311 write(g_fdDlp, &dlpHeaderSize, sizeof(struct DlpHeader));
312 uint8_t buffer[800] = {0};
313 write(g_fdDlp, buffer, 800);
314
315 lseek(g_fdDlp, 8, SEEK_SET);
316 write(g_fdDlp, &header, sizeof(struct DlpHeader));
317 std::string certStr = "{\"aeskeyLen\":16, \"aeskey\":\"11223344556677889900112233445566\",\"ivLen\":16,"
318 "\"iv\":\"11223344556677889900112233445566\",\"ownerAccount\":\"test\",\"ownerAccountId\":\"test\","
319 "\"ownerAccountType\":0}";
320 lseek(g_fdDlp, header.certOffset, SEEK_SET);
321 write(g_fdDlp, certStr.c_str(), certStr.length());
322 lseek(g_fdDlp, 0, SEEK_SET);
323 std::string appId = "test_appId_passed";
324 EXPECT_EQ(DLP_PARSE_ERROR_FILE_FORMAT_ERROR,
325 DlpFileManager::GetInstance().OpenRawDlpFile(g_fdDlp, filePtr, appId, "txt"));
326
327 close(g_fdDlp);
328 unlink("/data/fuse_test_dlp.txt");
329 g_fdDlp = -1;
330 }
331
332 /**
333 * @tc.name: ParseDlpFileFormat003
334 * @tc.desc: test parse dlp file formate error
335 * @tc.type: FUNC
336 * @tc.require:
337 */
338 HWTEST_F(DlpFileManagerTest, ParseDlpFileFormat003, TestSize.Level0)
339 {
340 DLP_LOG_INFO(LABEL, "ParseDlpFileFormat003");
341 g_fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
342 ASSERT_NE(g_fdDlp, -1);
343 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpRawFile>(g_fdDlp, "txt");
344 ASSERT_NE(filePtr, nullptr);
345
346 struct DlpHeader header = {
347 .magic = DLP_FILE_MAGIC,
348 .txtOffset = sizeof(struct DlpHeader) + 256 + 32,
349 .txtSize = 0,
350 .certOffset = sizeof(struct DlpHeader),
351 .certSize = 256,
352 .contactAccountOffset = sizeof(struct DlpHeader) + 256,
353 .contactAccountSize = 32
354 };
355
356 write(g_fdDlp, &header, sizeof(struct DlpHeader));
357 std::string certStr = "{\"aeskeyLen\":16, \"aeskey\":\"11223344556677889900112233445566\",\"ivLen\":16,"
358 "\"iv\":\"11223344556677889900112233445566\",\"ownerAccount\":\"test\",\"ownerAccountId\":\"test\","
359 "\"ownerAccountType\":1}";
360 write(g_fdDlp, certStr.c_str(), certStr.length());
361 lseek(g_fdDlp, sizeof(struct DlpHeader) + 256, SEEK_SET);
362 uint8_t buffer[32] = {0};
363 write(g_fdDlp, buffer, 32);
364
365 lseek(g_fdDlp, 0, SEEK_SET);
366
367 // make SetCipher failed
368 DlpCMockCondition condition;
369 condition.mockSequence = { false, false, false, false, false, false, false, true };
370 SetMockConditions("memcpy_s", condition);
371 std::string appId = "test_appId_passed";
372 EXPECT_EQ(DLP_PARSE_ERROR_FILE_FORMAT_ERROR,
373 DlpFileManager::GetInstance().OpenRawDlpFile(g_fdDlp, filePtr, appId, "txt"));
374 CleanMockConditions();
375
376 close(g_fdDlp);
377 unlink("/data/fuse_test_dlp.txt");
378 g_fdDlp = -1;
379 }
380
381 /**
382 * @tc.name: ParseDlpFileFormat004
383 * @tc.desc: test parse dlp file formate error with offineAccess is true
384 * @tc.type: FUNC
385 * @tc.require:
386 */
387 HWTEST_F(DlpFileManagerTest, ParseDlpFileFormat004, TestSize.Level0)
388 {
389 DLP_LOG_INFO(LABEL, "UpdateDlpFileContentSize001");
390 g_fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
391 ASSERT_NE(g_fdDlp, -1);
392 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpRawFile>(g_fdDlp, "txt");
393 ASSERT_NE(filePtr, nullptr);
394 filePtr->SetOfflineAccess(true);
395
396 std::string appId = "test_appId_passed";
397 EXPECT_EQ(DLP_PARSE_ERROR_FILE_FORMAT_ERROR,
398 DlpFileManager::GetInstance().OpenRawDlpFile(g_fdDlp, filePtr, appId, "txt"));
399
400 close(g_fdDlp);
401 unlink("/data/fuse_test_dlp.txt");
402 g_fdDlp = -1;
403 }
404
405 /**
406 * @tc.name: FreeChiperBlob001
407 * @tc.desc: test free chiper blob abnormal branch
408 * @tc.type: FUNC
409 * @tc.require:
410 */
411 HWTEST_F(DlpFileManagerTest, FreeChiperBlob001, TestSize.Level0)
412 {
413 DLP_LOG_INFO(LABEL, "FreeChiperBlob001");
414 struct DlpBlob key = {
415 .data = nullptr,
416 .size = 0
417 };
418 struct DlpBlob certData = {
419 .data = nullptr,
420 .size = 0
421 };
422
423 struct DlpUsageSpec spec = {
424 .algParam = nullptr
425 };
426
427 struct DlpBlob hmacKey = {
428 .data = nullptr,
429 .size = 0
430 };
431
432 // algparm nullptr
433 DlpFileManager::GetInstance().FreeChiperBlob(key, certData, spec, hmacKey);
434
435 // algparm iv nullptr
436 spec.algParam = new (std::nothrow) struct DlpCipherParam;
437 ASSERT_NE(spec.algParam, nullptr);
438 spec.algParam->iv.data = nullptr;
439 DlpFileManager::GetInstance().FreeChiperBlob(key, certData, spec, hmacKey);
440
441 ASSERT_EQ(spec.algParam, nullptr);
442 }
443
444 /**
445 * @tc.name: SetDlpFileParams001
446 * @tc.desc: test set dlp file params with prepare ciper failed
447 * @tc.type: FUNC
448 * @tc.require:
449 */
450 HWTEST_F(DlpFileManagerTest, SetDlpFileParams001, TestSize.Level0)
451 {
452 DLP_LOG_INFO(LABEL, "SetDlpFileParams001");
453 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpZipFile>(1000, DLP_TEST_DIR, 0, "txt");
454 ASSERT_NE(filePtr, nullptr);
455 DlpProperty property;
456
457 // PrepareDlpEncryptParms fail
458 DlpCMockCondition condition;
459 condition.mockSequence = { true };
460 SetMockConditions("RAND_bytes", condition);
461 EXPECT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR,
462 DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property));
463 CleanMockConditions();
464
465 // SetCipher fail
466 property.ownerAccount = "owner";
467 property.ownerAccountId = "owner";
468 property.contactAccount = "owner";
469 property.ownerAccountType = CLOUD_ACCOUNT;
470
471 condition.mockSequence = { false, false, false, false, false, false, false, false, false,
472 false, false, false, false, false, false, false, false, false,
473 false, false, false, false, false, false, true };
474 SetMockConditions("memcpy_s", condition);
475 int res = DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property);
476 EXPECT_TRUE(res == DLP_OK || res == DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL);
477 DLP_LOG_INFO(LABEL, "SetDlpFileParams001 %{public}d", GetMockConditionCounts("memcpy_s"));
478 CleanMockConditions();
479 }
480
481 /**
482 * @tc.name: SetDlpFileParams002
483 * @tc.desc: test set dlp file params with set policy failed
484 * @tc.type: FUNC
485 * @tc.require:
486 */
487 HWTEST_F(DlpFileManagerTest, SetDlpFileParams002, TestSize.Level0)
488 {
489 DLP_LOG_INFO(LABEL, "SetDlpFileParams002");
490 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpZipFile>(1000, DLP_TEST_DIR, 0, "txt");
491 ASSERT_NE(filePtr, nullptr);
492 DlpProperty property;
493
494 // SetPolicy fail
495 property.ownerAccount = "";
496 property.ownerAccountId = "";
497 property.contactAccount = "owner";
498 property.ownerAccountType = CLOUD_ACCOUNT;
499
500 EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID,
501 DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property));
502 }
503
504 /**
505 * @tc.name: SetDlpFileParams003
506 * @tc.desc: test set dlp file params with set cert failed
507 * @tc.type: FUNC
508 * @tc.require:
509 */
510 HWTEST_F(DlpFileManagerTest, SetDlpFileParams003, TestSize.Level0)
511 {
512 DLP_LOG_INFO(LABEL, "SetDlpFileParams003");
513 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpZipFile>(1000, DLP_TEST_DIR, 0, "txt");
514 ASSERT_NE(filePtr, nullptr);
515 DlpProperty property;
516
517 // SetPolicy fail
518 property.ownerAccount = "owner";
519 property.ownerAccountId = "owner";
520 property.contactAccount = "account";
521 property.ownerAccountType = CLOUD_ACCOUNT;
522
523 DlpCMockCondition condition;
524 condition.mockSequence = {
525 false, false, false, false, false, false,
526 false, false, false, false, true
527 };
528 SetMockConditions("memcpy_s", condition);
529 int res = DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property);
530 EXPECT_TRUE(res == DLP_OK || res == DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL);
531 CleanMockConditions();
532 }
533
534 /**
535 * @tc.name: SetDlpFileParams004
536 * @tc.desc: test set dlp file params with contact account empty
537 * @tc.type: FUNC
538 * @tc.require:
539 */
540 HWTEST_F(DlpFileManagerTest, SetDlpFileParams004, TestSize.Level0)
541 {
542 DLP_LOG_INFO(LABEL, "SetDlpFileParams004");
543 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpZipFile>(1000, DLP_TEST_DIR, 0, "txt");
544 ASSERT_NE(filePtr, nullptr);
545 DlpProperty property;
546
547 // SetPolicy fail
548 property.ownerAccount = "owner";
549 property.ownerAccountId = "owner";
550 property.contactAccount = "";
551 property.ownerAccountType = CLOUD_ACCOUNT;
552
553 EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID,
554 DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property));
555 CleanMockConditions();
556 }
557
558 /**
559 * @tc.name: GenerateDlpFile001
560 * @tc.desc: test generate dlp file with wrong params
561 * @tc.type: FUNC
562 * @tc.require:
563 */
564 HWTEST_F(DlpFileManagerTest, GenerateDlpFile001, TestSize.Level0)
565 {
566 DLP_LOG_INFO(LABEL, "GenerateDlpFile001");
567 std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpZipFile>(1000, DLP_TEST_DIR, 0, "txt");
568 ASSERT_NE(filePtr, nullptr);
569 DlpProperty property;
570 property.ownerAccount = "owner";
571 property.ownerAccountId = "owner";
572 property.contactAccount = "owner";
573 property.ownerAccountType = DOMAIN_ACCOUNT;
574
575 EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
576 DlpFileManager::GetInstance().GenerateDlpFile(-1, 1000, property, filePtr, DLP_TEST_DIR));
577
578 EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
579 DlpFileManager::GetInstance().GenerateDlpFile(1000, -1, property, filePtr, DLP_TEST_DIR));
580
581 DlpFileManager::GetInstance().AddDlpFileNode(filePtr);
582 EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL,
583 DlpFileManager::GetInstance().GenerateDlpFile(1000, 1000, property, filePtr, DLP_TEST_DIR));
584 DlpFileManager::GetInstance().RemoveDlpFileNode(filePtr);
585
586 DlpFileManager::DlpFileMes dlpFileMes;
587 DlpProperty rawProperty;
588 std::shared_ptr<DlpFile> rawFilePtr = std::make_shared<DlpRawFile>(1000, "mp4");
589 DlpFileManager::GetInstance().GenRawDlpFile(dlpFileMes, rawProperty, rawFilePtr);
590
591 std::vector<uint8_t> offlineCert;
592 DlpFileManager::GetInstance().DlpRawHmacCheckAndUpdata(rawFilePtr, offlineCert);
593 }
594
595 /**
596 * @tc.name: GenerateDlpFile002
597 * @tc.desc: test generate dlp file with wrong property
598 * @tc.type: FUNC
599 * @tc.require:
600 */
601 HWTEST_F(DlpFileManagerTest, GenerateDlpFile002, TestSize.Level0)
602 {
603 DLP_LOG_INFO(LABEL, "GenerateDlpFile002");
604 std::shared_ptr<DlpFile> filePtr;
605 DlpProperty property;
606 property.ownerAccount = "";
607 property.ownerAccountId = "";
608 property.contactAccount = "owner";
609 property.ownerAccountType = CLOUD_ACCOUNT;
610
611 int plainFileFd = open("/data/file_test.txt", O_CREAT | O_RDWR | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
612 char buffer[] = "123456";
613 ASSERT_NE(write(plainFileFd, buffer, sizeof(buffer)), -1);
614 EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
615 DlpFileManager::GetInstance().GenerateDlpFile(plainFileFd, 1000, property, filePtr, DLP_TEST_DIR));
616 close(plainFileFd);
617 }
618
619 /**
620 * @tc.name: GenerateDlpFile003
621 * @tc.desc: test generate dlp file with generate real file failed
622 * @tc.type: FUNC
623 * @tc.require:
624 */
625 HWTEST_F(DlpFileManagerTest, GenerateDlpFile003, TestSize.Level0)
626 {
627 DLP_LOG_INFO(LABEL, "GenerateDlpFile003");
628 std::shared_ptr<DlpFile> filePtr;
629 DlpProperty property;
630 property.ownerAccount = "owner";
631 property.ownerAccountId = "owner";
632 property.contactAccount = "owner";
633 property.ownerAccountType = CLOUD_ACCOUNT;
634
635 int plainFileFd = open("/data/file_test.txt", O_CREAT | O_RDWR | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
636 char buffer[] = "123456";
637 ASSERT_NE(write(plainFileFd, buffer, sizeof(buffer)), -1);
638 EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
639 DlpFileManager::GetInstance().GenerateDlpFile(plainFileFd, 1000, property, filePtr, DLP_TEST_DIR));
640 close(plainFileFd);
641 }
642
643 /**
644 * @tc.name: OpenDlpFile001
645 * @tc.desc: test open dlp file params with wrong params
646 * @tc.type: FUNC
647 * @tc.require:
648 */
649 HWTEST_F(DlpFileManagerTest, OpenDlpFile001, TestSize.Level0)
650 {
651 DLP_LOG_INFO(LABEL, "OpenDlpFile001");
652 std::shared_ptr<DlpFile> filePtr;
653 DlpProperty property;
654 property.ownerAccount = "owner";
655 property.ownerAccountId = "owner";
656 property.contactAccount = "owner";
657 property.ownerAccountType = CLOUD_ACCOUNT;
658 std::string appId = "test_appId_passed";
659 std::string appIdFake = "test_appId_failed";
660
661 EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
662 DlpFileManager::GetInstance().OpenDlpFile(-1, filePtr, "", appId));
663 EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
664 DlpFileManager::GetInstance().OpenDlpFile(-1, filePtr, "", appIdFake));
665
666 int plainFileFd = open("/data/file_test.txt", O_CREAT | O_RDWR | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
667 int dlpFileFd = open("/data/file_test.txt.dlp", O_CREAT | O_RDWR | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
668 char buffer[] = "123456";
669 ASSERT_NE(write(plainFileFd, buffer, sizeof(buffer)), -1);
670
671 ASSERT_NE(DLP_PARSE_ERROR_FD_ERROR,
672 DlpFileManager::GetInstance().GenerateDlpFile(plainFileFd, dlpFileFd, property, filePtr, DLP_TEST_DIR));
673 close(plainFileFd);
674
675 std::shared_ptr<DlpFile> filePtr1 = std::make_shared<DlpZipFile>(dlpFileFd, DLP_TEST_DIR, 0, "txt");
676 ASSERT_NE(filePtr1, nullptr);
677 DlpFileManager::GetInstance().RemoveDlpFileNode(filePtr1);
678 DlpFileManager::GetInstance().AddDlpFileNode(filePtr1);
679 EXPECT_EQ(DLP_OK,
680 DlpFileManager::GetInstance().OpenDlpFile(dlpFileFd, filePtr, "", appId));
681 EXPECT_EQ(filePtr1, filePtr);
682 DlpFileManager::GetInstance().RemoveDlpFileNode(filePtr1);
683
684 EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL,
685 DlpFileManager::GetInstance().OpenDlpFile(dlpFileFd, filePtr, "", appId));
686 close(dlpFileFd);
687 }
688
689 /**
690 * @tc.name: CloseDlpFile001
691 * @tc.desc: test close dlp file with wrong params
692 * @tc.type: FUNC
693 * @tc.require:
694 */
695 HWTEST_F(DlpFileManagerTest, CloseDlpFile001, TestSize.Level0)
696 {
697 DLP_LOG_INFO(LABEL, "CloseDlpFile001");
698 EXPECT_EQ(DLP_PARSE_ERROR_PTR_NULL,
699 DlpFileManager::GetInstance().CloseDlpFile(nullptr));
700 }
701
702 /**
703 * @tc.name: RecoverDlpFile001
704 * @tc.desc: test close dlp file with wrong params
705 * @tc.type: FUNC
706 * @tc.require:
707 */
708 HWTEST_F(DlpFileManagerTest, RecoverDlpFile001, TestSize.Level0)
709 {
710 DLP_LOG_INFO(LABEL, "RecoverDlpFile001");
711 std::shared_ptr<DlpFile> filePtr = nullptr;
712 EXPECT_EQ(DLP_PARSE_ERROR_PTR_NULL,
713 DlpFileManager::GetInstance().RecoverDlpFile(filePtr, 1000));
714
715 filePtr = std::make_shared<DlpZipFile>(1000, DLP_TEST_DIR, 0, "txt");
716 ASSERT_NE(filePtr, nullptr);
717 EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
718 DlpFileManager::GetInstance().RecoverDlpFile(filePtr, -1));
719 }
720
721 /**
722 * @tc.name: CleanTempBlob001
723 * @tc.desc: test param tagIv whether pointer is null
724 * @tc.type: FUNC
725 * @tc.require:issue:IAIFTY
726 */
727 HWTEST_F(DlpFileManagerTest, CleanTempBlob001, TestSize.Level0)
728 {
729 DLP_LOG_INFO(LABEL, "CleanTempBlob001");
730
731 DlpBlob key;
732 DlpCipherParam* tagIv;
733 DlpBlob hmacKey;
734 uint8_t g_iv[2] = { 0x90, 0xd5 };
735 hmacKey.data = g_iv;
736 ASSERT_TRUE(hmacKey.data != nullptr);
737
738 DlpFileManager::GetInstance().CleanTempBlob(key, &tagIv, hmacKey);
739 ASSERT_TRUE(key.data == nullptr);
740 ASSERT_TRUE(tagIv == nullptr);
741
742 tagIv = new (std::nothrow) struct DlpCipherParam;
743 ASSERT_NE(tagIv, nullptr);
744 tagIv->iv.data = g_iv;
745 ASSERT_TRUE(tagIv->iv.data != nullptr);
746 DlpFileManager::GetInstance().CleanTempBlob(key, &tagIv, hmacKey);
747 ASSERT_TRUE(tagIv == nullptr);
748 }
749
750 /**
751 * @tc.name: GenerateCertBlob001
752 * @tc.desc: test param whether cert and certData are empty
753 * @tc.type: FUNC
754 * @tc.require:issue:IAIFTY
755 */
756 HWTEST_F(DlpFileManagerTest, GenerateCertBlob001, TestSize.Level0)
757 {
758 DLP_LOG_INFO(LABEL, "GenerateCertBlob001");
759
760 std::vector<uint8_t> cert;
761 struct DlpBlob certData;
762 certData.data = new (std::nothrow) uint8_t[15];
763 ASSERT_TRUE(cert.size() == 0);
764 EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID,
765 DlpFileManager::GetInstance().GenerateCertBlob(cert, certData));
766
767 cert.push_back(1);
768 ASSERT_TRUE(certData.data != nullptr);
769 EXPECT_EQ(DLP_OK, DlpFileManager::GetInstance().GenerateCertBlob(cert, certData));
770 delete[] certData.data;
771 certData.data = nullptr;
772 certData.size = 0;
773 }
774 } // namespace DlpPermission
775 } // namespace Security
776 } // namespace OHOS