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 26using namespace testing::ext; 27using namespace UpdaterUt; 28using namespace Updater; 29using namespace std; 30 31namespace UpdaterUt { 32void BlockSetUnitTest::SetUp(void) 33{ 34 cout << "SetUpTestCase" << endl; 35} 36 37void BlockSetUnitTest::TearDown(void) 38{ 39 cout << "TearDownTestCase" << endl; 40} 41 42HWTEST_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 69HWTEST_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 81HWTEST_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 size_t ret = blk.WriteDataToBlock(fd, buffer); 95 close(fd); 96 EXPECT_NE(ret, 0); 97} 98 99HWTEST_F(BlockSetUnitTest, blockset_test_004, TestSize.Level1) 100{ 101 cout << "Blockset ut two blocks overlap"; 102 std::vector<uint8_t> srcBuffer; 103 srcBuffer.resize(H_BLOCK_SIZE); 104 std::vector<uint8_t> tgtBuffer; 105 tgtBuffer.resize(H_BLOCK_SIZE); 106 BlockSet blk(std::vector<BlockPair> {BlockPair{0, 1}}); 107 std::fill(srcBuffer.begin(), srcBuffer.end(), 0); 108 std::fill(tgtBuffer.begin(), tgtBuffer.end(), 0); 109 BlockSet::MoveBlock(srcBuffer, blk, tgtBuffer); 110 EXPECT_EQ(tgtBuffer.size(), H_BLOCK_SIZE); 111} 112 113HWTEST_F(BlockSetUnitTest, blockset_test_005, TestSize.Level1) 114{ 115 std::string hashValue = "5aa246ebe8e817740f12cc0f6e536c5ea22e5db177563a1caea5a86614275546"; 116 std::string blockInfo = "2,20755,21031 276 2,20306,20582"; 117 std::string cmdLine = std::string("move ") + hashValue + " " + blockInfo; 118 int fd = open("/data/updater/updater/blocksetTest.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO); 119 if (fd < 0) { 120 printf("Open file failed"); 121 return; 122 } 123 std::unique_ptr<TransferParams> transferParams = std::make_unique<TransferParams>(); 124 transferParams->writerThreadInfo = std::make_unique<WriterThreadInfo>(); 125 Command *cmd = new Command(transferParams.get()); 126 cmd->Init(cmdLine); 127 cmd->SetFileDescriptor(fd); 128 BlockSet targetBlock; 129 size_t blockSize = H_BLOCK_SIZE; 130 std::vector<uint8_t> srcBuffer(blockSize); 131 std::vector<uint8_t> patchBuffer(blockSize); 132 bool isImgDiff = true; 133 int ret = targetBlock.WriteDiffToBlock(const_cast<const Command &>(*cmd), 134 srcBuffer, patchBuffer.data(), blockSize, isImgDiff); 135 EXPECT_EQ(ret, -1); 136 isImgDiff = false; 137 ret = targetBlock.WriteDiffToBlock(const_cast<const Command &>(*cmd), 138 srcBuffer, patchBuffer.data(), blockSize, isImgDiff); 139 EXPECT_EQ(ret, -1); 140 close(fd); 141 delete cmd; 142} 143 144HWTEST_F(BlockSetUnitTest, blockset_test_006, TestSize.Level1) 145{ 146 int fd = open("/data/updater/updater/blocksetTest.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO); 147 if (fd < 0) { 148 printf("Open file failed"); 149 return; 150 } 151 std::vector<uint8_t> buffer; 152 buffer.resize(H_BLOCK_SIZE); 153 BlockSet myBlock; 154 size_t ret = myBlock.ReadDataFromBlock(fd, buffer); 155 EXPECT_EQ(ret, 0); 156 BlockSet myBlock2({std::vector<BlockPair>{}}); 157 BlockSet myBlock3({std::vector<BlockPair>{BlockPair{0, 1}}}); 158 BlockSet myBlock4({std::vector<BlockPair>{BlockPair{-1, 0}}}); 159 ret = myBlock3.ReadDataFromBlock(fd, buffer); 160 EXPECT_EQ(ret, 0); 161 ret = myBlock3.WriteDataToBlock(fd, buffer); 162 EXPECT_EQ(ret, 4096); 163 ret = myBlock4.ReadDataFromBlock(fd, buffer); 164 EXPECT_EQ(ret, 0); 165 close(fd); 166} 167} 168