• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "database_helper_fuzzer.h"
17 
18 #include <string>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #include "securec.h"
21 #include <string_ex.h>
22 
23 #define private public
24 #define protected public
25 #include "event_define.h"
26 #include "acquire_data_subscribe_manager.h"
27 #include "acquire_data_callback_proxy.h"
28 #include "data_collect_manager_callback_proxy.h"
29 #include "data_collect_manager_service.h"
30 #include "data_collect_manager_idl_stub.h"
31 #include "security_event_query_callback_proxy.h"
32 #include "database_helper.h"
33 #include "database_manager.h"
34 #include "database.h"
35 #include "risk_event_rdb_helper.h"
36 #include "store_define.h"
37 #undef private
38 #undef prtected
39 
40 using namespace OHOS::Security::SecurityGuard;
41 namespace {
42     constexpr int MAX_STRING_SIZE = 1024;
43 }
44 namespace OHOS {
45 
DatabaseHelperFuzzTest(const uint8_t * data,size_t size)46 bool DatabaseHelperFuzzTest(const uint8_t* data, size_t size)
47 {
48     FuzzedDataProvider fdp(data, size);
49     int64_t eventId = fdp.ConsumeIntegral<int64_t>();
50     std::vector<int64_t> eventIds{eventId};
51     std::string string = fdp.ConsumeRandomLengthString(MAX_STRING_SIZE);
52     DatabaseHelper helper{string};
53     SecEvent event = {
54         .eventId = eventId,
55         .version = string,
56     };
57     std::vector<SecEvent> events{event};
58     GenericValues value{};
59     helper.Init();
60     helper.InsertEvent(event);
61     helper.QueryAllEvent(events);
62     helper.QueryRecentEventByEventId(eventId, event);
63     helper.QueryRecentEventByEventId(eventIds, events);
64     helper.QueryEventByEventId(eventId, events);
65     helper.QueryEventByEventId(eventIds, events);
66     helper.QueryEventByEventIdAndDate(eventIds, events, string, string);
67     helper.QueryEventByEventType(eventId, events);
68     helper.QueryEventByLevel(eventId, events);
69     helper.QueryEventByOwner(string, events);
70     helper.CountAllEvent();
71     helper.CountEventByEventId(eventId);
72     helper.DeleteOldEventByEventId(eventId, eventId);
73     helper.DeleteAllEventByEventId(eventId);
74     helper.FlushAllEvent();
75     helper.QueryEventBase(value, events);
76     helper.CreateTable();
77     helper.SetValuesBucket(event, value);
78     helper.Release();
79     return true;
80 }
81 
RiskEventRdbHelperFuzzTest()82 void RiskEventRdbHelperFuzzTest()
83 {
84     Security::SecurityGuard::RiskEventRdbHelper::GetInstance().Init();
85 }
86 }  // namespace OHOS
87 
LLVMFuzzerInitialize(int * argc,char *** argv)88 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
89 {
90     OHOS::RiskEventRdbHelperFuzzTest();
91     return 0;
92 }
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97     /* Run your code on date */
98     OHOS::DatabaseHelperFuzzTest(data, size);
99     return 0;
100 }