• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "backupservicestub_fuzzer.h"
17 
18 #include <cstring>
19 
20 #include "b_incremental_data.h"
21 #include "b_session_backup.h"
22 #include "b_session_restore.h"
23 #include "message_parcel.h"
24 #include "refbase.h"
25 #include "service.h"
26 #include "service_reverse.h"
27 
28 namespace OHOS {
29 using namespace FileManagement::Backup;
30 
31 constexpr int32_t SERVICE_ID = 5203;
32 
33 template<class T>
TypeCast(const uint8_t * data,int * pos=nullptr)34 T TypeCast(const uint8_t *data, int *pos = nullptr)
35 {
36     if (pos) {
37         *pos += sizeof(T);
38     }
39     return *(reinterpret_cast<const T*>(data));
40 }
41 
42 template <typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,Parcel & data)43 void WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data)
44 {
45     if (!data.WriteUint32(parcelableVector.size())) {
46         return;
47     }
48 
49     for (const auto &parcelable : parcelableVector) {
50         if (!data.WriteParcelable(&parcelable)) {
51             return;
52         }
53     }
54 
55     return;
56 }
57 
OnRemoteRequestFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)58 bool OnRemoteRequestFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
59 {
60     if (data == nullptr || size < sizeof(uint32_t)) {
61         return true;
62     }
63 
64     MessageParcel msg;
65     MessageParcel reply;
66     MessageOption option;
67 
68     int pos = 0;
69     uint32_t code = TypeCast<uint32_t>(data, &pos);
70     msg.WriteInterfaceToken(ServiceStub::GetDescriptor());
71 
72     service->OnRemoteRequest(code, msg, reply, option);
73     return true;
74 }
75 
CmdInitRestoreSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)76 bool CmdInitRestoreSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
77 {
78     MessageParcel msg;
79     MessageParcel reply;
80 
81     try {
82         BSessionRestore::Callbacks callbacks;
83         msg.WriteRemoteObject(new ServiceReverse(callbacks));
84         msg.WriteBuffer(data, size);
85         service->CmdInitRestoreSession(msg, reply);
86     } catch (OHOS::FileManagement::Backup::BError &err) {
87         // Only filter BError errors, Other results are not expected.
88     }
89     return true;
90 }
91 
CmdInitBackupSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)92 bool CmdInitBackupSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
93 {
94     MessageParcel msg;
95     MessageParcel reply;
96 
97     try {
98         BSessionBackup::Callbacks callbacks;
99         msg.WriteRemoteObject(new ServiceReverse(callbacks));
100         msg.WriteBuffer(data, size);
101         service->CmdInitBackupSession(msg, reply);
102     } catch (OHOS::FileManagement::Backup::BError &err) {
103         // Only filter BError errors, Other results are not expected.
104     }
105     return true;
106 }
107 
CmdStartFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)108 bool CmdStartFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
109 {
110     MessageParcel msg;
111     MessageParcel reply;
112 
113     try {
114         msg.WriteBuffer(data, size);
115         service->CmdStart(msg, reply);
116     } catch (OHOS::FileManagement::Backup::BError &err) {
117         // Only filter BError errors, Other results are not expected.
118     }
119     return true;
120 }
121 
CmdGetLocalCapabilitiesFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)122 bool CmdGetLocalCapabilitiesFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
123 {
124     MessageParcel msg;
125     MessageParcel reply;
126 
127     try {
128         msg.WriteBuffer(data, size);
129         service->CmdGetLocalCapabilities(msg, reply);
130     } catch (OHOS::FileManagement::Backup::BError &err) {
131         // Only filter BError errors, Other results are not expected.
132     }
133     return true;
134 }
135 
CmdPublishFileFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)136 bool CmdPublishFileFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
137 {
138     if (data == nullptr || size < sizeof(TmpFileSN)) {
139         return true;
140     }
141 
142     MessageParcel msg;
143     MessageParcel reply;
144 
145     try {
146         int pos = 0;
147         BFileInfo info;
148         info.sn = TypeCast<TmpFileSN>(data, &pos);
149         int len = (size - pos) >> 1;
150         info.owner = string(reinterpret_cast<const char*>(data + pos), len);
151         info.fileName = string(reinterpret_cast<const char*>(data + pos + len), len);
152         msg.WriteParcelable(&info);
153         service->CmdPublishFile(msg, reply);
154     } catch (OHOS::FileManagement::Backup::BError &err) {
155         // Only filter BError errors, Other results are not expected.
156     }
157     return true;
158 }
159 
CmdAppFileReadyFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)160 bool CmdAppFileReadyFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
161 {
162     if (data == nullptr || size < sizeof(int32_t) + sizeof(bool) + sizeof(int)) {
163         return true;
164     }
165 
166     MessageParcel msg;
167     MessageParcel reply;
168 
169     try {
170         int pos = 0;
171         int fd = TypeCast<int>(data, &pos);
172         int32_t errCode = TypeCast<int32_t>(data + pos, &pos);
173         bool fdFlag = TypeCast<bool>(data + pos, &pos);
174         msg.WriteString(string(reinterpret_cast<const char*>(data + pos), size - pos));
175         msg.WriteBool(fdFlag);
176         if (fdFlag) {
177             msg.WriteFileDescriptor(fd);
178         }
179         msg.WriteInt32(errCode);
180         service->CmdAppFileReady(msg, reply);
181     } catch (OHOS::FileManagement::Backup::BError &err) {
182         // Only filter BError errors, Other results are not expected.
183     }
184     return true;
185 }
186 
CmdAppDoneFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)187 bool CmdAppDoneFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
188 {
189     if (data == nullptr || size < sizeof(int32_t)) {
190         return true;
191     }
192 
193     MessageParcel msg;
194     MessageParcel reply;
195 
196     try {
197         int32_t errCode = TypeCast<int32_t>(data);
198         msg.WriteInt32(errCode);
199         service->CmdAppDone(msg, reply);
200     } catch (OHOS::FileManagement::Backup::BError &err) {
201         // Only filter BError errors, Other results are not expected.
202     }
203     return true;
204 }
205 
CmdResultReportFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)206 bool CmdResultReportFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
207 {
208     if (data == nullptr || size < sizeof(int32_t) + sizeof(int32_t)) {
209         return true;
210     }
211 
212     MessageParcel msg;
213     MessageParcel reply;
214 
215     try {
216         int pos = 0;
217         int32_t scenario = TypeCast<int32_t>(data, &pos);
218         int32_t errCode = TypeCast<int32_t>(data + pos, &pos);
219         msg.WriteString(string(reinterpret_cast<const char*>(data + pos), size - pos));
220         msg.WriteInt32(scenario);
221         msg.WriteInt32(errCode);
222         service->CmdResultReport(msg, reply);
223     } catch (OHOS::FileManagement::Backup::BError &err) {
224         // Only filter BError errors, Other results are not expected.
225     }
226     return true;
227 }
228 
CmdGetFileHandleFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)229 bool CmdGetFileHandleFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
230 {
231     MessageParcel msg;
232     MessageParcel reply;
233 
234     try {
235         int len = size >> 1;
236         msg.WriteString(string(reinterpret_cast<const char*>(data), len));
237         msg.WriteString(string(reinterpret_cast<const char*>(data + len), size - len));
238         service->CmdGetFileHandle(msg, reply);
239     } catch (OHOS::FileManagement::Backup::BError &err) {
240         // Only filter BError errors, Other results are not expected.
241     }
242     return true;
243 }
244 
CmdAppendBundlesRestoreSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)245 bool CmdAppendBundlesRestoreSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
246 {
247     if (data == nullptr || size < sizeof(int) + sizeof(int32_t) + sizeof(int32_t)) {
248         return true;
249     }
250 
251     MessageParcel msg;
252     MessageParcel reply;
253 
254     try {
255         int pos = 0;
256         int fd = TypeCast<int>(data, &pos);
257         int32_t type = TypeCast<int32_t>(data + pos, &pos);
258         int32_t userId = TypeCast<int32_t>(data + pos, &pos);
259         vector<string> bundleNames;
260         bundleNames.emplace_back(string(reinterpret_cast<const char*>(data + pos), size - pos));
261         msg.WriteFileDescriptor(fd);
262         msg.WriteStringVector(bundleNames);
263         msg.WriteInt32(type);
264         msg.WriteInt32(userId);
265         service->CmdAppendBundlesRestoreSession(msg, reply);
266     } catch (OHOS::FileManagement::Backup::BError &err) {
267         // Only filter BError errors, Other results are not expected.
268     }
269     return true;
270 }
271 
CmdAppendBundlesDetailsRestoreSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)272 bool CmdAppendBundlesDetailsRestoreSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
273 {
274     if (data == nullptr || size < sizeof(int) + sizeof(int32_t) + sizeof(int32_t)) {
275         return true;
276     }
277 
278     MessageParcel msg;
279     MessageParcel reply;
280 
281     try {
282         int pos = 0;
283         int fd = TypeCast<int>(data, &pos);
284         int32_t type = TypeCast<int32_t>(data + pos, &pos);
285         int32_t userId = TypeCast<int32_t>(data + pos, &pos);
286         int len = (size - pos) >> 1;
287         vector<string> bundleNames;
288         bundleNames.emplace_back(string(reinterpret_cast<const char*>(data + pos), len));
289         vector<string> detailInfos;
290         detailInfos.emplace_back(string(reinterpret_cast<const char*>(data + pos + len), len));
291         msg.WriteFileDescriptor(fd);
292         msg.WriteStringVector(bundleNames);
293         msg.WriteStringVector(detailInfos);
294         msg.WriteInt32(type);
295         msg.WriteInt32(userId);
296         service->CmdAppendBundlesDetailsRestoreSession(msg, reply);
297     } catch (OHOS::FileManagement::Backup::BError &err) {
298         // Only filter BError errors, Other results are not expected.
299     }
300     return true;
301 }
302 
CmdAppendBundlesBackupSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)303 bool CmdAppendBundlesBackupSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
304 {
305     MessageParcel msg;
306     MessageParcel reply;
307 
308     try {
309         vector<string> bundleNames;
310         bundleNames.emplace_back(string(reinterpret_cast<const char*>(data), size));
311         msg.WriteStringVector(bundleNames);
312         service->CmdAppendBundlesBackupSession(msg, reply);
313     } catch (OHOS::FileManagement::Backup::BError &err) {
314         // Only filter BError errors, Other results are not expected.
315     }
316     return true;
317 }
318 
CmdAppendBundlesDetailsBackupSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)319 bool CmdAppendBundlesDetailsBackupSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
320 {
321     MessageParcel msg;
322     MessageParcel reply;
323 
324     try {
325         int len = size >> 1;
326         vector<string> bundleNames;
327         bundleNames.emplace_back(string(reinterpret_cast<const char*>(data), len));
328         vector<string> detailInfos;
329         detailInfos.emplace_back(string(reinterpret_cast<const char*>(data + len), len));
330         msg.WriteStringVector(bundleNames);
331         msg.WriteStringVector(detailInfos);
332         service->CmdAppendBundlesDetailsBackupSession(msg, reply);
333     } catch (OHOS::FileManagement::Backup::BError &err) {
334         // Only filter BError errors, Other results are not expected.
335     }
336     return true;
337 }
338 
CmdFinishFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)339 bool CmdFinishFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
340 {
341     MessageParcel msg;
342     MessageParcel reply;
343     try {
344         msg.WriteBuffer(data, size);
345         service->CmdFinish(msg, reply);
346     } catch (OHOS::FileManagement::Backup::BError &err) {
347         // Only filter BError errors, Other results are not expected.
348     }
349     return true;
350 }
351 
CmdReleaseFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)352 [[maybe_unused]] bool CmdReleaseFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
353 {
354     MessageParcel msg;
355     MessageParcel reply;
356     try {
357         msg.WriteBuffer(data, size);
358         service->CmdRelease(msg, reply);
359     } catch (OHOS::FileManagement::Backup::BError &err) {
360         // Only filter BError errors, Other results are not expected.
361     }
362     return true;
363 }
364 
CmdGetLocalCapabilitiesIncrementalFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)365 bool CmdGetLocalCapabilitiesIncrementalFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
366 {
367     MessageParcel msg;
368     MessageParcel reply;
369 
370     try {
371         vector<BIncrementalData> bundleNames;
372         bundleNames.emplace_back(string(reinterpret_cast<const char*>(data), size), 0);
373         WriteParcelableVector(bundleNames, msg);
374         service->CmdGetLocalCapabilitiesIncremental(msg, reply);
375     } catch (OHOS::FileManagement::Backup::BError &err) {
376         // Only filter BError errors, Other results are not expected.
377     }
378     return true;
379 }
380 
CmdGetAppLocalListAndDoIncrementalBackupFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)381 bool CmdGetAppLocalListAndDoIncrementalBackupFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
382 {
383     MessageParcel msg;
384     MessageParcel reply;
385     try {
386         msg.WriteBuffer(data, size);
387         service->CmdGetAppLocalListAndDoIncrementalBackup(msg, reply);
388     } catch (OHOS::FileManagement::Backup::BError &err) {
389         // Only filter BError errors, Other results are not expected.
390     }
391     return true;
392 }
393 
CmdInitIncrementalBackupSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)394 bool CmdInitIncrementalBackupSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
395 {
396     MessageParcel msg;
397     MessageParcel reply;
398 
399     try {
400         BIncrementalBackupSession::Callbacks callbacks;
401         msg.WriteRemoteObject(new ServiceReverse(callbacks));
402         msg.WriteBuffer(data, size);
403         service->CmdInitIncrementalBackupSession(msg, reply);
404     } catch (OHOS::FileManagement::Backup::BError &err) {
405         // Only filter BError errors, Other results are not expected.
406     }
407     return true;
408 }
409 
CmdAppendBundlesIncrementalBackupSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)410 bool CmdAppendBundlesIncrementalBackupSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
411 {
412     MessageParcel msg;
413     MessageParcel reply;
414 
415     try {
416         vector<BIncrementalData> bundlesToBackup;
417         bundlesToBackup.emplace_back(string(reinterpret_cast<const char*>(data), size), 0);
418         WriteParcelableVector(bundlesToBackup, msg);
419         service->CmdAppendBundlesIncrementalBackupSession(msg, reply);
420     } catch (OHOS::FileManagement::Backup::BError &err) {
421         // Only filter BError errors, Other results are not expected.
422     }
423     return true;
424 }
425 
CmdAppendBundlesDetailsIncrementalBackupSessionFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)426 bool CmdAppendBundlesDetailsIncrementalBackupSessionFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
427 {
428     MessageParcel msg;
429     MessageParcel reply;
430 
431     try {
432         int len = size >> 2;
433         vector<BIncrementalData> bundlesToBackup;
434         bundlesToBackup.emplace_back(string(reinterpret_cast<const char*>(data), len), 0);
435         std::vector<std::string> infos;
436         infos.emplace_back(string(reinterpret_cast<const char*>(data + len), len));
437         infos.emplace_back(string(reinterpret_cast<const char*>(data + len + len), len));
438         WriteParcelableVector(bundlesToBackup, msg);
439         msg.WriteStringVector(infos);
440         service->CmdAppendBundlesDetailsIncrementalBackupSession(msg, reply);
441     } catch (OHOS::FileManagement::Backup::BError &err) {
442         // Only filter BError errors, Other results are not expected.
443     }
444     return true;
445 }
446 
CmdPublishIncrementalFileFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)447 bool CmdPublishIncrementalFileFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
448 {
449     if (data == nullptr || size < sizeof(TmpFileSN)) {
450         return true;
451     }
452 
453     MessageParcel msg;
454     MessageParcel reply;
455 
456     try {
457         int pos = 0;
458         BFileInfo info;
459         info.sn = TypeCast<TmpFileSN>(data, &pos);
460         int len = (size - pos) >> 1;
461         info.owner = string(reinterpret_cast<const char*>(data + pos), len);
462         info.fileName = string(reinterpret_cast<const char*>(data + pos + len), len);
463         msg.WriteParcelable(&info);
464         service->CmdPublishIncrementalFile(msg, reply);
465     } catch (OHOS::FileManagement::Backup::BError &err) {
466         // Only filter BError errors, Other results are not expected.
467     }
468     return true;
469 }
470 
CmdPublishSAIncrementalFileFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)471 [[maybe_unused]] bool CmdPublishSAIncrementalFileFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
472 {
473     if (data == nullptr || size < sizeof(int) + sizeof(TmpFileSN)) {
474         return true;
475     }
476 
477     MessageParcel msg;
478     MessageParcel reply;
479 
480     try {
481         int pos = 0;
482         BFileInfo info;
483         int fd = TypeCast<int>(data, &pos);
484         info.sn = TypeCast<TmpFileSN>(data, &pos);
485         int len = (size - pos) >> 1;
486         info.owner = string(reinterpret_cast<const char*>(data + pos), len);
487         info.fileName = string(reinterpret_cast<const char*>(data + pos + len), len);
488         msg.WriteParcelable(&info);
489         msg.WriteFileDescriptor(fd);
490         service->CmdPublishSAIncrementalFile(msg, reply);
491     } catch (OHOS::FileManagement::Backup::BError &err) {
492         // Only filter BError errors, Other results are not expected.
493     }
494     return true;
495 }
496 
CmdAppIncrementalFileReadyFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)497 bool CmdAppIncrementalFileReadyFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
498 {
499     if (data == nullptr || size < sizeof(int32_t) + sizeof(bool) + sizeof(int) + sizeof(int)) {
500         return true;
501     }
502 
503     MessageParcel msg;
504     MessageParcel reply;
505 
506     try {
507         int pos = 0;
508         int fd1 = TypeCast<int>(data, &pos);
509         int fd2 = TypeCast<int>(data + pos, &pos);
510         int32_t errCode = TypeCast<int32_t>(data + pos, &pos);
511         bool fdFlag = TypeCast<bool>(data + pos, &pos);
512         msg.WriteString(string(reinterpret_cast<const char*>(data + pos), size - pos));
513         msg.WriteBool(fdFlag);
514         if (fdFlag) {
515             msg.WriteFileDescriptor(fd1);
516             msg.WriteFileDescriptor(fd2);
517         }
518         msg.WriteInt32(errCode);
519         service->CmdAppIncrementalFileReady(msg, reply);
520     } catch (OHOS::FileManagement::Backup::BError &err) {
521         // Only filter BError errors, Other results are not expected.
522     }
523     return true;
524 }
525 
CmdAppIncrementalDoneFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)526 bool CmdAppIncrementalDoneFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
527 {
528     if (data == nullptr || size < sizeof(int32_t)) {
529         return true;
530     }
531 
532     MessageParcel msg;
533     MessageParcel reply;
534 
535     try {
536         int32_t errCode = TypeCast<int32_t>(data);
537         msg.WriteInt32(errCode);
538         service->CmdAppIncrementalDone(msg, reply);
539     } catch (OHOS::FileManagement::Backup::BError &err) {
540         // Only filter BError errors, Other results are not expected.
541     }
542     return true;
543 }
544 
CmdGetIncrementalFileHandleFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)545 bool CmdGetIncrementalFileHandleFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
546 {
547     MessageParcel msg;
548     MessageParcel reply;
549 
550     try {
551         int len = size >> 1;
552         msg.WriteString(string(reinterpret_cast<const char*>(data), len));
553         msg.WriteString(string(reinterpret_cast<const char*>(data + len), size - len));
554         service->CmdGetIncrementalFileHandle(msg, reply);
555     } catch (OHOS::FileManagement::Backup::BError &err) {
556         // Only filter BError errors, Other results are not expected.
557     }
558     return true;
559 }
560 
CmdGetBackupInfoFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)561 bool CmdGetBackupInfoFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
562 {
563     MessageParcel msg;
564     MessageParcel reply;
565 
566     try {
567         msg.WriteString(string(reinterpret_cast<const char*>(data), size));
568         service->CmdGetBackupInfo(msg, reply);
569     } catch (OHOS::FileManagement::Backup::BError &err) {
570         // Only filter BError errors, Other results are not expected.
571     }
572     return true;
573 }
574 
CmdUpdateTimerFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)575 bool CmdUpdateTimerFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
576 {
577     if (data == nullptr || size < sizeof(int32_t)) {
578         return true;
579     }
580 
581     MessageParcel msg;
582     MessageParcel reply;
583 
584     try {
585         int pos = 0;
586         int32_t timeout = TypeCast<int32_t>(data, &pos);
587         msg.WriteString(string(reinterpret_cast<const char*>(data + pos), size - pos));
588         msg.WriteInt32(timeout);
589         service->CmdUpdateTimer(msg, reply);
590     } catch (OHOS::FileManagement::Backup::BError &err) {
591         // Only filter BError errors, Other results are not expected.
592     }
593     return true;
594 }
595 
CmdUpdateSendRateFuzzTest(sptr<Service> service,const uint8_t * data,size_t size)596 bool CmdUpdateSendRateFuzzTest(sptr<Service> service, const uint8_t *data, size_t size)
597 {
598     if (data == nullptr || size < sizeof(int32_t)) {
599         return true;
600     }
601 
602     MessageParcel msg;
603     MessageParcel reply;
604 
605     try {
606         int pos = 0;
607         int32_t sendRate = TypeCast<int32_t>(data, &pos);
608         msg.WriteString(string(reinterpret_cast<const char*>(data + pos), size - pos));
609         msg.WriteInt32(sendRate);
610         service->CmdUpdateSendRate(msg, reply);
611     } catch (OHOS::FileManagement::Backup::BError &err) {
612         // Only filter BError errors, Other results are not expected.
613     }
614     return true;
615 }
616 } // namespace OHOS
617 
618 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)619 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
620 {
621     OHOS::sptr service(new OHOS::FileManagement::Backup::Service(OHOS::SERVICE_ID));
622     if (service == nullptr) {
623         return 0;
624     }
625 
626     try {
627         OHOS::OnRemoteRequestFuzzTest(service, data, size);
628         CmdInitRestoreSessionFuzzTest(service, data, size);
629         CmdInitBackupSessionFuzzTest(service, data, size);
630         CmdStartFuzzTest(service, data, size);
631         CmdGetLocalCapabilitiesFuzzTest(service, data, size);
632         CmdPublishFileFuzzTest(service, data, size);
633         CmdAppFileReadyFuzzTest(service, data, size);
634         CmdAppDoneFuzzTest(service, data, size);
635         CmdResultReportFuzzTest(service, data, size);
636         CmdGetFileHandleFuzzTest(service, data, size);
637         CmdAppendBundlesRestoreSessionFuzzTest(service, data, size);
638         CmdAppendBundlesDetailsRestoreSessionFuzzTest(service, data, size);
639         CmdAppendBundlesBackupSessionFuzzTest(service, data, size);
640         CmdAppendBundlesDetailsBackupSessionFuzzTest(service, data, size);
641         CmdFinishFuzzTest(service, data, size);
642         CmdGetLocalCapabilitiesIncrementalFuzzTest(service, data, size);
643         CmdGetAppLocalListAndDoIncrementalBackupFuzzTest(service, data, size);
644         CmdInitIncrementalBackupSessionFuzzTest(service, data, size);
645         CmdAppendBundlesIncrementalBackupSessionFuzzTest(service, data, size);
646         CmdAppendBundlesDetailsIncrementalBackupSessionFuzzTest(service, data, size);
647         CmdPublishIncrementalFileFuzzTest(service, data, size);
648         CmdAppIncrementalFileReadyFuzzTest(service, data, size);
649         CmdAppIncrementalDoneFuzzTest(service, data, size);
650         CmdGetIncrementalFileHandleFuzzTest(service, data, size);
651         CmdGetBackupInfoFuzzTest(service, data, size);
652         CmdUpdateTimerFuzzTest(service, data, size);
653         CmdUpdateSendRateFuzzTest(service, data, size);
654     } catch (OHOS::FileManagement::Backup::BError &err) {
655         // Only filter BError errors, Other results are not expected.
656     }
657     return 0;
658 }