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 "imagepatch_unittest.h" 17#include <cerrno> 18#include <cstdio> 19#include <fcntl.h> 20#include <iostream> 21#include <sys/mman.h> 22#include <sys/mount.h> 23#include <sys/stat.h> 24#include <unistd.h> 25#include <vector> 26#include "applypatch/block_writer.h" 27#include "applypatch/data_writer.h" 28#include "log/log.h" 29#include "patch/update_patch.h" 30#include "pkg_utils.h" 31#include "utils.h" 32 33using namespace testing::ext; 34using namespace Updater; 35namespace UpdaterUt { 36bool ImagePatchTest::ReadContentFromFile(const std::string& file, std::string &content) const 37{ 38 int flags = O_RDONLY | O_CLOEXEC; 39 int fd = open(file.c_str(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 40 if (fd < 0) { 41 return false; 42 } 43 44 struct stat st {}; 45 if (fstat(fd, &st) < 0) { 46 close(fd); 47 return false; 48 } 49 content.reserve(st.st_size); 50 constexpr size_t bufferSize = 1024; 51 char buffer[bufferSize]; 52 ssize_t n; 53 while ((n = read(fd, buffer, sizeof(buffer))) > 0) { 54 content.append(buffer, n); 55 } 56 close(fd); 57 return ((n == 0) ? true : false); 58} 59 60int ImagePatchTest::TestZipModeImagePatch() const 61{ 62 std::string expectedSHA256 = "980571599cea18fc164d03dfd26df7e666c346c6a40df49c22dcec15f060c984"; 63 std::string sourceData; 64 auto rc = ReadContentFromFile("/data/updater/applypatch/source.zip", sourceData); 65 EXPECT_TRUE(rc); 66 std::string fileName = Hpackage::GetName("/data/updater/applypatch/source.zip"); 67 printf("filename: %s\n", fileName.c_str()); 68 std::string patchFile = "/data/updater/applypatch/zip-patch-file"; 69 std::string patchContent; 70 rc = ReadContentFromFile(patchFile, patchContent); 71 EXPECT_TRUE(rc); 72 UpdatePatch::PatchParam param = { 73 reinterpret_cast<uint8_t *>(sourceData.data()), sourceData.size(), 74 reinterpret_cast<uint8_t *>(patchContent.data()), patchContent.size() 75 }; 76 return RunImageApplyPatch(param, "/data/updater/applypatch/out_put_zip.zip", expectedSHA256); 77} 78 79int ImagePatchTest::TestNormalModeImagePatch() const 80{ 81 std::string expectedSHA256 = "d5c87f954c3fb45685888d5edd359c27950a1a0acd33c45ad4e284b6a85686e5"; 82 std::string sourceData; 83 auto rc = ReadContentFromFile("/data/updater/diffpatch/patchtest.old", sourceData); 84 EXPECT_TRUE(rc); 85 std::string patchFile = "/data/updater/diffpatch/patchtest.img_patch"; 86 std::string patchContent; 87 rc = ReadContentFromFile(patchFile, patchContent); 88 EXPECT_TRUE(rc); 89 UpdatePatch::PatchParam param = { 90 reinterpret_cast<uint8_t *>(sourceData.data()), sourceData.size(), 91 reinterpret_cast<uint8_t *>(patchContent.data()), patchContent.size() 92 }; 93 return RunImageApplyPatch(param, "/data/updater/diffpatch/out_put_zip.zip", expectedSHA256); 94} 95 96int ImagePatchTest::TestGZipModeImagePatch() const 97{ 98 std::string expectedSHA256 = "805486a0df9b8919107ef6bf383452e642aca5d371848e4c7a9b8b59cd741b1f"; 99 std::string sourceData; 100 auto rc = ReadContentFromFile("/data/updater/applypatch/TestGZipModeImagePatch.old.gz", sourceData); 101 EXPECT_TRUE(rc); 102 std::string patchContent; 103 rc = ReadContentFromFile("/data/updater/applypatch/TestGZipModeImagePatch.gz.patch", patchContent); 104 EXPECT_TRUE(rc); 105 UpdatePatch::PatchParam param = { 106 reinterpret_cast<uint8_t *>(sourceData.data()), sourceData.size(), 107 reinterpret_cast<uint8_t *>(patchContent.data()), patchContent.size() 108 }; 109 int ret = RunImageApplyPatch(param, "/data/updater/applypatch/out_put_gzip.gzip", expectedSHA256); 110 return ret; 111} 112 113int ImagePatchTest::TestLZ4ModeImagePatch() const 114{ 115 std::string expectedSHA256 = "ec500f45b48886dd20e1e0042a74954026b9c59e5168e1d6465d928cea7a1064"; 116 std::string sourceData; 117 auto rc = ReadContentFromFile("/data/updater/diffpatch/PatchLz4test_old.lz4", sourceData); 118 EXPECT_TRUE(rc); 119 std::string patchContent; 120 rc = ReadContentFromFile("/data/updater/diffpatch/PatchLz4test_lz4.img_patch", patchContent); 121 EXPECT_TRUE(rc); 122 UpdatePatch::PatchParam param = { 123 reinterpret_cast<uint8_t *>(sourceData.data()), sourceData.size(), 124 reinterpret_cast<uint8_t *>(patchContent.data()), patchContent.size() 125 }; 126 RunImageApplyPatch(param, "/data/updater/diffpatch/out_put_lz4.lz4", expectedSHA256); 127 return 0; 128} 129 130HWTEST_F(ImagePatchTest, TestZipModeImagePatch, TestSize.Level1) 131{ 132 ImagePatchTest test; 133 EXPECT_EQ(0, test.TestZipModeImagePatch()); 134} 135 136HWTEST_F(ImagePatchTest, TestGZipModeImagePatch, TestSize.Level1) 137{ 138 ImagePatchTest test; 139 EXPECT_EQ(0, test.TestGZipModeImagePatch()); 140} 141 142HWTEST_F(ImagePatchTest, TestLZ4ModeImagePatch, TestSize.Level1) 143{ 144 ImagePatchTest test; 145 EXPECT_EQ(0, test.TestLZ4ModeImagePatch()); 146} 147 148HWTEST_F(ImagePatchTest, TestNormalModeImagePatch, TestSize.Level1) 149{ 150 ImagePatchTest test; 151 EXPECT_EQ(0, test.TestNormalModeImagePatch()); 152} 153} // namespace updater_ut 154