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 #include "onstop_fuzzer.h"
16 #include <string>
17 #include <memory>
18 #include "watcher.h"
19 #include "watcher_proxy.h"
20 #include "init_utils.h"
21 #define private public
22 #define protected public
23 #include "watcher_manager.h"
24 #undef protected
25 #undef private
26 using namespace OHOS::init_param;
27
28 class FuzzWatcher final : public Watcher {
29 public:
FuzzWatcher()30 FuzzWatcher() {}
31 ~FuzzWatcher() = default;
32
OnParameterChange(const std::string & prefix,const std::string & name,const std::string & value)33 int32_t OnParameterChange(const std::string &prefix, const std::string &name, const std::string &value) override
34 {
35 return 0;
36 }
37 };
38 namespace OHOS {
FuzzCheckAppWatchPermission(const uint8_t * data,size_t size)39 bool FuzzCheckAppWatchPermission(const uint8_t* data, size_t size)
40 {
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 = 1;
44 sptr<Watcher> watcher = new FuzzWatcher();
45 watcherManager->AddRemoteWatcher(id, watcherId, watcher);
46 watcherManager->StartLoop();
47 watcherManager->OnStop();
48 sleep(1);
49 return true;
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::FuzzCheckAppWatchPermission(data, size);
58 return 0;
59 }