• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "openremoteuri_fuzzer.h"
17 
18 #include "pasteboard_client_adapter_impl.h"
19 
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 using namespace OHOS::NWeb;
23 
24 namespace OHOS {
25 namespace {
26     constexpr uint8_t TEST_STRING_LENGTH = 8;
27 }
OpenRemoteUriFuzzTest(const uint8_t * data,size_t size)28 bool OpenRemoteUriFuzzTest(const uint8_t* data, size_t size)
29 {
30     if ((data == nullptr) || (size == 0)) {
31         return false;
32     }
33     std::string path((const char*)data, size);
34     PasteBoardClientAdapterImpl::GetInstance().OpenRemoteUri(path);
35     PasteBoardClientAdapterImpl::GetInstance().IsLocalPaste();
36     PasteBoardClientAdapterImpl::GetInstance().GetTokenId();
37     return true;
38 }
39 
SetPastDataFuzzTest(const uint8_t * data,size_t size)40 bool SetPastDataFuzzTest(const uint8_t* data, size_t size)
41 {
42     if ((data == nullptr) || (size == 0)) {
43         return false;
44     }
45     FuzzedDataProvider dataProvider(data, size);
46     std::string html = dataProvider.ConsumeRandomLengthString(TEST_STRING_LENGTH);
47     std::shared_ptr<PasteDataAdapterImpl> dataAdapterImpl = std::make_shared<PasteDataAdapterImpl>();
48     dataAdapterImpl->AddHtmlRecord(html);
49     dataAdapterImpl->AddTextRecord(html);
50     PasteRecordVector datas = dataAdapterImpl->AllRecords();
51     PasteRecordVector pasteDatas;
52     PasteBoardClientAdapterImpl::GetInstance().GetPasteData(pasteDatas);
53 
54     PasteBoardClientAdapterImpl::GetInstance().SetPasteData(datas, CopyOptionMode::NONE);
55     PasteBoardClientAdapterImpl::GetInstance().SetPasteData(datas, CopyOptionMode::IN_APP);
56     PasteBoardClientAdapterImpl::GetInstance().SetPasteData(datas, CopyOptionMode::LOCAL_DEVICE);
57     PasteBoardClientAdapterImpl::GetInstance().SetPasteData(datas, CopyOptionMode::CROSS_DEVICE);
58 
59     PasteBoardClientAdapterImpl::GetInstance().GetPasteData(pasteDatas);
60     PasteBoardClientAdapterImpl::GetInstance().Clear();
61     return true;
62 }
63 } // namespace OHOS
64 
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
67 {
68     /* Run your code on data */
69     OHOS::OpenRemoteUriFuzzTest(data, size);
70     OHOS::SetPastDataFuzzTest(data, size);
71     return 0;
72 }
73