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 "dataservicestub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "kvstore_data_service.h"
22 #include "message_parcel.h"
23 #include "securec.h"
24 
25 using namespace OHOS::DistributedKv;
26 
27 namespace OHOS {
28 const std::u16string INTERFACE_TOKEN = u"OHOS.DistributedKv.IKvStoreDataService";
29 const uint32_t CODE_MIN = 0;
30 const uint32_t CODE_MAX = 10;
31 
OnRemoteRequestFuzz(const uint8_t * data,size_t size)32 bool OnRemoteRequestFuzz(const uint8_t *data, size_t size)
33 {
34     uint32_t code = static_cast<uint32_t>(*data) % (CODE_MAX - CODE_MIN + 1) + CODE_MIN;
35     MessageParcel request;
36     request.WriteInterfaceToken(INTERFACE_TOKEN);
37     request.WriteBuffer(data, size);
38     request.RewindRead(0);
39     MessageParcel reply;
40     MessageOption option;
41     std::shared_ptr<KvStoreDataServiceStub> kvStoreDataServiceStub = std::make_shared<KvStoreDataService>();
42     kvStoreDataServiceStub->OnRemoteRequest(code, request, reply, option);
43     return true;
44 }
45 } // namespace OHOS
46 
47 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)48 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
49 {
50     if (data == nullptr) {
51         return 0;
52     }
53 
54     OHOS::OnRemoteRequestFuzz(data, size);
55 
56     return 0;
57 }