• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundle_resource_manager.h"
22 #include "bmsbundleresourcemanager_fuzzer.h"
23 #include "bms_fuzztest_util.h"
24 #include "securec.h"
25 
26 using namespace OHOS::AppExecFwk;
27 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
28 namespace OHOS {
29 constexpr size_t U32_AT_SIZE = 4;
30 constexpr uint32_t CODE_MAX = 8;
31 const int32_t USERID = 100;
32 const std::string MODULE_NAME = "entry";
33 const std::string ABILITY_NAME = "com.example.bmsaccesstoken1.MainAbility";
34 const std::string BUNDLE_NAME_NO_ICON = "com.third.hiworld.example1";
35 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)36 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
37 {
38     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
39     FuzzedDataProvider fdp(data, size);
40     std::string bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
41     int32_t userId = GenerateRandomUser(fdp);
42     int32_t appIndex = fdp.ConsumeIntegral<int32_t>();
43     ResourceInfo resourceInfo;
44     resourceInfo.bundleName_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
45     resourceInfo.label_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
46     resourceInfo.icon_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
47     resourceInfo.appIndex_ = Constants::UNSPECIFIED_USERID;
48     std::vector<ResourceInfo> resourceInfos;
49     resourceInfos.push_back(resourceInfo);
50     std::vector<ResourceInfo> resourceInfos2;
51     manager->DeleteNotExistResourceInfo();
52     std::map<std::string, std::vector<ResourceInfo>> resourceInfosMap;
53     resourceInfosMap[BUNDLE_NAME_NO_ICON] = resourceInfos;
54     std::map<std::string, std::vector<ResourceInfo>> resourceInfosMap2;
55     std::vector<std::string> existResourceNames;
56     manager->DeleteNotExistResourceInfo(resourceInfosMap, existResourceNames);
57     manager->DeleteNotExistResourceInfo(bundleName, appIndex, resourceInfos);
58     std::string moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
59     std::string abilityName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
60     manager->AddAllResourceInfo(USERID, 0);
61     uint32_t type = fdp.ConsumeIntegral<uint32_t>();
62     int32_t oldUserId = fdp.ConsumeIntegral<int32_t>();
63     manager->AddAllResourceInfo(userId, type);
64     manager->InnerProcessResourceInfoBySystemLanguageChanged(resourceInfosMap);
65     manager->InnerProcessResourceInfoBySystemThemeChanged(resourceInfosMap, userId);
66     uint32_t tempTaskNumber = fdp.ConsumeIntegral<uint32_t>();
67     ResourceInfo resourceInfo2;
68     resourceInfo2.label_ = "";
69     resourceInfo2.icon_ = "";
70     manager->DeleteResourceInfo(bundleName);
71     std::vector<std::string> keyNames;
72     manager->GetAllResourceName(keyNames);
73     BundleResourceInfo info;
74     uint32_t flags = fdp.ConsumeIntegral<uint32_t>();
75     manager->GetBundleResourceInfo(bundleName, 0, info);
76     manager->GetBundleResourceInfo(bundleName, flags, info, appIndex);
77     std::vector<LauncherAbilityResourceInfo> launcherInfos;
78     manager->GetLauncherAbilityResourceInfo(bundleName,
79         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL), launcherInfos);
80     manager->GetLauncherAbilityResourceInfo(bundleName, flags, launcherInfos, appIndex);
81     std::vector<BundleResourceInfo> infos;
82     manager->GetAllBundleResourceInfo(0, infos);
83     manager->GetAllBundleResourceInfo(flags, infos);
84     manager->GetAllLauncherAbilityResourceInfo(flags, launcherInfos);
85     manager->FilterLauncherAbilityResourceInfoWithFlag(1, bundleName, launcherInfos);
86     manager->FilterLauncherAbilityResourceInfoWithFlag(flags, bundleName, launcherInfos);
87     manager->FilterLauncherAbilityResourceInfoWithFlag(0, bundleName, launcherInfos);
88     std::vector<AbilityInfo> abilityInfos;
89     manager->GetLauncherAbilityInfos(bundleName, abilityInfos);
90     manager->GetLauncherAbilityInfos("", abilityInfos);
91     manager->CheckResourceFlags(static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL));
92     manager->CheckResourceFlags(static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL));
93     manager->CheckResourceFlags(static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON));
94     manager->CheckResourceFlags(static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_DRAWABLE_DESCRIPTOR));
95     manager->CheckResourceFlags(static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ONLY_WITH_MAIN_ABILITY));
96     manager->CheckResourceFlags(0);
97     ResourceInfo resourceInfo3;
98     resourceInfo3.label_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
99     resourceInfo3.bundleName_ = "ohos.global.systemres";
100     resourceInfo3.appIndex_ = 0;
101     ResourceInfo resourceInfo4;
102     resourceInfo4.label_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
103     resourceInfo4.bundleName_ = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
104     resourceInfo4.icon_ = "";
105     resourceInfo4.appIndex_ = 1;
106     manager->ProcessResourceInfoWhenParseFailed(resourceInfo);
107     manager->ProcessResourceInfoWhenParseFailed(resourceInfo2);
108     manager->ProcessResourceInfoWhenParseFailed(resourceInfo3);
109     manager->ProcessResourceInfoWhenParseFailed(resourceInfo4);
110     manager->GetDefaultIcon(resourceInfo);
111     manager->SendBundleResourcesChangedEvent(userId, type);
112     std::string targetBundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
113     manager->GetTargetBundleName(bundleName, targetBundleName);
114     manager->GetBundleResourceInfoForCloneBundle(bundleName, appIndex,
115         static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE), resourceInfos);
116 
117     std::vector<LauncherAbilityResourceInfo> extensionAbilityResourceInfo;
118     manager->GetExtensionAbilityResourceInfo(bundleName, ExtensionAbilityType::RECENT_PHOTO, flags,
119         extensionAbilityResourceInfo, appIndex);
120     manager->GetExtensionAbilityResourceInfo(bundleName, ExtensionAbilityType::RECENT_PHOTO,
121         static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL), extensionAbilityResourceInfo, appIndex);
122     manager->DeleteAllResourceInfo();
123     manager->ProcessResourceInfoNoNeedToParseOtherIcon(resourceInfos);
124     manager->PrepareSysRes();
125     BundleResourceInfo bundleResourceInfo;
126     bundleResourceInfo.appIndex = Constants::UNSPECIFIED_USERID;
127     BundleResourceInfo bundleResourceInfo2;
128     bundleResourceInfo2.appIndex = fdp.ConsumeIntegral<int32_t>();
129     LauncherAbilityResourceInfo launcherAbilityResourceInfo;
130     launcherAbilityResourceInfo.appIndex = Constants::UNSPECIFIED_USERID;
131     LauncherAbilityResourceInfo launcherAbilityResourceInfo2;
132     launcherAbilityResourceInfo2.appIndex = fdp.ConsumeIntegral<int32_t>();
133     manager->IsLauncherAbility(launcherAbilityResourceInfo, abilityInfos);
134     return true;
135 }
136 }
137 
138 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)139 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
140 {
141     // Run your code on data.
142     OHOS::DoSomethingInterestingWithMyAPI(data, size);
143     return 0;
144 }