1 /*
2 * Copyright (c) 2022 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 "matchingskills_fuzzer.h"
17 #include "securec.h"
18 #define private public
19 #define protected public
20 #include "matching_skills.h"
21 #undef private
22 #undef protected
23
24 namespace OHOS {
25 namespace {
26 constexpr size_t U32_AT_SIZE = 4;
27 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)28 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
29 {
30 std::string stringData(data);
31 size_t index = U32_AT(reinterpret_cast<const uint8_t*>(data));
32 Parcel parcel;
33 // test MatchingSkills class function
34 EventFwk::MatchingSkills matchingSkills;
35 // test HasEntity function
36 matchingSkills.HasEntity(stringData);
37 // test RemoveEntity function
38 matchingSkills.RemoveEntity(stringData);
39 // test CountEntities function
40 matchingSkills.CountEntities();
41 // test GetEvents function
42 matchingSkills.GetEvents();
43 // test RemoveEvent function
44 matchingSkills.RemoveEvent(stringData);
45 // test HasEvent function
46 matchingSkills.HasEvent(stringData);
47 // test HasScheme function
48 matchingSkills.HasScheme(stringData);
49 // test RemoveScheme function
50 matchingSkills.RemoveScheme(stringData);
51 // test CountSchemes function
52 matchingSkills.CountSchemes();
53 // test MatchEvent function
54 matchingSkills.MatchEvent(stringData);
55 // test MatchEntity function
56 std::vector<std::string> permissions;
57 permissions.emplace_back(stringData);
58 matchingSkills.MatchEntity(permissions);
59 // test MatchScheme function
60 matchingSkills.MatchScheme(stringData);
61 matchingSkills.GetEntity(index);
62 matchingSkills.GetEvent(index);
63 matchingSkills.GetScheme(index);
64 matchingSkills.ReadFromParcel(parcel);
65 matchingSkills.Unmarshalling(parcel);
66 // test Match function
67 AAFwk::Want want;
68 return matchingSkills.Match(want);
69 }
70 }
71
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75 /* Run your code on data */
76 if (data == nullptr) {
77 return 0;
78 }
79
80 if (size < OHOS::U32_AT_SIZE) {
81 return 0;
82 }
83
84 char* ch = (char *)malloc(size + 1);
85 if (ch == nullptr) {
86 return 0;
87 }
88
89 (void)memset_s(ch, size + 1, 0x00, size + 1);
90 if (memcpy_s(ch, size, data, size) != EOK) {
91 free(ch);
92 ch = nullptr;
93 return 0;
94 }
95
96 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
97 free(ch);
98 ch = nullptr;
99 return 0;
100 }
101