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 #include <cstddef>
16 #include <cstdint>
17 
18 #include "intell_voice_manager.h"
19 #include "intell_voice_log.h"
20 #include "i_intell_voice_engine.h"
21 #include "i_intell_voice_engine_callback.h"
22 #include "enroll_intell_voice_engine.h"
23 #include "wakeup_intell_voice_engine.h"
24 
25 const int32_t LIMITSIZE = 4;
26 
27 using namespace std;
28 using namespace OHOS::IntellVoiceEngine;
29 using namespace OHOS::IntellVoice;
30 
31 namespace OHOS {
32 namespace IntellVoiceTests {
33 class EngineEventFuzzCallback : public OHOS::IntellVoiceEngine::IIntellVoiceEngineEventCallback {
34 public:
35     explicit EngineEventFuzzCallback() = default;
36     virtual ~EngineEventFuzzCallback() = default;
OnEvent(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent & param)37     virtual void OnEvent(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &param) {};
38 };
39 
IntellVoiceManagerFuzzTest(const uint8_t * data,size_t size)40 void IntellVoiceManagerFuzzTest(const uint8_t* data, size_t size)
41 {
42     if ((data == nullptr) || (size < LIMITSIZE)) {
43         return;
44     }
45     IntellVoiceEngineType type = *reinterpret_cast<const IntellVoiceEngineType *>(data);
46     sptr<IIntellVoiceEngine> engine;
47     IntellVoiceManager::GetInstance()->CreateIntellVoiceEngine(type, engine);
48     IntellVoiceManager::GetInstance()->ReleaseIntellVoiceEngine(type);
49 }
50 
EnrollEngineFuzzTest(const uint8_t * data,size_t size)51 void EnrollEngineFuzzTest(const uint8_t* data, size_t size)
52 {
53     if ((data == nullptr) || (size < LIMITSIZE)) {
54         return;
55     }
56     EnrollIntelligentVoiceEngineDescriptor descriptor = {};
57     auto enrollEngine = std::make_shared<EnrollIntellVoiceEngine>(descriptor);
58 
59     EngineConfig config = {"zh_CN", "zh_CN"};
60     enrollEngine->Init(config);
61 
62     bool isLast = false;
63     enrollEngine->Start(isLast);
64 
65     enrollEngine->Commit();
66 
67     int32_t sensibility = *reinterpret_cast<const int32_t *>(data);
68     enrollEngine->SetSensibility(sensibility);
69 
70     WakeupHapInfo info = {"com.aibase", "WakeUpExtAbility"};
71     enrollEngine->SetWakeupHapInfo(info);
72 
73     enrollEngine->SetParameter("key", "value");
74 
75     shared_ptr<EngineEventFuzzCallback> engineEventFuzzCallback =
76         std::make_shared<EngineEventFuzzCallback>();
77     enrollEngine->SetCallback(engineEventFuzzCallback);
78 
79     enrollEngine->Stop();
80 
81     enrollEngine->Release();
82 }
83 
WakeupEngineFuzzTest(const uint8_t * data,size_t size)84 void WakeupEngineFuzzTest(const uint8_t* data, size_t size)
85 {
86     if ((data == nullptr) || (size < LIMITSIZE)) {
87         return;
88     }
89 
90     WakeupIntelligentVoiceEngineDescriptor descriptor = {};
91     auto wakeEngine = std::make_shared<WakeupIntellVoiceEngine>(descriptor);
92 
93     int32_t sensibility = *reinterpret_cast<const int32_t *>(data);
94     wakeEngine->SetSensibility(sensibility);
95 
96     WakeupHapInfo info = {"com.aibase", "WakeUpExtAbility"};
97     wakeEngine->SetWakeupHapInfo(info);
98 
99     wakeEngine->SetParameter("key", "value");
100 
101     shared_ptr<EngineEventFuzzCallback> engineEventFuzzCallback =
102         std::make_shared<EngineEventFuzzCallback>();
103     wakeEngine->SetCallback(engineEventFuzzCallback);
104 
105     wakeEngine->Release();
106 }
107 } // namespace.OHOS
108 } // namespace.IntellVoiceTests
109 
110 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)111 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
112 {
113     /* Run your code on data */
114     OHOS::IntellVoiceTests::IntellVoiceManagerFuzzTest(data, size);
115     OHOS::IntellVoiceTests::EnrollEngineFuzzTest(data, size);
116     OHOS::IntellVoiceTests::WakeupEngineFuzzTest(data, size);
117     return 0;
118 }
119