1 /*
2 * Copyright (c) 2023 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 "imodule_update_fuzzer.h"
17
18 #include <array>
19 #include <cstddef>
20 #include <cstdint>
21 #include <iostream>
22 #include <string>
23 #include <vector>
24 #include "module_ipc_helper.h"
25 #include "module_update_kits.h"
26 #include "module_update_kits_impl.h"
27
28 namespace OHOS {
29 using namespace SysInstaller;
FuzzModuleUpdate(const uint8_t * data,size_t size)30 void FuzzModuleUpdate(const uint8_t* data, size_t size)
31 {
32 ModuleUpdateKits &moduleUpdateKits = ModuleUpdateKits::GetInstance();
33 moduleUpdateKits.InstallModulePackage(std::string(reinterpret_cast<const char*>(data), size));
34 moduleUpdateKits.UninstallModulePackage(std::string(reinterpret_cast<const char*>(data), size));
35 std::list<ModulePackageInfo> infos;
36 moduleUpdateKits.GetModulePackageInfo(std::string(reinterpret_cast<const char*>(data), size), infos);
37 moduleUpdateKits.ExitModuleUpdate();
38 }
39
40 class ProcessCallback : public ISysInstallerCallbackFunc {
41 public:
42 ProcessCallback() = default;
43 ~ProcessCallback() = default;
OnUpgradeProgress(UpdateStatus updateStatus,int percent,const std::string & resultMsg)44 void OnUpgradeProgress(UpdateStatus updateStatus, int percent, const std::string &resultMsg) override
45 {
46 printf("ProgressCallback progress %d percent %d msg %s\n", updateStatus, percent,
47 resultMsg.c_str());
48 }
OnUpgradeDealLen(UpdateStatus updateStatus,int dealLen,const std::string & resultMsg)49 void OnUpgradeDealLen(UpdateStatus updateStatus, int dealLen, const std::string &resultMsg) override
50 {
51 printf("ProgressCallback progress %d dealLen %d msg %s\n", updateStatus, dealLen,
52 resultMsg.c_str());
53 }
54 };
55
FuzzModuleUpdateOther(const uint8_t * data,size_t size)56 void FuzzModuleUpdateOther(const uint8_t* data, size_t size)
57 {
58 ModuleUpdateKits &moduleUpdateKits = ModuleUpdateKits::GetInstance();
59 std::vector<HmpVersionInfo> versionInfo = moduleUpdateKits.GetHmpVersionInfo();
60 sptr<ISysInstallerCallbackFunc> callback = new ProcessCallback;
61 moduleUpdateKits.StartUpdateHmpPackage(std::string(reinterpret_cast<const char*>(data), size),
62 callback);
63 std::vector<HmpUpdateInfo> updateInfo = moduleUpdateKits.GetHmpUpdateResult();
64 moduleUpdateKits.ExitModuleUpdate();
65 }
66 }
67
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71 /* Run your code on data */
72 OHOS::FuzzModuleUpdate(data, size);
73 return 0;
74 }
75
76