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 #define private public
17 #include <cstddef>
18 #include <cstdint>
19 #include <fuzzer/FuzzedDataProvider.h>
20
21 #include "bundle_resource/bundle_resource_configuration.h"
22 #include "bundle_resource/bundle_resource_rdb.h"
23 #include "bundle_resource/resource_info.h"
24
25 #include "bmsbundleresource_fuzzer.h"
26 #include "bms_fuzztest_util.h"
27
28 using namespace OHOS::AppExecFwk;
29 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
30 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
32 {
33 BundleResourceRdb rdb;
34 FuzzedDataProvider fdp(data, size);
35
36 ResourceInfo info;
37 info.bundleName_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
38 rdb.AddResourceInfo(info);
39 info.moduleName_ = "";
40 rdb.AddResourceInfo(info);
41
42 std::vector<ResourceInfo> infos;
43 rdb.AddResourceInfos(infos);
44 infos.push_back(info);
45 rdb.AddResourceInfos(infos);
46 info.icon_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
47 infos.push_back(info);
48 rdb.AddResourceInfos(infos);
49
50 rdb.DeleteResourceInfo(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
51 rdb.DeleteResourceInfo("");
52 rdb.DeleteResourceInfo("invalid_key");
53
54 std::vector<std::string> keyNames;
55 rdb.GetResourceNameByBundleName(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH),
56 fdp.ConsumeIntegral<int32_t>(), keyNames);
57 rdb.GetResourceNameByBundleName("", fdp.ConsumeIntegral<int32_t>(), keyNames);
58
59 BundleResourceInfo bundleResourceInfo;
60 rdb.GetBundleResourceInfo(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH),
61 fdp.ConsumeIntegral<uint32_t>(), bundleResourceInfo, fdp.ConsumeIntegral<int32_t>());
62 rdb.GetBundleResourceInfo("", fdp.ConsumeIntegral<uint32_t>(), bundleResourceInfo, fdp.ConsumeIntegral<int32_t>());
63
64 std::vector<LauncherAbilityResourceInfo> launcherAbilityResourceInfos;
65 rdb.GetLauncherAbilityResourceInfo(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH),
66 fdp.ConsumeIntegral<uint32_t>(), launcherAbilityResourceInfos, fdp.ConsumeIntegral<int32_t>());
67 rdb.GetLauncherAbilityResourceInfo("", fdp.ConsumeIntegral<uint32_t>(), launcherAbilityResourceInfos,
68 fdp.ConsumeIntegral<int32_t>());
69
70 rdb.ConvertToBundleResourceInfo(nullptr, fdp.ConsumeIntegral<uint32_t>(), bundleResourceInfo);
71
72 LauncherAbilityResourceInfo launcherAbilityResourceInfo;
73 rdb.ConvertToLauncherAbilityResourceInfo(nullptr, fdp.ConsumeIntegral<uint32_t>(), launcherAbilityResourceInfo);
74
75 rdb.ConvertToExtensionAbilityResourceInfo(nullptr, fdp.ConsumeIntegral<uint32_t>(),
76 launcherAbilityResourceInfo);
77
78 std::string systemState;
79 rdb.GetCurrentSystemState(systemState);
80
81 rdb.GetExtensionAbilityResourceInfo(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH),
82 ExtensionAbilityType::DRIVER, fdp.ConsumeIntegral<uint32_t>(), launcherAbilityResourceInfos,
83 fdp.ConsumeIntegral<int32_t>());
84
85 BundleResourceConfiguration cfg;
86
87 cfg.InitResourceGlobalConfig(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH), nullptr);
88
89 std::vector<std::string> overlayHaps;
90 overlayHaps.push_back(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
91 cfg.InitResourceGlobalConfig(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH), overlayHaps, nullptr,
92 fdp.ConsumeBool(), fdp.ConsumeBool());
93
94 ResourceInfo resourceInfo;
95 resourceInfo.abilityName_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
96 resourceInfo.GetKey();
97
98 resourceInfo.ConvertFromLauncherAbilityResourceInfo(launcherAbilityResourceInfo);
99 resourceInfo.appIndex_ = CODE_MIN_ONE;
100 resourceInfo.ConvertFromLauncherAbilityResourceInfo(launcherAbilityResourceInfo);
101
102 return true;
103 }
104 }
105
106 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109 /* Run your code on data */
110 OHOS::DoSomethingInterestingWithMyAPI(data, size);
111 return 0;
112 }
113