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 "routermaphelper_fuzzer.h"
18
19 #include "router_map_helper.h"
20 #include "securec.h"
21
22 using namespace OHOS::AppExecFwk;
23 namespace OHOS {
24 constexpr size_t U32_AT_SIZE = 4;
25 constexpr uint8_t ENABLE = 2;
26 constexpr uint8_t CODE_MAX = 22;
27
DoSomethingInterestingWithMyAPI(const char * data,size_t size)28 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
29 {
30 BundleInfo info;
31 info.appId = std::string(data, size);
32 std::vector<RouterItem> routerArrayList;
33 std::vector<RouterItem> routerArray;
34 std::set<std::string> moduleNameSet;
35 RouterMapHelper::MergeRouter(info);
36 RouterMapHelper::MergeRouter(routerArrayList, routerArray, moduleNameSet);
37 std::string version1 = "1.0.0";
38 std::string version2 = "2.0.0";
39 RouterMapHelper::Compare(version1, version2);
40 SemVer semVer1(version1);
41 SemVer semVer2(version2);
42 RouterMapHelper::Compare(semVer1, semVer2);
43 RouterMapHelper::CompareIdentifiers(version1, version2);
44 RouterMapHelper::CompareMain(semVer1, semVer2);
45 RouterMapHelper::ComparePre(semVer1, semVer2);
46 RouterMapHelper::ExtractVersionFromOhmurl(version1);
47 return true;
48 }
49 } // namespace OHOS
50
51 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)52 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
53 {
54 /* Run your code on data */
55 if (data == nullptr) {
56 return 0;
57 }
58
59 /* Validate the length of size */
60 if (size < OHOS::U32_AT_SIZE) {
61 return 0;
62 }
63
64 char* ch = (char*)malloc(size + 1);
65 if (ch == nullptr) {
66 std::cout << "malloc failed." << std::endl;
67 return 0;
68 }
69
70 (void)memset_s(ch, size + 1, 0x00, size + 1);
71 if (memcpy_s(ch, size + 1, data, size) != EOK) {
72 std::cout << "copy failed." << std::endl;
73 free(ch);
74 ch = nullptr;
75 return 0;
76 }
77
78 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
79 free(ch);
80 ch = nullptr;
81 return 0;
82 }