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