• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->OnStopService();
76     voicePtr->Initial();
77     voicePtr->QueryValue(CONTROL_SWITCH, value);
78     voicePtr->IsStartVoiceBroadcast();
79     voicePtr->IsSwitchOn(CONTROL_SWITCH);
80     voicePtr->OnStartService(DIALING, ACCOUNT_ID);
81     voicePtr->MuteRinger();
82     voicePtr->UpdateReplyData(value);
83     voicePtr->GetSendString(infoptr);
84     voicePtr->SendRequest(infoptr, true);
85     voicePtr->OnStopService();
86     voicePtr->Release();
87 }
88 
UpdateValueFunc(const uint8_t * data,size_t size)89 void UpdateValueFunc(const uint8_t *data, size_t size)
90 {
91     if (!IsServiceInited()) {
92         return;
93     }
94 
95     AppExecFwk::ElementName element;
96     sptr<AAFwk::IAbilityConnection> connectCallback = sptr<VoiceAssistantConnectCallback>::MakeSptr(ACCOUNT_ID);
97     sptr<AAFwk::IDataAbilityObserver> settingsCallback = sptr<VoiceAssistantSwitchObserver>::MakeSptr();
98     std::shared_ptr<CallVoiceAssistantManager> voicePtr = CallVoiceAssistantManager::GetInstance();
99     sptr<IRemoteObject> remoteObject = sptr<VoiceAssistantConnectCallback>::MakeSptr(CALL_STATE_NUM);
100     std::string event = "test.test.test.test";
101     EventFwk::MatchingSkills matchingSkills;
102     matchingSkills.AddEvent(event);
103     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
104     auto subscriber = std::make_shared<VoiceAssistantRingSubscriber>(subscribeInfo);
105     EventFwk::CommonEventData eventData;
106     AAFwk::Want want;
107     want.SetAction(event);
108     want.SetParam(event, event);
109     eventData.SetWant(want);
110     settingsCallback->OnChange();
111     connectCallback->OnAbilityConnectDone(element, remoteObject, ACCOUNT_ID);
112     connectCallback->OnAbilityDisconnectDone(element, ACCOUNT_ID);
113     subscriber->OnReceiveEvent(eventData);
114     subscriber->Initial();
115     subscriber->Release();
116     voicePtr->SetIsControlSwitchOn(true);
117     voicePtr->GetIsControlSwitchOn();
118     voicePtr->GetIsPlayRing();
119     voicePtr->UpdateVoipCallState(ACCOUNT_ID);
120 }
121 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)122 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
123 {
124     if (data == nullptr || size == 0) {
125         return;
126     }
127 
128     ListenCallStateFunc(data, size);
129     SendRequestFunc(data, size);
130     UpdateValueFunc(data, size);
131 }
132 } // namespace OHOS
133 
134 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)135 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
136 {
137     OHOS::AddCallTokenFuzzer token;
138     /* Run your code on data */
139     OHOS::DoSomethingInterestingWithMyAPI(data, size);
140     return 0;
141 }
142