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