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_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 class MockRemoteObject final : public IRemoteObject {
46 public:
MockRemoteObject()47 MockRemoteObject() : IRemoteObject(u"")
48 {
49 }
GetObjectRefCount()50 int32_t GetObjectRefCount() { return 0; };
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)51 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { return 0; };
AddDeathRecipient(const sptr<DeathRecipient> & recipient)52 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; };
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)53 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; };
Dump(int fd,const std::vector<std::u16string> & args)54 int Dump(int fd, const std::vector<std::u16string> &args) { return 0; };
55 };
56
DatabaseFuzzTest(const uint8_t * data,size_t size)57 bool DatabaseFuzzTest(const uint8_t* data, size_t size)
58 {
59 Database database{};
60 FuzzedDataProvider fdp(data, size);
61 int32_t int32 = fdp.ConsumeIntegral<int32_t>();
62 int64_t int64 = fdp.ConsumeIntegral<int64_t>();
63 std::string string = fdp.ConsumeRandomLengthString(MAX_STRING_SIZE);
64 GenericValues value{};
65 std::vector<std::string> strings;
66 std::vector<GenericValues> values{value};
67 std::vector<std::string> columns{string};
68 database.Insert(int64, string, value);
69 database.BatchInsert(int64, string, values);
70 database.Update(int32, string, value);
71 database.Delete(int32, string, value);
72 database.Query(string, value, values);
73 database.ExecuteSql(string);
74 database.ExecuteAndGetLong(int64, string, strings);
75 database.Count(int64, string);
76 database.BeginTransaction();
77 database.RollBack();
78 database.Commit();
79 return true;
80 }
81 } // namespace OHOS
82
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
85 {
86 /* Run your code on date */
87 OHOS::DatabaseFuzzTest(data, size);
88 return 0;
89 }