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 }
163 
164 /**
165  * @tc.name: PrepareDlpEncryptParms001
166  * @tc.desc: test prepare dlp encrypt params error
167  * @tc.type: FUNC
168  * @tc.require:
169  */
170 HWTEST_F(DlpFileManagerTest, PrepareDlpEncryptParms001, TestSize.Level1)
171 {
172     DLP_LOG_INFO(LABEL, "PrepareDlpEncryptParms001");
173 
174     PermissionPolicy policy;
175     policy.aeskey_ = new (std::nothrow) uint8_t[16];
176     ASSERT_NE(policy.aeskey_, nullptr);
177     policy.aeskeyLen_ = 16;
178     policy.iv_ = new (std::nothrow) uint8_t[16];
179     ASSERT_NE(policy.iv_, nullptr);
180     policy.ivLen_ = 16;
181 
182     policy.hmacKey_ = new (std::nothrow) uint8_t[16];
183     ASSERT_NE(policy.hmacKey_, nullptr);
184     policy.hmacKeyLen_ = 16;
185 
186     policy.ownerAccountType_ = CLOUD_ACCOUNT;
187     policy.ownerAccount_ = "test";
188     policy.ownerAccountId_ = "test";
189     struct DlpBlob key;
190     struct DlpUsageSpec usage;
191     struct DlpBlob certData;
192     struct DlpBlob hmacKey;
193 
194     // key create fail
195     DlpCMockCondition condition;
196     condition.mockSequence = { true };
197     SetMockConditions("RAND_bytes", condition);
198     EXPECT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR,
199         DlpFileManager::GetInstance().PrepareDlpEncryptParms(policy, key, usage, certData, hmacKey));
200     CleanMockConditions();
201 
202     // iv create fail
203     condition.mockSequence = { false, true };
204     SetMockConditions("RAND_bytes", condition);
205     EXPECT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR,
206         DlpFileManager::GetInstance().PrepareDlpEncryptParms(policy, key, usage, certData, hmacKey));
207     CleanMockConditions();
208 
209     // create cert data failed with memcpy_s fail
210     condition.mockSequence = { false, false, false, false, false, false, false, false, false, false, true };
211     SetMockConditions("memcpy_s", condition);
212     int res = DlpFileManager::GetInstance().PrepareDlpEncryptParms(policy, key, usage, certData, hmacKey);
213     EXPECT_TRUE(res == DLP_OK || res == DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL);
214     DLP_LOG_INFO(LABEL, "PrepareDlpEncryptParms001 %{public}d", GetMockConditionCounts("memcpy_s"));
215     CleanMockConditions();
216 }
217 
218 /**
219  * @tc.name: ParseDlpFileFormat001
220  * @tc.desc: test parse dlp file format error
221  * @tc.type: FUNC
222  * @tc.require:
223  */
224 HWTEST_F(DlpFileManagerTest, ParseDlpFileFormat001, TestSize.Level1)
225 {
226     DLP_LOG_INFO(LABEL, "UpdateDlpFileContentSize001");
227     g_fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
228     ASSERT_NE(g_fdDlp, -1);
229     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(g_fdDlp, DLP_TEST_DIR, 0, false);
230     ASSERT_NE(filePtr, nullptr);
231     std::string appId = "test_appId_passed";
232 
233     filePtr->dlpFd_ = -1;
234     EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR, DlpFileManager::GetInstance().ParseDlpFileFormat(filePtr, "", appId));
235     filePtr->dlpFd_ = g_fdDlp;
236 
237     struct DlpHeader header = {
238         .magic = DLP_FILE_MAGIC,
239         .version = 1,
240         .txtOffset = sizeof(struct DlpHeader) + 64,
241         .txtSize = 0,
242         .certOffset = sizeof(struct DlpHeader),
243         .certSize = 32,
244         .contactAccountOffset = sizeof(struct DlpHeader) + 32,
245         .contactAccountSize = 32
246     };
247 
248     write(g_fdDlp, &header, sizeof(struct DlpHeader));
249     uint8_t buffer[64] = {0};
250     write(g_fdDlp, buffer, 64);
251     lseek(g_fdDlp, 0, SEEK_SET);
252     EXPECT_EQ(DLP_SERVICE_ERROR_JSON_OPERATE_FAIL,
253         DlpFileManager::GetInstance().ParseDlpFileFormat(filePtr, "", appId));
254 
255     close(g_fdDlp);
256     unlink("/data/fuse_test_dlp.txt");
257     g_fdDlp = -1;
258 }
259 
260 /**
261  * @tc.name: ParseDlpFileFormat002
262  * @tc.desc: test parse dlp file formate error
263  * @tc.type: FUNC
264  * @tc.require:
265  */
266 HWTEST_F(DlpFileManagerTest, ParseDlpFileFormat002, TestSize.Level1)
267 {
268     DLP_LOG_INFO(LABEL, "ParseDlpFileFormat002");
269     g_fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
270     ASSERT_NE(g_fdDlp, -1);
271 
272     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(g_fdDlp, DLP_TEST_DIR, 0, false);
273     ASSERT_NE(filePtr, nullptr);
274 
275     struct DlpHeader header = {
276         .magic = DLP_FILE_MAGIC,
277         .version = 1,
278         .txtOffset = sizeof(struct DlpHeader) + 256 + 32,
279         .txtSize = 0,
280         .certOffset = sizeof(struct DlpHeader),
281         .certSize = 256,
282         .contactAccountOffset = sizeof(struct DlpHeader) + 256,
283         .contactAccountSize = 32
284     };
285 
286     write(g_fdDlp, &header, sizeof(struct DlpHeader));
287     std::string certStr = "{\"aeskeyLen\":16, \"aeskey\":\"11223344556677889900112233445566\",\"ivLen\":16,"
288         "\"iv\":\"11223344556677889900112233445566\",\"ownerAccount\":\"test\",\"ownerAccountId\":\"test\","
289         "\"ownerAccountType\":0}";
290     write(g_fdDlp, certStr.c_str(), certStr.length());
291     lseek(g_fdDlp, sizeof(struct DlpHeader) + 256, SEEK_SET);
292     uint8_t buffer[32] = {0};
293     write(g_fdDlp, buffer, 32);
294 
295     lseek(g_fdDlp, 0, SEEK_SET);
296     std::string appId = "test_appId_passed";
297     EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID, DlpFileManager::GetInstance().ParseDlpFileFormat(filePtr, "", appId));
298 
299     close(g_fdDlp);
300     unlink("/data/fuse_test_dlp.txt");
301     g_fdDlp = -1;
302 }
303 
304 /**
305  * @tc.name: ParseDlpFileFormat003
306  * @tc.desc: test parse dlp file formate error
307  * @tc.type: FUNC
308  * @tc.require:
309  */
310 HWTEST_F(DlpFileManagerTest, ParseDlpFileFormat003, TestSize.Level1)
311 {
312     DLP_LOG_INFO(LABEL, "ParseDlpFileFormat003");
313     g_fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
314     ASSERT_NE(g_fdDlp, -1);
315     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(g_fdDlp, DLP_TEST_DIR, 0, false);
316     ASSERT_NE(filePtr, nullptr);
317 
318     struct DlpHeader header = {
319         .magic = DLP_FILE_MAGIC,
320         .version = 1,
321         .txtOffset = sizeof(struct DlpHeader) + 256 + 32,
322         .txtSize = 0,
323         .certOffset = sizeof(struct DlpHeader),
324         .certSize = 256,
325         .contactAccountOffset = sizeof(struct DlpHeader) + 256,
326         .contactAccountSize = 32
327     };
328 
329     write(g_fdDlp, &header, sizeof(struct DlpHeader));
330     std::string certStr = "{\"aeskeyLen\":16, \"aeskey\":\"11223344556677889900112233445566\",\"ivLen\":16,"
331         "\"iv\":\"11223344556677889900112233445566\",\"ownerAccount\":\"test\",\"ownerAccountId\":\"test\","
332         "\"ownerAccountType\":1}";
333     write(g_fdDlp, certStr.c_str(), certStr.length());
334     lseek(g_fdDlp, sizeof(struct DlpHeader) + 256, SEEK_SET);
335     uint8_t buffer[32] = {0};
336     write(g_fdDlp, buffer, 32);
337 
338     lseek(g_fdDlp, 0, SEEK_SET);
339 
340     // make SetCipher failed
341     DlpCMockCondition condition;
342     condition.mockSequence = { false, false, false, false, false, false, false, true };
343     SetMockConditions("memcpy_s", condition);
344     std::string appId = "test_appId_passed";
345     EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID,
346         DlpFileManager::GetInstance().ParseDlpFileFormat(filePtr, "", appId));
347     CleanMockConditions();
348 
349     close(g_fdDlp);
350     unlink("/data/fuse_test_dlp.txt");
351     g_fdDlp = -1;
352 }
353 
354 /**
355  * @tc.name: ParseDlpFileFormat004
356  * @tc.desc: test parse dlp file formate error with offineAccess is true
357  * @tc.type: FUNC
358  * @tc.require:
359  */
360 HWTEST_F(DlpFileManagerTest, ParseDlpFileFormat004, TestSize.Level1)
361 {
362         DLP_LOG_INFO(LABEL, "UpdateDlpFileContentSize001");
363     g_fdDlp = open("/data/fuse_test_dlp.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
364     ASSERT_NE(g_fdDlp, -1);
365     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(g_fdDlp, DLP_TEST_DIR, 0, false);
366     ASSERT_NE(filePtr, nullptr);
367     filePtr->SetOfflineAccess(true);
368 
369     std::string appId = "test_appId_passed";
370     filePtr->dlpFd_ = -1;
371     EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR, DlpFileManager::GetInstance().ParseDlpFileFormat(filePtr, "", appId));
372 
373     close(g_fdDlp);
374     unlink("/data/fuse_test_dlp.txt");
375     g_fdDlp = -1;
376 }
377 
378 /**
379  * @tc.name: FreeChiperBlob001
380  * @tc.desc: test free chiper blob abnormal branch
381  * @tc.type: FUNC
382  * @tc.require:
383  */
384 HWTEST_F(DlpFileManagerTest, FreeChiperBlob001, TestSize.Level1)
385 {
386     DLP_LOG_INFO(LABEL, "FreeChiperBlob001");
387     struct DlpBlob key = {
388         .data = nullptr,
389         .size = 0
390     };
391     struct DlpBlob certData = {
392         .data = nullptr,
393         .size = 0
394     };
395 
396     struct DlpUsageSpec spec = {
397         .algParam = nullptr
398     };
399 
400     struct DlpBlob hmacKey = {
401         .data = nullptr,
402         .size = 0
403     };
404 
405     // algparm nullptr
406     DlpFileManager::GetInstance().FreeChiperBlob(key, certData, spec, hmacKey);
407 
408     // algparm iv nullptr
409     spec.algParam = new (std::nothrow) struct DlpCipherParam;
410     ASSERT_NE(spec.algParam, nullptr);
411     spec.algParam->iv.data = nullptr;
412     DlpFileManager::GetInstance().FreeChiperBlob(key, certData, spec, hmacKey);
413 
414     ASSERT_EQ(spec.algParam, nullptr);
415 }
416 
417 /**
418  * @tc.name: SetDlpFileParams001
419  * @tc.desc: test set dlp file params with prepare ciper failed
420  * @tc.type: FUNC
421  * @tc.require:
422  */
423 HWTEST_F(DlpFileManagerTest, SetDlpFileParams001, TestSize.Level1)
424 {
425     DLP_LOG_INFO(LABEL, "SetDlpFileParams001");
426     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(1000, DLP_TEST_DIR, 0, false);
427     ASSERT_NE(filePtr, nullptr);
428     DlpProperty property;
429 
430     // PrepareDlpEncryptParms fail
431     DlpCMockCondition condition;
432     condition.mockSequence = { true };
433     SetMockConditions("RAND_bytes", condition);
434     EXPECT_EQ(DLP_PARSE_ERROR_CRYPTO_ENGINE_ERROR,
435         DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property));
436     CleanMockConditions();
437 
438     // SetCipher fail
439     property.ownerAccount = "owner";
440     property.ownerAccountId = "owner";
441     property.contactAccount = "owner";
442     property.ownerAccountType = DOMAIN_ACCOUNT;
443 
444     condition.mockSequence = { false, false, false, false, false, false, false, false, false,
445         false, false, false, false, false, false, false, false, false,
446         false, false, false, false, false, false, true };
447     SetMockConditions("memcpy_s", condition);
448     int res = DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property);
449     EXPECT_TRUE(res == DLP_OK || res == DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL);
450     DLP_LOG_INFO(LABEL, "SetDlpFileParams001 %{public}d", GetMockConditionCounts("memcpy_s"));
451     CleanMockConditions();
452 }
453 
454 /**
455  * @tc.name: SetDlpFileParams002
456  * @tc.desc: test set dlp file params with set policy failed
457  * @tc.type: FUNC
458  * @tc.require:
459  */
460 HWTEST_F(DlpFileManagerTest, SetDlpFileParams002, TestSize.Level1)
461 {
462     DLP_LOG_INFO(LABEL, "SetDlpFileParams002");
463     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(1000, DLP_TEST_DIR, 0, false);
464     ASSERT_NE(filePtr, nullptr);
465     DlpProperty property;
466 
467     // SetPolicy fail
468     property.ownerAccount = "";
469     property.ownerAccountId = "";
470     property.contactAccount = "owner";
471     property.ownerAccountType = DOMAIN_ACCOUNT;
472 
473     EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID,
474         DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property));
475 }
476 
477 /**
478  * @tc.name: SetDlpFileParams003
479  * @tc.desc: test set dlp file params with set cert failed
480  * @tc.type: FUNC
481  * @tc.require:
482  */
483 HWTEST_F(DlpFileManagerTest, SetDlpFileParams003, TestSize.Level1)
484 {
485     DLP_LOG_INFO(LABEL, "SetDlpFileParams003");
486     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(1000, DLP_TEST_DIR, 0, false);
487     ASSERT_NE(filePtr, nullptr);
488     DlpProperty property;
489 
490     // SetPolicy fail
491     property.ownerAccount = "owner";
492     property.ownerAccountId = "owner";
493     property.contactAccount = "account";
494     property.ownerAccountType = CLOUD_ACCOUNT;
495 
496     DlpCMockCondition condition;
497     condition.mockSequence = {
498         false, false, false, false, false, false,
499         false, false, false, false, true
500     };
501     SetMockConditions("memcpy_s", condition);
502     int res = DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property);
503     EXPECT_TRUE(res == DLP_OK || res == DLP_PARSE_ERROR_MEMORY_OPERATE_FAIL);
504     CleanMockConditions();
505 }
506 
507 /**
508  * @tc.name: SetDlpFileParams004
509  * @tc.desc: test set dlp file params with contact account empty
510  * @tc.type: FUNC
511  * @tc.require:
512  */
513 HWTEST_F(DlpFileManagerTest, SetDlpFileParams004, TestSize.Level1)
514 {
515     DLP_LOG_INFO(LABEL, "SetDlpFileParams004");
516     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(1000, DLP_TEST_DIR, 0, false);
517     ASSERT_NE(filePtr, nullptr);
518     DlpProperty property;
519 
520     // SetPolicy fail
521     property.ownerAccount = "owner";
522     property.ownerAccountId = "owner";
523     property.contactAccount = "";
524     property.ownerAccountType = DOMAIN_ACCOUNT;
525 
526     EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID,
527         DlpFileManager::GetInstance().SetDlpFileParams(filePtr, property));
528     CleanMockConditions();
529 }
530 
531 /**
532  * @tc.name: GenerateDlpFile001
533  * @tc.desc: test generate dlp file with wrong params
534  * @tc.type: FUNC
535  * @tc.require:
536  */
537 HWTEST_F(DlpFileManagerTest, GenerateDlpFile001, TestSize.Level1)
538 {
539     DLP_LOG_INFO(LABEL, "GenerateDlpFile001");
540     std::shared_ptr<DlpFile> filePtr = std::make_shared<DlpFile>(1000, DLP_TEST_DIR, 0, false);
541     ASSERT_NE(filePtr, nullptr);
542     DlpProperty property;
543     property.ownerAccount = "owner";
544     property.ownerAccountId = "owner";
545     property.contactAccount = "owner";
546     property.ownerAccountType = DOMAIN_ACCOUNT;
547 
548     EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
549         DlpFileManager::GetInstance().GenerateDlpFile(-1, 1000, property, filePtr, DLP_TEST_DIR));
550 
551     EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
552         DlpFileManager::GetInstance().GenerateDlpFile(1000, -1, property, filePtr, DLP_TEST_DIR));
553 
554     DlpFileManager::GetInstance().AddDlpFileNode(filePtr);
555     EXPECT_EQ(DLP_PARSE_ERROR_FILE_ALREADY_OPENED,
556         DlpFileManager::GetInstance().GenerateDlpFile(1000, 1000, property, filePtr, DLP_TEST_DIR));
557     DlpFileManager::GetInstance().RemoveDlpFileNode(filePtr);
558 }
559 
560 /**
561  * @tc.name: GenerateDlpFile002
562  * @tc.desc: test generate dlp file with wrong property
563  * @tc.type: FUNC
564  * @tc.require:
565  */
566 HWTEST_F(DlpFileManagerTest, GenerateDlpFile002, TestSize.Level1)
567 {
568     DLP_LOG_INFO(LABEL, "GenerateDlpFile002");
569     std::shared_ptr<DlpFile> filePtr;
570     DlpProperty property;
571     property.ownerAccount = "";
572     property.ownerAccountId = "";
573     property.contactAccount = "owner";
574     property.ownerAccountType = DOMAIN_ACCOUNT;
575 
576     EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID,
577         DlpFileManager::GetInstance().GenerateDlpFile(1000, 1000, property, filePtr, DLP_TEST_DIR));
578 }
579 
580 /**
581  * @tc.name: GenerateDlpFile003
582  * @tc.desc: test generate dlp file with generate real file failed
583  * @tc.type: FUNC
584  * @tc.require:
585  */
586 HWTEST_F(DlpFileManagerTest, GenerateDlpFile003, TestSize.Level1)
587 {
588     DLP_LOG_INFO(LABEL, "GenerateDlpFile003");
589     std::shared_ptr<DlpFile> filePtr;
590     DlpProperty property;
591     property.ownerAccount = "owner";
592     property.ownerAccountId = "owner";
593     property.contactAccount = "owner";
594     property.ownerAccountType = DOMAIN_ACCOUNT;
595 
596     EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL,
597         DlpFileManager::GetInstance().GenerateDlpFile(1000, 1000, property, filePtr, DLP_TEST_DIR));
598 }
599 
600 /**
601  * @tc.name: OpenDlpFile001
602  * @tc.desc: test open dlp file params with wrong params
603  * @tc.type: FUNC
604  * @tc.require:
605  */
606 HWTEST_F(DlpFileManagerTest, OpenDlpFile001, TestSize.Level1)
607 {
608     DLP_LOG_INFO(LABEL, "OpenDlpFile001");
609     std::shared_ptr<DlpFile> filePtr;
610     DlpProperty property;
611     property.ownerAccount = "owner";
612     property.ownerAccountId = "owner";
613     property.contactAccount = "owner";
614     property.ownerAccountType = DOMAIN_ACCOUNT;
615     std::string appId = "test_appId_passed";
616     std::string appIdFake = "test_appId_failed";
617 
618     EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
619         DlpFileManager::GetInstance().OpenDlpFile(-1, filePtr, "", appId));
620     EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
621         DlpFileManager::GetInstance().OpenDlpFile(-1, filePtr, "", appIdFake));
622 
623     std::shared_ptr<DlpFile> filePtr1 = std::make_shared<DlpFile>(1000, DLP_TEST_DIR, 0, false);
624     ASSERT_NE(filePtr1, nullptr);
625     DlpFileManager::GetInstance().AddDlpFileNode(filePtr1);
626 
627     EXPECT_EQ(DLP_OK,
628         DlpFileManager::GetInstance().OpenDlpFile(1000, filePtr, "", appId));
629     EXPECT_EQ(filePtr1, filePtr);
630     DlpFileManager::GetInstance().RemoveDlpFileNode(filePtr1);
631 
632     EXPECT_EQ(DLP_PARSE_ERROR_FILE_OPERATE_FAIL,
633         DlpFileManager::GetInstance().OpenDlpFile(1000, filePtr, "", appId));
634 }
635 
636 /**
637  * @tc.name: CloseDlpFile001
638  * @tc.desc: test close dlp file with wrong params
639  * @tc.type: FUNC
640  * @tc.require:
641  */
642 HWTEST_F(DlpFileManagerTest, CloseDlpFile001, TestSize.Level1)
643 {
644     DLP_LOG_INFO(LABEL, "CloseDlpFile001");
645     EXPECT_EQ(DLP_PARSE_ERROR_PTR_NULL,
646         DlpFileManager::GetInstance().CloseDlpFile(nullptr));
647 }
648 
649 /**
650  * @tc.name: RecoverDlpFile001
651  * @tc.desc: test close dlp file with wrong params
652  * @tc.type: FUNC
653  * @tc.require:
654  */
655 HWTEST_F(DlpFileManagerTest, RecoverDlpFile001, TestSize.Level1)
656 {
657     DLP_LOG_INFO(LABEL, "RecoverDlpFile001");
658     std::shared_ptr<DlpFile> filePtr = nullptr;
659     EXPECT_EQ(DLP_PARSE_ERROR_PTR_NULL,
660         DlpFileManager::GetInstance().RecoverDlpFile(filePtr, 1000));
661 
662     filePtr = std::make_shared<DlpFile>(1000, DLP_TEST_DIR, 0, false);
663     ASSERT_NE(filePtr, nullptr);
664     EXPECT_EQ(DLP_PARSE_ERROR_FD_ERROR,
665         DlpFileManager::GetInstance().RecoverDlpFile(filePtr, -1));
666 }
667 
668 /**
669  * @tc.name: CleanTempBlob001
670  * @tc.desc: test param tagIv whether pointer is null
671  * @tc.type: FUNC
672  * @tc.require:issue:IAIFTY
673  */
674 HWTEST_F(DlpFileManagerTest, CleanTempBlob001, TestSize.Level1)
675 {
676     DLP_LOG_INFO(LABEL, "CleanTempBlob001");
677 
678     DlpBlob key;
679     DlpCipherParam* tagIv;
680     DlpBlob hmacKey;
681     uint8_t g_iv[2] = { 0x90, 0xd5 };
682     hmacKey.data = g_iv;
683     ASSERT_TRUE(hmacKey.data != nullptr);
684 
685     DlpFileManager::GetInstance().CleanTempBlob(key, &tagIv, hmacKey);
686     ASSERT_TRUE(key.data == nullptr);
687     ASSERT_TRUE(tagIv == nullptr);
688 
689     tagIv = new (std::nothrow) struct DlpCipherParam;
690     ASSERT_NE(tagIv, nullptr);
691     tagIv->iv.data = g_iv;
692     ASSERT_TRUE(tagIv->iv.data != nullptr);
693     DlpFileManager::GetInstance().CleanTempBlob(key, &tagIv, hmacKey);
694     ASSERT_TRUE(tagIv == nullptr);
695 }
696 
697 /**
698  * @tc.name: GenerateCertBlob001
699  * @tc.desc: test param whether cert and certData are empty
700  * @tc.type: FUNC
701  * @tc.require:issue:IAIFTY
702  */
703 HWTEST_F(DlpFileManagerTest, GenerateCertBlob001, TestSize.Level1)
704 {
705     DLP_LOG_INFO(LABEL, "GenerateCertBlob001");
706 
707     std::vector<uint8_t> cert;
708     struct DlpBlob certData;
709     certData.data = new (std::nothrow) uint8_t[15];
710     ASSERT_TRUE(cert.size() == 0);
711     EXPECT_EQ(DLP_PARSE_ERROR_VALUE_INVALID,
712         DlpFileManager::GetInstance().GenerateCertBlob(cert, certData));
713 
714     cert.push_back(1);
715     ASSERT_TRUE(certData.data != nullptr);
716     EXPECT_EQ(DLP_OK, DlpFileManager::GetInstance().GenerateCertBlob(cert, certData));
717 }
718 }  // namespace DlpPermission
719 }  // namespace Security
720 }  // namespace OHOS