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 "bms_fuzztest_util.h"
22 #include "extend_resource_manager_host_impl.h"
23 #include "inner_bundle_info.h"
24
25 using namespace OHOS::AppExecFwk;
26 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
27 namespace OHOS {
GenerateExtendResourceInfo(FuzzedDataProvider & fdp,ExtendResourceInfo & extendResourceInfo)28 void GenerateExtendResourceInfo(FuzzedDataProvider& fdp, ExtendResourceInfo &extendResourceInfo)
29 {
30 extendResourceInfo.iconId = fdp.ConsumeIntegral<uint32_t>();
31 extendResourceInfo.moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
32 extendResourceInfo.filePath = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
33 }
34
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)35 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
36 {
37 ExtendResourceManagerHostImpl impl;
38 FuzzedDataProvider fdp(data, size);
39 std::vector<std::string> filePaths = GenerateStringArray(fdp);
40 std::string bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
41 impl.AddExtResource(bundleName, filePaths);
42
43 std::vector<ExtendResourceInfo> infos;
44 for (size_t i = 0; i < filePaths.size(); ++i) {
45 ExtendResourceInfo info;
46 GenerateExtendResourceInfo(fdp, info);
47 infos.emplace_back(info);
48 }
49 impl.InnerSaveExtendResourceInfo(bundleName, filePaths, infos);
50 infos.clear();
51 impl.ParseExtendResourceFile(bundleName, filePaths, infos);
52 int32_t userId = GenerateRandomUser(fdp);
53 impl.CheckWhetherDynamicIconNeedProcess(bundleName, userId);
54 impl.CheckAcrossUserPermission(userId);
55 impl.IsNeedUpdateBundleResourceInfo(bundleName, userId);
56 std::vector<DynamicIconInfo> dynamicInfos;
57 impl.GetDynamicIconInfo(bundleName, dynamicInfos);
58 dynamicInfos.clear();
59 impl.GetAllDynamicIconInfo(userId, dynamicInfos);
60 impl.GetAllDynamicIconInfo(dynamicInfos);
61 InnerBundleInfo innerBundleInfo;
62 int32_t appIndex = fdp.ConsumeIntegral<int32_t>();
63 impl.CheckParamInvalid(innerBundleInfo, userId, appIndex);
64 std::string moduleName;
65 impl.GetDynamicIcon(bundleName, userId, appIndex, moduleName);
66 impl.ResetBundleResourceIcon(bundleName, userId, appIndex);
67 impl.DisableDynamicIcon(bundleName, userId, appIndex);
68 ExtendResourceInfo extendResourceInfo;
69 GenerateExtendResourceInfo(fdp, extendResourceInfo);
70 impl.ParseBundleResource(bundleName, extendResourceInfo, userId, appIndex);
71 moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
72 impl.EnableDynamicIcon(bundleName, moduleName, userId, appIndex);
73
74 infos.emplace_back(extendResourceInfo);
75 std::vector<std::string> moduleNames = GenerateStringArray(fdp);
76 impl.InnerRemoveExtendResources(bundleName, moduleNames, infos);
77 return true;
78 }
79 }
80
81 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84 // Run your code on data.
85 OHOS::DoSomethingInterestingWithMyAPI(data, size);
86 return 0;
87 }