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 "formtimermgrannex_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include "common_event_support.h"
22
23 #define private public
24 #define protected public
25 #include "data_center/form_info/form_info_mgr.h"
26 #include "common/timer_mgr/form_timer_mgr.h"
27 #include "common/timer_mgr/form_refresh_limiter.h"
28 #undef private
29 #undef protected
30 #include "securec.h"
31
32 using namespace OHOS::AppExecFwk;
33
34 namespace OHOS {
35 constexpr int32_t LIMIT_MAX = 55;
36 constexpr int32_t INDEX_MAX = 2;
37 constexpr char FMS_TIME_SPEED[] = "fms.time_speed";
FormRefreshLimiterTest(FuzzedDataProvider * fdp)38 void FormRefreshLimiterTest(FuzzedDataProvider *fdp)
39 {
40 FormRefreshLimiter formRefreshLimiter;
41 int64_t formId = fdp->ConsumeIntegral<int64_t>();
42 int32_t limitCount = fdp->ConsumeIntegralInRange(1, LIMIT_MAX);
43 formRefreshLimiter.AddItem(formId);
44 for (int32_t i = 0; i <= limitCount; i++) {
45 formRefreshLimiter.Increase(formId);
46 }
47 formRefreshLimiter.IsEnableRefresh(formId);
48 formRefreshLimiter.GetRefreshCount(formId);
49 formRefreshLimiter.GetRemindList();
50 formRefreshLimiter.GetRemindListAndResetLimit();
51 formRefreshLimiter.GetRemindList();
52 formRefreshLimiter.MarkRemind(formId);
53 formRefreshLimiter.DeleteItem(formId);
54 formRefreshLimiter.ResetLimit();
55 }
56
FormTimeMgrTest(FuzzedDataProvider * fdp)57 void FormTimeMgrTest(FuzzedDataProvider *fdp)
58 {
59 FormTimerMgr formTimerMgr;
60 int64_t formId = fdp->ConsumeIntegral<int64_t>();
61 std::vector<UpdateType> types = {UpdateType::TYPE_INTERVAL_CHANGE, UpdateType::TYPE_ATTIME_CHANGE,
62 UpdateType::TYPE_INTERVAL_TO_ATTIME, UpdateType::TYPE_ATTIME_TO_INTERVAL,
63 UpdateType::TYPE_INTERVAL_ONCE, UpdateType::TYPE_NO_CHANGE};
64 UpdateType type = types.at(fdp->ConsumeIntegralInRange<size_t>(0, types.size() - 1));
65 FormTimerCfg timerCfg;
66 timerCfg.enableUpdate = fdp->ConsumeBool();
67 timerCfg.updateDuration = fdp->ConsumeIntegral<int64_t>();
68 timerCfg.updateAtHour = fdp->ConsumeIntegral<int>();
69 timerCfg.updateAtMin = fdp->ConsumeIntegral<int>();
70 int32_t index1 = fdp->ConsumeIntegralInRange(0, INDEX_MAX);
71 int32_t index2 = fdp->ConsumeIntegralInRange(0, INDEX_MAX);
72 std::vector<std::vector<int>> updateAtTimes = {};
73 std::vector<int> updateAtTimeVector = {};
74 for (int32_t i = 0; i < index1; i++) {
75 updateAtTimeVector.push_back(fdp->ConsumeIntegral<int>());
76 }
77 for (int32_t i = 0; i < index2; i++) {
78 updateAtTimes.push_back(updateAtTimeVector);
79 }
80 timerCfg.updateAtTimes = updateAtTimes;
81 formTimerMgr.UpdateFormTimer(formId, type, timerCfg);
82 formTimerMgr.IsNeedUpdate();
83 formTimerMgr.dynamicRefreshTasks_.clear();
84 formTimerMgr.dynamicAlarmTimerId_ = fdp->ConsumeIntegral<uint64_t>();
85 formTimerMgr.reportDiskUseTimerId_ = fdp->ConsumeIntegral<uint64_t>();
86 long updateAtTime = fdp->ConsumeIntegral<long>();
87 int32_t userId = fdp->ConsumeIntegral<int32_t>();
88 formTimerMgr.currentDynamicWantAgent_ = formTimerMgr.GetUpdateAtWantAgent(updateAtTime, userId);
89 formTimerMgr.UpdateDynamicAlarm();
90 formTimerMgr.StartDiskUseInfoReportTimer();
91 formTimerMgr.ClearDiskInfoReportTimer();
92 FormTimerMgr::TimerReceiver timerReceiver;
93 EventFwk::CommonEventData eventData;
94 Want want;
95 std::vector<std::string> actions = {FMS_TIME_SPEED, EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED,
96 Constants::ACTION_UPDATEATTIMER, fdp->ConsumeRandomLengthString()};
97 std::vector<int32_t> actionTypes = {Constants::TYPE_RESET_LIMIT, Constants::TYPE_STATIC_UPDATE,
98 Constants::TYPE_DYNAMIC_UPDATE, fdp->ConsumeIntegral<int32_t>()};
99 std::string action = actions.at(fdp->ConsumeIntegralInRange<size_t>(0, actions.size() - 1));
100 int32_t actionType = actionTypes.at(fdp->ConsumeIntegralInRange<size_t>(0, actions.size() - 1));
101 want.SetAction(action);
102 want.SetParam(Constants::KEY_ACTION_TYPE, actionType);
103 eventData.SetWant(want);
104 timerReceiver.OnReceiveEvent(eventData);
105 }
106
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)107 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
108 {
109 if (fdp == nullptr) {
110 return true;
111 }
112 OHOS::FormRefreshLimiterTest(fdp);
113 OHOS::FormTimeMgrTest(fdp);
114 FormTimerMgr formTimerMgr;
115 int64_t formId = fdp->ConsumeIntegral<int64_t>();
116 int64_t nextTime = fdp->ConsumeIntegral<int64_t>();
117 int32_t userId = fdp->ConsumeIntegral<int32_t>();
118 std::vector<std::vector<int>> updateAtTimes = {};
119 formTimerMgr.AddFormTimerForMultiUpdate(formId, updateAtTimes, userId);
120 std::vector<int> updateAtTime1 = {fdp->ConsumeIntegral<int>(), fdp->ConsumeIntegral<int>()};
121 std::vector<int> updateAtTime2 = {fdp->ConsumeIntegral<int>(), fdp->ConsumeIntegral<int>()};
122 updateAtTimes = {updateAtTime1, updateAtTime2};
123 formTimerMgr.AddFormTimerForMultiUpdate(formId, updateAtTimes, userId);
124 formTimerMgr.ClearLimiterTimerResource();
125 formTimerMgr.GetLimiterWantAgent();
126 formTimerMgr.UpdateDynamicAlarm();
127 formTimerMgr.GetDynamicWantAgent(nextTime, userId);
128 formTimerMgr.ClearDynamicResource();
129 long nowTime = fdp->ConsumeIntegral<long>();
130 UpdateAtItem updateAtItem;
131 formTimerMgr.FindNextAtTimerItem(nowTime, updateAtItem);
132 formTimerMgr.ClearIntervalTimer();
133 FormTimer timerTask;
134 formTimerMgr.ExecTimerTask(timerTask);
135 formTimerMgr.Init();
136 FormInfoHelper formInfoHelper;
137 std::string bundleName = fdp->ConsumeRandomLengthString();
138 FormInfo formInfo;
139 std::vector<FormInfo> formInfos;
140 formInfos.emplace_back(formInfo);
141 formInfoHelper.LoadFormConfigInfoByBundleName(bundleName, formInfos, userId);
142 return formTimerMgr.IsActiveUser(userId);
143 }
144 }
145
146 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)147 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
148 {
149 FuzzedDataProvider fdp(data, size);
150 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
151 return 0;
152 }