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 "bmsbundleresourcehelper_fuzzer.h"
18
19 #include "bundle_resource_helper.h"
20 #include "securec.h"
21
22 using namespace OHOS::AppExecFwk;
23 namespace OHOS {
24 constexpr size_t U32_AT_SIZE = 4;
25 const int32_t USERID = 100;
26
DoSomethingInterestingWithMyAPI(const char * data,size_t size)27 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
28 {
29 BundleResourceHelper::BundleSystemStateInit();
30 BundleResourceHelper::RegisterConfigurationObserver();
31 BundleResourceHelper::RegisterCommonEventSubscriber();
32 std::string bundleName(data, size);
33 BundleResourceHelper::AddResourceInfoByBundleName(bundleName, USERID, ADD_RESOURCE_TYPE::INSTALL_BUNDLE, true);
34 BundleResourceHelper::DeleteAllResourceInfo();
35 std::vector<std::string> resourceNames;
36 BundleResourceHelper::GetAllBundleResourceName(resourceNames);
37 BundleResourceHelper::ParseBundleName(bundleName);
38 std::string moduleName(data, size);
39 BundleResourceHelper::SetOverlayEnabled(bundleName, moduleName, true, USERID);
40 int32_t appIndex = 0;
41 BundleResourceHelper::AddCloneBundleResourceInfo(bundleName, USERID, appIndex, true);
42 BundleResourceHelper::DeleteCloneBundleResourceInfo(bundleName, USERID, appIndex, false);
43 BundleResourceHelper::DeleteNotExistResourceInfo();
44 return true;
45 }
46 } // namespace OHOS
47
48 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)49 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
50 {
51 /* Run your code on data */
52 if (data == nullptr) {
53 return 0;
54 }
55
56 if (size < OHOS::U32_AT_SIZE) {
57 return 0;
58 }
59
60 char* ch = static_cast<char*>(malloc(size + 1));
61 if (ch == nullptr) {
62 return 0;
63 }
64
65 (void)memset_s(ch, size + 1, 0x00, size + 1);
66 if (memcpy_s(ch, size, data, size) != EOK) {
67 free(ch);
68 ch = nullptr;
69 return 0;
70 }
71 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
72 free(ch);
73 ch = nullptr;
74 return 0;
75 }