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 "formabilityconnection_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 "common/connection/form_ability_connection.h"
25 #include "ams_mgr/form_ams_helper.h"
26 #include "form_provider/connection/form_batch_delete_connection.h"
27 #include "form_provider/connection/form_cast_temp_connection.h"
28 #undef private
29 #undef protected
30 #include "securec.h"
31
32 using namespace OHOS::AppExecFwk;
33
34 namespace OHOS {
DoSomethingInterestingPart2(FuzzedDataProvider * fdp)35 void DoSomethingInterestingPart2(FuzzedDataProvider *fdp)
36 {
37 FormAmsHelper formAmsHelper;
38 std::string bundleName = fdp->ConsumeRandomLengthString();
39 std::string abilityName = fdp->ConsumeRandomLengthString();
40 AppExecFwk::ElementName element;
41 sptr<IRemoteObject> providerToken = nullptr;
42 sptr<IRemoteObject> remoteObjects = nullptr;
43 int resultCode = fdp->ConsumeIntegral<int>();
44 Want want;
45 sptr<AAFwk::IAbilityConnection> connect = nullptr;
46 formAmsHelper.GetAbilityManager();
47 formAmsHelper.ConnectServiceAbility(want, connect);
48 formAmsHelper.DisconnectServiceAbility(connect);
49 formAmsHelper.DisconnectServiceAbilityDelay(connect);
50 formAmsHelper.StopExtensionAbility(want);
51 formAmsHelper.RegisterConfigurationObserver();
52 formAmsHelper.UnRegisterConfigurationObserver();
53 sptr<AAFwk::IAbilityManager> abilityManager = nullptr;
54 formAmsHelper.SetAbilityManager(abilityManager);
55 formAmsHelper.DisconnectAbilityTask(connect);
56 int32_t userId = fdp->ConsumeIntegral<int32_t>();
57 formAmsHelper.StartAbility(want, userId);
58 std::set<int64_t> formIds;
59 sptr<IAbilityConnection> batchDeleteConnection = new FormBatchDeleteConnection(formIds, bundleName, abilityName);
60 batchDeleteConnection->OnAbilityConnectDone(element, providerToken, userId);
61 int64_t formId = fdp->ConsumeIntegral<int64_t>();
62 sptr<IAbilityConnection> castTempConnection = new FormCastTempConnection(formId, bundleName, abilityName);
63 castTempConnection->OnAbilityConnectDone(element, remoteObjects, resultCode);
64 }
65
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)66 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
67 {
68 if (fdp == nullptr) {
69 return true;
70 }
71 FormAbilityConnection formAbilityConnection;
72 AppExecFwk::ElementName element;
73 sptr<IRemoteObject> remoteObjects = nullptr;
74 int resultCode = fdp->ConsumeIntegral<int>();
75 bool isFreeInstall = fdp->ConsumeBool();
76 formAbilityConnection.SetFreeInstall(isFreeInstall);
77 int32_t connectId = fdp->ConsumeIntegral<int32_t>();
78 formAbilityConnection.SetConnectId(connectId);
79 formAbilityConnection.formId_ = fdp->ConsumeIntegral<int64_t>();
80 formAbilityConnection.bundleName_ = fdp->ConsumeRandomLengthString();
81 formAbilityConnection.abilityName_ = fdp->ConsumeRandomLengthString();
82 formAbilityConnection.isFreeInstall_ = fdp->ConsumeBool();
83 formAbilityConnection.connectId_ = fdp->ConsumeIntegral<int32_t>();
84 formAbilityConnection.OnAbilityConnectDone(element, remoteObjects, resultCode);
85 formAbilityConnection.GetProviderKey();
86 std::string bundleName = fdp->ConsumeRandomLengthString();
87 std::string abilityName = fdp->ConsumeRandomLengthString();
88 formAbilityConnection.SetProviderKey(bundleName, abilityName);
89 int64_t formId = fdp->ConsumeIntegral<int64_t>();
90 formAbilityConnection.SetFormId(formId);
91 formAbilityConnection.GetFormId();
92 sptr<IRemoteObject> hostToken = nullptr;
93 formAbilityConnection.SetHostToken(hostToken);
94 formAbilityConnection.GetHostToken();
95 sptr<IRemoteObject> providerToken = nullptr;
96 formAbilityConnection.SetProviderToken(providerToken);
97 formAbilityConnection.GetProviderToken();
98 formAbilityConnection.GetAppMgr();
99 formAbilityConnection.onFormAppConnect();
100 formAbilityConnection.ReportFormAppUnbindEvent();
101 formAbilityConnection.GetBundleName();
102 formAbilityConnection.GetAppFormPid();
103 formAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
104 wptr<IRemoteObject> remoteObject1;
105 formAbilityConnection.OnConnectDied(remoteObject1);
106 DoSomethingInterestingPart2(fdp);
107 return formAbilityConnection.GetConnectId();
108 }
109 }
110
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114 FuzzedDataProvider fdp(data, size);
115 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
116 return 0;
117 }