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 "wallpaper_service_mock_fuzzer.h"
17
18 #include <fuzzer/FuzzedDataProvider.h>
19
20 #include <cstdint>
21 #include <iostream>
22
23 #include "iwallpaper_service.h"
24 #include "message_parcel.h"
25 #include "wallpaper_service_stub.h"
26
27 namespace {
28 using namespace OHOS;
29 using namespace OHOS::WallpaperMgrService;
30
31 constexpr uint32_t CODE_MIN = 0;
32 constexpr uint32_t CODE_MAX =
33 static_cast<uint32_t>(IWallpaperServiceIpcCode::COMMAND_IS_DEFAULT_WALLPAPER_RESOURCE) + 1;
34
35 const std::u16string WALLPAPERSERVICES_INTERFACE_TOKEN = u"OHOS.WallpaperMgrService.IWallpaperService";
36
37 class WallpaperServiceStubMock : public WallpaperServiceStub {
38 public:
SetWallpaper(int fd,int32_t wallpaperType,int32_t length)39 ErrCode SetWallpaper(int fd, int32_t wallpaperType, int32_t length) override
40 {
41 (void)fd;
42 (void)wallpaperType;
43 (void)length;
44 return 0;
45 }
46
SetAllWallpapers(const WallpaperPictureInfoByParcel & allWallpaperPictures,int32_t wallpaperType,const std::vector<int> & fdVector)47 ErrCode SetAllWallpapers(const WallpaperPictureInfoByParcel &allWallpaperPictures, int32_t wallpaperType,
48 const std::vector<int> &fdVector) override
49 {
50 (void)allWallpaperPictures;
51 (void)wallpaperType;
52 (void)fdVector;
53 return 0;
54 }
55
SetWallpaperByPixelMap(const WallpaperRawData & wallpaperRawdata,int32_t wallpaperType)56 ErrCode SetWallpaperByPixelMap(const WallpaperRawData &wallpaperRawdata, int32_t wallpaperType) override
57 {
58 (void)wallpaperRawdata;
59 (void)wallpaperType;
60 return 0;
61 }
62
GetPixelMap(int32_t wallpaperType,int32_t & size,int & fd)63 ErrCode GetPixelMap(int32_t wallpaperType, int32_t &size, int &fd) override
64 {
65 (void)wallpaperType;
66 (void)size;
67 (void)fd;
68 return 0;
69 }
70
GetCorrespondWallpaper(int32_t wallpaperType,int32_t foldState,int32_t rotateState,int32_t & size,int & fd)71 ErrCode GetCorrespondWallpaper(
72 int32_t wallpaperType, int32_t foldState, int32_t rotateState, int32_t &size, int &fd) override
73 {
74 (void)wallpaperType;
75 (void)foldState;
76 (void)rotateState;
77 (void)size;
78 (void)fd;
79 return 0;
80 }
81
GetColors(int32_t wallpaperType,std::vector<uint64_t> & colors)82 ErrCode GetColors(int32_t wallpaperType, std::vector<uint64_t> &colors) override
83 {
84 (void)wallpaperType;
85 (void)colors;
86 return 0;
87 }
88
GetFile(int32_t wallpaperType,int & wallpaperFd)89 ErrCode GetFile(int32_t wallpaperType, int &wallpaperFd) override
90 {
91 (void)wallpaperType;
92 (void)wallpaperFd;
93 return 0;
94 }
95
GetWallpaperId(int32_t wallpaperType)96 ErrCode GetWallpaperId(int32_t wallpaperType) override
97 {
98 (void)wallpaperType;
99 return 0;
100 }
101
IsChangePermitted(bool & isChangePermitted)102 ErrCode IsChangePermitted(bool &isChangePermitted) override
103 {
104 (void)isChangePermitted;
105 return 0;
106 }
107
IsOperationAllowed(bool & isOperationAllowed)108 ErrCode IsOperationAllowed(bool &isOperationAllowed) override
109 {
110 (void)isOperationAllowed;
111 return 0;
112 }
113
ResetWallpaper(int32_t wallpaperType)114 ErrCode ResetWallpaper(int32_t wallpaperType) override
115 {
116 (void)wallpaperType;
117 return 0;
118 }
119
On(const std::string & type,const sptr<IWallpaperEventListener> & listener)120 ErrCode On(const std::string &type, const sptr<IWallpaperEventListener> &listener) override
121 {
122 (void)type;
123 (void)listener;
124 return 0;
125 }
126
Off(const std::string & type,const sptr<IWallpaperEventListener> & listener)127 ErrCode Off(const std::string &type, const sptr<IWallpaperEventListener> &listener) override
128 {
129 (void)type;
130 (void)listener;
131 return 0;
132 }
133
RegisterWallpaperCallback(const sptr<IWallpaperCallback> & wallpaperCallback,bool & registerWallpaperCallback)134 ErrCode RegisterWallpaperCallback(
135 const sptr<IWallpaperCallback> &wallpaperCallback, bool ®isterWallpaperCallback) override
136 {
137 (void)wallpaperCallback;
138 (void)registerWallpaperCallback;
139 return 0;
140 }
141
SetWallpaperV9(int fd,int32_t wallpaperType,int32_t length)142 ErrCode SetWallpaperV9(int fd, int32_t wallpaperType, int32_t length) override
143 {
144 (void)fd;
145 (void)wallpaperType;
146 (void)length;
147 return 0;
148 }
149
SetWallpaperV9ByPixelMap(const WallpaperRawData & wallpaperRawdata,int32_t wallpaperType)150 ErrCode SetWallpaperV9ByPixelMap(const WallpaperRawData &wallpaperRawdata, int32_t wallpaperType) override
151 {
152 (void)wallpaperRawdata;
153 (void)wallpaperType;
154 return 0;
155 }
156
GetPixelMapV9(int32_t wallpaperType,int32_t & size,int & fd)157 ErrCode GetPixelMapV9(int32_t wallpaperType, int32_t &size, int &fd) override
158 {
159 (void)wallpaperType;
160 (void)size;
161 (void)fd;
162 return 0;
163 }
164
GetColorsV9(int32_t wallpaperType,std::vector<uint64_t> & colors)165 ErrCode GetColorsV9(int32_t wallpaperType, std::vector<uint64_t> &colors) override
166 {
167 (void)wallpaperType;
168 (void)colors;
169 return 0;
170 }
171
ResetWallpaperV9(int32_t wallpaperType)172 ErrCode ResetWallpaperV9(int32_t wallpaperType) override
173 {
174 (void)wallpaperType;
175 return 0;
176 }
177
SetVideo(int fd,int32_t wallpaperType,int32_t length)178 ErrCode SetVideo(int fd, int32_t wallpaperType, int32_t length) override
179 {
180 (void)fd;
181 (void)wallpaperType;
182 (void)length;
183 return 0;
184 }
185
SetCustomWallpaper(int fd,int32_t wallpaperType,int32_t length)186 ErrCode SetCustomWallpaper(int fd, int32_t wallpaperType, int32_t length) override
187 {
188 (void)fd;
189 (void)wallpaperType;
190 (void)length;
191 return 0;
192 }
193
SendEvent(const std::string & eventType)194 ErrCode SendEvent(const std::string &eventType) override
195 {
196 (void)eventType;
197 return 0;
198 }
199
IsDefaultWallpaperResource(int32_t userId,int32_t wallpaperType,bool & isDefaultWallpaperResource)200 ErrCode IsDefaultWallpaperResource(
201 int32_t userId, int32_t wallpaperType, bool &isDefaultWallpaperResource) override
202 {
203 (void)userId;
204 (void)wallpaperType;
205 (void)isDefaultWallpaperResource;
206 return 0;
207 }
208
CallbackParcel(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)209 int32_t CallbackParcel(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
210 {
211 return 0;
212 };
213 };
214
215 class WallpaperFuzzTest {
216 public:
WallpaperFuzzTest()217 WallpaperFuzzTest()
218 {
219 isInited_ = false;
220 stub_ = std::make_shared<WallpaperServiceStubMock>();
221 if (stub_ == nullptr) {
222 return;
223 }
224 isInited_ = true;
225 }
226
~WallpaperFuzzTest()227 ~WallpaperFuzzTest()
228 {
229 stub_ = nullptr;
230 isInited_ = false;
231 }
232
IsInited() const233 bool IsInited() const noexcept
234 {
235 return isInited_;
236 }
237
DoRemoteRequest(FuzzedDataProvider & provider)238 void DoRemoteRequest(FuzzedDataProvider &provider)
239 {
240 MessageParcel data;
241 MessageParcel reply;
242 MessageOption option;
243 uint32_t code = provider.ConsumeIntegralInRange<uint32_t>(CODE_MIN, CODE_MAX);
244 data.WriteInterfaceToken(WALLPAPERSERVICES_INTERFACE_TOKEN);
245 std::vector<uint8_t> remaining_data = provider.ConsumeRemainingBytes<uint8_t>();
246 data.WriteBuffer(static_cast<void *>(remaining_data.data()), remaining_data.size());
247 data.RewindRead(0);
248 if (stub_ != nullptr) {
249 stub_->OnRemoteRequest(code, data, reply, option);
250 }
251 }
252
253 private:
254 std::atomic<bool> isInited_;
255 std::shared_ptr<WallpaperServiceStubMock> stub_;
256 };
257 } // namespace
258
259 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)260 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
261 {
262 static WallpaperFuzzTest wallpaperFuzzTest;
263 if (!wallpaperFuzzTest.IsInited()) {
264 return 0;
265 }
266 FuzzedDataProvider provider(data, size);
267 /* Run your code on data */
268 wallpaperFuzzTest.DoRemoteRequest(provider);
269 return 0;
270 }