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