1 /*
2  * Copyright (c) 2022-2023 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 #include "urihandler_fuzzer.h"
17 
18 #include "copy_uri_handler.h"
19 #include "paste_uri_handler.h"
20 
21 using namespace OHOS::MiscServices;
22 
23 namespace OHOS {
24 
25 template<class T>
TypeCast(const uint8_t * data,int * pos=nullptr)26 T TypeCast(const uint8_t *data, int *pos = nullptr)
27 {
28     if (pos) {
29         *pos += sizeof(T);
30     }
31     return *(reinterpret_cast<const T*>(data));
32 }
33 
FuzzUriToFd(const uint8_t * rawData,size_t size)34 bool FuzzUriToFd(const uint8_t *rawData, size_t size)
35 {
36     if (rawData == nullptr || size < (sizeof(uint32_t) + sizeof(uint32_t))) {
37         return true;
38     }
39     int pos = 0;
40     uint32_t value = TypeCast<uint32_t>(rawData, &pos);
41     std::string uri(reinterpret_cast<const char *>(rawData + pos), size - pos);
42     bool isClient = value % 2;
43     CopyUriHandler uriHandler;
44     uriHandler.ToFd(uri, isClient);
45     return true;
46 }
47 
FuzzCopyUriHandlerToUri(const uint8_t * rawData,size_t size)48 bool FuzzCopyUriHandlerToUri(const uint8_t *rawData, size_t size)
49 {
50     if (rawData == nullptr || size < sizeof(int32_t)) {
51         return true;
52     }
53     int32_t fd = TypeCast<int32_t>(rawData, nullptr);
54     CopyUriHandler uriHandler;
55     (void)uriHandler.ToUri(fd);
56     return true;
57 }
58 
FuzzPasteUriHandlerToUri(const uint8_t * rawData,size_t size)59 bool FuzzPasteUriHandlerToUri(const uint8_t *rawData, size_t size)
60 {
61     if (rawData == nullptr || size < sizeof(int32_t)) {
62         return true;
63     }
64     int32_t fd = TypeCast<int32_t>(rawData, nullptr);
65     if (fd >= 0) {
66         return true;
67     }
68     PasteUriHandler pasteUriHandler;
69     (void)pasteUriHandler.ToUri(fd);
70     return true;
71 }
72 
FuzzPasteUriHandlerIsPaste(const uint8_t * rawData,size_t size)73 bool FuzzPasteUriHandlerIsPaste(const uint8_t *rawData, size_t size)
74 {
75     PasteUriHandler pasteUriHandler;
76     (void)pasteUriHandler.IsPaste();
77     return true;
78 }
79 } // namespace OHOS
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
83 {
84     /* Run your code on data */
85     OHOS::FuzzUriToFd(data, size);
86     OHOS::FuzzCopyUriHandlerToUri(data, size);
87     OHOS::FuzzPasteUriHandlerToUri(data, size);
88     OHOS::FuzzPasteUriHandlerIsPaste(data, size);
89     return 0;
90 }