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 <cstddef>
17 #include <cstdint>
18 
19 #include "message_parcel.h"
20 #include "wallpaper_service.h"
21 #include "wallpaperstub_fuzzer.h"
22 
23 using namespace OHOS::WallpaperMgrService;
24 constexpr size_t THRESHOLD = 4;
25 namespace OHOS {
26 const std::u16string WALLPAPERSERVICES_INTERFACE_TOKEN = u"ohos.Wallpaper.IWallpaperService";
27 
ConvertToUint32(const uint8_t * ptr)28 uint32_t ConvertToUint32(const uint8_t *ptr)
29 {
30     if (ptr == nullptr) {
31         return 0;
32     }
33     uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
34     return bigVar;
35 }
36 
FuzzTestRemoteRequest(const uint8_t * rawData,size_t size)37 void FuzzTestRemoteRequest(const uint8_t *rawData, size_t size)
38 {
39     MessageParcel data;
40     data.WriteInterfaceToken(WALLPAPERSERVICES_INTERFACE_TOKEN);
41     data.WriteBuffer(rawData, size);
42     data.RewindRead(0);
43     MessageParcel reply;
44     MessageOption option;
45     std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
46     uint32_t code = ConvertToUint32(rawData);
47     wallpaperService->OnRemoteRequest(code, data, reply, option);
48 }
49 } // namespace OHOS
50 
51 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)52 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
53 {
54     if (size < THRESHOLD) {
55         return 0;
56     }
57     OHOS::FuzzTestRemoteRequest(data, size);
58     return 0;
59 }