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 "fuzz_common_base.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(FuzzData fuzzData)28 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
29 {
30     std::string stringData = fuzzData.GenerateRandomString();
31     size_t index = fuzzData.GetData<size_t>();
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     matchingSkills.AddEntity(stringData);
67     matchingSkills.AddEvent(stringData);
68     matchingSkills.CountEvent();
69     matchingSkills.AddScheme(stringData);
70     matchingSkills.Marshalling(parcel);
71     // test Match function
72     AAFwk::Want want;
73     return matchingSkills.Match(want);
74 }
75 }
76 
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
79 {
80     /* Run your code on data */
81     if (data == nullptr) {
82         return 0;
83     }
84 
85     if (size < OHOS::U32_AT_SIZE) {
86         return 0;
87     }
88     OHOS::FuzzData fuzzData(data, size);
89     OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
90     return 0;
91 }
92