• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "enroll_engine_test.h"
16 
17 #include "intell_voice_log.h"
18 #include "enroll_intell_voice_engine.h"
19 
20 #define LOG_TAG "TestEnrollEngine"
21 
22 using namespace OHOS::IntellVoice;
23 using namespace OHOS::IntellVoiceEngine;
24 using OHOS::IntellVoiceEngine::EvaluationResult;
25 namespace OHOS {
26 namespace IntellVoiceFuzzTest {
27 
28 constexpr int INTELL_ENROLL_TEST_RANDOM_NUM = 11;
29 
30 static EnrollIntellVoiceEngine *g_enrollEngine = new EnrollIntellVoiceEngine({"小艺小艺"});
31 
32 class TestEnrollEngineEventCallback : public IIntellVoiceEngineEventCallback {
33 private:
34     void OnEvent(const IntellVoiceEngineCallBackEvent &param) override;
35 };
36 
OnEvent(const IntellVoiceEngineCallBackEvent & param)37 void TestEnrollEngineEventCallback::OnEvent(const IntellVoiceEngineCallBackEvent &param)
38 {}
39 
TestInit(const uint8_t * data,size_t sizeIn)40 static void TestInit(const uint8_t *data, size_t sizeIn)
41 {
42     INTELL_VOICE_LOG_INFO("EnrollEngine test Init start");
43     EngineConfig config = {"zh", "CN"};
44     int32_t ret = g_enrollEngine->Init(config);
45     INTELL_VOICE_LOG_INFO("EnrollEngine test Init end, result:%{public}d", ret);
46 }
47 
TestStart(const uint8_t * data,size_t sizeIn)48 static void TestStart(const uint8_t *data, size_t sizeIn)
49 {
50     INTELL_VOICE_LOG_INFO("EnrollEngine test Start start");
51     int32_t ret = g_enrollEngine->Start(false);
52     INTELL_VOICE_LOG_INFO("EnrollEngine test Start end, result:%{public}d", ret);
53 }
54 
TestStop(const uint8_t * data,size_t sizeIn)55 static void TestStop(const uint8_t *data, size_t sizeIn)
56 {
57     INTELL_VOICE_LOG_INFO("EnrollEngine test Stop start");
58     int32_t ret = g_enrollEngine->Stop();
59     INTELL_VOICE_LOG_INFO("EnrollEngine test Stop end, result:%{public}d", ret);
60 }
61 
TestCommit(const uint8_t * data,size_t sizeIn)62 static void TestCommit(const uint8_t *data, size_t sizeIn)
63 {
64     INTELL_VOICE_LOG_INFO("EnrollEngine test Commit start");
65     int32_t ret = g_enrollEngine->Commit();
66     INTELL_VOICE_LOG_INFO("EnrollEngine test Commit end, result:%{public}d", ret);
67 }
68 
TestSetSensibility(const uint8_t * data,size_t sizeIn)69 static void TestSetSensibility(const uint8_t *data, size_t sizeIn)
70 {
71     INTELL_VOICE_LOG_INFO("EnrollEngine test SetSensibility start");
72     int32_t ret = g_enrollEngine->SetSensibility(static_cast<int32_t>(*data));
73     INTELL_VOICE_LOG_INFO("EnrollEngine test SetSensibility end, result:%{public}d", ret);
74 }
75 
TestSetWakeupHapInfo(const uint8_t * data,size_t sizeIn)76 static void TestSetWakeupHapInfo(const uint8_t *data, size_t sizeIn)
77 {
78     INTELL_VOICE_LOG_INFO("EnrollEngine test SetSensibility start");
79     WakeupHapInfo config = {"com.huawei.hmos.aibase", "WakeUpExtAbility"};
80     int32_t ret = g_enrollEngine->SetWakeupHapInfo(config);
81     INTELL_VOICE_LOG_INFO("EnrollEngine test SetSensibility end, result:%{public}d", ret);
82 }
83 
TestSetParameter(const uint8_t * data,size_t sizeIn)84 static void TestSetParameter(const uint8_t *data, size_t sizeIn)
85 {
86     INTELL_VOICE_LOG_INFO("EnrollEngine test SetParameter start");
87     std::string key = "key";
88     std::string value = "123456";
89     int32_t ret = g_enrollEngine->SetParameter(key, value);
90     INTELL_VOICE_LOG_INFO("EnrollEngine test SetParameter end, result:%{public}d", ret);
91 }
92 
TestGetParameter(const uint8_t * data,size_t sizeIn)93 static void TestGetParameter(const uint8_t *data, size_t sizeIn)
94 {
95     INTELL_VOICE_LOG_INFO("EnrollEngine test GetParameter start");
96     std::string key = "key";
97     std::string value = g_enrollEngine->GetParameter(key);
98     INTELL_VOICE_LOG_INFO("EnrollEngine test GetParameter end, result:%{public}s", value.c_str());
99 }
100 
TestRelease(const uint8_t * data,size_t sizeIn)101 static void TestRelease(const uint8_t *data, size_t sizeIn)
102 {
103     INTELL_VOICE_LOG_INFO("EnrollEngine test Release start");
104     int32_t ret = g_enrollEngine->Release();
105     INTELL_VOICE_LOG_INFO("EnrollEngine test Release end, result:%{public}d", ret);
106 }
107 
TestSetCallback(const uint8_t * data,size_t sizeIn)108 static void TestSetCallback(const uint8_t *data, size_t sizeIn)
109 {
110     INTELL_VOICE_LOG_INFO("EnrollEngine test SetCallback start");
111     std::shared_ptr<TestEnrollEngineEventCallback> cb = std::make_shared<TestEnrollEngineEventCallback>();
112     int32_t ret = g_enrollEngine->SetCallback(cb);
113     INTELL_VOICE_LOG_INFO("EnrollEngine test SetCallback end, result:%{public}d", ret);
114 }
115 
TestEvaluate(const uint8_t * data,size_t sizeIn)116 static void TestEvaluate(const uint8_t *data, size_t sizeIn)
117 {
118     INTELL_VOICE_LOG_INFO("EnrollEngine test Evaluate start");
119     EvaluationResult infos;
120     int32_t ret = g_enrollEngine->Evaluate(std::to_string(sizeIn), infos);
121     INTELL_VOICE_LOG_INFO("EnrollEngine test Evaluate end, result:%{public}d", ret);
122 }
123 
124 namespace {
125 void (*g_enrollFuncTable[INTELL_ENROLL_TEST_RANDOM_NUM])(const uint8_t *, size_t) = {TestInit,
126     TestStart,
127     TestStop,
128     TestCommit,
129     TestSetSensibility,
130     TestSetWakeupHapInfo,
131     TestSetParameter,
132     TestGetParameter,
133     TestRelease,
134     TestSetCallback,
135     TestEvaluate};
136 }
TestEnrollEngineRandomFuzzer(const uint8_t * data,size_t sizeIn)137 void TestEnrollEngineRandomFuzzer(const uint8_t *data, size_t sizeIn)
138 {
139     INTELL_VOICE_LOG_INFO("TestEnrollRandomFuzzer in");
140     if (data == nullptr) {
141         return;
142     }
143     uint8_t type = data[0] % INTELL_ENROLL_TEST_RANDOM_NUM;
144     INTELL_VOICE_LOG_INFO("function id: %{public}d", type);
145     (*g_enrollFuncTable[type])(data, sizeIn);
146     return;
147 }
148 }  // namespace IntellVoiceFuzzTest
149 }  // namespace OHOS