• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
34     BundleResourceHelper::DeleteResourceInfo(bundleName);
35     BundleResourceHelper::DeleteAllResourceInfo();
36     std::vector<std::string> resourceNames;
37     BundleResourceHelper::GetAllBundleResourceName(resourceNames);
38     BundleResourceHelper::ParseBundleName(bundleName);
39     std::string moduleName(data, size);
40     BundleResourceHelper::SetOverlayEnabled(bundleName, moduleName, true, USERID);
41     int32_t appIndex = 0;
42     BundleResourceHelper::AddCloneBundleResourceInfo(bundleName, appIndex, USERID);
43     BundleResourceHelper::DeleteCloneBundleResourceInfo(bundleName, appIndex, USERID);
44     BundleResourceHelper::DeleteNotExistResourceInfo();
45     return true;
46 }
47 } // namespace OHOS
48 
49 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)50 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
51 {
52     /* Run your code on data */
53     if (data == nullptr) {
54         return 0;
55     }
56 
57     if (size < OHOS::U32_AT_SIZE) {
58         return 0;
59     }
60 
61     char* ch = static_cast<char*>(malloc(size + 1));
62     if (ch == nullptr) {
63         return 0;
64     }
65 
66     (void)memset_s(ch, size + 1, 0x00, size + 1);
67     if (memcpy_s(ch, size, data, size) != EOK) {
68         free(ch);
69         ch = nullptr;
70         return 0;
71     }
72     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
73     free(ch);
74     ch = nullptr;
75     return 0;
76 }