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, resultMsg.c_str());
47     }
48 };
49 
FuzzModuleUpdateOther(const uint8_t * data,size_t size)50 void FuzzModuleUpdateOther(const uint8_t* data, size_t size)
51 {
52     ModuleUpdateKits &moduleUpdateKits = ModuleUpdateKits::GetInstance();
53     std::vector<HmpVersionInfo> versionInfo = moduleUpdateKits.GetHmpVersionInfo();
54     sptr<ISysInstallerCallbackFunc> callback = new ProcessCallback;
55     moduleUpdateKits.StartUpdateHmpPackage(std::string(reinterpret_cast<const char*>(data), size),
56         callback);
57     std::vector<HmpUpdateInfo> updateInfo = moduleUpdateKits.GetHmpUpdateResult();
58     moduleUpdateKits.ExitModuleUpdate();
59 }
60 }
61 
62 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
64 {
65     /* Run your code on data */
66     OHOS::FuzzModuleUpdate(data, size);
67     return 0;
68 }
69 
70