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 "bundleoverlayinstallchecker_fuzzer.h"
18
19 #include "bundle_overlay_install_checker.h"
20 #include "securec.h"
21
22 using namespace OHOS::AppExecFwk;
23 namespace OHOS {
24 constexpr size_t U32_AT_SIZE = 4;
25 constexpr uint32_t CODE_MAX = 8;
26 const std::string TEST_BUNDLE_NAME = "testBundleName";
27 const int32_t USERID = 100;
28
DoSomethingInterestingWithMyAPI(const char * data,size_t size)29 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
30 {
31 BundleOverlayInstallChecker checker;
32 std::unordered_map<std::string, InnerBundleInfo> newInfos;
33 int32_t overlayType = NON_OVERLAY_TYPE;
34 checker.CheckOverlayInstallation(newInfos, USERID, overlayType);
35 InnerBundleInfo innerBundleInfo;
36 checker.CheckInternalBundle(newInfos, innerBundleInfo);
37 checker.CheckExternalBundle(innerBundleInfo, USERID);
38 checker.CheckTargetBundle(TEST_BUNDLE_NAME, "", "", USERID);
39 InnerBundleInfo otherInnerBundleInfo;
40 checker.CheckOverlayUpdate(innerBundleInfo, otherInnerBundleInfo, USERID);
41 checker.CheckHapType(innerBundleInfo);
42 checker.CheckBundleType(innerBundleInfo);
43 checker.CheckTargetModule(TEST_BUNDLE_NAME, std::string(data, size));
44 return true;
45 }
46 }
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 }