1 /*
2 * Copyright (c) 2024-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 <unistd.h>
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <gtest/gtest.h>
21
22 #include "data_source.h"
23 #include "hap_signer_block_utils.h"
24 #include "random_access_file.h"
25 #include "random_access_file_input.h"
26 #include "random_access_file_output.h"
27
28 namespace OHOS {
29 namespace SignatureTools {
30 const std::string UNSIGNED_HAP_PATH = "./zip/unsigned_random_access_file.hap";
31 const std::string SIGNED_HAP_PATH = "./zip/signed_random_access_file.hap";
32 class RandomAccessFileInputOutputTest : public testing::Test {
33 public:
34 static void SetUpTestCase(void);
35 static void TearDownTestCase(void);
36 void SetUp();
37 void TearDown();
38 };
39
SetUpTestCase(void)40 void RandomAccessFileInputOutputTest::SetUpTestCase(void)
41 {
42 (void)rename("./zip/unsigned_random_access_file.txt", UNSIGNED_HAP_PATH.c_str());
43 (void)rename("./zip/signed_random_access_file.txt", SIGNED_HAP_PATH.c_str());
44 sync();
45 }
46
TearDownTestCase(void)47 void RandomAccessFileInputOutputTest::TearDownTestCase(void)
48 {
49 }
50
SetUp()51 void RandomAccessFileInputOutputTest::SetUp()
52 {
53 }
54
TearDown()55 void RandomAccessFileInputOutputTest::TearDown()
56 {
57 }
58
59 /**
60 * @tc.name: Test Init Function
61 * @tc.desc: Test function of RandomAccessFile::Init() interface for SUCCESS.
62 * @tc.type: FUNC
63 * @tc.require: SR000H63TL
64 */
65 HWTEST_F(RandomAccessFileInputOutputTest, InitTest001, testing::ext::TestSize.Level1)
66 {
67 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
68 bool res = outputHap->Init(UNSIGNED_HAP_PATH);
69 EXPECT_EQ(res, true);
70 }
71
72 /**
73 * @tc.name: Test Init Function
74 * @tc.desc: Test function of RandomAccessFile::Init() interface for FAIL.
75 * @tc.type: FUNC
76 * @tc.require: SR000H63TL
77 */
78 HWTEST_F(RandomAccessFileInputOutputTest, InitTest002, testing::ext::TestSize.Level1)
79 {
80 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
81 /*
82 * @tc.steps: step1. test Init function
83 * @tc.expected: step1. make the random access file is not exist, the return will be false.
84 */
85 bool res = outputHap->Init("");
86 EXPECT_EQ(res, false);
87 }
88
89 /**
90 * @tc.name: Test WriteToFile Function
91 * @tc.desc: Test function of RandomAccessFile::WriteToFile() interface for SUCCESS.
92 * @tc.type: FUNC
93 * @tc.require: SR000H63TL
94 */
95 HWTEST_F(RandomAccessFileInputOutputTest, WriteToFileTest001, testing::ext::TestSize.Level1)
96 {
97 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
98 bool res = outputHap->Init(UNSIGNED_HAP_PATH);
99 EXPECT_EQ(res, true);
100 ByteBuffer buffer(1);
101 buffer.PutByte(1);
102 EXPECT_EQ(outputHap->WriteToFile(buffer, 0, 1) > 0, true);
103 }
104
105 /**
106 * @tc.name: Test WriteToFile Function
107 * @tc.desc: Test function of RandomAccessFile::WriteToFile() interface for FAIL.
108 * @tc.type: FUNC
109 * @tc.require: SR000H63TL
110 */
111 HWTEST_F(RandomAccessFileInputOutputTest, WriteToFileTest002, testing::ext::TestSize.Level1)
112 {
113 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
114 bool res = outputHap->Init(UNSIGNED_HAP_PATH);
115 EXPECT_EQ(res, true);
116 /*
117 * @tc.steps: step1. test WriteToFile function
118 * @tc.expected: step1. make the ByteBuffer's capacity is 0, the return will be -1.
119 */
120 ByteBuffer buffer;
121 EXPECT_EQ(outputHap->WriteToFile(buffer, 0, 0), -1);
122 }
123
124 /**
125 * @tc.name: Test WriteToFile Function
126 * @tc.desc: Test function of RandomAccessFile::WriteToFile() interface for FAIL.
127 * @tc.type: FUNC
128 * @tc.require: SR000H63TL
129 */
130 HWTEST_F(RandomAccessFileInputOutputTest, WriteToFileTest003, testing::ext::TestSize.Level1)
131 {
132 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
133 bool res = outputHap->Init(UNSIGNED_HAP_PATH);
134 EXPECT_EQ(res, true);
135 ByteBuffer buffer(1);
136 buffer.PutByte(1);
137 /*
138 * @tc.steps: step1. test WriteToFile function
139 * @tc.expected: step1. make the position is -1, the return will be READ_OFFSET_OUT_OF_RANGE.
140 */
141 EXPECT_EQ(outputHap->WriteToFile(buffer, -1, 0), READ_OFFSET_OUT_OF_RANGE);
142 }
143
144 /**
145 * @tc.name: Test ReadFileFullyFromOffset Function
146 * @tc.desc: Test function of RandomAccessFile::ReadFileFullyFromOffset() interface for SUCCESS.
147 * @tc.type: FUNC
148 * @tc.require: SR000H63TL
149 */
150 HWTEST_F(RandomAccessFileInputOutputTest, ReadFileFullyFromOffsetTest002, testing::ext::TestSize.Level1)
151 {
152 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
153 ASSERT_TRUE(outputHap->Init(UNSIGNED_HAP_PATH));
154 std::string buf(1, 0);
155 EXPECT_GT(outputHap->ReadFileFullyFromOffset(buf.data(), 0, 1), 0);
156 }
157
158 /**
159 * @tc.name: Test RandomAccessFileInput Function
160 * @tc.desc: Test function of RandomAccessFileInput::RandomAccessFileInput() interface for SUCCESS.
161 * @tc.type: FUNC
162 * @tc.require: SR000H63TL
163 */
164 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileInputTest001, testing::ext::TestSize.Level1)
165 {
166 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
167 EXPECT_EQ(outputHap->Init(SIGNED_HAP_PATH), true);
168 std::shared_ptr<ZipDataInput> outputHapIn = std::make_shared<RandomAccessFileInput>(*outputHap, 1, 1);
169 EXPECT_EQ(outputHapIn != nullptr, true);
170 }
171
172 /**
173 * @tc.name: Test RandomAccessFileInput Function
174 * @tc.desc: Test function of RandomAccessFileInput::RandomAccessFileInput() interface.
175 * @tc.type: FUNC
176 * @tc.require: SR000H63TL
177 */
178 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileInputTest002, testing::ext::TestSize.Level1)
179 {
180 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
181 EXPECT_EQ(outputHap->Init(SIGNED_HAP_PATH), true);
182 /*
183 * @tc.steps: step1. test RandomAccessFileInput Constructor.
184 * @tc.expected: step1. make the size is -1, the operation will be return.
185 */
186 std::shared_ptr<ZipDataInput> outputHapIn = std::make_shared<RandomAccessFileInput>(*outputHap, 1, -1);
187 EXPECT_EQ(outputHapIn != nullptr, true);
188 }
189
190 /**
191 * @tc.name: Test RandomAccessFileInput Function
192 * @tc.desc: Test function of RandomAccessFileInput::RandomAccessFileInput() interface for FAIL.
193 * @tc.type: FUNC
194 * @tc.require: SR000H63TL
195 */
196 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileInputTest003, testing::ext::TestSize.Level1)
197 {
198 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
199 EXPECT_EQ(outputHap->Init(SIGNED_HAP_PATH), true);
200 /*
201 * @tc.steps: step1. test RandomAccessFileInput Constructor.
202 * @tc.expected: step1. make the offset is -1, the operation will be return.
203 */
204 std::shared_ptr<ZipDataInput> outputHapIn = std::make_shared<RandomAccessFileInput>(*outputHap, -1, 1);
205 EXPECT_EQ(outputHapIn != nullptr, true);
206 }
207
208 /**
209 * @tc.name: Test Slice Function
210 * @tc.desc: Test function of RandomAccessFileInput::Slice() interface for SUCCESS.
211 * @tc.type: FUNC
212 * @tc.require: SR000H63TL
213 */
214 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileInputSliceTest001, testing::ext::TestSize.Level1)
215 {
216 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
217 EXPECT_EQ(outputHap->Init(SIGNED_HAP_PATH), true);
218 std::shared_ptr<ZipDataInput> outputHapIn = std::make_shared<RandomAccessFileInput>(*outputHap);
219 std::pair<ByteBuffer, int64_t> eocdPair;
220 EXPECT_EQ(HapSignerBlockUtils::FindEocdInHap(*outputHap, eocdPair), true);
221 int64_t centralDirectoryOffset;
222 EXPECT_EQ(HapSignerBlockUtils::GetCentralDirectoryOffset(eocdPair.first, eocdPair.second, centralDirectoryOffset),
223 true);
224 DataSource* beforeCentralDir = outputHapIn->Slice(0, centralDirectoryOffset);
225 EXPECT_EQ(beforeCentralDir != nullptr, true);
226 }
227
228 /**
229 * @tc.name: Test Slice Function
230 * @tc.desc: Test function of RandomAccessFileInput::Slice() interface
231 * @tc.type: FUNC
232 * @tc.require: SR000H63TL
233 */
234 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileInputSliceTest002, testing::ext::TestSize.Level1)
235 {
236 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
237 ASSERT_TRUE(outputHap->Init(SIGNED_HAP_PATH));
238 auto outputHapIn1 = std::make_shared<RandomAccessFileInput>(*outputHap, 0, 10);
239 // step1: make the offset is less than 0
240 ASSERT_EQ(outputHapIn1->Slice(-1, 10), nullptr);
241
242 auto outputHapIn2 = std::make_shared<RandomAccessFileInput>(*outputHap, 0, 10);
243 // step2: make the size is less than 0
244 ASSERT_EQ(outputHapIn2->Slice(0, -1), nullptr);
245
246 auto outputHapIn3 = std::make_shared<RandomAccessFileInput>(*outputHap, 0, 10);
247 // step3: make the offset is greater than RandomAccessFile's length
248 ASSERT_EQ(outputHapIn3->Slice(20, 1), nullptr);
249
250 auto outputHapIn4 = std::make_shared<RandomAccessFileInput>(*outputHap, 0, 10);
251 // step4: make the offset plus the size is greater than RandomAccessFile's length
252 ASSERT_EQ(outputHapIn4->Slice(0, 20), nullptr);
253
254 auto outputHapIn5 = std::make_shared<RandomAccessFileInput>(*outputHap, 0, 10);
255 // step5: make the offset is zero and the offset is RandomAccessFile's length
256 ASSERT_NE(outputHapIn5->Slice(0, 10), nullptr);
257
258 auto outputHapIn6 = std::make_shared<RandomAccessFileInput>(*outputHap, 0, 10);
259 // step6: make the offset plus the offset is overflow
260 ASSERT_NE(outputHapIn6->Slice(1, LLONG_MAX), nullptr);
261
262 auto outputHapIn7 = std::make_shared<RandomAccessFileInput>(*outputHap, 0, 10);
263 // step7: make the offset is not equal to zero and the offset is not equal to RandomAccessFile's length
264 ASSERT_NE(outputHapIn7->Slice(1, 5), nullptr);
265 }
266
267 /**
268 * @tc.name: Test CreateByteBuffer Function
269 * @tc.desc: Test function of RandomAccessFileInput::CreateByteBuffer() interface for SUCCESS.
270 * @tc.type: FUNC
271 * @tc.require: SR000H63TL
272 */
273 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileInputCreateByteBufferTest001, testing::ext::TestSize.Level1)
274 {
275 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
276 EXPECT_EQ(outputHap->Init(SIGNED_HAP_PATH), true);
277 std::shared_ptr<ZipDataInput> outputHapIn = std::make_shared<RandomAccessFileInput>(*outputHap);
278 std::pair<ByteBuffer, int64_t> eocdPair;
279 EXPECT_EQ(HapSignerBlockUtils::FindEocdInHap(*outputHap, eocdPair), true);
280 int64_t centralDirectoryOffset;
281 EXPECT_EQ(HapSignerBlockUtils::GetCentralDirectoryOffset(eocdPair.first, eocdPair.second, centralDirectoryOffset),
282 true);
283 DataSource* beforeCentralDir = outputHapIn->Slice(0, centralDirectoryOffset);
284 EXPECT_EQ(beforeCentralDir != nullptr, true);
285 long centralDirectorySize;
286 EXPECT_EQ(HapSignerBlockUtils::GetCentralDirectorySize(eocdPair.first, centralDirectorySize), true);
287 ByteBuffer centralDirBuffer = outputHapIn->CreateByteBuffer(centralDirectoryOffset, centralDirectorySize);
288 EXPECT_EQ(centralDirBuffer.GetCapacity() > 0, true);
289 }
290
291 /**
292 * @tc.name: Test CreateByteBuffer Function
293 * @tc.desc: Test function of RandomAccessFileInput::CreateByteBuffer() interface for FAIL.
294 * @tc.type: FUNC
295 * @tc.require: SR000H63TL
296 */
297 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileInputCreateByteBufferTest002, testing::ext::TestSize.Level1)
298 {
299 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
300 ASSERT_TRUE(outputHap->Init(SIGNED_HAP_PATH));
301 std::shared_ptr<ZipDataInput> outputHapIn = std::make_shared<RandomAccessFileInput>(*outputHap);
302 std::pair<ByteBuffer, int64_t> eocdPair;
303 ASSERT_TRUE(HapSignerBlockUtils::FindEocdInHap(*outputHap, eocdPair));
304 /*
305 * @tc.steps: step1. test CreateByteBuffer function.
306 * @tc.expected: step1. make the offset is -1, the CheckBoundValid function return will be false,
307 * and ByteBuffer's capacity is 0
308 */
309 ByteBuffer centralDirBuffer1 = outputHapIn->CreateByteBuffer(-1, 1);
310 ASSERT_EQ(centralDirBuffer1.GetCapacity(), 0);
311 /*
312 * @tc.steps: step2. test CreateByteBuffer function.
313 * @tc.expected: step2. make the size is -1, the CopyTo function return will be false, and ByteBuffer's
314 * capacity is 0
315 */
316 ByteBuffer centralDirBuffer2 = outputHapIn->CreateByteBuffer(1, 0);
317 ASSERT_EQ(centralDirBuffer2.GetCapacity(), 0);
318 }
319
320 /**
321 * @tc.name: Test Write ByteBuffer Function
322 * @tc.desc: Test function of RandomAccessFileOutput::Write() interface for SUCCESS.
323 * @tc.type: FUNC
324 * @tc.require: SR000H63TL
325 */
326 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileOutputWriteTest001, testing::ext::TestSize.Level1)
327 {
328 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
329 EXPECT_EQ(outputHap->Init(UNSIGNED_HAP_PATH), true);
330 std::pair<ByteBuffer, int64_t> eocdPair;
331 EXPECT_EQ(HapSignerBlockUtils::FindEocdInHap(*outputHap, eocdPair), true);
332 int64_t centralDirectoryOffset;
333 EXPECT_EQ(HapSignerBlockUtils::GetCentralDirectoryOffset(eocdPair.first, eocdPair.second, centralDirectoryOffset),
334 true);
335 std::shared_ptr<RandomAccessFileOutput> outputHapOut =
336 std::make_shared<RandomAccessFileOutput>(outputHap.get(), centralDirectoryOffset);
337 ByteBuffer signingBlock(1);
338 signingBlock.PutByte(1);
339 EXPECT_EQ(outputHapOut->Write(signingBlock), true);
340 }
341
342 /**
343 * @tc.name: Test Write ByteBuffer Function
344 * @tc.desc: Test function of RandomAccessFileOutput::Write() interface for FAIL.
345 * @tc.type: FUNC
346 * @tc.require: SR000H63TL
347 */
348 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileOutputWriteTest002, testing::ext::TestSize.Level1)
349 {
350 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
351 EXPECT_EQ(outputHap->Init(UNSIGNED_HAP_PATH), true);
352 std::pair<ByteBuffer, int64_t> eocdPair;
353 EXPECT_EQ(HapSignerBlockUtils::FindEocdInHap(*outputHap, eocdPair), true);
354 int64_t centralDirectoryOffset;
355 EXPECT_EQ(HapSignerBlockUtils::GetCentralDirectoryOffset(eocdPair.first, eocdPair.second, centralDirectoryOffset),
356 true);
357 std::shared_ptr<RandomAccessFileOutput> outputHapOut =
358 std::make_shared<RandomAccessFileOutput>(outputHap.get(), centralDirectoryOffset);
359 /*
360 * @tc.steps: step1. test Write function.
361 * @tc.expected: step1. make the ByteBuffer is empty, the return will be false.
362 */
363 ByteBuffer signingBlock;
364 EXPECT_EQ(outputHapOut->Write(signingBlock), false);
365 }
366
367 /**
368 * @tc.name: Test Write ByteBuffer Function
369 * @tc.desc: Test function of RandomAccessFileOutput() interface for SUCCESS.
370 * @tc.type: FUNC
371 * @tc.require: SR000H63TL
372 */
373 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileOutputTest001, testing::ext::TestSize.Level1)
374 {
375 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
376 EXPECT_EQ(outputHap->Init(UNSIGNED_HAP_PATH), true);
377 std::shared_ptr<RandomAccessFileOutput> outputHapOut =
378 std::make_shared<RandomAccessFileOutput>(outputHap.get());
379 EXPECT_EQ(outputHapOut != nullptr, true);
380 }
381
382 /**
383 * @tc.name: Test Write ByteBuffer Function
384 * @tc.desc: Test function of RandomAccessFileOutput() interface for FAIL.
385 * @tc.type: FUNC
386 * @tc.require: SR000H63TL
387 */
388 HWTEST_F(RandomAccessFileInputOutputTest, RandomAccessFileOutputTest002, testing::ext::TestSize.Level1)
389 {
390 std::shared_ptr<RandomAccessFile> outputHap = std::make_shared<RandomAccessFile>();
391 EXPECT_EQ(outputHap->Init(UNSIGNED_HAP_PATH), true);
392 /*
393 * @tc.steps: step1. test RandomAccessFileOutput Constructor.
394 * @tc.expected: step1. make the startPosition is -1, the operation will be return.
395 */
396 std::shared_ptr<RandomAccessFileOutput> outputHapOut =
397 std::make_shared<RandomAccessFileOutput>(outputHap.get(), -1);
398 EXPECT_EQ(outputHapOut != nullptr, true);
399 }
400 } // namespace SignatureTools
401 } // namespace OHOS