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 "data_collect_manager_service_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
DataCollectManagerServiceFuzzTest(const uint8_t * data,size_t size)57 bool DataCollectManagerServiceFuzzTest(const uint8_t* data, size_t size)
58 {
59 FuzzedDataProvider fdp(data, size);
60 int64_t eventId = fdp.ConsumeIntegral<int64_t>();
61 std::string string = fdp.ConsumeRandomLengthString(MAX_STRING_SIZE);
62 int fd = fdp.ConsumeIntegral<int32_t>();
63 std::vector<std::u16string> args{Str8ToStr16(string)};
64 Security::SecurityCollector::SecurityEventRuler ruler{eventId};
65 Security::SecurityCollector::Event event{eventId, string, string, string};
66 Security::SecurityCollector::SecurityCollectorSubscribeInfo subscribeInfo{event};
67 RequestCondition condition;
68 sptr<IRemoteObject> obj(new (std::nothrow) MockRemoteObject());
69 auto proxy = iface_cast<ISecurityEventQueryCallback>(obj);
70 sptr<IRemoteObject> callback(new (std::nothrow) MockRemoteObject());
71 DataCollectManagerService service(DATA_COLLECT_MANAGER_SA_ID, false);
72 service.Dump(fd, args);
73 service.RequestDataSubmit(eventId, string, string, string);
74 service.RequestDataSubmitAsync(eventId, string, string, string);
75 service.OnAddSystemAbility(fd, string);
76 service.OnRemoveSystemAbility(fd, string);
77 service.QueryEventByRuler(proxy, ruler);
78 service.CollectorStart(subscribeInfo, callback);
79 service.CollectorStop(subscribeInfo, callback);
80 service.ConfigUpdate(fd, string);
81 service.WriteRemoteFileToLocal(fd, string);
82 DataCollectManagerService::GetSecEventsFromConditions(condition);
83 return true;
84 }
85
86 } // namespace OHOS
87
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90 {
91 /* Run your code on date */
92 OHOS::DataCollectManagerServiceFuzzTest(data, size);
93 return 0;
94 }