• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2021 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 "blockset_unittest.h"
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20 #include <fcntl.h>
21 #include <vector>
22 #include "applypatch/block_set.h"
23 #include "applypatch/command.h"
24 #include "log/log.h"
25 
26 using namespace testing::ext;
27 using namespace updater_ut;
28 using namespace updater;
29 using namespace std;
30 
31 namespace updater_ut {
SetUp(void)32 void BlockSetUnitTest::SetUp(void)
33 {
34     cout << "SetUpTestCase" << endl;
35 }
36 
TearDown(void)37 void BlockSetUnitTest::TearDown(void)
38 {
39     cout << "TearDownTestCase" << endl;
40 }
41 
42 HWTEST_F(BlockSetUnitTest, blockset_test_001, TestSize.Level1)
43 {
44     cout << "Blockset ut start";
45     BlockSet block(std::vector<BlockPair> {BlockPair{0, 1}});
46     cout << "Blockset ut init end";
47     size_t countOfRanges = block.CountOfRanges();
48     cout << "Blockset ranges: " << countOfRanges;
49     auto itBegin = block.Begin();
50     auto itEnd = block.End();
51     auto itCBegin = block.CBegin();
52     auto itCEnd = block.CEnd();
53     auto itCrBegin = block.CrBegin();
54     auto itCrEnd = block.CrEnd();
55     if (itBegin != itEnd)
56     cout << "Right iterator";
57     if (itCBegin != itCEnd)
58     cout << "Right iterator";
59     if (itCrBegin != itCrEnd)
60     cout << "Right iterator";
61     std::vector<uint8_t> buffer;
62     buffer.resize(H_BLOCK_SIZE);
63     std::fill(buffer.begin(), buffer.end(), 0);
64     string sha256 = "fdfasdf";
65     auto ret = block.VerifySha256(buffer, block.TotalBlockSize(), sha256);
66     EXPECT_EQ(ret, -1);
67 }
68 
69 HWTEST_F(BlockSetUnitTest, blockset_test_002, TestSize.Level1)
70 {
71     cout << "Blockset ut two blocks overlap";
72     BlockSet block(std::vector<BlockPair> {BlockPair{0, 1}});
73     BlockSet block2(std::vector<BlockPair> {BlockPair{0, 1}});
74     BlockSet block3(std::vector<BlockPair> {BlockPair{2, 3}});
75     bool ret = BlockSet::IsTwoBlocksOverlap(block, block2);
76     EXPECT_EQ(ret, true);
77     ret = BlockSet::IsTwoBlocksOverlap(block, block3);
78     EXPECT_EQ(ret, false);
79 }
80 
81 HWTEST_F(BlockSetUnitTest, blockset_test_003, TestSize.Level1)
82 {
83     cout << "Blockset ut two blocks overlap";
84     std::vector<uint8_t> buffer;
85     buffer.resize(H_BLOCK_SIZE);
86     BlockSet blk(std::vector<BlockPair> {BlockPair{0, 1}});
87     std::fill(buffer.begin(), buffer.end(), 0);
88     std::string filename = "/tmp/ut_blockset";
89     int fd = open(filename.c_str(), O_RDWR);
90     if (fd < 0) {
91         printf("Open file failed");
92         return;
93     }
94     blk.WriteDataToBlock(fd, buffer);
95     close(fd);
96 }
97 
98 HWTEST_F(BlockSetUnitTest, blockset_test_004, TestSize.Level1)
99 {
100     cout << "Blockset ut two blocks overlap";
101     std::vector<uint8_t> srcBuffer;
102     srcBuffer.resize(H_BLOCK_SIZE);
103     std::vector<uint8_t> tgtBuffer;
104     tgtBuffer.resize(H_BLOCK_SIZE);
105     BlockSet blk(std::vector<BlockPair> {BlockPair{0, 1}});
106     std::fill(srcBuffer.begin(), srcBuffer.end(), 0);
107     std::fill(tgtBuffer.begin(), tgtBuffer.end(), 0);
108     BlockSet::MoveBlock(srcBuffer, blk, tgtBuffer);
109 }
110 
111 HWTEST_F(BlockSetUnitTest, blockset_test_005, TestSize.Level1)
112 {
113     std::string hashValue = "5aa246ebe8e817740f12cc0f6e536c5ea22e5db177563a1caea5a86614275546";
114     std::string blockInfo = "2,20755,21031 276 2,20306,20582";
115     std::string cmdLine = std::string("move ") + hashValue + " " + blockInfo;
116     int fd = open("/data/updater/updater/blocksetTest.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO);
117     if (fd < 0) {
118         printf("Open file failed");
119         return;
120     }
121     Command *cmd = new Command();
122     cmd->Init(cmdLine);
123     cmd->SetFileDescriptor(fd);
124     BlockSet targetBlock;
125     size_t tgtBlockSize = H_BLOCK_SIZE;
126     std::vector<uint8_t> buffer(tgtBlockSize);
127     bool isImgDiff = true;
128     int ret = targetBlock.WriteDiffToBlock(const_cast<const Command &>(*cmd), buffer, tgtBlockSize, isImgDiff);
129     EXPECT_EQ(ret, -1);
130     isImgDiff = false;
131     ret = targetBlock.WriteDiffToBlock(const_cast<const Command &>(*cmd), buffer, tgtBlockSize, isImgDiff);
132     EXPECT_EQ(ret, -1);
133     close(fd);
134     delete cmd;
135 }
136 }
137