1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "deeplinkreserveconfig_fuzzer.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include <cstddef>
19eace7efcSopenharmony_ci#include <cstdint>
20eace7efcSopenharmony_ci#include <iostream>
21eace7efcSopenharmony_ci
22eace7efcSopenharmony_ci#include "securec.h"
23eace7efcSopenharmony_ci
24eace7efcSopenharmony_ci#define private public
25eace7efcSopenharmony_ci#include "deeplink_reserve_config.h"
26eace7efcSopenharmony_ci#undef private
27eace7efcSopenharmony_ci
28eace7efcSopenharmony_ciusing json = nlohmann::json;
29eace7efcSopenharmony_ciusing namespace OHOS::AAFwk;
30eace7efcSopenharmony_cinamespace OHOS {
31eace7efcSopenharmony_cinamespace {
32eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0;
33eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1;
34eace7efcSopenharmony_ciconstexpr int INPUT_TWO = 2;
35eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3;
36eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024;
37eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4;
38eace7efcSopenharmony_ciconstexpr size_t OFFSET_ZERO = 24;
39eace7efcSopenharmony_ciconstexpr size_t OFFSET_ONE = 16;
40eace7efcSopenharmony_ciconstexpr size_t OFFSET_TWO = 8;
41eace7efcSopenharmony_ciconst std::string CONFIG_PATH = "/etc/ability_runtime/deeplink_reserve_config.json";
42eace7efcSopenharmony_ciconst std::string DEFAULT_RESERVE_CONFIG_PATH = "/system/etc/deeplink_reserve_config.json";
43eace7efcSopenharmony_ciconst std::string DEEPLINK_RESERVED_URI_NAME = "deepLinkReservedUri";
44eace7efcSopenharmony_ciconst std::string BUNDLE_NAME = "bundleName";
45eace7efcSopenharmony_ciconst std::string URIS_NAME = "uris";
46eace7efcSopenharmony_ciconst std::string SCHEME_NAME = "scheme";
47eace7efcSopenharmony_ciconst std::string HOST_NAME = "host";
48eace7efcSopenharmony_ciconst std::string PORT_NAME = "port";
49eace7efcSopenharmony_ciconst std::string PATH_NAME = "path";
50eace7efcSopenharmony_ciconst std::string PATH_START_WITH_NAME = "pathStartWith";
51eace7efcSopenharmony_ciconst std::string PATH_REGEX_NAME = "pathRegex";
52eace7efcSopenharmony_ciconst std::string TYPE_NAME = "type";
53eace7efcSopenharmony_ciconst std::string UTD_NAME = "utd";
54eace7efcSopenharmony_ciconst std::string PORT_SEPARATOR = ":";
55eace7efcSopenharmony_ciconst std::string SCHEME_SEPARATOR = "://";
56eace7efcSopenharmony_ciconst std::string PATH_SEPARATOR = "/";
57eace7efcSopenharmony_ciconst std::string PARAM_SEPARATOR = "?";
58eace7efcSopenharmony_ci}
59eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr)
60eace7efcSopenharmony_ci{
61eace7efcSopenharmony_ci    // convert fuzz input data to an integer
62eace7efcSopenharmony_ci    return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) |
63eace7efcSopenharmony_ci        ptr[INPUT_THREE];
64eace7efcSopenharmony_ci}
65eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
66eace7efcSopenharmony_ci{
67eace7efcSopenharmony_ci    auto &deepLinkReserveConfig = DeepLinkReserveConfig::GetInstance();
68eace7efcSopenharmony_ci    deepLinkReserveConfig.GetConfigPath();
69eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadConfiguration();
70eace7efcSopenharmony_ci    std::string linkString(data, size);
71eace7efcSopenharmony_ci    std::string bundleName(data, size);
72eace7efcSopenharmony_ci    deepLinkReserveConfig.isLinkReserved(linkString, bundleName);
73eace7efcSopenharmony_ci    ReserveUri reservedUri;
74eace7efcSopenharmony_ci    std::string link(data, size);
75eace7efcSopenharmony_ci    std::string strParam(data, size);
76eace7efcSopenharmony_ci    deepLinkReserveConfig.isUriMatched(reservedUri, link);
77eace7efcSopenharmony_ci    reservedUri.scheme = strParam;
78eace7efcSopenharmony_ci    deepLinkReserveConfig.isUriMatched(reservedUri, link);
79eace7efcSopenharmony_ci    reservedUri.host = strParam;
80eace7efcSopenharmony_ci    deepLinkReserveConfig.isUriMatched(reservedUri, link);
81eace7efcSopenharmony_ci    reservedUri.port = strParam;
82eace7efcSopenharmony_ci    deepLinkReserveConfig.isUriMatched(reservedUri, link);
83eace7efcSopenharmony_ci    reservedUri.path = strParam;
84eace7efcSopenharmony_ci    deepLinkReserveConfig.isUriMatched(reservedUri, link);
85eace7efcSopenharmony_ci    reservedUri.pathStartWith = strParam;
86eace7efcSopenharmony_ci    deepLinkReserveConfig.isUriMatched(reservedUri, link);
87eace7efcSopenharmony_ci    reservedUri.pathRegex = strParam;
88eace7efcSopenharmony_ci    deepLinkReserveConfig.isUriMatched(reservedUri, link);
89eace7efcSopenharmony_ci    std::vector<ReserveUri> uriList;
90eace7efcSopenharmony_ci    json jsonUriObject;
91eace7efcSopenharmony_ci    jsonUriObject["SCHEME_NAME"] = SCHEME_NAME;
92eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList);
93eace7efcSopenharmony_ci    jsonUriObject["HOST_NAME"] = HOST_NAME;
94eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList);
95eace7efcSopenharmony_ci    jsonUriObject["PORT_NAME"] = PORT_NAME;
96eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList);
97eace7efcSopenharmony_ci    jsonUriObject["PATH_NAME"] = PATH_NAME;
98eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList);
99eace7efcSopenharmony_ci    jsonUriObject["PATH_START_WITH_NAME"] = PATH_START_WITH_NAME;
100eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList);
101eace7efcSopenharmony_ci    jsonUriObject["PATH_REGEX_NAME"] = PATH_REGEX_NAME;
102eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList);
103eace7efcSopenharmony_ci    jsonUriObject["TYPE_NAME"] = TYPE_NAME;
104eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList);
105eace7efcSopenharmony_ci    jsonUriObject["UTD_NAME"] = UTD_NAME;
106eace7efcSopenharmony_ci    deepLinkReserveConfig.LoadReservedUrilItem(jsonUriObject, uriList);
107eace7efcSopenharmony_ci    return true;
108eace7efcSopenharmony_ci}
109eace7efcSopenharmony_ci
110eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPIOne(const char* data, size_t size)
111eace7efcSopenharmony_ci{
112eace7efcSopenharmony_ci    auto &deepLinkReserveConfig1 = DeepLinkReserveConfig::GetInstance();
113eace7efcSopenharmony_ci    std::string filePath(data, size);
114eace7efcSopenharmony_ci    json jsonBuf;
115eace7efcSopenharmony_ci    deepLinkReserveConfig1.ReadFileInfoJson(filePath, jsonBuf);
116eace7efcSopenharmony_ci    json object;
117eace7efcSopenharmony_ci    deepLinkReserveConfig1.LoadReservedUriList(object);
118eace7efcSopenharmony_ci    object["DEEPLINK_RESERVED_URI_NAME"] = DEEPLINK_RESERVED_URI_NAME;
119eace7efcSopenharmony_ci    deepLinkReserveConfig1.LoadReservedUriList(object);
120eace7efcSopenharmony_ci    int32_t userId = static_cast<int32_t>(GetU32Data(data));
121eace7efcSopenharmony_ci    object["BUNDLE_NAME"] = userId;
122eace7efcSopenharmony_ci    deepLinkReserveConfig1.LoadReservedUriList(object);
123eace7efcSopenharmony_ci    object["BUNDLE_NAME"] = BUNDLE_NAME;
124eace7efcSopenharmony_ci    deepLinkReserveConfig1.LoadReservedUriList(object);
125eace7efcSopenharmony_ci    json uriArray = { "uri1", "uri2", "uri3" };
126eace7efcSopenharmony_ci    object["URIS_NAME"] = uriArray;
127eace7efcSopenharmony_ci    deepLinkReserveConfig1.LoadReservedUriList(object);
128eace7efcSopenharmony_ci    object["URIS_NAME"] = URIS_NAME;
129eace7efcSopenharmony_ci    deepLinkReserveConfig1.LoadReservedUriList(object);
130eace7efcSopenharmony_ci    return true;
131eace7efcSopenharmony_ci}
132eace7efcSopenharmony_ci}
133eace7efcSopenharmony_ci
134eace7efcSopenharmony_ci/* Fuzzer entry point */
135eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
136eace7efcSopenharmony_ci{
137eace7efcSopenharmony_ci    /* Run your code on data */
138eace7efcSopenharmony_ci    if (data == nullptr) {
139eace7efcSopenharmony_ci        std::cout << "invalid data" << std::endl;
140eace7efcSopenharmony_ci        return 0;
141eace7efcSopenharmony_ci    }
142eace7efcSopenharmony_ci
143eace7efcSopenharmony_ci    /* Validate the length of size */
144eace7efcSopenharmony_ci    if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
145eace7efcSopenharmony_ci        return 0;
146eace7efcSopenharmony_ci    }
147eace7efcSopenharmony_ci
148eace7efcSopenharmony_ci    char* ch = (char*)malloc(size + 1);
149eace7efcSopenharmony_ci    if (ch == nullptr) {
150eace7efcSopenharmony_ci        std::cout << "malloc failed." << std::endl;
151eace7efcSopenharmony_ci        return 0;
152eace7efcSopenharmony_ci    }
153eace7efcSopenharmony_ci
154eace7efcSopenharmony_ci    (void)memset_s(ch, size + 1, 0x00, size + 1);
155eace7efcSopenharmony_ci    if (memcpy_s(ch, size + 1, data, size) != EOK) {
156eace7efcSopenharmony_ci        std::cout << "copy failed." << std::endl;
157eace7efcSopenharmony_ci        free(ch);
158eace7efcSopenharmony_ci        ch = nullptr;
159eace7efcSopenharmony_ci        return 0;
160eace7efcSopenharmony_ci    }
161eace7efcSopenharmony_ci
162eace7efcSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(ch, size);
163eace7efcSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPIOne(ch, size);
164eace7efcSopenharmony_ci    free(ch);
165eace7efcSopenharmony_ci    ch = nullptr;
166eace7efcSopenharmony_ci    return 0;
167eace7efcSopenharmony_ci}
168eace7efcSopenharmony_ci
169