1 /*
2  * Copyright (c) 2023 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 "callvoiceassistantmanager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #define private public
21 #include "addcalltoken_fuzzer.h"
22 #include "cs_call.h"
23 #include "call_voice_assistant_manager.h"
24 #include "call_manager_base.h"
25 
26 using namespace OHOS::Telephony;
27 namespace OHOS {
28 const int32_t CALL_STATE_NUM = 8;
29 const int32_t CALL_ID = -1;
30 const int32_t ACCOUNT_ID = -1;
31 const std::string CONTROL_SWITCH = "incoming_call_voice_control_switch";
32 const std::string INCOMING = "come";
33 const std::string DIALING = "dial";
34 
ListenCallStateFunc(const uint8_t *data, size_t size)35 void ListenCallStateFunc(const uint8_t *data, size_t size)
36 {
37     if (!IsServiceInited()) {
38         return;
39     }
40 
41     DialParaInfo paraInfo;
42     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
43     TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
44     TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
45     std::shared_ptr<CallVoiceAssistantManager> voicePtr = CallVoiceAssistantManager::GetInstance();
46     voicePtr->CallStateUpdated(callObjectPtr, priorState, nextState);
47     voicePtr->CallStatusDialing(CALL_ID, ACCOUNT_ID);
48     voicePtr->CallStatusIncoming(CALL_ID, ACCOUNT_ID);
49     voicePtr->CallStatusActive(CALL_ID, ACCOUNT_ID);
50     voicePtr->CallStatusDisconnected(CALL_ID, ACCOUNT_ID);
51     voicePtr->ConnectAbility(ACCOUNT_ID);
52     voicePtr->OnStartService(INCOMING, ACCOUNT_ID);
53     voicePtr->RegisterListenSwitchState();
54     voicePtr->PublishCommonEvent(true, INCOMING);
55     voicePtr->DisconnectAbility();
56     voicePtr->UnRegisterListenSwitchState();
57     voicePtr->PublishCommonEvent(false, DIALING);
58     voicePtr->OnStopService();
59     voicePtr->Release();
60 }
61 
SendRequestFunc(const uint8_t *data, size_t size)62 void SendRequestFunc(const uint8_t *data, size_t size)
63 {
64     if (!IsServiceInited()) {
65         return;
66     }
67 
68     std::string value = "";
69     ContactInfo contactInfo;
70     sptr<IRemoteObject> remoteObject = sptr<VoiceAssistantConnectCallback>::MakeSptr(ACCOUNT_ID);
71     std::shared_ptr<CallVoiceAssistantManager> voicePtr = CallVoiceAssistantManager::GetInstance();
72     auto infoptr = voicePtr->GetContactInfo(ACCOUNT_ID);
73     voicePtr->UpdateNumberLocation(value, ACCOUNT_ID);
74     voicePtr->UpdateContactInfo(contactInfo, ACCOUNT_ID);
75     voicePtr->UpdateRemoteObject(remoteObject, ACCOUNT_ID);
76     voicePtr->OnStopService();
77     voicePtr->Initial();
78     voicePtr->QueryValue(CONTROL_SWITCH, value);
79     voicePtr->IsStartVoiceBroadcast();
80     voicePtr->IsSwitchOn(CONTROL_SWITCH);
81     voicePtr->OnStartService(DIALING, ACCOUNT_ID);
82     voicePtr->MuteRinger();
83     voicePtr->UpdateReplyData(value);
84     voicePtr->GetSendString(infoptr);
85     voicePtr->SendRequest(infoptr, true);
86     voicePtr->OnStopService();
87     voicePtr->Release();
88 }
89 
UpdateValueFunc(const uint8_t *data, size_t size)90 void UpdateValueFunc(const uint8_t *data, size_t size)
91 {
92     if (!IsServiceInited()) {
93         return;
94     }
95 
96     AppExecFwk::ElementName element;
97     sptr<AAFwk::IAbilityConnection> connectCallback = sptr<VoiceAssistantConnectCallback>::MakeSptr(ACCOUNT_ID);
98     sptr<AAFwk::IDataAbilityObserver> settingsCallback = sptr<VoiceAssistantSwitchObserver>::MakeSptr();
99     std::shared_ptr<CallVoiceAssistantManager> voicePtr = CallVoiceAssistantManager::GetInstance();
100     sptr<IRemoteObject> remoteObject = sptr<VoiceAssistantConnectCallback>::MakeSptr(CALL_STATE_NUM);
101     std::string event = "test.test.test.test";
102     EventFwk::MatchingSkills matchingSkills;
103     matchingSkills.AddEvent(event);
104     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
105     auto subscriber = std::make_shared<VoiceAssistantRingSubscriber>(subscribeInfo);
106     EventFwk::CommonEventData eventData;
107     AAFwk::Want want;
108     want.SetAction(event);
109     want.SetParam(event, event);
110     eventData.SetWant(want);
111     settingsCallback->OnChange();
112     connectCallback->OnAbilityConnectDone(element, remoteObject, ACCOUNT_ID);
113     connectCallback->OnAbilityDisconnectDone(element, ACCOUNT_ID);
114     subscriber->OnReceiveEvent(eventData);
115     subscriber->Initial();
116     subscriber->Release();
117     voicePtr->SetIsControlSwitchOn(true);
118     voicePtr->GetIsControlSwitchOn();
119     voicePtr->GetIsPlayRing();
120     voicePtr->UpdateVoipCallState(ACCOUNT_ID);
121 }
122 
DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)123 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
124 {
125     if (data == nullptr || size == 0) {
126         return;
127     }
128 
129     ListenCallStateFunc(data, size);
130     SendRequestFunc(data, size);
131     UpdateValueFunc(data, size);
132 }
133 } // namespace OHOS
134 
135 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)136 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
137 {
138     OHOS::AddCallTokenFuzzer token;
139     /* Run your code on data */
140     OHOS::DoSomethingInterestingWithMyAPI(data, size);
141     return 0;
142 }
143