• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dlp_raw_file_test.h"
17 #include <cstdio>
18 #include <cstring>
19 #include <fcntl.h>
20 #include <iostream>
21 #include <fstream>
22 #include <thread>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #define private public
26 #include "dlp_file.h"
27 #include "dlp_raw_file.h"
28 #include "dlp_zip_file.h"
29 #include "dlp_file_manager.h"
30 #undef private
31 #include "dlp_crypt.h"
32 #include "dlp_permission.h"
33 
34 using namespace testing::ext;
35 using namespace OHOS::Security::DlpPermission;
36 using namespace std;
37 
38 namespace {
39 static const uint64_t DLP_CERT_SIZE = 1024 * 1024;
40 static const uint32_t DLP_HEAD_SIZE = 10 * 1024 * 1024;
41 static const std::string DLP_TEST_DIR = "/data/dlpTest/";
42 static constexpr int32_t SECOND = 2;
43 }
44 
SetUpTestCase()45 void DlpRawFileTest::SetUpTestCase() {}
46 
TearDownTestCase()47 void DlpRawFileTest::TearDownTestCase() {}
48 
SetUp()49 void DlpRawFileTest::SetUp() {}
50 
TearDown()51 void DlpRawFileTest::TearDown() {}
52 
53 /**
54  * @tc.name: ParseRawDlpHeaderTest
55  * @tc.desc: test ParseRawDlpHeader
56  * @tc.type: FUNC
57  * @tc.require:
58  */
59 HWTEST_F(DlpRawFileTest, ParseRawDlpHeaderTest, TestSize.Level0)
60 {
61     uint64_t fileLen = DLP_CERT_SIZE;
62     uint32_t dlpHeaderSize = DLP_HEAD_SIZE;
63     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
64     ASSERT_EQ(filePtr->ParseRawDlpHeader(fileLen, dlpHeaderSize), DLP_PARSE_ERROR_FD_ERROR);
65 }
66 
67 /**
68  * @tc.name: ParseRawDlpHeaderTest002
69  * @tc.desc: test ParseRawDlpHeader
70  * @tc.type: FUNC
71  * @tc.require:
72  */
73 HWTEST_F(DlpRawFileTest, ParseRawDlpHeaderTest002, TestSize.Level0)
74 {
75     int32_t fd = open("/data/fuse_test_dlp.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
76     ASSERT_NE(fd, -1);
77     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
78     ASSERT_NE(filePtr, nullptr);
79 
80     struct DlpHeader header = {
81         .magic = DLP_FILE_MAGIC,
82         .fileType = 10,
83         .offlineAccess = 0,
84         .algType = DLP_MODE_CTR,
85         .txtOffset = sizeof(struct DlpHeader) + 108,
86         .txtSize = 100,
87         .hmacOffset = sizeof(struct DlpHeader) + 208,
88         .hmacSize = 64,
89         .certOffset = sizeof(struct DlpHeader) + 272,
90         .certSize = 256,
91         .contactAccountOffset = sizeof(struct DlpHeader) + 8,
92         .contactAccountSize = 100,
93         .offlineCertOffset = sizeof(struct DlpHeader) + 272,
94         .offlineCertSize = 0
95     };
96     uint32_t version = 3;
97     uint32_t dlpHeaderSize = sizeof(struct DlpHeader);
98     write(fd, &version, sizeof(struct DlpHeader));
99     write(fd, &dlpHeaderSize, sizeof(struct DlpHeader));
100     uint8_t buffer[800] = {0};
101     write(fd, buffer, 800);
102 
103     lseek(fd, 8, SEEK_SET);
104     write(fd, &header, sizeof(struct DlpHeader));
105     std::string certStr = "{\"aeskeyLen\":16, \"aeskey\":\"11223344556677889900112233445566\",\"ivLen\":16,"
106         "\"iv\":\"11223344556677889900112233445566\",\"ownerAccount\":\"test\",\"ownerAccountId\":\"test\","
107         "\"ownerAccountType\":0}";
108     lseek(fd, header.certOffset, SEEK_SET);
109     write(fd, certStr.c_str(), certStr.length());
110     lseek(fd, 0, SEEK_SET);
111     filePtr->ParseRawDlpHeader(dlpHeaderSize, dlpHeaderSize);
112     close(fd);
113     unlink("/data/fuse_test_dlp.txt");
114     fd = -1;
115 }
116 
117 /**
118  * @tc.name: ParseRawDlpHeaderTest003
119  * @tc.desc: test ParseRawDlpHeader
120  * @tc.type: FUNC
121  * @tc.require:
122  */
123 HWTEST_F(DlpRawFileTest, ParseRawDlpHeaderTest003, TestSize.Level0)
124 {
125     int32_t fd = open("/data/fuse_test_dlp.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
126     ASSERT_NE(fd, -1);
127     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
128     ASSERT_NE(filePtr, nullptr);
129 
130     struct DlpHeader header = {
131         .magic = DLP_FILE_MAGIC,
132         .fileType = 10,
133         .offlineAccess = 0,
134         .algType = DLP_MODE_CTR,
135         .txtOffset = sizeof(struct DlpHeader) + 108,
136         .txtSize = 100,
137         .hmacOffset = sizeof(struct DlpHeader) + 208,
138         .hmacSize = 64,
139         .certOffset = sizeof(struct DlpHeader) + 272,
140         .certSize = 256,
141         .contactAccountOffset = sizeof(struct DlpHeader) + 8,
142         .contactAccountSize = 100,
143         .offlineCertOffset = sizeof(struct DlpHeader) + 272,
144         .offlineCertSize = 0
145     };
146     uint32_t version = 3;
147     uint32_t dlpHeaderSize = sizeof(struct DlpHeader);
148     write(fd, &version, sizeof(struct DlpHeader));
149     write(fd, &dlpHeaderSize, sizeof(struct DlpHeader));
150     uint8_t buffer[800] = {0};
151     write(fd, buffer, 800);
152 
153     lseek(fd, 8, SEEK_SET);
154     write(fd, &header, sizeof(struct DlpHeader));
155     std::string certStr = "{\"aeskeyLen\":16, \"aeskey\":\"11223344556677889900112233445566\",\"ivLen\":16,"
156         "\"iv\":\"11223344556677889900112233445566\",\"ownerAccount\":\"test\",\"ownerAccountId\":\"test\","
157         "\"ownerAccountType\":0}";
158     lseek(fd, header.certOffset, SEEK_SET);
159     write(fd, certStr.c_str(), certStr.length());
160     lseek(fd, 0, SEEK_SET);
161     filePtr->ParseRawDlpHeader(DLP_HEAD_SIZE + 808, DLP_HEAD_SIZE);
162     close(fd);
163     unlink("/data/fuse_test_dlp.txt");
164     fd = -1;
165 }
166 
167 /**
168  * @tc.name: ParseRawDlpHeaderTest004
169  * @tc.desc: test ParseRawDlpHeader
170  * @tc.type: FUNC
171  * @tc.require:
172  */
173 HWTEST_F(DlpRawFileTest, ParseRawDlpHeaderTest004, TestSize.Level0)
174 {
175     int32_t fd = open("/data/fuse_test_dlp.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
176     ASSERT_NE(fd, -1);
177     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
178     ASSERT_NE(filePtr, nullptr);
179 
180     struct DlpHeader header = {
181         .magic = DLP_FILE_MAGIC,
182         .fileType = 10,
183         .offlineAccess = 0,
184         .algType = DLP_MODE_CTR,
185         .txtOffset = sizeof(struct DlpHeader) + 108,
186         .txtSize = 100,
187         .hmacOffset = sizeof(struct DlpHeader) + 208,
188         .hmacSize = 64,
189         .certOffset = sizeof(struct DlpHeader) + 272,
190         .certSize = 256,
191         .contactAccountOffset = sizeof(struct DlpHeader) + 8,
192         .contactAccountSize = 100,
193         .offlineCertOffset = sizeof(struct DlpHeader) + 272,
194         .offlineCertSize = 0
195     };
196     uint32_t version = 3;
197     uint32_t dlpHeaderSize = sizeof(struct DlpHeader);
198     write(fd, &version, sizeof(struct DlpHeader));
199     write(fd, &dlpHeaderSize, sizeof(struct DlpHeader));
200     uint8_t buffer[800] = {0};
201     write(fd, buffer, 800);
202 
203     lseek(fd, 8, SEEK_SET);
204     write(fd, &header, sizeof(struct DlpHeader));
205     std::string certStr = "{\"aeskeyLen\":16, \"aeskey\":\"11223344556677889900112233445566\",\"ivLen\":16,"
206         "\"iv\":\"11223344556677889900112233445566\",\"ownerAccount\":\"test\",\"ownerAccountId\":\"test\","
207         "\"ownerAccountType\":0}";
208     lseek(fd, header.certOffset, SEEK_SET);
209     write(fd, certStr.c_str(), certStr.length());
210     lseek(fd, 0, SEEK_SET);
211     filePtr->ParseRawDlpHeader(808, dlpHeaderSize);
212     close(fd);
213     unlink("/data/fuse_test_dlp.txt");
214     fd = -1;
215 }
216 
217 /**
218  * @tc.name: ParseRawDlpHeaderTest005
219  * @tc.desc: test ParseRawDlpHeader
220  * @tc.type: FUNC
221  * @tc.require:
222  */
223 HWTEST_F(DlpRawFileTest, ParseRawDlpHeaderTest005, TestSize.Level0)
224 {
225     int32_t fd = open("/data/fuse_test_dlp.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
226     ASSERT_NE(fd, -1);
227     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
228     ASSERT_NE(filePtr, nullptr);
229 
230     struct DlpHeader header = {
231         .magic = DLP_FILE_MAGIC,
232         .fileType = 10,
233         .offlineAccess = 0,
234         .algType = DLP_MODE_CTR,
235         .txtOffset = sizeof(struct DlpHeader) + 108,
236         .txtSize = 0,
237         .hmacOffset = sizeof(struct DlpHeader) + 108,
238         .hmacSize = 64,
239         .certOffset = sizeof(struct DlpHeader) + 172,
240         .certSize = 256,
241         .contactAccountOffset = sizeof(struct DlpHeader) + 8,
242         .contactAccountSize = 100,
243         .offlineCertOffset = sizeof(struct DlpHeader) + 272,
244         .offlineCertSize = 0
245     };
246     uint32_t version = 3;
247     uint32_t dlpHeaderSize = sizeof(struct DlpHeader);
248     write(fd, &version, sizeof(struct DlpHeader));
249     write(fd, &dlpHeaderSize, sizeof(struct DlpHeader));
250     uint8_t buffer[800] = {0};
251     write(fd, buffer, 800);
252 
253     lseek(fd, 8, SEEK_SET);
254     write(fd, &header, sizeof(struct DlpHeader));
255     std::string certStr = "{\"aeskeyLen\":16, \"aeskey\":\"11223344556677889900112233445566\",\"ivLen\":16,"
256         "\"iv\":\"11223344556677889900112233445566\",\"ownerAccount\":\"test\",\"ownerAccountId\":\"test\","
257         "\"ownerAccountType\":0}";
258     lseek(fd, header.certOffset, SEEK_SET);
259     write(fd, certStr.c_str(), certStr.length());
260     lseek(fd, 0, SEEK_SET);
261     filePtr->ParseRawDlpHeader(400, dlpHeaderSize);
262     close(fd);
263     unlink("/data/fuse_test_dlp.txt");
264     fd = -1;
265 }
266 
267 /**
268  * @tc.name: ParseRawDlpHeaderTest006
269  * @tc.desc: test ParseRawDlpHeader
270  * @tc.type: FUNC
271  * @tc.require:
272  */
273 HWTEST_F(DlpRawFileTest, ParseRawDlpHeaderTest006, TestSize.Level0)
274 {
275     int32_t fd = open("/data/fuse_test_dlp.txt.dlp", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
276     ASSERT_NE(fd, -1);
277     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
278     ASSERT_NE(filePtr, nullptr);
279 
280     struct DlpHeader header = {
281         .magic = DLP_FILE_MAGIC,
282         .fileType = 10,
283         .offlineAccess = 0,
284         .algType = DLP_MODE_CTR,
285         .txtOffset = sizeof(struct DlpHeader) + 108,
286         .txtSize = 0,
287         .hmacOffset = sizeof(struct DlpHeader) + 108,
288         .hmacSize = 64,
289         .certOffset = sizeof(struct DlpHeader) + 172,
290         .certSize = 256,
291         .contactAccountOffset = sizeof(struct DlpHeader) + 8,
292         .contactAccountSize = 100,
293         .offlineCertOffset = sizeof(struct DlpHeader) + 272,
294         .offlineCertSize = 0
295     };
296     uint32_t version = 3;
297     uint32_t dlpHeaderSize = sizeof(struct DlpHeader);
298     write(fd, &version, sizeof(struct DlpHeader));
299     write(fd, &dlpHeaderSize, sizeof(struct DlpHeader));
300     uint8_t buffer[800] = {0};
301     write(fd, buffer, 800);
302 
303     lseek(fd, 8, SEEK_SET);
304     write(fd, &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     lseek(fd, header.certOffset, SEEK_SET);
309     write(fd, certStr.c_str(), certStr.length());
310     lseek(fd, 0, SEEK_SET);
311     filePtr->ParseRawDlpHeader(808, dlpHeaderSize);
312     close(fd);
313     unlink("/data/fuse_test_dlp.txt");
314     fd = -1;
315 }
316 
317 /**
318  * @tc.name: HmacCheckTest
319  * @tc.desc: test HmacCheck
320  * @tc.type: FUNC
321  * @tc.require:
322  */
323 HWTEST_F(DlpRawFileTest, HmacCheckTest, TestSize.Level0)
324 {
325     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
326     filePtr->version_ = SECOND;
327     ASSERT_EQ(filePtr->HmacCheck(), DLP_OK);
328 }
329 
330 /**
331  * @tc.name: GetOfflineCertSizeTest
332  * @tc.desc: test GetOfflineCertSize
333  * @tc.type: FUNC
334  * @tc.require:
335  */
336 HWTEST_F(DlpRawFileTest, GetOfflineCertSizeTest, TestSize.Level0)
337 {
338     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
339     ASSERT_EQ(filePtr->GetOfflineCertSize(), 0);
340 }
341 
342 /**
343  * @tc.name: DoDlpHIAECryptOperationTest
344  * @tc.desc: test DoDlpHIAECryptOperation
345  * @tc.type: FUNC
346  * @tc.require:
347  */
348 HWTEST_F(DlpRawFileTest, DoDlpHIAECryptOperationTest, TestSize.Level0)
349 {
350     DlpBlob message1 = { 0, nullptr };
351     DlpBlob message2 = { 0, nullptr };
352     std::shared_ptr<DlpRawFile> filePtr = std::make_shared<DlpRawFile>(-1, "mp4");
353     ASSERT_EQ(filePtr->DoDlpHIAECryptOperation(message1, message2, 0, true), DLP_PARSE_ERROR_VALUE_INVALID);
354 }