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 "pasteboarddisposable_fuzzer.h"
17
18 #include <fuzzer/FuzzedDataProvider.h>
19
20 #include "pasteboard_disposable_manager.h"
21 #include "pasteboard_error.h"
22 #include "ffrt/ffrt_utils.h"
23
24 namespace {
25 using namespace OHOS::MiscServices;
26
27 constexpr uint32_t MAX_BUNDLE_NAME_LENGTH = 127;
28 constexpr uint32_t MAX_ENUM_VALUE = 5;
29
30 class DisposableObserverImpl : public IPasteboardDisposableObserver {
31 public:
OnTextReceived(const std::string & text,int32_t errCode)32 void OnTextReceived(const std::string &text, int32_t errCode) override
33 {
34 (void)text;
35 (void)errCode;
36 }
37
AsObject()38 OHOS::sptr<OHOS::IRemoteObject> AsObject() override
39 {
40 return nullptr;
41 }
42 };
43
44 class DelayGetterImpl : public IPasteboardDelayGetter {
45 public:
GetPasteData(const std::string & type,PasteData & data)46 void GetPasteData(const std::string &type, PasteData &data) override
47 {
48 (void)type;
49 data.AddTextRecord(text_);
50 }
51
GetUnifiedData(const std::string & type,OHOS::UDMF::UnifiedData & data)52 void GetUnifiedData(const std::string &type, OHOS::UDMF::UnifiedData &data) override
53 {
54 (void)type;
55 (void)data;
56 }
57
AsObject()58 OHOS::sptr<OHOS::IRemoteObject> AsObject() override
59 {
60 return nullptr;
61 }
62
63 std::string text_;
64 };
65
66 class EntryGetterImpl : public IPasteboardEntryGetter {
67 public:
GetRecordValueByType(uint32_t recordId,PasteDataEntry & value)68 int32_t GetRecordValueByType(uint32_t recordId, PasteDataEntry &value) override
69 {
70 (void)recordId;
71 value.SetValue(text_);
72 return static_cast<int32_t>(PasteboardError::E_OK);
73 }
74
AsObject()75 OHOS::sptr<OHOS::IRemoteObject> AsObject() override
76 {
77 return nullptr;
78 }
79
80 std::string text_;
81 };
82
83 class TestEnv {
84 public:
TestEnv()85 TestEnv()
86 {
87 SetUpTestCase();
88 }
89
~TestEnv()90 ~TestEnv()
91 {
92 TearDownTestCase();
93 }
94
SetUpTestCase()95 static void SetUpTestCase()
96 {
97 }
98
TearDownTestCase()99 static void TearDownTestCase()
100 {
101 }
102
SetUp()103 void SetUp()
104 {
105 }
106
TearDown()107 void TearDown()
108 {
109 FFRTPool::Clear();
110 DisposableManager::GetInstance().disposableInfoList_.clear();
111 }
112 };
113
TestAddDisposableInfo(const uint8_t * data,size_t size)114 void TestAddDisposableInfo(const uint8_t *data, size_t size)
115 {
116 FuzzedDataProvider fdp(data, size);
117
118 pid_t pid = fdp.ConsumeIntegral<pid_t>();
119 uint32_t tokenId = fdp.ConsumeIntegral<uint32_t>();
120 std::string bundle = fdp.ConsumeRandomLengthString(MAX_BUNDLE_NAME_LENGTH);
121 DisposableType type = static_cast<DisposableType>(fdp.ConsumeIntegralInRange<uint32_t>(0, MAX_ENUM_VALUE));
122 uint32_t maxLen = fdp.ConsumeIntegral<uint32_t>();
123 auto observer = fdp.ConsumeBool() ? nullptr : OHOS::sptr<DisposableObserverImpl>::MakeSptr();
124 DisposableInfo info(pid, tokenId, bundle, type, maxLen, observer);
125 DisposableManager::GetInstance().AddDisposableInfo(info);
126 }
127
TestTryProcessDisposableData(const uint8_t * data,size_t size)128 void TestTryProcessDisposableData(const uint8_t *data, size_t size)
129 {
130 FuzzedDataProvider fdp(data, size);
131
132 pid_t pid = fdp.ConsumeIntegral<pid_t>();
133 uint32_t tokenId = fdp.ConsumeIntegral<uint32_t>();
134 std::string bundle = fdp.ConsumeRandomLengthString(MAX_BUNDLE_NAME_LENGTH);
135 DisposableType type = static_cast<DisposableType>(fdp.ConsumeIntegralInRange<uint32_t>(0, MAX_ENUM_VALUE));
136 uint32_t maxLen = fdp.ConsumeIntegral<uint32_t>();
137 auto observer = fdp.ConsumeBool() ? nullptr : OHOS::sptr<DisposableObserverImpl>::MakeSptr();
138 DisposableInfo info(pid, tokenId, bundle, type, maxLen, observer);
139
140 pid_t pid2 = fdp.ConsumeIntegral<pid_t>();
141 uint32_t tokenId2 = fdp.ConsumeIntegral<uint32_t>();
142 std::string bundle2 = fdp.ConsumeRandomLengthString(MAX_BUNDLE_NAME_LENGTH);
143 DisposableType type2 = static_cast<DisposableType>(fdp.ConsumeIntegralInRange<uint32_t>(0, MAX_ENUM_VALUE));
144 uint32_t maxLen2 = fdp.ConsumeIntegral<uint32_t>();
145 auto observer2 = fdp.ConsumeBool() ? nullptr : OHOS::sptr<DisposableObserverImpl>::MakeSptr();
146 DisposableInfo info2(pid2, tokenId2, bundle2, type2, maxLen2, observer2);
147
148 std::string text = fdp.ConsumeRandomLengthString();
149 PasteData pasteData;
150 pasteData.AddTextRecord(text);
151 auto delayGetter = OHOS::sptr<DelayGetterImpl>::MakeSptr();
152 delayGetter->text_ = text;
153 auto entryGetter = OHOS::sptr<EntryGetterImpl>::MakeSptr();
154 entryGetter->text_ = text;
155
156 DisposableManager::GetInstance().disposableInfoList_ = {info, info2};
157 DisposableManager::GetInstance().TryProcessDisposableData(bundle, pasteData, delayGetter, entryGetter);
158 }
159
TestRemoveDisposableInfo(const uint8_t * data,size_t size)160 void TestRemoveDisposableInfo(const uint8_t *data, size_t size)
161 {
162 FuzzedDataProvider fdp(data, size);
163
164 pid_t pid = fdp.ConsumeIntegral<pid_t>();
165 uint32_t tokenId = fdp.ConsumeIntegral<uint32_t>();
166 std::string bundle = fdp.ConsumeRandomLengthString(MAX_BUNDLE_NAME_LENGTH);
167 DisposableType type = static_cast<DisposableType>(fdp.ConsumeIntegralInRange<uint32_t>(0, MAX_ENUM_VALUE));
168 uint32_t maxLen = fdp.ConsumeIntegral<uint32_t>();
169 auto observer = fdp.ConsumeBool() ? nullptr : OHOS::sptr<DisposableObserverImpl>::MakeSptr();
170 DisposableInfo info(pid, tokenId, bundle, type, maxLen, observer);
171
172 pid_t pid2 = fdp.ConsumeIntegral<pid_t>();
173 uint32_t tokenId2 = fdp.ConsumeIntegral<uint32_t>();
174 std::string bundle2 = fdp.ConsumeRandomLengthString(MAX_BUNDLE_NAME_LENGTH);
175 DisposableType type2 = static_cast<DisposableType>(fdp.ConsumeIntegralInRange<uint32_t>(0, MAX_ENUM_VALUE));
176 uint32_t maxLen2 = fdp.ConsumeIntegral<uint32_t>();
177 auto observer2 = fdp.ConsumeBool() ? nullptr : OHOS::sptr<DisposableObserverImpl>::MakeSptr();
178 DisposableInfo info2(pid2, tokenId2, bundle2, type2, maxLen2, observer2);
179
180 DisposableManager::GetInstance().disposableInfoList_ = {info, info2};
181 DisposableManager::GetInstance().RemoveDisposableInfo(pid, fdp.ConsumeBool());
182 }
183 } // anonymous namespace
184
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)185 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
186 {
187 static TestEnv env;
188 env.SetUp();
189
190 TestAddDisposableInfo(data, size);
191 TestTryProcessDisposableData(data, size);
192 TestRemoveDisposableInfo(data, size);
193
194 env.TearDown();
195 return 0;
196 }