1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2023 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 "diff_patch/diff_patch_interface.h"
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include <array>
19fb299fa2Sopenharmony_ci#include <cstddef>
20fb299fa2Sopenharmony_ci#include <cstdint>
21fb299fa2Sopenharmony_ci#include <iostream>
22fb299fa2Sopenharmony_ci#include <string>
23fb299fa2Sopenharmony_ci#include <vector>
24fb299fa2Sopenharmony_ci#include <fstream>
25fb299fa2Sopenharmony_ci#include <cstdio>
26fb299fa2Sopenharmony_ci#include "log/log.h"
27fb299fa2Sopenharmony_ci#include <fuzzer/FuzzedDataProvider.h>
28fb299fa2Sopenharmony_ci
29fb299fa2Sopenharmony_ciusing namespace Updater;
30fb299fa2Sopenharmony_cinamespace OHOS {
31fb299fa2Sopenharmony_ci    bool WriteDataToFile(const char* data, size_t size, const char* filePath, bool isAppend = false)
32fb299fa2Sopenharmony_ci    {
33fb299fa2Sopenharmony_ci        std::ofstream ofs;
34fb299fa2Sopenharmony_ci        if (isAppend) {
35fb299fa2Sopenharmony_ci            ofs.open(filePath, std::ios::app | std::ios::binary);
36fb299fa2Sopenharmony_ci        } else {
37fb299fa2Sopenharmony_ci            ofs.open(filePath, std::ios::ate | std::ios::binary);
38fb299fa2Sopenharmony_ci        }
39fb299fa2Sopenharmony_ci        if (!ofs.is_open()) {
40fb299fa2Sopenharmony_ci            LOG(ERROR) << "open " << filePath << " failed";
41fb299fa2Sopenharmony_ci            return false;
42fb299fa2Sopenharmony_ci        }
43fb299fa2Sopenharmony_ci        ofs.write(data, size);
44fb299fa2Sopenharmony_ci        ofs.close();
45fb299fa2Sopenharmony_ci        return true;
46fb299fa2Sopenharmony_ci    }
47fb299fa2Sopenharmony_ci
48fb299fa2Sopenharmony_ci    void FuzzApplyPatch(const uint8_t* data, size_t size)
49fb299fa2Sopenharmony_ci    {
50fb299fa2Sopenharmony_ci        FuzzedDataProvider fdp(data, size);
51fb299fa2Sopenharmony_ci        const int magicNumSize = 4;
52fb299fa2Sopenharmony_ci        const char* bspatchPath = "/data/applyPatchfuzzBspatch";
53fb299fa2Sopenharmony_ci        const char* imgpatchPath = "/data/applyPatchfuzzImgpatch";
54fb299fa2Sopenharmony_ci        const char* oldFilePath = "/data/applyPatchfuzzOldFile";
55fb299fa2Sopenharmony_ci        const char* newFilePath = "/data/applyPatchfuzzNewFile";
56fb299fa2Sopenharmony_ci        bool isPkgFormat = false;
57fb299fa2Sopenharmony_ci        bool ret = WriteDataToFile(reinterpret_cast<const char*>(data), size, oldFilePath);
58fb299fa2Sopenharmony_ci        isPkgFormat = fdp.ConsumeBool();
59fb299fa2Sopenharmony_ci        if (isPkgFormat) {
60fb299fa2Sopenharmony_ci            ret &= WriteDataToFile("BSDIFF40", magicNumSize, bspatchPath);
61fb299fa2Sopenharmony_ci            ret &= WriteDataToFile(reinterpret_cast<const char*>(data), size, bspatchPath, true);
62fb299fa2Sopenharmony_ci            ApplyPatch(bspatchPath, oldFilePath, newFilePath);
63fb299fa2Sopenharmony_ci        } else {
64fb299fa2Sopenharmony_ci            ret &= WriteDataToFile("PKGDIFF0", magicNumSize, imgpatchPath);
65fb299fa2Sopenharmony_ci            ret &= WriteDataToFile(reinterpret_cast<const char*>(data), size, imgpatchPath, true);
66fb299fa2Sopenharmony_ci            ApplyPatch(imgpatchPath, oldFilePath, newFilePath);
67fb299fa2Sopenharmony_ci        }
68fb299fa2Sopenharmony_ci        if (!ret) {
69fb299fa2Sopenharmony_ci            LOG(ERROR) << "an invalid fuzztest due to input file creation failure";
70fb299fa2Sopenharmony_ci        }
71fb299fa2Sopenharmony_ci    }
72fb299fa2Sopenharmony_ci}
73fb299fa2Sopenharmony_ci
74fb299fa2Sopenharmony_ci/* Fuzzer entry point */
75fb299fa2Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76fb299fa2Sopenharmony_ci{
77fb299fa2Sopenharmony_ci    /* Run your code on data */
78fb299fa2Sopenharmony_ci    OHOS::FuzzApplyPatch(data, size);
79fb299fa2Sopenharmony_ci    return 0;
80fb299fa2Sopenharmony_ci}
81