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#ifndef IMAGEPATH_UNITTEST_H
17fb299fa2Sopenharmony_ci#define IMAGEPATH_UNITTEST_H
18fb299fa2Sopenharmony_ci#include <fcntl.h>
19fb299fa2Sopenharmony_ci#include <gtest/gtest.h>
20fb299fa2Sopenharmony_ci#include <iostream>
21fb299fa2Sopenharmony_ci#include <libgen.h>
22fb299fa2Sopenharmony_ci#include <memory>
23fb299fa2Sopenharmony_ci#include <string>
24fb299fa2Sopenharmony_ci#include <vector>
25fb299fa2Sopenharmony_ci#include "applypatch/block_set.h"
26fb299fa2Sopenharmony_ci#include "applypatch/block_writer.h"
27fb299fa2Sopenharmony_ci#include "applypatch/data_writer.h"
28fb299fa2Sopenharmony_ci#include "log.h"
29fb299fa2Sopenharmony_ci#include "mount.h"
30fb299fa2Sopenharmony_ci#include "patch/update_patch.h"
31fb299fa2Sopenharmony_ci#include "utils.h"
32fb299fa2Sopenharmony_ci
33fb299fa2Sopenharmony_ciconstexpr int O_BINARY = 0;
34fb299fa2Sopenharmony_cinamespace UpdaterUt {
35fb299fa2Sopenharmony_ci#define UNUSED(x) (void)(x)
36fb299fa2Sopenharmony_ciusing namespace Updater;
37fb299fa2Sopenharmony_ciclass FileWriter : public DataWriter {
38fb299fa2Sopenharmony_cipublic:
39fb299fa2Sopenharmony_ci    virtual bool Write(const uint8_t *addr, size_t len, const void *context)
40fb299fa2Sopenharmony_ci    {
41fb299fa2Sopenharmony_ci        UNUSED(context);
42fb299fa2Sopenharmony_ci        write(fd_, addr, len);
43fb299fa2Sopenharmony_ci
44fb299fa2Sopenharmony_ci        if (fsync(fd_) == -1) {
45fb299fa2Sopenharmony_ci            LOG(ERROR) << "Failed to fsync ";
46fb299fa2Sopenharmony_ci            return -1;
47fb299fa2Sopenharmony_ci        }
48fb299fa2Sopenharmony_ci        currentBlockLeft_ -= len;
49fb299fa2Sopenharmony_ci        totalWritten_ += len;
50fb299fa2Sopenharmony_ci        return true;
51fb299fa2Sopenharmony_ci    }
52fb299fa2Sopenharmony_ci    virtual ~FileWriter() {}
53fb299fa2Sopenharmony_ci    FileWriter(int fd, BlockSet &bs) : fd_(fd), bs_(bs), totalWritten_(0), currentBlockLeft_(0) {}
54fb299fa2Sopenharmony_ci    FileWriter(const FileWriter&) = delete;
55fb299fa2Sopenharmony_ci    const FileWriter& operator=(const FileWriter&) = delete;
56fb299fa2Sopenharmony_ciprivate:
57fb299fa2Sopenharmony_ci    int fd_;
58fb299fa2Sopenharmony_ci    BlockSet bs_;
59fb299fa2Sopenharmony_ci    size_t totalWritten_;
60fb299fa2Sopenharmony_ci    size_t currentBlockLeft_;
61fb299fa2Sopenharmony_ci};
62fb299fa2Sopenharmony_ci
63fb299fa2Sopenharmony_ciclass ImagePatchTest : public ::testing::Test {
64fb299fa2Sopenharmony_cipublic:
65fb299fa2Sopenharmony_ci    ImagePatchTest() = default;
66fb299fa2Sopenharmony_ci    virtual ~ImagePatchTest() {
67fb299fa2Sopenharmony_ci    }
68fb299fa2Sopenharmony_ci    int TestZipModeImagePatch() const;
69fb299fa2Sopenharmony_ci    int TestGZipModeImagePatch() const;
70fb299fa2Sopenharmony_ci    int TestLZ4ModeImagePatch() const;
71fb299fa2Sopenharmony_ci    int TestNormalModeImagePatch() const;
72fb299fa2Sopenharmony_ci    int RunImageApplyPatch(UpdatePatch::PatchParam &param, const std::string &target,
73fb299fa2Sopenharmony_ci        const std::string &expectedSHA256) const
74fb299fa2Sopenharmony_ci    {
75fb299fa2Sopenharmony_ci        mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
76fb299fa2Sopenharmony_ci        int fd = open(target.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, mode);
77fb299fa2Sopenharmony_ci        EXPECT_GE(fd, 0);
78fb299fa2Sopenharmony_ci        BlockSet targetBlk;
79fb299fa2Sopenharmony_ci        targetBlk.ParserAndInsert({
80fb299fa2Sopenharmony_ci            "2", "0", "1"
81fb299fa2Sopenharmony_ci        });
82fb299fa2Sopenharmony_ci        std::unique_ptr<FileWriter> writer = std::make_unique<FileWriter>(fd, targetBlk);
83fb299fa2Sopenharmony_ci        std::vector<uint8_t> empty;
84fb299fa2Sopenharmony_ci        int32_t ret = UpdatePatch::UpdateApplyPatch::ApplyImagePatch(param, empty,
85fb299fa2Sopenharmony_ci            [&](size_t start, const UpdatePatch::BlockBuffer &data, size_t size) -> int {
86fb299fa2Sopenharmony_ci                bool ret = writer->Write(data.buffer, size, nullptr);
87fb299fa2Sopenharmony_ci                return ret ? 0 : -1;
88fb299fa2Sopenharmony_ci            }, expectedSHA256);
89fb299fa2Sopenharmony_ci        close(fd);
90fb299fa2Sopenharmony_ci        return ret;
91fb299fa2Sopenharmony_ci    }
92fb299fa2Sopenharmony_ci
93fb299fa2Sopenharmony_ciprotected:
94fb299fa2Sopenharmony_ci    void SetUp()
95fb299fa2Sopenharmony_ci    {
96fb299fa2Sopenharmony_ci        LoadSpecificFstab("/data/updater/applypatch/etc/fstab.imagepatch");
97fb299fa2Sopenharmony_ci        std::string basePath = "/data/updater/imgpatch";
98fb299fa2Sopenharmony_ci        Updater::Utils::MkdirRecursive(basePath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
99fb299fa2Sopenharmony_ci    }
100fb299fa2Sopenharmony_ci    void TearDown() {}
101fb299fa2Sopenharmony_ci    void TestBody() {}
102fb299fa2Sopenharmony_ci
103fb299fa2Sopenharmony_ciprivate:
104fb299fa2Sopenharmony_ci    bool ReadContentFromFile(const std::string &file, std::string &content) const;
105fb299fa2Sopenharmony_ci};
106fb299fa2Sopenharmony_ci} // namespace updater_ut
107fb299fa2Sopenharmony_ci#endif // IMAGEPATH_UNITTEST_H
108