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 "formsupplycallback_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "data_center/form_info/form_info_storage.h"
23 #define private public
24 #define protected public
25 #include "common/util/form_dump_mgr.h"
26 #include "common/util/form_serial_queue.h"
27 #include "form_provider/form_supply_callback.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 INDEX_MAX = 2;
FormSerialQueueTest(FuzzedDataProvider * fdp)36 void FormSerialQueueTest(FuzzedDataProvider *fdp)
37 {
38 std::string queueName = fdp->ConsumeRandomLengthString();
39 FormSerialQueue formSerialQueue(queueName);
40 uint64_t ms = fdp->ConsumeIntegral<uint64_t>();
41 uint32_t msg = fdp->ConsumeIntegral<uint32_t>();
42 auto testTask = []() {};
43 int64_t num1 = fdp->ConsumeIntegral<int64_t>();
44 int64_t num2 = fdp->ConsumeIntegral<int64_t>();
45 int64_t num3 = fdp->ConsumeIntegral<int64_t>();
46 std::string str1 = fdp->ConsumeRandomLengthString();
47 std::pair<int64_t, int64_t> eventMsg = std::make_pair(num1, num2);
48 std::pair<int64_t, std::string> eventMsgStr = std::make_pair(num3, str1);
49 formSerialQueue.ScheduleTask(ms, testTask);
50 formSerialQueue.ScheduleDelayTask(eventMsg, msg, testTask);
51 formSerialQueue.CancelDelayTask(eventMsg);
52 formSerialQueue.ScheduleDelayTask(eventMsgStr, msg, testTask);
53 formSerialQueue.CancelDelayTask(eventMsgStr);
54 }
55
FormDumpMgrTestPartOne(FuzzedDataProvider * fdp)56 void FormDumpMgrTestPartOne(FuzzedDataProvider *fdp)
57 {
58 FormDumpMgr formDumpMgr;
59 std::vector<Constants::FormLocation> locations = {Constants::FormLocation::OTHER,
60 Constants::FormLocation::DESKTOP, Constants::FormLocation::FORM_CENTER,
61 Constants::FormLocation::FORM_MANAGER, Constants::FormLocation::NEGATIVE_SCREEN,
62 Constants::FormLocation::FORM_CENTER_NEGATIVE_SCREEN,
63 Constants::FormLocation::FORM_MANAGER_NEGATIVE_SCREEN,
64 Constants::FormLocation::SCREEN_LOCK, Constants::FormLocation::AI_SUGGESTION};
65 Constants::FormLocation formLocation = locations.at(fdp->ConsumeIntegralInRange<size_t>(0, locations.size() - 1));
66 std::string infosResult = fdp->ConsumeRandomLengthString();
67 formDumpMgr.AppendFormLocation(formLocation, infosResult);
68 std::vector<BundleType> formBundleTypes = {BundleType::APP, BundleType::ATOMIC_SERVICE, BundleType::SHARED};
69 BundleType formBundleType = formBundleTypes.at(fdp->ConsumeIntegralInRange<size_t>(0, formBundleTypes.size() - 1));
70 std::string formInfo = fdp->ConsumeRandomLengthString();
71 formDumpMgr.AppendBundleType(formBundleType, formInfo);
72 std::string key = fdp->ConsumeRandomLengthString();
73 std::string value = fdp->ConsumeRandomLengthString();
74 std::unordered_map<std::string, std::string> liveFormStatusMap = {{key, value}};
75 std::string formId = fdp->ConsumeRandomLengthString();
76 formDumpMgr.AppendLiveFormStatus(formId, liveFormStatusMap, formInfo);
77 }
78
FormDumpMgrTest(FuzzedDataProvider * fdp)79 void FormDumpMgrTest(FuzzedDataProvider *fdp)
80 {
81 FormDumpMgr formDumpMgr;
82 FormDBInfo formDBInfo;
83 std::vector<FormDBInfo> storageInfos;
84 storageInfos.emplace_back(formDBInfo);
85 std::string formInfoes = fdp->ConsumeRandomLengthString();
86 formDumpMgr.DumpStorageFormInfos(storageInfos, formInfoes);
87 FormRecord formRecord;
88 std::vector<FormRecord> formRecordInfos;
89 formRecordInfos.emplace_back(formRecord);
90 formDumpMgr.DumpFormInfos(formRecordInfos, formInfoes);
91 FormHostRecord formHostRecord;
92 std::string formInfo = fdp->ConsumeRandomLengthString();
93 formDumpMgr.DumpFormHostInfo(formHostRecord, formInfo);
94 formDumpMgr.DumpFormInfo(formRecord, formInfo);
95 formDumpMgr.DumpTemporaryFormInfos(formRecordInfos, formInfo);
96 std::vector<FormInfo> bundleFormInfos;
97 formDumpMgr.DumpStaticBundleFormInfos(bundleFormInfos, formInfo);
98 int32_t tokenId = fdp->ConsumeIntegral<int32_t>();
99 std::string bundleName = fdp->ConsumeRandomLengthString();
100 int32_t instIndex = fdp->ConsumeIntegral<int32_t>();
101 int32_t userId = fdp->ConsumeIntegral<int32_t>();
102 formDumpMgr.DumpHasFormVisible(tokenId, bundleName, userId, instIndex, formInfo);
103 std::vector<std::string> subscribedKeys;
104 int32_t index = fdp->ConsumeIntegralInRange(0, INDEX_MAX);
105 for (int32_t i = 0; i < index; i++) {
106 subscribedKeys.push_back(fdp->ConsumeRandomLengthString());
107 }
108 int64_t count = fdp->ConsumeIntegral<int64_t>();
109 std::string formHostBundleName = fdp->ConsumeRandomLengthString();
110 RunningFormInfo runningFormInfo;
111 runningFormInfo.hostBundleName = formHostBundleName;
112 std::vector<FormVisibilityType> formVisiblitys = {FormVisibilityType::UNKNOWN,
113 FormVisibilityType::VISIBLE, FormVisibilityType::INVISIBLE};
114 std::vector<FormUsageState> usageStates = {FormUsageState::USED, FormUsageState::UNUSED};
115 runningFormInfo.formVisiblity = formVisiblitys.at(fdp->ConsumeIntegralInRange<size_t>(0,
116 formVisiblitys.size() - 1));
117 runningFormInfo.formUsageState = usageStates.at(fdp->ConsumeIntegralInRange<size_t>(0,
118 usageStates.size() - 1));
119 std::vector<RunningFormInfo> runningFormInfos = {runningFormInfo};
120 formDumpMgr.DumpFormSubscribeInfo(subscribedKeys, count, formInfo);
121 std::string infosResult = fdp->ConsumeRandomLengthString();
122 formDumpMgr.AppendRunningFormInfos(formHostBundleName, runningFormInfos, infosResult);
123 formDumpMgr.DumpRunningFormInfos(runningFormInfos, infosResult);
124 OHOS::FormDumpMgrTestPartOne(fdp);
125 }
126
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)127 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
128 {
129 if (fdp == nullptr) {
130 return true;
131 }
132 FormSupplyCallback formSupplyCallback;
133 formSupplyCallback.GetInstance();
134 FormProviderInfo formProviderInfo;
135 Want want;
136 formSupplyCallback.OnAcquire(formProviderInfo, want);
137 formSupplyCallback.OnEventHandle(want);
138 FormState state = FormState::UNKNOWN;
139 std::string provider = fdp->ConsumeRandomLengthString();
140 Want wantArg;
141 formSupplyCallback.OnAcquireStateResult(state, provider, wantArg, want);
142 sptr<FormAbilityConnection> connection = nullptr;
143 formSupplyCallback.AddConnection(connection);
144 int32_t connectId = fdp->ConsumeIntegral<int32_t>();
145 formSupplyCallback.RemoveConnection(connectId);
146 formSupplyCallback.CanDisconnect(connection);
147 int64_t formId = fdp->ConsumeIntegral<int64_t>();
148 std::string remoteDeviceId = fdp->ConsumeRandomLengthString();
149 AAFwk::WantParams wantParams;
150 int64_t requestCode = fdp->ConsumeIntegral<int64_t>();
151 bool result = fdp->ConsumeBool();
152 formSupplyCallback.OnShareAcquire(formId, remoteDeviceId, wantParams, requestCode, result);
153 sptr<IRemoteObject> hostToken = nullptr;
154 formSupplyCallback.RemoveConnection(formId, hostToken);
155 formSupplyCallback.HandleHostDied(hostToken);
156 int32_t userId = fdp->ConsumeIntegral<int32_t>();
157 FormInfo info;
158 std::vector<FormInfo> formInfos;
159 formInfos.emplace_back(info);
160 AAFwk::FormInfoStorage formInfoStorage(userId, formInfos);
161 formInfoStorage.GetAllFormsInfo(userId, formInfos);
162 std::string moduleName = fdp->ConsumeRandomLengthString();
163 formInfoStorage.GetFormsInfoByModule(userId, moduleName, formInfos);
164 OHOS::FormDumpMgrTest(fdp);
165 OHOS::FormSerialQueueTest(fdp);
166 return formSupplyCallback.IsRemoveConnection(formId, hostToken);
167 }
168 }
169
170 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)171 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
172 {
173 FuzzedDataProvider fdp(data, size);
174 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
175 return 0;
176 }