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 "intention_service_fuzzer.h"
17
18 #include <thread>
19
20 #include "intention_service.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "MsdpIntentionServiceFuzzTest"
24
25
26 namespace {
27 using namespace OHOS;
28 using namespace OHOS::Msdp;
29
30 const std::u16string INTENTION_INTERFACE_TOKEN = u"OHOS.Msdp.IIntention";
31
32 const std::vector<IIntentionIpcCode > CODE_LIST = {
33 IIntentionIpcCode::COMMAND_SOCKET,
34 IIntentionIpcCode::COMMAND_ENABLE_COOPERATE,
35 IIntentionIpcCode::COMMAND_DISABLE_COOPERATE,
36 IIntentionIpcCode::COMMAND_START_COOPERATE,
37 IIntentionIpcCode::COMMAND_START_COOPERATE_WITH_OPTIONS,
38 IIntentionIpcCode::COMMAND_STOP_COOPERATE,
39 IIntentionIpcCode::COMMAND_REGISTER_COOPERATE_LISTENER,
40 IIntentionIpcCode::COMMAND_UNREGISTER_COOPERATE_LISTENER,
41 IIntentionIpcCode::COMMAND_REGISTER_HOT_AREA_LISTENER,
42 IIntentionIpcCode::COMMAND_UNREGISTER_HOT_AREA_LISTENER,
43 IIntentionIpcCode::COMMAND_REGISTER_MOUSE_EVENT_LISTENER,
44 IIntentionIpcCode::COMMAND_UNREGISTER_MOUSE_EVENT_LISTENER,
45 IIntentionIpcCode::COMMAND_GET_COOPERATE_STATE_SYNC,
46 IIntentionIpcCode::COMMAND_GET_COOPERATE_STATE_ASYNC,
47 IIntentionIpcCode::COMMAND_SET_DAMPLING_COEFFICIENT,
48 IIntentionIpcCode::COMMAND_START_DRAG,
49 IIntentionIpcCode::COMMAND_STOP_DRAG,
50 IIntentionIpcCode::COMMAND_ENABLE_INTERNAL_DROP_ANIMATION,
51 IIntentionIpcCode::COMMAND_ADD_DRAGLISTENER,
52 IIntentionIpcCode::COMMAND_REMOVE_DRAGLISTENER,
53 IIntentionIpcCode::COMMAND_ADD_SUBSCRIPT_LISTENER,
54 IIntentionIpcCode::COMMAND_REMOVE_SUBSCRIPT_LISTENER,
55 IIntentionIpcCode::COMMAND_SET_DRAG_WINDOW_VISIBLE,
56 IIntentionIpcCode::COMMAND_UPDATE_DRAG_STYLE,
57 IIntentionIpcCode::COMMAND_UPDATE_SHADOW_PIC,
58 IIntentionIpcCode::COMMAND_GET_DRAG_TARGET_PID,
59 IIntentionIpcCode::COMMAND_GET_UD_KEY,
60 IIntentionIpcCode::COMMAND_GET_SHADOW_OFFSET,
61 IIntentionIpcCode::COMMAND_GET_DRAG_DATA,
62 IIntentionIpcCode::COMMAND_UPDATE_PREVIEW_STYLE,
63 IIntentionIpcCode::COMMAND_UPDATE_PREVIEW_STYLE_WITH_ANIMATION,
64 IIntentionIpcCode::COMMAND_ROTATE_DRAG_WINDOW_SYNC,
65 IIntentionIpcCode::COMMAND_SET_DRAG_WINDOW_SCREEN_ID,
66 IIntentionIpcCode::COMMAND_GET_DRAG_SUMMARY,
67 IIntentionIpcCode::COMMAND_SET_DRAG_SWITCH_STATE,
68 IIntentionIpcCode::COMMAND_SET_APP_DRAG_SWITCH_STATE,
69 IIntentionIpcCode::COMMAND_GET_DRAG_STATE,
70 IIntentionIpcCode::COMMAND_ENABLE_UPPER_CENTER_MODE,
71 IIntentionIpcCode::COMMAND_GET_DRAG_ACTION,
72 IIntentionIpcCode::COMMAND_GET_EXTRA_INFO,
73 IIntentionIpcCode::COMMAND_ADD_PRIVILEGE,
74 IIntentionIpcCode::COMMAND_ERASE_MOUSE_ICON,
75 IIntentionIpcCode::COMMAND_SET_MOUSE_DRAG_MONITOR_STATE,
76 IIntentionIpcCode::COMMAND_SET_DRAGGABLE_STATE,
77 IIntentionIpcCode::COMMAND_GET_APP_DRAG_SWITCH_STATE,
78 IIntentionIpcCode::COMMAND_SET_DRAGGABLE_STATE_ASYNC,
79 IIntentionIpcCode::COMMAND_GET_DRAG_BUNDLE_INFO,
80 IIntentionIpcCode::COMMAND_SUBSCRIBE_CALLBACK,
81 IIntentionIpcCode::COMMAND_UNSUBSCRIBE_CALLBACK,
82 IIntentionIpcCode::COMMAND_NOTIFY_METADATA_BINDING_EVENT,
83 IIntentionIpcCode::COMMAND_SUBMIT_METADATA,
84 IIntentionIpcCode::COMMAND_BOOMERANG_ENCODE_IMAGE,
85 IIntentionIpcCode::COMMAND_BOOMERANG_DECODE_IMAGE,
86 IIntentionIpcCode::COMMAND_SUBSCRIBE_STATIONARY_CALLBACK,
87 IIntentionIpcCode::COMMAND_UNSUBSCRIBE_STATIONARY_CALLBACK,
88 IIntentionIpcCode::COMMAND_GET_DEVICE_STATUS_DATA,
89 IIntentionIpcCode::COMMAND_START_COOPERATE_WITH_OPTIONS,
90 IIntentionIpcCode::COMMAND_ENABLE_INTERNAL_DROP_ANIMATION,
91 IIntentionIpcCode::COMMAND_GET_DEVICE_POSTURE_DATA_SYNC,
92 IIntentionIpcCode::COMMAND_GET_PAGE_CONTENT,
93 IIntentionIpcCode::COMMAND_SEND_CONTROL_EVENT,
94 };
95
96 class IntentionServiceMock : public IntentionStub {
97 public:
Socket(const std::string & programName,int32_t moduleType,int & socketFd,int32_t & tokenType)98 ErrCode Socket(const std::string& programName, int32_t moduleType, int& socketFd, int32_t& tokenType) override
99 {
100 (void)programName;
101 (void)moduleType;
102 (void)socketFd;
103 (void)tokenType;
104 return 0;
105 }
106
107 // Cooperate
EnableCooperate(int32_t userData)108 ErrCode EnableCooperate(int32_t userData) override
109 {
110 (void)userData;
111 return 0;
112 }
113
DisableCooperate(int32_t userData)114 ErrCode DisableCooperate(int32_t userData) override
115 {
116 (void)userData;
117 return 0;
118 }
119
StartCooperate(const std::string & remoteNetworkId,int32_t userData,int32_t startDeviceId,bool checkPermission)120 ErrCode StartCooperate(const std::string& remoteNetworkId, int32_t userData, int32_t startDeviceId,
121 bool checkPermission) override
122 {
123 (void)remoteNetworkId;
124 (void)userData;
125 (void)startDeviceId;
126 (void)checkPermission;
127 return 0;
128 }
129
StartCooperateWithOptions(const std::string & remoteNetworkId,int32_t userData,int32_t startDeviceId,bool checkPermission,const SequenceableCooperateOptions & options)130 ErrCode StartCooperateWithOptions(const std::string& remoteNetworkId, int32_t userData, int32_t startDeviceId,
131 bool checkPermission, const SequenceableCooperateOptions& options) override
132 {
133 (void)remoteNetworkId;
134 (void)userData;
135 (void)startDeviceId;
136 (void)checkPermission;
137 (void)options;
138 return 0;
139 }
140
StopCooperate(int32_t userData,bool isUnchained,bool checkPermission)141 ErrCode StopCooperate(int32_t userData, bool isUnchained, bool checkPermission) override
142 {
143 (void)userData;
144 (void)isUnchained;
145 (void)checkPermission;
146 return 0;
147 }
148
RegisterCooperateListener()149 ErrCode RegisterCooperateListener() override
150 {
151 return 0;
152 }
153
UnregisterCooperateListener()154 ErrCode UnregisterCooperateListener() override
155 {
156 return 0;
157 }
158
RegisterHotAreaListener(int32_t userData,bool checkPermission)159 ErrCode RegisterHotAreaListener(int32_t userData, bool checkPermission) override
160 {
161 (void)userData;
162 (void)checkPermission;
163 return 0;
164 }
165
UnregisterHotAreaListener()166 ErrCode UnregisterHotAreaListener() override
167 {
168 return 0;
169 }
170
RegisterMouseEventListener(const std::string & networkId)171 ErrCode RegisterMouseEventListener(const std::string& networkId) override
172 {
173 (void)networkId;
174 return 0;
175 }
176
UnregisterMouseEventListener(const std::string & networkId)177 ErrCode UnregisterMouseEventListener(const std::string& networkId) override
178 {
179 (void)networkId;
180 return 0;
181 }
182
GetCooperateStateSync(const std::string & udid,bool & state)183 ErrCode GetCooperateStateSync(const std::string& udid, bool& state) override
184 {
185 (void)udid;
186 (void)state;
187 return 0;
188 }
189
GetCooperateStateAsync(const std::string & networkId,int32_t userData,bool isCheckPermission)190 ErrCode GetCooperateStateAsync(const std::string& networkId, int32_t userData, bool isCheckPermission) override
191 {
192 (void)networkId;
193 (void)userData;
194 (void)isCheckPermission;
195 return 0;
196 }
197
SetDamplingCoefficient(uint32_t direction,double coefficient)198 ErrCode SetDamplingCoefficient(uint32_t direction, double coefficient) override
199 {
200 (void)direction;
201 (void)coefficient;
202 return 0;
203 }
204
205 // Drag
StartDrag(const SequenceableDragData & sequenceableDragData)206 ErrCode StartDrag(const SequenceableDragData &sequenceableDragData) override
207 {
208 (void)sequenceableDragData;
209 return 0;
210 }
211
StopDrag(const SequenceableDragResult & sequenceableDragResult)212 ErrCode StopDrag(const SequenceableDragResult &sequenceableDragResult) override
213 {
214 (void)sequenceableDragResult;
215 return 0;
216 }
217
EnableInternalDropAnimation(const std::string & animationInfo)218 ErrCode EnableInternalDropAnimation(const std::string &animationInfo) override
219 {
220 (void)animationInfo;
221 return 0;
222 }
223
AddDraglistener(bool isJsCaller)224 ErrCode AddDraglistener(bool isJsCaller) override
225 {
226 (void)isJsCaller;
227 return 0;
228 }
229
RemoveDraglistener(bool isJsCaller)230 ErrCode RemoveDraglistener(bool isJsCaller) override
231 {
232 (void)isJsCaller;
233 return 0;
234 }
235
AddSubscriptListener()236 ErrCode AddSubscriptListener() override
237 {
238 return 0;
239 }
240
RemoveSubscriptListener()241 ErrCode RemoveSubscriptListener() override
242 {
243 return 0;
244 }
245
SetDragWindowVisible(const SequenceableDragVisible & sequenceableDragVisible)246 ErrCode SetDragWindowVisible(const SequenceableDragVisible &sequenceableDragVisible) override
247 {
248 (void)sequenceableDragVisible;
249 return 0;
250 }
251
UpdateDragStyle(int32_t style,int32_t eventId)252 ErrCode UpdateDragStyle(int32_t style, int32_t eventId) override
253 {
254 (void)style;
255 (void)eventId;
256 return 0;
257 }
258
UpdateShadowPic(const std::shared_ptr<PixelMap> & pixelMap,int32_t x,int32_t y)259 ErrCode UpdateShadowPic(const std::shared_ptr<PixelMap>& pixelMap, int32_t x, int32_t y) override
260 {
261 (void)pixelMap;
262 (void)x;
263 (void)y;
264 return 0;
265 }
266
GetDragTargetPid(int32_t & targetPid)267 ErrCode GetDragTargetPid(int32_t &targetPid) override
268 {
269 (void)targetPid;
270 return 0;
271 }
272
GetUdKey(std::string & udKey)273 ErrCode GetUdKey(std::string &udKey) override
274 {
275 (void)udKey;
276 return 0;
277 }
278
GetShadowOffset(int32_t & offsetX,int32_t & offsetY,int32_t & width,int32_t & height)279 ErrCode GetShadowOffset(int32_t &offsetX, int32_t &offsetY, int32_t &width, int32_t &height) override
280 {
281 (void)offsetX;
282 (void)offsetY;
283 (void)width;
284 (void)height;
285 return 0;
286 }
287
GetDragData(SequenceableDragData & sequenceableDragData)288 ErrCode GetDragData(SequenceableDragData &sequenceableDragData) override
289 {
290 (void)sequenceableDragData;
291 return 0;
292 }
293
UpdatePreviewStyle(const SequenceablePreviewStyle & sequenceablePreviewStyle)294 ErrCode UpdatePreviewStyle(const SequenceablePreviewStyle &sequenceablePreviewStyle) override
295 {
296 (void)sequenceablePreviewStyle;
297 return 0;
298 }
299
UpdatePreviewStyleWithAnimation(const SequenceablePreviewAnimation & sequenceablePreviewAnimation)300 ErrCode UpdatePreviewStyleWithAnimation(const SequenceablePreviewAnimation &sequenceablePreviewAnimation) override
301 {
302 (void)sequenceablePreviewAnimation;
303 return 0;
304 }
305
RotateDragWindowSync(const SequenceableRotateWindow & sequenceableRotateWindow)306 ErrCode RotateDragWindowSync(const SequenceableRotateWindow &sequenceableRotateWindow) override
307 {
308 (void)sequenceableRotateWindow;
309 return 0;
310 }
311
SetDragWindowScreenId(uint64_t displayId,uint64_t screenId)312 ErrCode SetDragWindowScreenId(uint64_t displayId, uint64_t screenId) override
313 {
314 (void)displayId;
315 (void)screenId;
316 return 0;
317 }
318
GetDragSummary(std::map<std::string,int64_t> & summarys,bool isJsCaller)319 ErrCode GetDragSummary(std::map<std::string, int64_t> &summarys, bool isJsCaller) override
320 {
321 (void)summarys;
322 (void)isJsCaller;
323 return 0;
324 }
325
GetDragSummaryInfo(SequenceableDragSummaryInfo & sequenceableDragSummaryInfo)326 ErrCode GetDragSummaryInfo(SequenceableDragSummaryInfo &sequenceableDragSummaryInfo) override
327 {
328 (void)sequenceableDragSummaryInfo;
329 return 0;
330 }
331
SetDragSwitchState(bool enable,bool isJsCaller)332 ErrCode SetDragSwitchState(bool enable, bool isJsCaller) override
333 {
334 (void)enable;
335 (void)isJsCaller;
336 return 0;
337 }
338
SetAppDragSwitchState(bool enable,const std::string & pkgName,bool isJsCaller)339 ErrCode SetAppDragSwitchState(bool enable, const std::string &pkgName, bool isJsCaller) override
340 {
341 (void)enable;
342 (void)pkgName;
343 (void)isJsCaller;
344 return 0;
345 }
346
GetDragState(int32_t & dragState)347 ErrCode GetDragState(int32_t &dragState) override
348 {
349 (void)dragState;
350 return 0;
351 }
352
EnableUpperCenterMode(bool enable)353 ErrCode EnableUpperCenterMode(bool enable) override
354 {
355 (void)enable;
356 return 0;
357 }
358
GetDragAction(int32_t & dragAction)359 ErrCode GetDragAction(int32_t &dragAction) override
360 {
361 (void)dragAction;
362 return 0;
363 }
364
GetExtraInfo(std::string & extraInfo)365 ErrCode GetExtraInfo(std::string &extraInfo) override
366 {
367 (void)extraInfo;
368 return 0;
369 }
370
AddPrivilege()371 ErrCode AddPrivilege() override
372 {
373 return 0;
374 }
375
EraseMouseIcon()376 ErrCode EraseMouseIcon() override
377 {
378 return 0;
379 }
380
SetMouseDragMonitorState(bool state)381 ErrCode SetMouseDragMonitorState(bool state) override
382 {
383 (void)state;
384 return 0;
385 }
386
SetDraggableState(bool state)387 ErrCode SetDraggableState(bool state) override
388 {
389 (void)state;
390 return 0;
391 }
392
GetAppDragSwitchState(bool & state)393 ErrCode GetAppDragSwitchState(bool &state) override
394 {
395 (void)state;
396 return 0;
397 }
398
SetDraggableStateAsync(bool state,int64_t downTime)399 ErrCode SetDraggableStateAsync(bool state, int64_t downTime) override
400 {
401 (void)state;
402 (void)downTime;
403 return 0;
404 }
405
GetDragBundleInfo(std::string & bundleName,bool & state)406 ErrCode GetDragBundleInfo(std::string &bundleName, bool &state) override
407 {
408 (void)bundleName;
409 (void)state;
410 return 0;
411 }
412
IsDragStart(bool & isStart)413 ErrCode IsDragStart(bool &isStart) override
414 {
415 (void)isStart;
416 return 0;
417 }
418
419 // Boomerang
SubscribeCallback(int32_t type,const std::string & bundleName,const sptr<IRemoteBoomerangCallback> & subCallback)420 ErrCode SubscribeCallback(int32_t type, const std::string& bundleName,
421 const sptr<IRemoteBoomerangCallback>& subCallback) override
422 {
423 (void)type;
424 (void)bundleName;
425 (void)subCallback;
426 return 0;
427 }
428
UnsubscribeCallback(int32_t type,const std::string & bundleName,const sptr<IRemoteBoomerangCallback> & unsubCallback)429 ErrCode UnsubscribeCallback(int32_t type, const std::string& bundleName,
430 const sptr<IRemoteBoomerangCallback>& unsubCallback) override
431 {
432 (void)type;
433 (void)bundleName;
434 (void)unsubCallback;
435 return 0;
436 }
437
NotifyMetadataBindingEvent(const std::string & bundleName,const sptr<IRemoteBoomerangCallback> & notifyCallback)438 ErrCode NotifyMetadataBindingEvent(const std::string& bundleName,
439 const sptr<IRemoteBoomerangCallback>& notifyCallback) override
440 {
441 (void)bundleName;
442 (void)notifyCallback;
443 return 0;
444 }
445
SubmitMetadata(const std::string & metadata)446 ErrCode SubmitMetadata(const std::string& metadata) override
447 {
448 (void)metadata;
449 return 0;
450 }
451
BoomerangEncodeImage(const std::shared_ptr<PixelMap> & pixelMap,const std::string & metadata,const sptr<IRemoteBoomerangCallback> & encodeCallback)452 ErrCode BoomerangEncodeImage(const std::shared_ptr<PixelMap>& pixelMap, const std::string& metadata,
453 const sptr<IRemoteBoomerangCallback>& encodeCallback) override
454 {
455 (void)pixelMap;
456 (void)metadata;
457 (void)encodeCallback;
458 return 0;
459 }
460
BoomerangDecodeImage(const std::shared_ptr<PixelMap> & pixelMap,const sptr<IRemoteBoomerangCallback> & decodeCallback)461 ErrCode BoomerangDecodeImage(const std::shared_ptr<PixelMap>& pixelMap,
462 const sptr<IRemoteBoomerangCallback>& decodeCallback) override
463 {
464 (void)pixelMap;
465 (void)decodeCallback;
466 return 0;
467 }
468
469 // Stationary
SubscribeStationaryCallback(int32_t type,int32_t event,int32_t latency,const sptr<IRemoteDevStaCallback> & subCallback)470 ErrCode SubscribeStationaryCallback(int32_t type, int32_t event,
471 int32_t latency, const sptr<IRemoteDevStaCallback> &subCallback) override
472 {
473 (void)type;
474 (void)event;
475 (void)latency;
476 (void)subCallback;
477 return 0;
478 }
479
UnsubscribeStationaryCallback(int32_t type,int32_t event,const sptr<IRemoteDevStaCallback> & unsubCallback)480 ErrCode UnsubscribeStationaryCallback(int32_t type, int32_t event,
481 const sptr<IRemoteDevStaCallback> &unsubCallback) override
482 {
483 (void)type;
484 (void)event;
485 (void)unsubCallback;
486 return 0;
487 }
488
GetDeviceStatusData(int32_t type,int32_t & replyType,int32_t & replyValue)489 ErrCode GetDeviceStatusData(int32_t type, int32_t &replyType, int32_t &replyValue) override
490 {
491 (void)type;
492 (void)replyType;
493 (void)replyValue;
494 return 0;
495 }
496
GetDevicePostureDataSync(SequenceablePostureData & data)497 ErrCode GetDevicePostureDataSync(SequenceablePostureData &data) override
498 {
499 (void)data;
500 return 0;
501 }
502
GetPageContent(const DeviceStatus::OnScreen::SequenceableContentOption & option,DeviceStatus::OnScreen::SequenceablePageContent & pageContent)503 ErrCode GetPageContent(const DeviceStatus::OnScreen::SequenceableContentOption &option,
504 DeviceStatus::OnScreen::SequenceablePageContent &pageContent) override
505 {
506 (void)option;
507 (void)pageContent;
508 return 0;
509 }
510
SendControlEvent(const DeviceStatus::OnScreen::SequenceableControlEvent & event)511 ErrCode SendControlEvent(const DeviceStatus::OnScreen::SequenceableControlEvent &event) override
512 {
513 (void)event;
514 return 0;
515 }
516 };
517
DoSleep(void)518 static inline void DoSleep(void)
519 {
520 uint32_t sleepMs = 10;
521 std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs));
522 }
523
524 class TestEnv {
525 public:
TestEnv()526 TestEnv()
527 {
528 isInited_ = false;
529 stub_ = new IntentionServiceMock();
530 if (stub_ == nullptr) {
531 return;
532 }
533 isInited_ = true;
534 }
535
~TestEnv()536 ~TestEnv()
537 {
538 isInited_ = false;
539 stub_ = nullptr;
540 }
541
IsInited() const542 bool IsInited() const noexcept
543 {
544 return isInited_;
545 }
546
DoRemoteRequest(IIntentionIpcCode code,MessageParcel & data)547 void DoRemoteRequest(IIntentionIpcCode code, MessageParcel &data)
548 {
549 MessageParcel reply;
550 MessageOption option;
551 if (stub_ != nullptr) {
552 stub_->OnRemoteRequest(static_cast<uint32_t>(code), data, reply, option);
553 }
554 }
555
556 private:
557 volatile bool isInited_;
558 sptr<IntentionServiceMock> stub_;
559 };
560 }
561
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)562 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
563 {
564 static TestEnv env;
565 if (!env.IsInited()) {
566 return 0;
567 }
568
569 if (data == nullptr || size == 0) {
570 return 0;
571 }
572 IIntentionIpcCode code = CODE_LIST[data[0] % CODE_LIST.size()];
573
574 OHOS::MessageParcel parcel;
575 parcel.WriteInterfaceToken(INTENTION_INTERFACE_TOKEN);
576 parcel.WriteBuffer(data + 1, size - 1);
577
578 env.DoRemoteRequest(code, parcel);
579 DoSleep();
580 return 0;
581 }