1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci
16bc03f14fSopenharmony_ci#include "urihandler_fuzzer.h"
17bc03f14fSopenharmony_ci
18bc03f14fSopenharmony_ci#include "copy_uri_handler.h"
19bc03f14fSopenharmony_ci#include "paste_uri_handler.h"
20bc03f14fSopenharmony_ci
21bc03f14fSopenharmony_ciusing namespace OHOS::MiscServices;
22bc03f14fSopenharmony_ci
23bc03f14fSopenharmony_cinamespace OHOS {
24bc03f14fSopenharmony_ci
25bc03f14fSopenharmony_citemplate<class T>
26bc03f14fSopenharmony_ciT TypeCast(const uint8_t *data, int *pos = nullptr)
27bc03f14fSopenharmony_ci{
28bc03f14fSopenharmony_ci    if (pos) {
29bc03f14fSopenharmony_ci        *pos += sizeof(T);
30bc03f14fSopenharmony_ci    }
31bc03f14fSopenharmony_ci    return *(reinterpret_cast<const T*>(data));
32bc03f14fSopenharmony_ci}
33bc03f14fSopenharmony_ci
34bc03f14fSopenharmony_cibool FuzzUriToFd(const uint8_t *rawData, size_t size)
35bc03f14fSopenharmony_ci{
36bc03f14fSopenharmony_ci    if (rawData == nullptr || size < (sizeof(uint32_t) + sizeof(uint32_t))) {
37bc03f14fSopenharmony_ci        return true;
38bc03f14fSopenharmony_ci    }
39bc03f14fSopenharmony_ci    int pos = 0;
40bc03f14fSopenharmony_ci    uint32_t value = TypeCast<uint32_t>(rawData, &pos);
41bc03f14fSopenharmony_ci    std::string uri(reinterpret_cast<const char *>(rawData + pos), size - pos);
42bc03f14fSopenharmony_ci    bool isClient = value % 2;
43bc03f14fSopenharmony_ci    CopyUriHandler uriHandler;
44bc03f14fSopenharmony_ci    uriHandler.ToFd(uri, isClient);
45bc03f14fSopenharmony_ci    return true;
46bc03f14fSopenharmony_ci}
47bc03f14fSopenharmony_ci
48bc03f14fSopenharmony_cibool FuzzCopyUriHandlerToUri(const uint8_t *rawData, size_t size)
49bc03f14fSopenharmony_ci{
50bc03f14fSopenharmony_ci    if (rawData == nullptr || size < sizeof(int32_t)) {
51bc03f14fSopenharmony_ci        return true;
52bc03f14fSopenharmony_ci    }
53bc03f14fSopenharmony_ci    int32_t fd = TypeCast<int32_t>(rawData, nullptr);
54bc03f14fSopenharmony_ci    CopyUriHandler uriHandler;
55bc03f14fSopenharmony_ci    (void)uriHandler.ToUri(fd);
56bc03f14fSopenharmony_ci    return true;
57bc03f14fSopenharmony_ci}
58bc03f14fSopenharmony_ci
59bc03f14fSopenharmony_cibool FuzzPasteUriHandlerToUri(const uint8_t *rawData, size_t size)
60bc03f14fSopenharmony_ci{
61bc03f14fSopenharmony_ci    if (rawData == nullptr || size < sizeof(int32_t)) {
62bc03f14fSopenharmony_ci        return true;
63bc03f14fSopenharmony_ci    }
64bc03f14fSopenharmony_ci    int32_t fd = TypeCast<int32_t>(rawData, nullptr);
65bc03f14fSopenharmony_ci    if (fd >= 0) {
66bc03f14fSopenharmony_ci        return true;
67bc03f14fSopenharmony_ci    }
68bc03f14fSopenharmony_ci    PasteUriHandler pasteUriHandler;
69bc03f14fSopenharmony_ci    (void)pasteUriHandler.ToUri(fd);
70bc03f14fSopenharmony_ci    return true;
71bc03f14fSopenharmony_ci}
72bc03f14fSopenharmony_ci
73bc03f14fSopenharmony_cibool FuzzPasteUriHandlerIsPaste(const uint8_t *rawData, size_t size)
74bc03f14fSopenharmony_ci{
75bc03f14fSopenharmony_ci    PasteUriHandler pasteUriHandler;
76bc03f14fSopenharmony_ci    (void)pasteUriHandler.IsPaste();
77bc03f14fSopenharmony_ci    return true;
78bc03f14fSopenharmony_ci}
79bc03f14fSopenharmony_ci} // namespace OHOS
80bc03f14fSopenharmony_ci
81bc03f14fSopenharmony_ci/* Fuzzer entry point */
82bc03f14fSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
83bc03f14fSopenharmony_ci{
84bc03f14fSopenharmony_ci    /* Run your code on data */
85bc03f14fSopenharmony_ci    OHOS::FuzzUriToFd(data, size);
86bc03f14fSopenharmony_ci    OHOS::FuzzCopyUriHandlerToUri(data, size);
87bc03f14fSopenharmony_ci    OHOS::FuzzPasteUriHandlerToUri(data, size);
88bc03f14fSopenharmony_ci    OHOS::FuzzPasteUriHandlerIsPaste(data, size);
89bc03f14fSopenharmony_ci    return 0;
90bc03f14fSopenharmony_ci}
91