1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci* Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci* you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci* You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci* 7fb299fa2Sopenharmony_ci* http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci* 9fb299fa2Sopenharmony_ci* Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci* See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci* limitations under the License. 14fb299fa2Sopenharmony_ci*/ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include "blockset_unittest.h" 17fb299fa2Sopenharmony_ci#include <cstdio> 18fb299fa2Sopenharmony_ci#include <cstdlib> 19fb299fa2Sopenharmony_ci#include <cstring> 20fb299fa2Sopenharmony_ci#include <fcntl.h> 21fb299fa2Sopenharmony_ci#include <vector> 22fb299fa2Sopenharmony_ci#include "applypatch/block_set.h" 23fb299fa2Sopenharmony_ci#include "applypatch/command.h" 24fb299fa2Sopenharmony_ci#include "log/log.h" 25fb299fa2Sopenharmony_ci 26fb299fa2Sopenharmony_ciusing namespace testing::ext; 27fb299fa2Sopenharmony_ciusing namespace UpdaterUt; 28fb299fa2Sopenharmony_ciusing namespace Updater; 29fb299fa2Sopenharmony_ciusing namespace std; 30fb299fa2Sopenharmony_ci 31fb299fa2Sopenharmony_cinamespace UpdaterUt { 32fb299fa2Sopenharmony_civoid BlockSetUnitTest::SetUp(void) 33fb299fa2Sopenharmony_ci{ 34fb299fa2Sopenharmony_ci cout << "SetUpTestCase" << endl; 35fb299fa2Sopenharmony_ci} 36fb299fa2Sopenharmony_ci 37fb299fa2Sopenharmony_civoid BlockSetUnitTest::TearDown(void) 38fb299fa2Sopenharmony_ci{ 39fb299fa2Sopenharmony_ci cout << "TearDownTestCase" << endl; 40fb299fa2Sopenharmony_ci} 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_ciHWTEST_F(BlockSetUnitTest, blockset_test_001, TestSize.Level1) 43fb299fa2Sopenharmony_ci{ 44fb299fa2Sopenharmony_ci cout << "Blockset ut start"; 45fb299fa2Sopenharmony_ci BlockSet block(std::vector<BlockPair> {BlockPair{0, 1}}); 46fb299fa2Sopenharmony_ci cout << "Blockset ut init end"; 47fb299fa2Sopenharmony_ci size_t countOfRanges = block.CountOfRanges(); 48fb299fa2Sopenharmony_ci cout << "Blockset ranges: " << countOfRanges; 49fb299fa2Sopenharmony_ci auto itBegin = block.Begin(); 50fb299fa2Sopenharmony_ci auto itEnd = block.End(); 51fb299fa2Sopenharmony_ci auto itCBegin = block.CBegin(); 52fb299fa2Sopenharmony_ci auto itCEnd = block.CEnd(); 53fb299fa2Sopenharmony_ci auto itCrBegin = block.CrBegin(); 54fb299fa2Sopenharmony_ci auto itCrEnd = block.CrEnd(); 55fb299fa2Sopenharmony_ci if (itBegin != itEnd) 56fb299fa2Sopenharmony_ci cout << "Right iterator"; 57fb299fa2Sopenharmony_ci if (itCBegin != itCEnd) 58fb299fa2Sopenharmony_ci cout << "Right iterator"; 59fb299fa2Sopenharmony_ci if (itCrBegin != itCrEnd) 60fb299fa2Sopenharmony_ci cout << "Right iterator"; 61fb299fa2Sopenharmony_ci std::vector<uint8_t> buffer; 62fb299fa2Sopenharmony_ci buffer.resize(H_BLOCK_SIZE); 63fb299fa2Sopenharmony_ci std::fill(buffer.begin(), buffer.end(), 0); 64fb299fa2Sopenharmony_ci string sha256 = "fdfasdf"; 65fb299fa2Sopenharmony_ci auto ret = block.VerifySha256(buffer, block.TotalBlockSize(), sha256); 66fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 67fb299fa2Sopenharmony_ci} 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_ciHWTEST_F(BlockSetUnitTest, blockset_test_002, TestSize.Level1) 70fb299fa2Sopenharmony_ci{ 71fb299fa2Sopenharmony_ci cout << "Blockset ut two blocks overlap"; 72fb299fa2Sopenharmony_ci BlockSet block(std::vector<BlockPair> {BlockPair{0, 1}}); 73fb299fa2Sopenharmony_ci BlockSet block2(std::vector<BlockPair> {BlockPair{0, 1}}); 74fb299fa2Sopenharmony_ci BlockSet block3(std::vector<BlockPair> {BlockPair{2, 3}}); 75fb299fa2Sopenharmony_ci bool ret = BlockSet::IsTwoBlocksOverlap(block, block2); 76fb299fa2Sopenharmony_ci EXPECT_EQ(ret, true); 77fb299fa2Sopenharmony_ci ret = BlockSet::IsTwoBlocksOverlap(block, block3); 78fb299fa2Sopenharmony_ci EXPECT_EQ(ret, false); 79fb299fa2Sopenharmony_ci} 80fb299fa2Sopenharmony_ci 81fb299fa2Sopenharmony_ciHWTEST_F(BlockSetUnitTest, blockset_test_003, TestSize.Level1) 82fb299fa2Sopenharmony_ci{ 83fb299fa2Sopenharmony_ci cout << "Blockset ut two blocks overlap"; 84fb299fa2Sopenharmony_ci std::vector<uint8_t> buffer; 85fb299fa2Sopenharmony_ci buffer.resize(H_BLOCK_SIZE); 86fb299fa2Sopenharmony_ci BlockSet blk(std::vector<BlockPair> {BlockPair{0, 1}}); 87fb299fa2Sopenharmony_ci std::fill(buffer.begin(), buffer.end(), 0); 88fb299fa2Sopenharmony_ci std::string filename = "/tmp/ut_blockset"; 89fb299fa2Sopenharmony_ci int fd = open(filename.c_str(), O_RDWR); 90fb299fa2Sopenharmony_ci if (fd < 0) { 91fb299fa2Sopenharmony_ci printf("Open file failed"); 92fb299fa2Sopenharmony_ci return; 93fb299fa2Sopenharmony_ci } 94fb299fa2Sopenharmony_ci size_t ret = blk.WriteDataToBlock(fd, buffer); 95fb299fa2Sopenharmony_ci close(fd); 96fb299fa2Sopenharmony_ci EXPECT_NE(ret, 0); 97fb299fa2Sopenharmony_ci} 98fb299fa2Sopenharmony_ci 99fb299fa2Sopenharmony_ciHWTEST_F(BlockSetUnitTest, blockset_test_004, TestSize.Level1) 100fb299fa2Sopenharmony_ci{ 101fb299fa2Sopenharmony_ci cout << "Blockset ut two blocks overlap"; 102fb299fa2Sopenharmony_ci std::vector<uint8_t> srcBuffer; 103fb299fa2Sopenharmony_ci srcBuffer.resize(H_BLOCK_SIZE); 104fb299fa2Sopenharmony_ci std::vector<uint8_t> tgtBuffer; 105fb299fa2Sopenharmony_ci tgtBuffer.resize(H_BLOCK_SIZE); 106fb299fa2Sopenharmony_ci BlockSet blk(std::vector<BlockPair> {BlockPair{0, 1}}); 107fb299fa2Sopenharmony_ci std::fill(srcBuffer.begin(), srcBuffer.end(), 0); 108fb299fa2Sopenharmony_ci std::fill(tgtBuffer.begin(), tgtBuffer.end(), 0); 109fb299fa2Sopenharmony_ci BlockSet::MoveBlock(srcBuffer, blk, tgtBuffer); 110fb299fa2Sopenharmony_ci EXPECT_EQ(tgtBuffer.size(), H_BLOCK_SIZE); 111fb299fa2Sopenharmony_ci} 112fb299fa2Sopenharmony_ci 113fb299fa2Sopenharmony_ciHWTEST_F(BlockSetUnitTest, blockset_test_005, TestSize.Level1) 114fb299fa2Sopenharmony_ci{ 115fb299fa2Sopenharmony_ci std::string hashValue = "5aa246ebe8e817740f12cc0f6e536c5ea22e5db177563a1caea5a86614275546"; 116fb299fa2Sopenharmony_ci std::string blockInfo = "2,20755,21031 276 2,20306,20582"; 117fb299fa2Sopenharmony_ci std::string cmdLine = std::string("move ") + hashValue + " " + blockInfo; 118fb299fa2Sopenharmony_ci int fd = open("/data/updater/updater/blocksetTest.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO); 119fb299fa2Sopenharmony_ci if (fd < 0) { 120fb299fa2Sopenharmony_ci printf("Open file failed"); 121fb299fa2Sopenharmony_ci return; 122fb299fa2Sopenharmony_ci } 123fb299fa2Sopenharmony_ci std::unique_ptr<TransferParams> transferParams = std::make_unique<TransferParams>(); 124fb299fa2Sopenharmony_ci transferParams->writerThreadInfo = std::make_unique<WriterThreadInfo>(); 125fb299fa2Sopenharmony_ci Command *cmd = new Command(transferParams.get()); 126fb299fa2Sopenharmony_ci cmd->Init(cmdLine); 127fb299fa2Sopenharmony_ci cmd->SetFileDescriptor(fd); 128fb299fa2Sopenharmony_ci BlockSet targetBlock; 129fb299fa2Sopenharmony_ci size_t blockSize = H_BLOCK_SIZE; 130fb299fa2Sopenharmony_ci std::vector<uint8_t> srcBuffer(blockSize); 131fb299fa2Sopenharmony_ci std::vector<uint8_t> patchBuffer(blockSize); 132fb299fa2Sopenharmony_ci bool isImgDiff = true; 133fb299fa2Sopenharmony_ci int ret = targetBlock.WriteDiffToBlock(const_cast<const Command &>(*cmd), 134fb299fa2Sopenharmony_ci srcBuffer, patchBuffer.data(), blockSize, isImgDiff); 135fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 136fb299fa2Sopenharmony_ci isImgDiff = false; 137fb299fa2Sopenharmony_ci ret = targetBlock.WriteDiffToBlock(const_cast<const Command &>(*cmd), 138fb299fa2Sopenharmony_ci srcBuffer, patchBuffer.data(), blockSize, isImgDiff); 139fb299fa2Sopenharmony_ci EXPECT_EQ(ret, -1); 140fb299fa2Sopenharmony_ci close(fd); 141fb299fa2Sopenharmony_ci delete cmd; 142fb299fa2Sopenharmony_ci} 143fb299fa2Sopenharmony_ci 144fb299fa2Sopenharmony_ciHWTEST_F(BlockSetUnitTest, blockset_test_006, TestSize.Level1) 145fb299fa2Sopenharmony_ci{ 146fb299fa2Sopenharmony_ci int fd = open("/data/updater/updater/blocksetTest.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO); 147fb299fa2Sopenharmony_ci if (fd < 0) { 148fb299fa2Sopenharmony_ci printf("Open file failed"); 149fb299fa2Sopenharmony_ci return; 150fb299fa2Sopenharmony_ci } 151fb299fa2Sopenharmony_ci std::vector<uint8_t> buffer; 152fb299fa2Sopenharmony_ci buffer.resize(H_BLOCK_SIZE); 153fb299fa2Sopenharmony_ci BlockSet myBlock; 154fb299fa2Sopenharmony_ci size_t ret = myBlock.ReadDataFromBlock(fd, buffer); 155fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 156fb299fa2Sopenharmony_ci BlockSet myBlock2({std::vector<BlockPair>{}}); 157fb299fa2Sopenharmony_ci BlockSet myBlock3({std::vector<BlockPair>{BlockPair{0, 1}}}); 158fb299fa2Sopenharmony_ci BlockSet myBlock4({std::vector<BlockPair>{BlockPair{-1, 0}}}); 159fb299fa2Sopenharmony_ci ret = myBlock3.ReadDataFromBlock(fd, buffer); 160fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 161fb299fa2Sopenharmony_ci ret = myBlock3.WriteDataToBlock(fd, buffer); 162fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 4096); 163fb299fa2Sopenharmony_ci ret = myBlock4.ReadDataFromBlock(fd, buffer); 164fb299fa2Sopenharmony_ci EXPECT_EQ(ret, 0); 165fb299fa2Sopenharmony_ci close(fd); 166fb299fa2Sopenharmony_ci} 167fb299fa2Sopenharmony_ci} 168