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 "update_bin/bin_flow_update.h"
17 #include "update_bin/bin_process.h"
18 #include "log.h"
19 
20 using namespace Hpackage;
21 using namespace Updater;
22 
23 namespace OHOS {
TestBinFlowUpdater(const uint8_t * data,size_t size)24     int TestBinFlowUpdater(const uint8_t* data, size_t size)
25     {
26         InitUpdaterLogger("UPDATER", "updater_log.log", "updater_status.log", "error_code.log");
27         LOG(INFO) << "TestBinFlowUpdater start";
28         std::string packagePath = std::string(reinterpret_cast<const char*>(data), size);
29         PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance();
30         if (pkgManager == nullptr) {
31             LOG(ERROR) << "pkgManager is nullptr";
32             return -1;
33         }
34 
35         std::vector<std::string> components;
36         int32_t ret = pkgManager->LoadPackage(packagePath, Utils::GetCertName(), components);
37         if (ret != PKG_SUCCESS) {
38             LOG(ERROR) << "Fail to load package";
39             PkgManager::ReleasePackageInstance(pkgManager);
40             return -1;
41         }
42 
43         ret = Updater::ExecUpdate(pkgManager, false, packagePath,
44             [](const char *cmd, const char *content) {
45                 LOG(INFO) << "pip msg, " << cmd << ":" << content;
46             });
47         PkgManager::ReleasePackageInstance(pkgManager);
48         return ret;
49     }
50 }
51 
52 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)53 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
54 {
55     /* Run your code on data */
56     OHOS::TestBinFlowUpdater(data, size);
57     return 0;
58 }
59 
60