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