• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "udmf_client_fuzzer.h"
17 
18 #include <unistd.h>
19 #include <fuzzer/FuzzedDataProvider.h>
20 
21 #include "accesstoken_kit.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24 
25 #include "udmf_client.h"
26 #include "text.h"
27 #include "plain_text.h"
28 #include "html.h"
29 #include "link.h"
30 #include "file.h"
31 #include "image.h"
32 #include "video.h"
33 #include "system_defined_record.h"
34 #include "system_defined_form.h"
35 #include "system_defined_appitem.h"
36 #include "system_defined_pixelmap.h"
37 #include "udmf_async_client.h"
38 #include "udmf_executor.h"
39 #include "unified_types.h"
40 
41 using namespace OHOS;
42 using namespace OHOS::Security::AccessToken;
43 using namespace OHOS::UDMF;
44 
45 namespace OHOS {
46 static constexpr int END_INTERVAL = 3;
47 
AllocHapToken()48 void AllocHapToken()
49 {
50     HapInfoParams info = {
51         .userID = 100,
52         .bundleName = "ohos.test.demo",
53         .instIndex = 0,
54         .appIDDesc = "ohos.test.demo"
55     };
56 
57     HapPolicyParams policy = {
58         .apl = APL_NORMAL,
59         .domain = "test.domain",
60         .permList = {
61             {
62                 .permissionName = "ohos.permission.test",
63                 .bundleName = "ohos.test.demo",
64                 .grantMode = 1,
65                 .availableLevel = APL_NORMAL,
66                 .label = "label",
67                 .labelId = 1,
68                 .description = "test",
69                 .descriptionId = 1
70             }
71         },
72         .permStateList = {
73             {
74                 .permissionName = "ohos.permission.test",
75                 .isGeneral = true,
76                 .resDeviceID = {"local"},
77                 .grantStatus = {PermissionState::PERMISSION_GRANTED},
78                 .grantFlags = {1}
79             }
80         }
81     };
82     auto tokenID = AccessTokenKit::AllocHapToken(info, policy);
83     SetSelfTokenID(tokenID.tokenIDEx);
84 }
85 
SetUpTestCase()86 void SetUpTestCase()
87 {
88     AllocHapToken();
89 }
90 
TearDown()91 void TearDown()
92 {
93     std::this_thread::sleep_for(std::chrono::seconds(END_INTERVAL));
94 }
95 
SetNativeToken()96 void SetNativeToken()
97 {
98     NativeTokenInfoParams infoInstance = {
99         .dcapsNum = 0,
100         .permsNum = 0,
101         .aclsNum = 0,
102         .dcaps = nullptr,
103         .perms = nullptr,
104         .acls = nullptr,
105         .processName = "msdp_sa",
106         .aplStr = "system_core",
107     };
108     auto tokenId = GetAccessTokenId(&infoInstance);
109     SetSelfTokenID(tokenId);
110     AccessTokenKit::ReloadNativeTokenInfo();
111 }
112 
SetHapToken()113 void SetHapToken()
114 {
115     auto tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.test.demo", 0);
116     SetSelfTokenID(tokenId);
117 }
118 
SetDataTextFuzz(FuzzedDataProvider & provider)119 void SetDataTextFuzz(FuzzedDataProvider &provider)
120 {
121     std::string skey = provider.ConsumeRandomLengthString();
122     std::string svalue = provider.ConsumeRandomLengthString();
123     UnifiedData unifiedData;
124     CustomOption option = {.intention = Intention::UD_INTENTION_BUTT};
125     auto text = std::make_shared<Text>();
126     UDDetails details;
127     details.insert({skey, svalue});
128     text->SetDetails(details);
129     unifiedData.AddRecord(text);
130     std::string key;
131     UdmfClient::GetInstance().SetData(option, unifiedData, key);
132 
133     SetNativeToken();
134 
135     QueryOption option2 = {.key = key};
136     Privilege privilege;
137     privilege.readPermission = "readPermission";
138     privilege.writePermission = "writePermission";
139     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
140 
141     SetHapToken();
142 
143     UnifiedData data2;
144     UdmfClient::GetInstance().GetData(option2, data2);
145 }
146 
SetDataPlainTextFuzz(FuzzedDataProvider & provider)147 void SetDataPlainTextFuzz(FuzzedDataProvider &provider)
148 {
149     std::string skey = provider.ConsumeRandomLengthString();
150     std::string svalue = provider.ConsumeRandomLengthString();
151     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
152     UnifiedData data1;
153     UDDetails details1;
154     auto plainText1 = std::make_shared<PlainText>();
155     details1.insert({skey, svalue});
156     plainText1->SetDetails(details1);
157     plainText1->SetContent(svalue + "content");
158     plainText1->SetAbstract(svalue + "abstract");
159     plainText1->GetContent();
160     plainText1->GetAbstract();
161     data1.AddRecord(plainText1);
162     std::string key;
163     UdmfClient::GetInstance().SetData(option1, data1, key);
164 
165     SetNativeToken();
166 
167     QueryOption option2 = {.key = key};
168     Privilege privilege;
169     privilege.readPermission = "readPermission";
170     privilege.writePermission = "writePermission";
171     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
172 
173     SetHapToken();
174 
175     UnifiedData data2;
176     UdmfClient::GetInstance().GetData(option2, data2);
177 }
178 
SetDataHtmlFuzz(FuzzedDataProvider & provider)179 void SetDataHtmlFuzz(FuzzedDataProvider &provider)
180 {
181     std::string skey = provider.ConsumeRandomLengthString();
182     std::string svalue = provider.ConsumeRandomLengthString();
183     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
184     UnifiedData data1;
185     std::string key;
186     auto html1 = std::make_shared<Html>();
187     UDDetails details1;
188     details1.insert({skey, svalue});
189     html1->SetDetails(details1);
190     html1->SetHtmlContent(svalue + "htmlcontent");
191     html1->SetPlainContent(svalue + "plainContent");
192     html1->GetHtmlContent();
193     html1->GetPlainContent();
194     data1.AddRecord(html1);
195     UdmfClient::GetInstance().SetData(option1, data1, key);
196 
197     SetNativeToken();
198 
199     QueryOption option2 = {.key = key};
200     Privilege privilege;
201     privilege.readPermission = "readPermission";
202     privilege.writePermission = "writePermission";
203     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
204 
205     SetHapToken();
206 
207     UnifiedData data2;
208     UdmfClient::GetInstance().GetData(option2, data2);
209 }
210 
SetDataLinkFuzz(FuzzedDataProvider & provider)211 void SetDataLinkFuzz(FuzzedDataProvider &provider)
212 {
213     std::string skey = provider.ConsumeRandomLengthString();
214     std::string svalue = provider.ConsumeRandomLengthString();
215     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
216     UnifiedData data1;
217     std::string key;
218     auto link1 = std::make_shared<Link>();
219     UDDetails details1;
220     details1.insert({skey, svalue});
221     link1->SetDetails(details1);
222     link1->SetUrl(svalue + "url");
223     link1->SetDescription(svalue + "description");
224     link1->GetUrl();
225     link1->GetDescription();
226     data1.AddRecord(link1);
227     UdmfClient::GetInstance().SetData(option1, data1, key);
228 
229     SetNativeToken();
230 
231     QueryOption option2 = {.key = key};
232     Privilege privilege;
233     privilege.readPermission = "readPermission";
234     privilege.writePermission = "writePermission";
235     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
236 
237     SetHapToken();
238 
239     UnifiedData data2;
240     UdmfClient::GetInstance().GetData(option2, data2);
241 }
242 
SetDataFileFuzz(FuzzedDataProvider & provider)243 void SetDataFileFuzz(FuzzedDataProvider &provider)
244 {
245     std::string svalue = provider.ConsumeRandomLengthString();
246     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
247     UnifiedData data1;
248     std::string key;
249     auto file1 = std::make_shared<File>();
250     file1->SetUri(svalue + "uri");
251     file1->SetRemoteUri(svalue + "remoteUri");
252     file1->GetUri();
253     file1->GetRemoteUri();
254     file1->GetSize();
255     data1.AddRecord(file1);
256     UdmfClient::GetInstance().SetData(option1, data1, key);
257 
258     SetNativeToken();
259 
260     QueryOption option2 = {.key = key};
261     Privilege privilege;
262     privilege.readPermission = "readPermission";
263     privilege.writePermission = "writePermission";
264     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
265 
266     SetHapToken();
267 
268     UnifiedData data2;
269     UdmfClient::GetInstance().GetData(option2, data2);
270 }
271 
SetDataImageFuzz(FuzzedDataProvider & provider)272 void SetDataImageFuzz(FuzzedDataProvider &provider)
273 {
274     std::string svalue = provider.ConsumeRandomLengthString();
275     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
276     UnifiedData data1;
277     std::string key;
278     auto image1 = std::make_shared<Image>();
279     image1->SetUri(svalue + "uri");
280     image1->SetRemoteUri(svalue + "remoteUri");
281     data1.AddRecord(image1);
282     UdmfClient::GetInstance().SetData(option1, data1, key);
283 
284     SetNativeToken();
285 
286     QueryOption option2 = {.key = key};
287     Privilege privilege;
288     privilege.readPermission = "readPermission";
289     privilege.writePermission = "writePermission";
290     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
291 
292     SetHapToken();
293 
294     UnifiedData data2;
295     UdmfClient::GetInstance().GetData(option2, data2);
296 }
297 
SetDataVideoFuzz(FuzzedDataProvider & provider)298 void SetDataVideoFuzz(FuzzedDataProvider &provider)
299 {
300     std::string svalue = provider.ConsumeRandomLengthString();
301     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
302     UnifiedData data1;
303     std::string key;
304     auto video1 = std::make_shared<Video>();
305     video1->SetUri(svalue + "uri");
306     video1->SetRemoteUri(svalue + "remoteUri");
307     data1.AddRecord(video1);
308     UdmfClient::GetInstance().SetData(option1, data1, key);
309 
310     SetNativeToken();
311 
312     QueryOption option2 = {.key = key};
313     Privilege privilege;
314     privilege.readPermission = "readPermission";
315     privilege.writePermission = "writePermission";
316     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
317 
318     SetHapToken();
319 
320     UnifiedData data2;
321     UdmfClient::GetInstance().GetData(option2, data2);
322 }
323 
SetDataSystemDefinedRecordFuzz(FuzzedDataProvider & provider)324 void SetDataSystemDefinedRecordFuzz(FuzzedDataProvider &provider)
325 {
326     std::string skey = provider.ConsumeRandomLengthString();
327     std::string svalue = provider.ConsumeRandomLengthString();
328     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
329     UnifiedData data1;
330     std::string key;
331     auto systemDefinedRecord1 = std::make_shared<SystemDefinedRecord>();
332     UDDetails details1;
333     details1.insert({skey, svalue});
334     systemDefinedRecord1->SetDetails(details1);
335     data1.AddRecord(systemDefinedRecord1);
336     UdmfClient::GetInstance().SetData(option1, data1, key);
337 
338     SetNativeToken();
339 
340     QueryOption option2 = {.key = key};
341     Privilege privilege;
342     privilege.readPermission = "readPermission";
343     privilege.writePermission = "writePermission";
344     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
345 
346     SetHapToken();
347 
348     UnifiedData data2;
349     UdmfClient::GetInstance().GetData(option2, data2);
350 }
351 
SetDataSystemDefinedFormFuzz(FuzzedDataProvider & provider)352 void SetDataSystemDefinedFormFuzz(FuzzedDataProvider &provider)
353 {
354     std::string skey = provider.ConsumeRandomLengthString();
355     std::string svalue = provider.ConsumeRandomLengthString();
356     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
357     UnifiedData data1;
358     std::string key;
359     auto systemDefinedForm1 = std::make_shared<SystemDefinedForm>();
360     UDDetails details1;
361     details1.insert({skey, svalue});
362     systemDefinedForm1->SetDetails(details1);
363     auto formId = 123;
364     systemDefinedForm1->SetFormId(formId);
365     systemDefinedForm1->SetFormName(svalue + "formName");
366     systemDefinedForm1->SetModule(svalue + "module");
367     systemDefinedForm1->SetAbilityName(svalue + "abilityName");
368     systemDefinedForm1->SetBundleName(svalue + "bundleName");
369     systemDefinedForm1->GetFormId();
370     systemDefinedForm1->GetFormName();
371     systemDefinedForm1->GetBundleName();
372     systemDefinedForm1->GetAbilityName();
373     systemDefinedForm1->GetModule();
374     data1.AddRecord(systemDefinedForm1);
375     UdmfClient::GetInstance().SetData(option1, data1, key);
376 
377     SetNativeToken();
378 
379     QueryOption option2 = {.key = key};
380     Privilege privilege;
381     privilege.readPermission = "readPermission";
382     privilege.writePermission = "writePermission";
383     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
384 
385     SetHapToken();
386 
387     UnifiedData data2;
388     UdmfClient::GetInstance().GetData(option2, data2);
389 }
390 
SetDataSystemDefinedAppItemFuzz(FuzzedDataProvider & provider)391 void SetDataSystemDefinedAppItemFuzz(FuzzedDataProvider &provider)
392 {
393     std::string skey = provider.ConsumeRandomLengthString();
394     std::string svalue = provider.ConsumeRandomLengthString();
395     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
396     UnifiedData data1;
397     std::string key;
398     auto systemDefinedAppItem1 = std::make_shared<SystemDefinedAppItem>();
399     UDDetails details1;
400     details1.insert({skey, svalue});
401     systemDefinedAppItem1->SetDetails(details1);
402     systemDefinedAppItem1->SetAppId(svalue + "appId");
403     systemDefinedAppItem1->SetAppName(svalue + "appName");
404     systemDefinedAppItem1->SetAppIconId(svalue + "appIconId");
405     systemDefinedAppItem1->SetAppLabelId(svalue + "appLabelId");
406     systemDefinedAppItem1->SetBundleName(svalue + "bundleName");
407     systemDefinedAppItem1->SetAbilityName(svalue + "abilityName");
408     systemDefinedAppItem1->GetAppId();
409     systemDefinedAppItem1->GetAppName();
410     systemDefinedAppItem1->GetBundleName();
411     systemDefinedAppItem1->GetAbilityName();
412     systemDefinedAppItem1->GetAppIconId();
413     systemDefinedAppItem1->GetAppLabelId();
414     data1.AddRecord(systemDefinedAppItem1);
415     UdmfClient::GetInstance().SetData(option1, data1, key);
416 
417     SetNativeToken();
418 
419     QueryOption option2 = {.key = key};
420     Privilege privilege;
421     privilege.readPermission = "readPermission";
422     privilege.writePermission = "writePermission";
423     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
424 
425     SetHapToken();
426 
427     UnifiedData data2;
428     UdmfClient::GetInstance().GetData(option2, data2);
429 }
430 
SetDataSystemDefinedPixelMapFuzz(FuzzedDataProvider & provider)431 void SetDataSystemDefinedPixelMapFuzz(FuzzedDataProvider &provider)
432 {
433     std::string skey = provider.ConsumeRandomLengthString();
434     std::string svalue = provider.ConsumeRandomLengthString();
435     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
436     UnifiedData data1;
437     std::string key;
438     auto systemDefinedPixelMap1 = std::make_shared<SystemDefinedPixelMap>();
439     UDDetails details1;
440     details1.insert({skey, svalue});
441     systemDefinedPixelMap1->SetDetails(details1);
442     std::vector<uint8_t> rawData1 = {1, 2, 3, 4, 5};
443     systemDefinedPixelMap1->SetRawData(rawData1);
444     data1.AddRecord(systemDefinedPixelMap1);
445     UdmfClient::GetInstance().SetData(option1, data1, key);
446 
447     SetNativeToken();
448 
449     QueryOption option2 = {.key = key};
450     Privilege privilege;
451     privilege.readPermission = "readPermission";
452     privilege.writePermission = "writePermission";
453     UdmfClient::GetInstance().AddPrivilege(option2, privilege);
454 
455     SetHapToken();
456 
457     UnifiedData data2;
458     UdmfClient::GetInstance().GetData(option2, data2);
459 }
460 
GetSummaryFuzz(FuzzedDataProvider & provider)461 void GetSummaryFuzz(FuzzedDataProvider &provider)
462 {
463     std::string skey = provider.ConsumeRandomLengthString();
464     std::string svalue = provider.ConsumeRandomLengthString();
465     CustomOption option1 = {.intention = Intention::UD_INTENTION_DRAG};
466     UnifiedData UData;
467     std::string key;
468 
469     UDDetails details;
470     details.insert({skey, svalue});
471 
472     auto text = std::make_shared<Text>();
473     text->SetDetails(details);
474     UData.AddRecord(text);
475 
476     auto plainText = std::make_shared<PlainText>();
477     plainText->SetDetails(details);
478     plainText->SetContent(svalue + "content");
479     plainText->SetAbstract(svalue + "abstract");
480     UData.AddRecord(plainText);
481 
482     auto file = std::make_shared<File>();
483     file->SetUri(svalue + "uri");
484     file->SetRemoteUri(svalue + "remoteUri");
485     UData.AddRecord(file);
486 
487     auto image = std::make_shared<Image>();
488     image->SetUri(svalue + "uri");
489     image->SetRemoteUri(svalue + "remoteUri");
490     UData.AddRecord(image);
491 
492     auto systemDefinedRecord = std::make_shared<SystemDefinedRecord>();
493     systemDefinedRecord->SetDetails(details);
494     UData.AddRecord(systemDefinedRecord);
495 
496     auto systemDefinedForm = std::make_shared<SystemDefinedForm>();
497     systemDefinedForm->SetDetails(details);
498     auto formId = 123;
499     systemDefinedForm->SetFormId(formId);
500     systemDefinedForm->SetFormName(svalue + "formName");
501     systemDefinedForm->SetModule(svalue + "module");
502     systemDefinedForm->SetAbilityName(svalue + "abilityName");
503     systemDefinedForm->SetBundleName(svalue + "bundleName");
504     UData.AddRecord(systemDefinedForm);
505 
506     UdmfClient::GetInstance().SetData(option1, UData, key);
507 
508     QueryOption option2 = {.key = key};
509     Summary summary;
510     UdmfClient::GetInstance().GetSummary(option2, summary);
511     UnifiedData data2;
512     UdmfClient::GetInstance().GetData(option2, data2);
513 }
514 
GetBatchDataByKeyFuzz(FuzzedDataProvider & provider)515 void GetBatchDataByKeyFuzz(FuzzedDataProvider &provider)
516 {
517     std::string skey = provider.ConsumeRandomLengthString();
518     CustomOption option1 = { .intention = UD_INTENTION_DATA_HUB };
519     UnifiedData data1;
520     std::string key;
521     auto plainText = std::make_shared<PlainText>();
522     data1.AddRecord(plainText);
523     UdmfClient::GetInstance().SetData(option1, data1, key);
524 
525     SetHapToken();
526     QueryOption option2 = { .key = key };
527     std::vector<UnifiedData> dataSet2;
528     UdmfClient::GetInstance().GetBatchData(option2, dataSet2);
529 
530     SetHapToken();
531     QueryOption option3 = { .key = skey };
532     std::vector<UnifiedData> dataSet3;
533     UdmfClient::GetInstance().GetBatchData(option3, dataSet3);
534 }
535 
GetBatchDataByIntentionFuzz(FuzzedDataProvider & provider)536 void GetBatchDataByIntentionFuzz(FuzzedDataProvider &provider)
537 {
538     std::string skey = provider.ConsumeRandomLengthString();
539     CustomOption option1 = { .intention = UD_INTENTION_DATA_HUB };
540     UnifiedData data1;
541     std::string key;
542     auto plainText = std::make_shared<PlainText>();
543     data1.AddRecord(plainText);
544     UdmfClient::GetInstance().SetData(option1, data1, key);
545 
546     SetHapToken();
547     Intention intention = UnifiedDataUtils::GetIntentionByString(skey);
548     QueryOption option2 = { .intention = intention };
549     std::vector<UnifiedData> dataSet;
550     UdmfClient::GetInstance().GetBatchData(option2, dataSet);
551 }
552 
DeleteDataByKeyFuzz(FuzzedDataProvider & provider)553 void DeleteDataByKeyFuzz(FuzzedDataProvider &provider)
554 {
555     std::string skey = provider.ConsumeRandomLengthString();
556     CustomOption option1 = { .intention = UD_INTENTION_DATA_HUB };
557     UnifiedData data1;
558     std::string key;
559     auto plainText = std::make_shared<PlainText>();
560     data1.AddRecord(plainText);
561     UdmfClient::GetInstance().SetData(option1, data1, key);
562 
563     SetHapToken();
564     QueryOption option2 = { .key = key };
565     std::vector<UnifiedData> dataSet2;
566     UdmfClient::GetInstance().DeleteData(option2, dataSet2);
567 
568     SetHapToken();
569     QueryOption option3 = { .key = skey };
570     std::vector<UnifiedData> dataSet3;
571     UdmfClient::GetInstance().DeleteData(option3, dataSet3);
572 }
573 
DeleteDataByIntentionFuzz(FuzzedDataProvider & provider)574 void DeleteDataByIntentionFuzz(FuzzedDataProvider &provider)
575 {
576     std::string skey = provider.ConsumeRandomLengthString();
577     CustomOption option1 = { .intention = UD_INTENTION_DATA_HUB };
578     UnifiedData data1;
579     std::string key;
580     auto plainText = std::make_shared<PlainText>();
581     data1.AddRecord(plainText);
582     UdmfClient::GetInstance().SetData(option1, data1, key);
583 
584     SetHapToken();
585     Intention intention = UnifiedDataUtils::GetIntentionByString(skey);
586     QueryOption option2 = { .intention = intention };
587     std::vector<UnifiedData> dataSet;
588     UdmfClient::GetInstance().DeleteData(option2, dataSet);
589 }
590 
UpdateDataFuzz(FuzzedDataProvider & provider)591 void UpdateDataFuzz(FuzzedDataProvider &provider)
592 {
593     std::string skey = provider.ConsumeRandomLengthString();
594     CustomOption option1 = { .intention = UD_INTENTION_DATA_HUB };
595     UnifiedData data1;
596     std::string key;
597     auto plainText = std::make_shared<PlainText>();
598     data1.AddRecord(plainText);
599     UdmfClient::GetInstance().SetData(option1, data1, key);
600 
601     SetHapToken();
602     UnifiedData data2;
603     data2.AddRecord(plainText);
604     QueryOption option2 = { .key = key };
605     UdmfClient::GetInstance().UpdateData(option2, data2);
606 
607     SetHapToken();
608     UnifiedData data3;
609     data3.AddRecord(plainText);
610     QueryOption option3 = { .key = skey };
611     UdmfClient::GetInstance().UpdateData(option3, data3);
612 }
613 
StartAsyncDataRetrievalFuzz(FuzzedDataProvider & provider)614 void StartAsyncDataRetrievalFuzz(FuzzedDataProvider &provider)
615 {
616     size_t dataSize = provider.ConsumeIntegralInRange<size_t>(1, 50);
617     std::vector<uint8_t> data = provider.ConsumeBytes<uint8_t>(dataSize);
618     CustomOption option1 = { .intention = UD_INTENTION_DATA_HUB };
619     UnifiedData data1;
620     std::string key;
621     auto plainText = std::make_shared<PlainText>();
622     std::string content = provider.ConsumeRandomLengthString();
623     plainText->SetContent(content);
624     data1.AddRecord(plainText);
625     UdmfClient::GetInstance().SetData(option1, data1, key);
626 
627     QueryOption query = {.key = key, .intention = UDMF::UD_INTENTION_DRAG};
628     GetDataParams params = {.query = query};
629     params.progressIndicator = ProgressIndicator::DEFAULT;
630     params.progressListener = [](ProgressInfo progressInfo, std::shared_ptr<UnifiedData> data) {};
631     UdmfAsyncClient::GetInstance().StartAsyncDataRetrieval(params);
632 }
633 
CancelAsyncDataRetrievalFuzz(FuzzedDataProvider & provider)634 void CancelAsyncDataRetrievalFuzz(FuzzedDataProvider &provider)
635 {
636     size_t dataSize = provider.ConsumeIntegralInRange<size_t>(1, 50);
637     std::vector<uint8_t> data = provider.ConsumeBytes<uint8_t>(dataSize);
638     CustomOption option1 = { .intention = UD_INTENTION_DATA_HUB };
639     UnifiedData data1;
640     std::string key;
641     auto plainText = std::make_shared<PlainText>();
642     std::string content = provider.ConsumeRandomLengthString();
643     plainText->SetContent(content);
644     data1.AddRecord(plainText);
645     UdmfClient::GetInstance().SetData(option1, data1, key);
646 
647     QueryOption query = {.key = key, .intention = UDMF::UD_INTENTION_DRAG};
648     GetDataParams params = {.query = query};
649     params.progressIndicator = ProgressIndicator::NONE;
650     params.progressListener = [](ProgressInfo progressInfo, std::shared_ptr<UnifiedData> data) {
651         std::this_thread::sleep_for(std::chrono::milliseconds(500));
652     };
653     UdmfAsyncClient::GetInstance().StartAsyncDataRetrieval(params);
654     UdmfAsyncClient::GetInstance().Cancel(key);
655 }
656 
SyncFuzz(FuzzedDataProvider & provider)657 void SyncFuzz(FuzzedDataProvider &provider)
658 {
659     QueryOption query;
660     query.key = provider.ConsumeRandomLengthString();
661     query.intention = static_cast<Intention>(
662         provider.ConsumeIntegralInRange<int32_t>(UD_INTENTION_BASE + 1, UD_INTENTION_BUTT - 1));
663     query.tokenId = provider.ConsumeIntegral<uint32_t>();
664 
665     std::vector<std::string> devices;
666     size_t devicesSize = provider.ConsumeIntegralInRange<size_t>(0, 10);
667     for (size_t i = 0; i < devicesSize; ++i) {
668         devices.push_back(provider.ConsumeRandomLengthString());
669     }
670     UdmfClient::GetInstance().Sync(query, devices);
671 }
672 
IsRemoteDataFuzz(FuzzedDataProvider & provider)673 void IsRemoteDataFuzz(FuzzedDataProvider &provider)
674 {
675     QueryOption query;
676     query.key = provider.ConsumeRandomLengthString();
677     query.intention = static_cast<Intention>(
678         provider.ConsumeIntegralInRange<int32_t>(UD_INTENTION_BASE + 1, UD_INTENTION_BUTT - 1));
679     query.tokenId = provider.ConsumeIntegral<uint32_t>();
680     bool res = false;
681     UdmfClient::GetInstance().IsRemoteData(query, res);
682 }
683 
SetAppShareOptionFuzz(FuzzedDataProvider & provider)684 void SetAppShareOptionFuzz(FuzzedDataProvider &provider)
685 {
686     Intention intention = static_cast<Intention>(
687         provider.ConsumeIntegralInRange<int32_t>(UD_INTENTION_BASE + 1, UD_INTENTION_BUTT - 1));
688     std::string intentionStr = UD_INTENTION_MAP.at(intention);
689 
690     ShareOptions shareOption = static_cast<ShareOptions>(
691         provider.ConsumeIntegralInRange<int32_t>(IN_APP, SHARE_OPTIONS_BUTT));
692     UdmfClient::GetInstance().SetAppShareOption(intentionStr, shareOption);
693 }
694 
GetAppShareOptionFuzz(FuzzedDataProvider & provider)695 void GetAppShareOptionFuzz(FuzzedDataProvider &provider)
696 {
697     Intention intention = static_cast<Intention>(
698         provider.ConsumeIntegralInRange<int32_t>(UD_INTENTION_BASE + 1, UD_INTENTION_BUTT - 1));
699     std::string intentionStr = UD_INTENTION_MAP.at(intention);
700 
701     ShareOptions shareOption;
702     UdmfClient::GetInstance().GetAppShareOption(intentionStr, shareOption);
703 }
704 
RemoveAppShareOptionFuzz(FuzzedDataProvider & provider)705 void RemoveAppShareOptionFuzz(FuzzedDataProvider &provider)
706 {
707     Intention intention = static_cast<Intention>(
708         provider.ConsumeIntegralInRange<int32_t>(UD_INTENTION_BASE + 1, UD_INTENTION_BUTT - 1));
709     std::string intentionStr = UD_INTENTION_MAP.at(intention);
710     UdmfClient::GetInstance().RemoveAppShareOption(intentionStr);
711 }
712 
GetParentTypeFuzz(FuzzedDataProvider & provider)713 void GetParentTypeFuzz(FuzzedDataProvider &provider)
714 {
715     Summary oldSummary;
716     Summary newSummary;
717 
718     size_t summarySize = provider.ConsumeIntegralInRange<size_t>(0, 10);
719     for (size_t i = 0; i < summarySize; ++i) {
720         std::string key = provider.ConsumeRandomLengthString();
721         int64_t value = provider.ConsumeIntegral<int64_t>();
722         oldSummary.summary[key] = value;
723     }
724     oldSummary.totalSize = provider.ConsumeIntegral<int64_t>();
725     UdmfClient::GetInstance().GetParentType(oldSummary, newSummary);
726 }
727 
SetDelayInfoFuzz(FuzzedDataProvider & provider)728 void SetDelayInfoFuzz(FuzzedDataProvider &provider)
729 {
730     DataLoadInfo dataLoadInfo1 {
731         .sequenceKey = provider.ConsumeRandomLengthString(),
732         .types{ provider.ConsumeRandomLengthString() },
733         .recordCount = provider.ConsumeIntegral<uint32_t>(),
734     };
735     auto loadHandler = [] (const std::string &udKey, const DataLoadInfo &dataLoadInfo) {};
736     DataLoadParams dataLoadParams = {
737         .loadHandler = loadHandler,
738         .dataLoadInfo = dataLoadInfo1,
739     };
740     std::string key = provider.ConsumeRandomLengthString();
741     UdmfClient::GetInstance().SetDelayInfo(dataLoadParams, key);
742 }
743 
PushDelayDataFuzz(FuzzedDataProvider & provider)744 void PushDelayDataFuzz(FuzzedDataProvider &provider)
745 {
746     DataLoadInfo dataLoadInfo1{
747         .sequenceKey = provider.ConsumeRandomLengthString(),
748         .types{provider.ConsumeRandomLengthString()},
749         .recordCount = provider.ConsumeIntegral<uint32_t>(),
750     };
751     auto loadHandler = [] (const std::string &udKey, const DataLoadInfo &dataLoadInfo) {};
752     DataLoadParams dataLoadParams = {
753         .loadHandler = loadHandler,
754         .dataLoadInfo = dataLoadInfo1,
755     };
756     std::string key = provider.ConsumeRandomLengthString();
757     UdmfClient::GetInstance().SetDelayInfo(dataLoadParams, key);
758 
759     auto text = std::make_shared<Text>();
760     UDDetails details;
761     std::string skey = provider.ConsumeRandomLengthString();
762     std::string svalue = provider.ConsumeRandomLengthString();
763     details.insert({skey, svalue});
764     text->SetDetails(details);
765     UnifiedData unifiedData;
766     unifiedData.AddRecord(text);
767     UdmfClient::GetInstance().PushDelayData(key, unifiedData);
768 }
769 
GetBundleNameByUdKeyFuzz(FuzzedDataProvider & provider)770 void GetBundleNameByUdKeyFuzz(FuzzedDataProvider &provider)
771 {
772     std::string key = provider.ConsumeRandomLengthString();
773     UdmfClient::GetInstance().GetBundleNameByUdKey(key);
774 }
775 }
776 
777 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)778 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
779 {
780     /* Run your code on data */
781     FuzzedDataProvider provider(data, size);
782     OHOS::SetUpTestCase();
783     OHOS::SetDataFileFuzz(provider);
784     OHOS::SetDataHtmlFuzz(provider);
785     OHOS::SetDataImageFuzz(provider);
786     OHOS::SetDataLinkFuzz(provider);
787     OHOS::SetDataPlainTextFuzz(provider);
788     OHOS::SetDataSystemDefinedAppItemFuzz(provider);
789     OHOS::SetDataSystemDefinedFormFuzz(provider);
790     OHOS::SetDataSystemDefinedPixelMapFuzz(provider);
791     OHOS::SetDataSystemDefinedRecordFuzz(provider);
792     OHOS::SetDataTextFuzz(provider);
793     OHOS::SetDataVideoFuzz(provider);
794     OHOS::GetSummaryFuzz(provider);
795     OHOS::GetBatchDataByKeyFuzz(provider);
796     OHOS::GetBatchDataByIntentionFuzz(provider);
797     OHOS::DeleteDataByKeyFuzz(provider);
798     OHOS::DeleteDataByIntentionFuzz(provider);
799     OHOS::UpdateDataFuzz(provider);
800     OHOS::StartAsyncDataRetrievalFuzz(provider);
801     OHOS::CancelAsyncDataRetrievalFuzz(provider);
802     OHOS::SyncFuzz(provider);
803     OHOS::IsRemoteDataFuzz(provider);
804     OHOS::SetAppShareOptionFuzz(provider);
805     OHOS::GetAppShareOptionFuzz(provider);
806     OHOS::RemoveAppShareOptionFuzz(provider);
807     OHOS::GetParentTypeFuzz(provider);
808     OHOS::SetDelayInfoFuzz(provider);
809     OHOS::PushDelayDataFuzz(provider);
810     OHOS::GetBundleNameByUdKeyFuzz(provider);
811     OHOS::TearDown();
812     return 0;
813 }