1 /*
2  * Copyright (c) 2024 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 #define private public
16 #define protected public
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "app_control_proxy.h"
22 
23 #include "defaultappmgr_fuzzer.h"
24 #include "default_app_mgr.h"
25 
26 using namespace OHOS::AppExecFwk;
27 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)28     bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
29     {
30         bool ret = false;
31         std::string type(reinterpret_cast<const char *>(data), size);
32 
33         BundleInfo bundleInfo;
34         auto errorCode = DefaultAppMgr::GetInstance().GetDefaultApplication(
35             reinterpret_cast<uintptr_t>(data), type, bundleInfo);
36 
37         Element element;
38         element.bundleName = "";
39         ret = DefaultAppMgr::GetInstance().VerifyElementFormat(element);
40         errorCode = DefaultAppMgr::GetInstance().SetDefaultApplication(
41             reinterpret_cast<uintptr_t>(data), type, element);
42         errorCode = DefaultAppMgr::GetInstance().ResetDefaultApplication(reinterpret_cast<uintptr_t>(data), type);
43         errorCode = DefaultAppMgr::GetInstance().IsDefaultApplication(reinterpret_cast<uintptr_t>(data), type, ret);
44         DefaultAppMgr::GetInstance().HandleCreateUser(reinterpret_cast<uintptr_t>(data));
45         DefaultAppMgr::GetInstance().HandleRemoveUser(reinterpret_cast<uintptr_t>(data));
46         DefaultAppMgr::GetInstance().HandleUninstallBundle(reinterpret_cast<uintptr_t>(data), type);
47         auto normalizedTypeVector = DefaultAppMgr::GetInstance().Normalize(type);
48         errorCode = DefaultAppMgr::GetInstance().GetDefaultApplication(
49             reinterpret_cast<uintptr_t>(data), type, bundleInfo);
50         AAFwk::Want want;
51         want.SetElementName("", "");
52         ret = DefaultAppMgr::GetInstance().GetDefaultApplication(want,
53             reinterpret_cast<uintptr_t>(data), bundleInfo.abilityInfos, bundleInfo.extensionInfos, true);
54         DefaultAppMgr::GetInstance().GetBrokerBundleInfo(element, bundleInfo);
55         DefaultAppMgr::GetInstance().Init();
56         auto isAppType = DefaultAppMgr::GetInstance().IsAppType(type);
57         ret = DefaultAppMgr::GetInstance().GetBundleInfo(reinterpret_cast<uintptr_t>(data), type, element, bundleInfo);
58         ret = DefaultAppMgr::GetInstance().IsSpecificMimeType(type);
59         std::vector<Skill> skills;
60         ret = DefaultAppMgr::GetInstance().IsMatch(type, skills);
61         ret = DefaultAppMgr::GetInstance().IsUserIdExist(reinterpret_cast<uintptr_t>(data));
62         ret = DefaultAppMgr::GetInstance().IsElementEmpty(element);
63         ret = DefaultAppMgr::GetInstance().IsElementValid(reinterpret_cast<uintptr_t>(data), type, element);
64         ret = DefaultAppMgr::GetInstance().IsEmailWant(want);
65         auto str = DefaultAppMgr::GetInstance().GetTypeFromWant(want);
66         ret = DefaultAppMgr::GetInstance().IsEmailSkillsValid(skills);
67         ret = DefaultAppMgr::GetInstance().IsBrowserSkillsValid(skills);
68         ret = DefaultAppMgr::GetInstance().MatchAppType(type, skills);
69         ret = DefaultAppMgr::GetInstance().MatchUtd(type, skills);
70         ret = DefaultAppMgr::GetInstance().MatchActionAndType(type, type, skills);
71         ret = DefaultAppMgr::GetInstance().GetBrokerBundleInfo(element, bundleInfo);
72         errorCode = DefaultAppMgr::GetInstance().GetBundleInfoByAppType(reinterpret_cast<uintptr_t>(data),
73             type, bundleInfo);
74         errorCode = DefaultAppMgr::GetInstance().GetBundleInfoByUtd(reinterpret_cast<uintptr_t>(data),
75             type, bundleInfo);
76         errorCode = DefaultAppMgr::GetInstance().VerifyPermission(type);
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 }