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
16 #define private public
17 #include "bundleresrdb_fuzzer.h"
18
19 #include "bundle_resource_rdb.h"
20 #include "bundle_resource_register.h"
21 #include "securec.h"
22
23 using namespace OHOS::AppExecFwk;
24 namespace OHOS {
25 constexpr size_t U32_AT_SIZE = 4;
26 constexpr uint32_t CODE_MAX = 8;
27 const int32_t USERID = 100;
28 const std::string MODULE_NAME = "entry";
29 const std::string ABILITY_NAME = "com.example.bmsaccesstoken1.MainAbility";
30
DoSomethingInterestingWithMyAPI(const char * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
32 {
33 BundleResourceRdb resourceRdb;
34 ResourceInfo resourceInfo;
35 std::string bundleName(data, size);
36 resourceInfo.bundleName_ = bundleName;
37 resourceRdb.AddResourceInfo(resourceInfo);
38 resourceRdb.DeleteResourceInfo(resourceInfo.GetKey());
39 std::vector<ResourceInfo> resourceInfos;
40 resourceInfos.push_back(resourceInfo);
41 resourceRdb.AddResourceInfos(resourceInfos);
42 resourceRdb.DeleteAllResourceInfo();
43 std::vector<std::string> keyNames;
44 resourceRdb.GetAllResourceName(keyNames);
45 int32_t appIndex = 1;
46 resourceRdb.GetResourceNameByBundleName(bundleName, appIndex, keyNames);
47 BundleResourceInfo info;
48 resourceRdb.GetBundleResourceInfo(resourceInfo.bundleName_,
49 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), info);
50 std::vector<LauncherAbilityResourceInfo> launcherInfos;
51 resourceRdb.GetLauncherAbilityResourceInfo(resourceInfo.bundleName_,
52 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), launcherInfos);
53 std::vector<BundleResourceInfo> infos;
54 resourceRdb.GetAllBundleResourceInfo(
55 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), infos);
56 resourceRdb.GetAllLauncherAbilityResourceInfo(
57 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), launcherInfos);
58 resourceRdb.UpdateResourceForSystemStateChanged(resourceInfos);
59 std::string systemState(data, size);
60 resourceRdb.GetCurrentSystemState(systemState);
61 resourceRdb.DeleteNotExistResourceInfo();
62 resourceRdb.ParseKey(resourceInfo.GetKey(), info);
63 LauncherAbilityResourceInfo launcherInfo;
64 resourceRdb.ParseKey(resourceInfo.GetKey(), launcherInfo);
65 resourceRdb.BackupRdb();
66 BundleResourceRegister::RegisterConfigurationObserver();
67 BundleResourceRegister::RegisterCommonEventSubscriber();
68 resourceInfo.ConvertFromBundleResourceInfo(info);
69 resourceInfo.ConvertFromLauncherAbilityResourceInfo(launcherInfo);
70 resourceInfo.InnerParseAppIndex(resourceInfo.GetKey());
71 return true;
72 }
73 }
74
75 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78 /* Run your code on data */
79 if (data == nullptr) {
80 return 0;
81 }
82
83 if (size < OHOS::U32_AT_SIZE) {
84 return 0;
85 }
86
87 char* ch = static_cast<char*>(malloc(size + 1));
88 if (ch == nullptr) {
89 return 0;
90 }
91
92 (void)memset_s(ch, size + 1, 0x00, size + 1);
93 if (memcpy_s(ch, size, data, size) != EOK) {
94 free(ch);
95 ch = nullptr;
96 return 0;
97 }
98 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
99 free(ch);
100 ch = nullptr;
101 return 0;
102 }