• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <fuzzer/FuzzedDataProvider.h>
17 
18 #include "rdbresultsetstub_fuzzer.h"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 
24 #include "rdb_result_set_impl.h"
25 #include "rdb_result_set_stub.h"
26 #include "message_parcel.h"
27 #include "store/cursor.h"
28 #include "securec.h"
29 
30 using namespace OHOS::DistributedRdb;
31 
32 namespace OHOS {
33 using Code = NativeRdb::RemoteResultSet::Code;
34 const std::u16string INTERFACE_TOKEN = u"OHOS::NativeRdb.IResultSet";
35 
OnRemoteRequestFuzz(FuzzedDataProvider & provider)36 bool OnRemoteRequestFuzz(FuzzedDataProvider &provider)
37 {
38     const int min = 0;
39     uint32_t code = provider.ConsumeIntegralInRange<uint32_t>(min, Code::CMD_MAX);
40     std::vector<uint8_t> remainingData = provider.ConsumeRemainingBytes<uint8_t>();
41     MessageParcel request;
42     request.WriteInterfaceToken(INTERFACE_TOKEN);
43     request.WriteBuffer(static_cast<void *>(remainingData.data()), remainingData.size());
44     request.RewindRead(0);
45     MessageParcel reply;
46     MessageOption option;
47     std::shared_ptr<DistributedData::Cursor> dbResultSet;
48     auto result = std::make_shared<RdbResultSetImpl>(dbResultSet);
49     std::shared_ptr<RdbResultSetStub> rdbResultSetStub = std::make_shared<RdbResultSetStub>(result);
50     rdbResultSetStub->OnRemoteRequest(code, request, reply, option);
51     return true;
52 }
53 } // namespace OHOS
54 
55 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
57 {
58     FuzzedDataProvider provider(data, size);
59     OHOS::OnRemoteRequestFuzz(provider);
60     return 0;
61 }