1 /*
2 * Copyright (c) 2024 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 #define private public
17 #include "settings_data_utils.h"
18 #undef private
19
20 #include "settingsdatautils_fuzzer.h"
21
22 using namespace OHOS::MiscServices;
23 namespace OHOS {
FuzzGetToken()24 void FuzzGetToken()
25 {
26 SettingsDataUtils::GetInstance().GetToken();
27 }
28
FuzzDataShareHelper()29 void FuzzDataShareHelper()
30 {
31 auto helper = SettingsDataUtils::GetInstance().CreateDataShareHelper(SETTING_URI_PROXY);
32 SettingsDataUtils::GetInstance().ReleaseDataShareHelper(helper);
33 }
34
FuzzCreateAndRegisterObserver(const std::string & uriProxy,const std::string & key,SettingsDataObserver::CallbackFunc func)35 void FuzzCreateAndRegisterObserver(
36 const std::string &uriProxy, const std::string &key, SettingsDataObserver::CallbackFunc func)
37 {
38 SettingsDataUtils::GetInstance().CreateAndRegisterObserver(uriProxy, key, func);
39 }
40
FuzzRegisterObserver(const std::string & uriProxy,const std::string & key,SettingsDataObserver::CallbackFunc & func)41 void FuzzRegisterObserver(const std::string &uriProxy, const std::string &key, SettingsDataObserver::CallbackFunc &func)
42 {
43 sptr<SettingsDataObserver> observer = new SettingsDataObserver(uriProxy, key, func);
44 SettingsDataUtils::GetInstance().RegisterObserver(observer);
45 }
46
FuzzUnregisterObserver(const std::string & uriProxy,const std::string & key,SettingsDataObserver::CallbackFunc & func)47 void FuzzUnregisterObserver(
48 const std::string &uriProxy, const std::string &key, SettingsDataObserver::CallbackFunc &func)
49 {
50 sptr<SettingsDataObserver> observer = new SettingsDataObserver(uriProxy, key, func);
51 SettingsDataUtils::GetInstance().UnregisterObserver(observer);
52 }
53
FuzzGenerateTargetUri(const std::string & key)54 void FuzzGenerateTargetUri(const std::string &key)
55 {
56 SettingsDataUtils::GetInstance().GenerateTargetUri(SETTING_URI_PROXY, key);
57 }
58
59
FuzzSetStringValue(const std::string & key)60 void FuzzSetStringValue(const std::string &key)
61 {
62 SettingsDataUtils::GetInstance().SetStringValue(SETTING_URI_PROXY, key, key);
63 }
64 } // namespace OHOS
65
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69 /* Run your code on data */
70 std::string fuzzedString(reinterpret_cast<const char *>(data), size);
71 SettingsDataObserver::CallbackFunc func;
72
73 OHOS::FuzzGetToken();
74 OHOS::FuzzDataShareHelper();
75 OHOS::FuzzCreateAndRegisterObserver(fuzzedString, fuzzedString, func);
76 OHOS::FuzzRegisterObserver(fuzzedString, fuzzedString, func);
77 OHOS::FuzzUnregisterObserver(fuzzedString, fuzzedString, func);
78 OHOS::FuzzGenerateTargetUri(fuzzedString);
79 return 0;
80 }