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