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 #include "wanteighth_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21
22 #define private public
23 #include "want.h"
24 #undef private
25 #include "securec.h"
26
27 using namespace OHOS::AAFwk;
28
29 namespace OHOS {
30 namespace {
31 constexpr size_t U32_AT_SIZE = 4;
32 }
33
DoSomethingInterestingWithMyAPI(const char * data,size_t size)34 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
35 {
36 std::shared_ptr<Want> want = std::make_shared<Want>();
37 want->GetOperation();
38 Want wantEqual;
39 want->IsEquals(wantEqual);
40 std::string uri(data, size);
41 want->GetUriString();
42 want->SetUri(uri);
43 want->GetUri();
44 want->CheckUri(uri);
45 want->WantToUri(wantEqual);
46 want->ToUri();
47 want->OperationEquals(wantEqual);
48 Parcel parcel;
49 want->ReadFromParcel(parcel);
50 want->ToUriStringInner(uri);
51 WantParams wantParams;
52 want->ReplaceParams(wantParams);
53 want->ReplaceParams(wantEqual);
54 std::string key(data, size);
55 want->RemoveParam(key);
56 want->ClearWant(&wantEqual);
57 std::string content(data, size);
58 want->ToJson();
59 want->ToString();
60 auto wantPtr = want->FromString(content);
61 if (wantPtr) {
62 delete wantPtr;
63 wantPtr = nullptr;
64 }
65 want->CloseAllFd();
66 want->RemoveAllFd();
67 want->DupAllFd();
68 std::vector<std::string> entities;
69 want->SetEntities(entities);
70 return true;
71 }
72 }
73
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77 /* Run your code on data */
78 if (data == nullptr) {
79 std::cout << "invalid data" << std::endl;
80 return 0;
81 }
82
83 /* Validate the length of size */
84 if (size < OHOS::U32_AT_SIZE) {
85 return 0;
86 }
87
88 char* ch = reinterpret_cast<char *>(malloc(size + 1));
89 if (ch == nullptr) {
90 std::cout << "malloc failed." << std::endl;
91 return 0;
92 }
93
94 (void)memset_s(ch, size + 1, 0x00, size + 1);
95 if (memcpy_s(ch, size, data, size) != EOK) {
96 std::cout << "copy failed." << std::endl;
97 free(ch);
98 ch = nullptr;
99 return 0;
100 }
101
102 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
103 free(ch);
104 ch = nullptr;
105 return 0;
106 }
107