• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "formdatamgr_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #define private public
23 #define protected public
24 #include "data_center/form_data_mgr.h"
25 #include "bms_mgr/form_bundle_event_callback.h"
26 #undef private
27 #undef protected
28 #include "securec.h"
29 
30 using namespace OHOS::AppExecFwk;
31 
32 namespace OHOS {
33 const std::string BMS_EVENT_ADDITIONAL_INFO_CHANGED = "bms.event.ADDITIONAL_INFO_CHANGED";
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)34 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
35 {
36     if (fdp == nullptr) {
37         return true;
38     }
39     FormItemInfo formInfo;
40     int callingUid = fdp->ConsumeIntegral<int>();
41     int32_t userId = fdp->ConsumeIntegral<int32_t>();
42     FormDataMgr::GetInstance().AllotFormRecord(formInfo, callingUid, userId);
43     int64_t formId = fdp->ConsumeIntegral<int64_t>();
44     FormDataMgr::GetInstance().DeleteFormRecord(formId);
45     sptr<IRemoteObject> callerToken = nullptr;
46     FormDataMgr::GetInstance().AllotFormHostRecord(formInfo, callerToken, formId, callingUid);
47     FormHostRecord record;
48     FormDataMgr::GetInstance().CreateHostRecord(formInfo, callerToken, callingUid, record);
49     FormDataMgr::GetInstance().CreateFormRecord(formInfo, callingUid, userId);
50     FormRecord records;
51     FormJsInfo formInfos;
52     FormDataMgr::GetInstance().CreateFormJsInfo(formId, records, formInfos);
53     FormDataMgr::GetInstance().CheckTempEnoughForm();
54     int32_t currentUserId = fdp->ConsumeIntegral<int32_t>();
55     FormDataMgr::GetInstance().CheckEnoughForm(callingUid, currentUserId);
56     FormDataMgr::GetInstance().DeleteTempForm(formId);
57     FormDataMgr::GetInstance().ExistTempForm(formId);
58     FormBundleEventCallback formBundleEventCallback;
59     EventFwk::CommonEventData eventData;
60     formBundleEventCallback.OnReceiveEvent(eventData);
61     Want want;
62     std::vector<std::string> actions = {EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED,
63         EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, BMS_EVENT_ADDITIONAL_INFO_CHANGED};
64     std::string action = actions.at(fdp->ConsumeIntegralInRange<size_t>(0, actions.size() - 1));
65     want.SetAction(action);
66     eventData.SetWant(want);
67     formBundleEventCallback.OnReceiveEvent(eventData);
68     return true;
69 }
70 }
71 
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75     FuzzedDataProvider fdp(data, size);
76     OHOS::DoSomethingInterestingWithMyAPI(&fdp);
77     return 0;
78 }