1 /*
2 * Copyright (c) 2025 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 <cstddef>
17 #include <cstdint>
18 #include <set>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #define private public
21 #include "bmspreinstallexceptionmgr_fuzzer.h"
22 #include "bundle_mgr_service.h"
23 #include "pre_install_exception_mgr.h"
24 #include "bms_fuzztest_util.h"
25 #include "securec.h"
26
27 using namespace OHOS::AppExecFwk;
28 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
29 constexpr size_t U32_AT_SIZE = 4;
30 const std::string BUNDLE_TEMP_NAME = "temp_bundle_name";
31 const std::string BUNDLE_PATH = "test.hap";
32 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)33 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
34 {
35 std::shared_ptr<BundleMgrService> bundleMgrService_ = DelayedSingleton<BundleMgrService>::GetInstance();
36 bundleMgrService_->InitBmsParam();
37 bundleMgrService_->InitPreInstallExceptionMgr();
38 auto preInstallExceptionMgr = bundleMgrService_->GetPreInstallExceptionMgr();
39 if (preInstallExceptionMgr == nullptr) {
40 return false;
41 }
42 FuzzedDataProvider fdp(data, size);
43 preInstallExceptionMgr->exceptionPaths_.insert("/module_update/test/");
44 preInstallExceptionMgr->exceptionBundleNames_.insert("/module_update/test/");
45 preInstallExceptionMgr->exceptionAppServicePaths_.insert("/module_update/test/");
46 preInstallExceptionMgr->exceptionAppServiceBundleNames_.insert("/data/app/el1/bundle/public/test/");
47 bundleMgrService_->GetBmsParam();
48 std::set<std::string> oldExceptionPaths;
49 std::set<std::string> oldExceptionBundleNames;
50 std::set<std::string> exceptionAppServicePaths;
51 std::set<std::string> exceptionAppServiceBundleNames;
52 preInstallExceptionMgr->GetAllPreInstallExceptionInfo(oldExceptionPaths, oldExceptionBundleNames,
53 exceptionAppServicePaths, exceptionAppServiceBundleNames);
54 preInstallExceptionMgr->LoadPreInstallExceptionInfosFromDb();
55 preInstallExceptionMgr->SavePreInstallExceptionInfosToDb();
56 preInstallExceptionMgr->DeletePreInstallExceptionInfosFromDb();
57 preInstallExceptionMgr->ClearAll();
58 std::set<std::string> exceptionPaths;
59 std::set<std::string> exceptionBundleNames;
60 preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames,
61 exceptionAppServicePaths, exceptionAppServiceBundleNames);
62 std::string bundleDir = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
63 std::string dir = "";
64 std::string bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
65 std::string bundle = "";
66 preInstallExceptionMgr->SavePreInstallExceptionBundleName(BUNDLE_TEMP_NAME);
67 preInstallExceptionMgr->SavePreInstallExceptionBundleName(bundle);
68 preInstallExceptionMgr->SavePreInstallExceptionPath(BUNDLE_PATH);
69 preInstallExceptionMgr->SavePreInstallExceptionPath(bundle);
70 preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames,
71 exceptionAppServicePaths, exceptionAppServiceBundleNames);
72 preInstallExceptionMgr->DeletePreInstallExceptionBundleName(BUNDLE_TEMP_NAME);
73 preInstallExceptionMgr->DeletePreInstallExceptionBundleName(bundleName);
74 preInstallExceptionMgr->DeletePreInstallExceptionBundleName(bundle);
75 preInstallExceptionMgr->SavePreInstallExceptionAppServiceBundleName(BUNDLE_TEMP_NAME);
76 preInstallExceptionMgr->SavePreInstallExceptionAppServiceBundleName(bundle);
77 preInstallExceptionMgr->DeletePreInstallExceptionAppServiceBundleName(BUNDLE_TEMP_NAME);
78 preInstallExceptionMgr->DeletePreInstallExceptionAppServiceBundleName(bundleName);
79 preInstallExceptionMgr->DeletePreInstallExceptionAppServiceBundleName(bundle);
80 preInstallExceptionMgr->SavePreInstallExceptionAppServicePath(BUNDLE_PATH);
81 preInstallExceptionMgr->SavePreInstallExceptionAppServicePath(dir);
82 preInstallExceptionMgr->DeletePreInstallExceptionPath(BUNDLE_PATH);
83 preInstallExceptionMgr->DeletePreInstallExceptionPath(bundleDir);
84 preInstallExceptionMgr->DeletePreInstallExceptionPath(dir);
85 preInstallExceptionMgr->DeletePreInstallExceptionAppServicePath(BUNDLE_PATH);
86 preInstallExceptionMgr->DeletePreInstallExceptionAppServicePath(bundleDir);
87 preInstallExceptionMgr->DeletePreInstallExceptionAppServicePath(dir);
88 preInstallExceptionMgr->GetAllPreInstallExceptionInfo(exceptionPaths, exceptionBundleNames,
89 exceptionAppServicePaths, exceptionAppServiceBundleNames);
90 return true;
91 }
92 }
93
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97 // Run your code on data.
98 OHOS::DoSomethingInterestingWithMyAPI(data, size);
99 return 0;
100 }