• 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 #define private public
17 #include <cstddef>
18 #include <cstdint>
19 #include <fuzzer/FuzzedDataProvider.h>
20 
21 #include "default_app_data.h"
22 #include "default_app_mgr.h"
23 #include "default_app_host_impl.h"
24 
25 #include "bmsdefaultapp_fuzzer.h"
26 #include "bms_fuzztest_util.h"
27 
28 using Want = OHOS::AAFwk::Want;
29 using json = nlohmann::json;
30 using namespace OHOS::AppExecFwk;
31 using namespace OHOS::AppExecFwk::BMSFuzzTestUtil;
32 const int32_t TEST_USERID_100 = 100;
33 const int32_t INVALID_USERID = -1;
34 const int32_t START_USERID = 0;
35 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)36 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
37 {
38     FuzzedDataProvider fdp(data, size);
39 
40     DefaultAppData defaultApp;
41     // test ToJson
42     nlohmann::json jsonObject1;
43     defaultApp.ToJson(jsonObject1);
44 
45     // test FromJson g_defaultAppJson not ok
46     nlohmann::json jsonObject2;
47     jsonObject2["ark_startup_cache"] = json::array();
48     defaultApp.FromJson(jsonObject2);
49 
50     // test ParseDefaultApplicationConfig, jsonObject3 is empty
51     nlohmann::json jsonObject3;
52     defaultApp.ParseDefaultApplicationConfig(jsonObject3);
53 
54     // test ParseDefaultApplicationConfig, jsonObject3 has no object
55     jsonObject3 = {"a", "b"};
56     defaultApp.ParseDefaultApplicationConfig(jsonObject3);
57 
58     // test IsDefaultApplication
59     DefaultAppMgr defaultAppMgr;
60     std::string type = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
61     bool isDefaultApp = false;
62     defaultAppMgr.IsDefaultApplication(START_USERID, type, isDefaultApp);
63     defaultAppMgr.IsDefaultApplication(TEST_USERID_100, type, isDefaultApp);
64     int32_t userId = fdp.ConsumeIntegral<int32_t>();
65     defaultAppMgr.IsDefaultApplication(userId, type, isDefaultApp);
66 
67     // test IsDefaultApplicationInternal
68     defaultAppMgr.IsDefaultApplicationInternal(START_USERID, "IMAGE", isDefaultApp);
69     defaultAppMgr.IsDefaultApplicationInternal(TEST_USERID_100, "IMAGE", isDefaultApp);
70     defaultAppMgr.IsDefaultApplicationInternal(fdp.ConsumeIntegral<int32_t>(),
71         fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH), isDefaultApp);
72 
73     // test GetDefaultApplicationInternal
74     BundleInfo bundleInfo;
75     defaultAppMgr.GetDefaultApplicationInternal(START_USERID, "BROWSER", bundleInfo, fdp.ConsumeBool());
76     defaultAppMgr.GetDefaultApplicationInternal(TEST_USERID_100, "BROWSER", bundleInfo, fdp.ConsumeBool());
77     defaultAppMgr.GetDefaultApplicationInternal(fdp.ConsumeIntegral<int32_t>(),
78         fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH),
79         bundleInfo, fdp.ConsumeBool());
80 
81     // test SetDefaultApplicationInternal
82     Element element;
83     defaultAppMgr.SetDefaultApplicationInternal(INVALID_USERID, "IMAGE", element);
84     defaultAppMgr.SetDefaultApplicationInternal(START_USERID, "IMAGE", element);
85     defaultAppMgr.SetDefaultApplicationInternal(TEST_USERID_100, "IMAGE", element);
86     element.abilityName = "com.test";
87     element.bundleName = "com.test";
88     element.extensionName = "com.test";
89     element.moduleName = "com.test";
90     defaultAppMgr.SetDefaultApplicationInternal(TEST_USERID_100, "IMAGE", element);
91     element.abilityName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
92     element.bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
93     element.extensionName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
94     element.moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
95     defaultAppMgr.SetDefaultApplicationInternal(fdp.ConsumeIntegral<int32_t>(),
96         fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH), element);
97 
98     // test ResetDefaultApplicationInternal
99     defaultAppMgr.ResetDefaultApplicationInternal(START_USERID, "IMAGE");
100     defaultAppMgr.ResetDefaultApplicationInternal(TEST_USERID_100, "IMAGE");
101     defaultAppMgr.ResetDefaultApplicationInternal(INVALID_USERID, "IMAGE");
102     defaultAppMgr.ResetDefaultApplicationInternal(fdp.ConsumeIntegral<int32_t>(),
103         fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
104 
105     // test HandleUninstallBundle
106     defaultAppMgr.HandleUninstallBundle(INVALID_USERID, "");
107     defaultAppMgr.HandleUninstallBundle(TEST_USERID_100, "");
108     defaultAppMgr.HandleUninstallBundle(TEST_USERID_100, "com.test");
109     defaultAppMgr.HandleUninstallBundle(fdp.ConsumeIntegral<int32_t>(),
110         fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
111 
112     // test IsBrowserWant
113     ElementName name;
114     std::string bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
115     std::string action = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
116     std::string uri = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
117     name.SetBundleName(bundleName);
118     Want want;
119     want.SetElement(name);
120     want.SetAction("ohos.want.action.viewData");
121     want.SetUri("https://123");
122     defaultAppMgr.IsBrowserWant(want);
123     want.SetAction("ohos.want");
124     defaultAppMgr.IsBrowserWant(want);
125     want.SetAction("ohos.want.action.viewData");
126     want.SetUri("htt");
127     defaultAppMgr.IsBrowserWant(want);
128     want.SetAction(action);
129     want.SetUri(uri);
130     defaultAppMgr.IsBrowserWant(want);
131 
132     // test IsEmailWant
133     want.SetAction("ohos.want.action.sendToData");
134     want.SetUri("mailto");
135     defaultAppMgr.IsEmailWant(want);
136     want.SetUri("");
137     defaultAppMgr.IsEmailWant(want);
138     want.SetAction(action);
139     want.SetUri(uri);
140     defaultAppMgr.IsEmailWant(want);
141 
142     // test GetTypeFromWant
143     want.SetAction("ohos.want.action.viewData");
144     want.SetUri("123");
145     want.SetType("");
146     defaultAppMgr.GetTypeFromWant(want);
147     want.SetType("456");
148     defaultAppMgr.GetTypeFromWant(want);
149     want.SetUri("://file://content://");
150     defaultAppMgr.GetTypeFromWant(want);
151     want.SetAction(action);
152     want.SetUri(uri);
153     defaultAppMgr.GetTypeFromWant(want);
154 
155     // test GetDefaultApplication
156     want.SetAction("ohos.want.action.viewData");
157     want.SetUri("https://123");
158     std::vector<AbilityInfo> abilityInfos;
159     std::vector<ExtensionAbilityInfo> extensionInfos;
160     defaultAppMgr.GetDefaultApplication(want, INVALID_USERID, abilityInfos, extensionInfos, true);
161     defaultAppMgr.GetDefaultApplication(want, START_USERID, abilityInfos, extensionInfos, true);
162     defaultAppMgr.GetDefaultApplication(want, TEST_USERID_100, abilityInfos, extensionInfos, true);
163     want.SetAction(action);
164     want.SetUri(uri);
165     defaultAppMgr.GetDefaultApplication(want, fdp.ConsumeIntegral<int32_t>(),
166         abilityInfos, extensionInfos, fdp.ConsumeBool());
167 
168     // test GetBundleInfoByAppType
169     std::string appType = "IMAGE";
170     BundleInfo bundleInfo2;
171     defaultAppMgr.GetBundleInfoByAppType(START_USERID, appType, bundleInfo2, false);
172     defaultAppMgr.GetBundleInfoByAppType(TEST_USERID_100, appType, bundleInfo2, false);
173 
174     // test GetBundleInfo
175     Element element3;
176     element3.abilityName = "";
177     element3.bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
178     element3.extensionName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
179     element3.moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
180     BundleInfo bundleInfo3;
181     defaultAppMgr.GetBundleInfo(START_USERID, appType, element3, bundleInfo3);
182     defaultAppMgr.GetBundleInfo(TEST_USERID_100, appType, element3, bundleInfo3);
183 
184     // test MatchActionAndType
185     std::string action4 = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
186     std::string type4 = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
187     std::vector<std::string> actions = {"ohos.want.action.viewData", "ohos.want.action.sendToData"};
188     SkillUri uri1;
189     std::vector<SkillUri> uris;
190     uris.push_back(uri1);
191     Skill skill1;
192     skill1.actions = actions;
193     skill1.uris = uris;
194     std::vector<Skill> skills;
195     skills.push_back(skill1);
196     defaultAppMgr.MatchActionAndType(action4, type4, skills);
197 
198     // test IsBrowserSkillsValid
199     defaultAppMgr.IsBrowserSkillsValid(skills);
200 
201     // test IsEmailSkillsValid
202     defaultAppMgr.IsEmailSkillsValid(skills);
203 
204     // test IsElementValid
205     Element element4;
206     element4.abilityName = "";
207     element4.bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
208     element4.extensionName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
209     element4.moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
210     std::string type5 = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
211     defaultAppMgr.IsElementValid(fdp.ConsumeIntegral<int32_t>(), type5, element4);
212 
213     // test GetBrokerBundleInfo
214     Element element5;
215     element5.abilityName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
216     element5.bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
217     element5.extensionName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
218     element5.moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
219     BundleInfo bundleInfo4;
220     defaultAppMgr.GetBrokerBundleInfo(element5, bundleInfo4);
221 
222     // test SendDefaultAppChangeEventIfNeeded
223     Element element6;
224     element6.abilityName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
225     element6.bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
226     element6.extensionName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
227     element6.moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
228     std::vector<std::string> normalizedTypeVec;
229     normalizedTypeVec.push_back(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
230     std::unordered_map<std::string, std::pair<bool, Element>> originStateMap;
231     originStateMap[fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH)] = {false, element6};
232     defaultAppMgr.SendDefaultAppChangeEventIfNeeded(fdp.ConsumeIntegral<int32_t>(),
233         normalizedTypeVec, originStateMap);
234 
235     // test GetDefaultInfo
236     std::unordered_map<std::string, std::pair<bool, Element>> defaultInfo;
237     defaultAppMgr.GetDefaultInfo(fdp.ConsumeIntegral<int32_t>(), normalizedTypeVec, defaultInfo);
238 
239     // test ShouldSendEvent
240     defaultAppMgr.ShouldSendEvent(true, element6, true, element6);
241     defaultAppMgr.ShouldSendEvent(false, element6, false, element6);
242     defaultAppMgr.ShouldSendEvent(fdp.ConsumeBool(), element6, fdp.ConsumeBool(), element6);
243 
244     // SendDefaultAppChangeEvent
245     std::vector<std::string> typeVec;
246     defaultAppMgr.SendDefaultAppChangeEvent(fdp.ConsumeIntegral<int32_t>(), typeVec);
247     typeVec.push_back("BROWSER");
248     typeVec.push_back("VIDEO");
249     typeVec.push_back(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
250     defaultAppMgr.SendDefaultAppChangeEvent(fdp.ConsumeIntegral<int32_t>(), typeVec);
251 
252     DefaultAppHostImpl defaultAppHostImpl;
253     type = "BROWSER";
254     BundleInfo bundleInfo5;
255     Element element7;
256     element7.abilityName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
257     element7.bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
258     element7.extensionName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
259     element7.moduleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
260     Want want2;
261     bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
262     action = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
263     uri = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
264     name.SetBundleName(bundleName);
265     want2.SetElement(name);
266     want2.SetAction("ohos.want.action.viewData");
267     want2.SetUri("https://123");
268     want2.SetElementName("", "");
269     defaultAppHostImpl.SetDefaultApplication(TEST_USERID_100, type, want2);
270     defaultAppHostImpl.SetDefaultApplication(START_USERID, type, want2);
271 
272     // test ResetDefaultApplication
273     defaultAppHostImpl.ResetDefaultApplication(TEST_USERID_100, type);
274     defaultAppHostImpl.ResetDefaultApplication(START_USERID, type);
275 
276     // test GetCallerName
277     defaultAppHostImpl.GetCallerName();
278 
279     return true;
280 }
281 }
282 
283 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)284 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
285 {
286     /* Run your code on data */
287     OHOS::DoSomethingInterestingWithMyAPI(data, size);
288     return 0;
289 }