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 "watcher.h"
17 #include "watcher_proxy.h"
18 #include "addremotewatcher_fuzzer.h"
19 #include <string>
20 #include <memory>
21 #include "watcher_utils.h"
22 #define protected public
23 #include "watcher_manager.h"
24 #undef protected
25 using namespace OHOS::init_param;
26
27 class FuzzWatcher final : public Watcher {
28 public:
FuzzWatcher()29 FuzzWatcher() {}
30 ~FuzzWatcher() = default;
OnParameterChange(const std::string & prefix,const std::string & name,const std::string & value)31 int32_t OnParameterChange(const std::string &prefix, const std::string &name, const std::string &value) override
32 {
33 return 0;
34 }
35 };
36 namespace OHOS {
FuzzAddRemoteWatcher(const uint8_t * data,size_t size)37 bool FuzzAddRemoteWatcher(const uint8_t* data, size_t size)
38 {
39 bool result = false;
40 sptr<Watcher> watcher = new FuzzWatcher();
41 std::unique_ptr<WatcherManager> watcherManager = std::make_unique<WatcherManager>(0, true);
42 uint32_t id = static_cast<const uint32_t>(*data);
43 uint32_t watcherId = 0;
44 watcherManager->OnStart();
45 if (!watcherManager->AddRemoteWatcher(id, watcherId, watcher)) {
46 result = true;
47 watcherManager->DelRemoteWatcher(watcherId);
48 };
49 return result;
50 }
51 }
52
53 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
55 {
56 /* Run your code on data */
57 OHOS::FuzzAddRemoteWatcher(data, size);
58 return 0;
59 }