• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "random_access_file_test.h"
17 
18 #include <string>
19 
20 #include <gtest/gtest.h>
21 
22 #include "byte_buffer.h"
23 #include "random_access_file.h"
24 
25 #include "hap_signer_block_utils.h"
26 #include "hap_signer_block_utils_test.h"
27 #include "test_const.h"
28 
29 using namespace testing::ext;
30 namespace OHOS {
31 namespace SignatureTools {
32 class RandomAccessFileTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35 
36     static void TearDownTestCase(void);
37 
38     void SetUp();
39 
40     void TearDown();
41 };
42 
SetUpTestCase(void)43 void RandomAccessFileTest::SetUpTestCase(void)
44 {
45 }
46 
TearDownTestCase(void)47 void RandomAccessFileTest::TearDownTestCase(void)
48 {
49 }
50 
SetUp()51 void RandomAccessFileTest::SetUp()
52 {
53 }
54 
TearDown()55 void RandomAccessFileTest::TearDown()
56 {
57 }
58 
59 /**
60  * @tc.name: Test ReadFileFullyFromOffset function
61  * @tc.desc: The static function will return each reading result;
62  * @tc.type: FUNC
63  */
64 HWTEST_F(RandomAccessFileTest, ReadFileFullyFromOffsetTest001, TestSize.Level1)
65 {
66     /*
67         * @tc.steps: step1. use null buffer to ReadFileFullyFromOffset .
68         * @tc.expected: step1. the return will be DEST_BUFFER_IS_NULL.
69         */
70     std::string filePath = "./hapVerify/test_hapverify.zip";
71     SignatureInfo si0;
72     int64_t sumLen = HapSignerBlockUtils::CreatTestZipFile(filePath, si0);
73     RandomAccessFile hapTestFile1;
74     bool initRet = hapTestFile1.Init(filePath);
75     ASSERT_TRUE(initRet);
76     ASSERT_TRUE(hapTestFile1.GetLength() == sumLen);
77     ReadFileErrorCode targetCode = DEST_BUFFER_IS_NULL;
78     int32_t ret = hapTestFile1.ReadFileFullyFromOffset(nullptr, 0, 0);
79     ASSERT_TRUE(ret == targetCode);
80     ByteBuffer nullBuffer;
81     ret = hapTestFile1.ReadFileFullyFromOffset(nullBuffer, 0);
82     ASSERT_TRUE(ret == targetCode);
83     /*
84         * @tc.steps: step2. use a buffer to read a null file.
85         * @tc.expected: step2. the return will be FILE_IS_CLOSE.
86         */
87     filePath = "./hapVerify/test_hapverify1.zip";
88     RandomAccessFile nullTestFile;
89     initRet = nullTestFile.Init(filePath);
90     ASSERT_FALSE(initRet);
91     std::unique_ptr<char[]> buffer = std::make_unique<char[]>(TEST_RANDOMREAD_LENGTH);
92     ASSERT_TRUE(buffer != nullptr);
93     ret = nullTestFile.ReadFileFullyFromOffset(buffer.get(), 0, TEST_RANDOMREAD_LENGTH);
94     ASSERT_EQ(ret, FILE_IS_CLOSE);
95     /*
96         * @tc.steps: step3. use a large buffer to read a mini file.
97         * @tc.expected: step3. the return will be READ_DATA_NOT_ENOUGH.
98         */
99     std::string testFile = "./hapVerify/test_hapverify.txt";
100     SignatureInfo si;
101     sumLen = HapSignerBlockUtils::CreatTestZipFile(testFile, si);
102     RandomAccessFile hapTestFile2;
103     initRet = hapTestFile2.Init(testFile);
104     ASSERT_TRUE(initRet);
105     ASSERT_TRUE(hapTestFile2.GetLength() == sumLen);
106     ret = hapTestFile2.ReadFileFullyFromOffset(buffer.get(), 0, TEST_RANDOMREAD_LENGTH);
107     ASSERT_EQ(ret, READ_OFFSET_OUT_OF_RANGE);
108     ByteBuffer hapBuffer(TEST_RANDOMREAD_LENGTH);
109     ret = hapTestFile2.ReadFileFullyFromOffset(hapBuffer, 0);
110     ASSERT_EQ(ret, READ_OFFSET_OUT_OF_RANGE);
111     /*
112         * @tc.steps: step4. use a negative offset to read a file.
113         * @tc.expected: step4. the return will be READ_OFFSET_OUT_OF_RANGE.
114         */
115     ret = hapTestFile2.ReadFileFullyFromOffset(hapBuffer, -1);
116     ASSERT_TRUE(ret == READ_OFFSET_OUT_OF_RANGE);
117     ret = hapTestFile2.ReadFileFullyFromOffset(buffer.get(), -1, TEST_RANDOMREAD_LENGTH);
118     ASSERT_TRUE(ret == READ_OFFSET_OUT_OF_RANGE);
119     buffer.reset(nullptr);
120 }
121 } // namespace SignatureTools
122 } // namespace OHOS