1 /*
2 * Copyright (c) 2021 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 "wlan_hdi_service_stub.h"
17 #include "Iwifi_hal.h"
18 #include "wlan_hal_proxy.h"
19 #include <gtest/gtest.h>
20 #include <servmgr_hdi.h>
21
22 #define HDF_LOG_TAG service_manager_test
23 using namespace OHOS;
24 using namespace testing::ext;
25 using namespace OHOS::HDI::WLAN::V1_0;
26
27 namespace HdiTest {
28 const int32_t WLAN_FREQ_MAX_NUM = 14;
29 const int32_t WLAN_TX_POWER = 160;
30
31 constexpr const char *WLAN_SERVICE_NAME = "wlan_hal_service";
32 class WifiHdiHalServiceTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38 };
39
SetUpTestCase()40 void WifiHdiHalServiceTest::SetUpTestCase()
41 {
42 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
43 ASSERT_TRUE(wlanObj != nullptr);
44 int32_t rc = wlanObj->wifiConstruct();
45 ASSERT_EQ(rc, HDF_SUCCESS);
46 }
47
TearDownTestCase()48 void WifiHdiHalServiceTest::TearDownTestCase()
49 {
50 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
51 ASSERT_TRUE(wlanObj != nullptr);
52 int32_t rc = wlanObj->wifiDestruct();
53 ASSERT_EQ(rc, HDF_SUCCESS);
54 }
55
SetUp()56 void WifiHdiHalServiceTest::SetUp()
57 {
58 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
59 ASSERT_TRUE(wlanObj != nullptr);
60 int32_t rc = wlanObj->start();
61 ASSERT_EQ(rc, HDF_SUCCESS);
62 }
63
TearDown()64 void WifiHdiHalServiceTest::TearDown()
65 {
66 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
67 ASSERT_TRUE(wlanObj != nullptr);
68 int32_t rc = wlanObj->stop();
69 ASSERT_EQ(rc, HDF_SUCCESS);
70 }
71
72 /**
73 * @tc.name: GetSupportFeatureComboTest_001
74 * @tc.desc: Wifi hdi get support feature and combo function test
75 * @tc.type: FUNC
76 */
77 HWTEST_F(WifiHdiHalServiceTest, GetSupportFeatureComboTest_001, Function | MediumTest | Level1)
78 {
79 std::vector<uint8_t> supType;
80 std::vector<uint64_t> combo;
81
82 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
83 ASSERT_TRUE(wlanObj != nullptr);
84 int32_t rc = wlanObj->getSupportFeature(supType);
85 ASSERT_EQ(rc, HDF_SUCCESS);
86 rc = wlanObj->getSupportCombo(combo);
87 ASSERT_EQ(rc, HDF_ERR_NOT_SUPPORT);
88 }
89
90 /**
91 * @tc.name: CreateFeatureTest_002
92 * @tc.desc: Wifi hdi create feature function test
93 * @tc.type: FUNC
94 */
95 HWTEST_F(WifiHdiHalServiceTest, CreateFeatureTest_002, Function | MediumTest | Level1)
96 {
97 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
98 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
99
100 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
101 ASSERT_TRUE(wlanObj != nullptr);
102 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
103 ASSERT_EQ(rc, HDF_SUCCESS);
104 rc = wlanObj->destroyFeature(ifeature);
105 ASSERT_EQ(rc, HDF_SUCCESS);
106 }
107
108 /**
109 * @tc.name: GetFeatureByIfNameTest_003
110 * @tc.desc: Wifi hdi get feature by ifname function test
111 * @tc.type: FUNC
112 */
113 HWTEST_F(WifiHdiHalServiceTest, GetFeatureByIfNameTest_003, Function | MediumTest | Level1)
114 {
115 std::string ifName = "wlan0";
116 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
117 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
118
119 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
120 ASSERT_TRUE(wlanObj != nullptr);
121 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
122 ASSERT_EQ(rc, HDF_SUCCESS);
123 rc = wlanObj->getFeatureByIfName(ifName, ifeature);
124 ASSERT_EQ(rc, HDF_SUCCESS);
125 rc = wlanObj->destroyFeature(ifeature);
126 ASSERT_EQ(rc, HDF_SUCCESS);
127 }
128
129 /**
130 * @tc.name: GetAsscociatedStasTest_004
131 * @tc.desc: Wifi hdi get assoc stas function test
132 * @tc.type: FUNC
133 */
134 HWTEST_F(WifiHdiHalServiceTest, GetAsscociatedStasTest_004, Function | MediumTest | Level1)
135 {
136 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
137 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
138 std::shared_ptr<StaInfo> staInfo = nullptr;
139 std::vector<uint32_t> num;
140 uint32_t count = 0;
141
142 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
143 ASSERT_TRUE(wlanObj != nullptr);
144 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
145 ASSERT_EQ(rc, HDF_SUCCESS);
146 rc = wlanObj->getAsscociatedStas(ifeature, staInfo, count, num);
147 ASSERT_EQ(rc, HDF_SUCCESS);
148 rc = wlanObj->destroyFeature(ifeature);
149 ASSERT_EQ(rc, HDF_SUCCESS);
150 }
151
152 /**
153 * @tc.name: SetCountryCodeTest_005
154 * @tc.desc: Wifi hdi set country code function test
155 * @tc.type: FUNC
156 */
157 HWTEST_F(WifiHdiHalServiceTest, SetCountryCodeTest_005, Function | MediumTest | Level1)
158 {
159 std::string code = "CN";
160 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
161 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
162
163 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
164 ASSERT_TRUE(wlanObj != nullptr);
165 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
166 ASSERT_EQ(rc, HDF_SUCCESS);
167 rc = wlanObj->setCountryCode(ifeature, code, sizeof(code));
168 ASSERT_EQ(rc, HDF_SUCCESS);
169 rc = wlanObj->destroyFeature(ifeature);
170 ASSERT_EQ(rc, HDF_SUCCESS);
171 }
172
173 /**
174 * @tc.name: GetNetworkIfaceNameTest_006
175 * @tc.desc: Wifi hdi get network interface name function test
176 * @tc.type: FUNC
177 */
178 HWTEST_F(WifiHdiHalServiceTest, GetNetworkIfaceNameTest_006, Function | MediumTest | Level1)
179 {
180 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
181 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
182
183 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
184 ASSERT_TRUE(wlanObj != nullptr);
185 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
186 ASSERT_EQ(rc, HDF_SUCCESS);
187 rc = wlanObj->getNetworkIfaceName(ifeature);
188 ASSERT_EQ(rc, HDF_SUCCESS);
189 rc = wlanObj->destroyFeature(ifeature);
190 ASSERT_EQ(rc, HDF_SUCCESS);
191 }
192
193 /**
194 * @tc.name: GetFeatureTypeTest_007
195 * @tc.desc: Wifi hdi get feature type function test
196 * @tc.type: FUNC
197 */
198 HWTEST_F(WifiHdiHalServiceTest, GetFeatureTypeTest_007, Function | MediumTest | Level1)
199 {
200 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
201 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
202
203 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
204 ASSERT_TRUE(wlanObj != nullptr);
205 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
206 ASSERT_EQ(rc, HDF_SUCCESS);
207 rc = wlanObj->getFeatureType(ifeature);
208 ASSERT_EQ(rc, HDF_SUCCESS);
209 rc = wlanObj->destroyFeature(ifeature);
210 ASSERT_EQ(rc, HDF_SUCCESS);
211 }
212
213 /**
214 * @tc.name: SetMacAddressTest_008
215 * @tc.desc: Wifi hdi set mac addr function test
216 * @tc.type: FUNC
217 */
218 HWTEST_F(WifiHdiHalServiceTest, SetMacAddressTest_008, Function | MediumTest | Level1)
219 {
220 std::vector<uint8_t> mac = {0x12, 0x34, 0x56, 0x78, 0xab, 0xcd};
221 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
222 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
223
224 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
225 ASSERT_TRUE(wlanObj != nullptr);
226 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
227 ASSERT_EQ(rc, HDF_SUCCESS);
228 rc = wlanObj->setMacAddress(ifeature, mac);
229 ASSERT_EQ(rc, HDF_SUCCESS);
230 rc = wlanObj->destroyFeature(ifeature);
231 ASSERT_EQ(rc, HDF_SUCCESS);
232 }
233
234 /**
235 * @tc.name: GetDeviceMacAddressTest_009
236 * @tc.desc: Wifi hdi get device mac addr function test
237 * @tc.type: FUNC
238 */
239 HWTEST_F(WifiHdiHalServiceTest, GetDeviceMacAddressTest_009, Function | MediumTest | Level1)
240 {
241 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
242 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
243 std::vector<uint8_t> mac = {0};
244
245 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
246 ASSERT_TRUE(wlanObj != nullptr);
247 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
248 ASSERT_EQ(rc, HDF_SUCCESS);
249 rc = wlanObj->getDeviceMacAddress(ifeature, mac, ETH_ADDR_LEN);
250 ASSERT_EQ(rc, HDF_SUCCESS);
251 rc = wlanObj->destroyFeature(ifeature);
252 ASSERT_EQ(rc, HDF_SUCCESS);
253 }
254
255 /**
256 * @tc.name: GetFreqsWithBandTest_010
257 * @tc.desc: Wifi hdi get freqs function test
258 * @tc.type: FUNC
259 */
260 HWTEST_F(WifiHdiHalServiceTest, GetFreqsWithBandTest_010, Function | MediumTest | Level1)
261 {
262 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
263 std::shared_ptr<WifiFeatureInfo> ifeature = nullptr;
264 std::vector<int32_t> freq = {0};
265 int32_t wlanBand = 0;
266 uint32_t count = 0;
267
268 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
269 ASSERT_TRUE(wlanObj != nullptr);
270 int32_t rc = wlanObj->createFeature(wlan_type, ifeature);
271 ASSERT_EQ(rc, HDF_SUCCESS);
272 rc = wlanObj->getFreqsWithBand(ifeature, wlanBand, freq, WLAN_FREQ_MAX_NUM, count);
273 ASSERT_EQ(rc, HDF_SUCCESS);
274 rc = wlanObj->destroyFeature(ifeature);
275 ASSERT_EQ(rc, HDF_SUCCESS);
276 }
277
278 /**
279 * @tc.name: SetTxPowerTest_011
280 * @tc.desc: Wifi hdi set tx power function test
281 * @tc.type: FUNC
282 */
283 HWTEST_F(WifiHdiHalServiceTest, SetTxPowerTest_011, Function | MediumTest | Level1)
284 {
285 int32_t wlan_type = PROTOCOL_80211_IFTYPE_AP;
286 std::shared_ptr<WifiFeatureInfo> feature = nullptr;
287 int32_t power = WLAN_TX_POWER;
288
289 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
290 ASSERT_TRUE(wlanObj != nullptr);
291 int32_t rc = wlanObj->createFeature(wlan_type, feature);
292 ASSERT_EQ(rc, HDF_SUCCESS);
293 rc = wlanObj->setTxPower(feature, power);
294 ASSERT_EQ(rc, HDF_SUCCESS);
295 rc = wlanObj->destroyFeature(feature);
296 ASSERT_EQ(rc, HDF_SUCCESS);
297 }
298
299 /**
300 * @tc.name: GetChipIdTest_012
301 * @tc.desc: Wifi hdi get chip id function test
302 * @tc.type: FUNC
303 */
304 HWTEST_F(WifiHdiHalServiceTest, GetChipIdTest_012, Function | MediumTest | Level1)
305 {
306 int32_t wlan_type = PROTOCOL_80211_IFTYPE_STATION;
307 std::shared_ptr<WifiFeatureInfo> feature = nullptr;
308 uint8_t chipId = 0;
309 unsigned int num = 0;
310 std::string ifName;
311
312 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
313 ASSERT_TRUE(wlanObj != nullptr);
314 int32_t rc = wlanObj->createFeature(wlan_type, feature);
315 ASSERT_EQ(rc, HDF_SUCCESS);
316 rc = wlanObj->getChipId(feature, chipId);
317 ASSERT_EQ(rc, HDF_SUCCESS);
318 rc = wlanObj->getIfNamesByChipId(chipId, ifName, num);
319 ASSERT_EQ(rc, HDF_SUCCESS);
320 rc = wlanObj->destroyFeature(feature);
321 ASSERT_EQ(rc, HDF_SUCCESS);
322 }
323
324 /**
325 * @tc.name: SetScanningMacAddressTest_013
326 * @tc.desc: Wifi hdi set scanning mac addr function test
327 * @tc.type: FUNC
328 */
329 HWTEST_F(WifiHdiHalServiceTest, SetScanningMacAddressTest_013, Function | MediumTest | Level1)
330 {
331 int32_t wlan_type = PROTOCOL_80211_IFTYPE_STATION;
332 std::shared_ptr<WifiFeatureInfo> feature = nullptr;
333 std::vector<uint8_t> scanMac = {0x12, 0x34, 0x56, 0x78, 0xab, 0xcd};
334
335 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
336 ASSERT_TRUE(wlanObj != nullptr);
337 int32_t rc = wlanObj->createFeature(wlan_type, feature);
338 ASSERT_EQ(rc, HDF_SUCCESS);
339 rc = wlanObj->setScanningMacAddress(feature, scanMac, ETH_ADDR_LEN);
340 ASSERT_EQ(rc, HDF_ERR_NOT_SUPPORT);
341 rc = wlanObj->destroyFeature(feature);
342 ASSERT_EQ(rc, HDF_SUCCESS);
343 }
344
345 static int32_t g_status = -1;
346
347 namespace {
HalResetCallback(int32_t event,struct HdfSBuf * reqData)348 int32_t HalResetCallback(int32_t event, struct HdfSBuf *reqData) {
349 HdfSbufReadInt32(reqData, &g_status);
350 printf("status is %d\n", g_status);
351 return HDF_SUCCESS;
352 }
353 }
354
355 /**
356 * @tc.name: RegisterEventCallbackTest_014
357 * @tc.desc: Wifi hdi reister event call back function test
358 * @tc.type: FUNC
359 */
360 HWTEST_F(WifiHdiHalServiceTest, RegisterEventCallbackTest_014, Function | MediumTest | Level1)
361 {
362 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
363 ASSERT_TRUE(wlanObj != nullptr);
364 int32_t rc = wlanObj->registerEventCallback(HalResetCallback);
365 ASSERT_EQ(rc, HDF_SUCCESS);
366 }
367
368 /**
369 * @tc.name: UnregisterEventCallbackTest_015
370 * @tc.desc: Wifi hdi unreister event call back function test
371 * @tc.type: FUNC
372 */
373 HWTEST_F(WifiHdiHalServiceTest, UnregisterEventCallbackTest_015, Function | MediumTest | Level1)
374 {
375 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
376 ASSERT_TRUE(wlanObj != nullptr);
377 int32_t rc = wlanObj->unregisterEventCallback();
378 ASSERT_EQ(rc, HDF_SUCCESS);
379 }
380
381 /**
382 * @tc.name: ResetDriverTest_016
383 * @tc.desc: Wifi hdi reset driver function test
384 * @tc.type: FUNC
385 */
386 HWTEST_F(WifiHdiHalServiceTest, ResetDriverTest_016, Function | MediumTest | Level1)
387 {
388 int32_t wlan_type = PROTOCOL_80211_IFTYPE_STATION;
389 std::shared_ptr<WifiFeatureInfo> feature = nullptr;
390 uint8_t chipId= 0;
391
392 auto wlanObj = IWlan::Get(WLAN_SERVICE_NAME);
393 ASSERT_TRUE(wlanObj != nullptr);
394 int32_t rc = wlanObj->registerEventCallback(HalResetCallback);
395 ASSERT_EQ(rc, HDF_SUCCESS);
396 rc = wlanObj->createFeature(wlan_type, feature);
397 ASSERT_EQ(rc, HDF_SUCCESS);
398 rc = wlanObj->getChipId(feature, chipId);
399 ASSERT_EQ(rc, HDF_SUCCESS);
400 rc = wlanObj->resetDriver(chipId);
401 ASSERT_EQ(rc, HDF_SUCCESS);
402 EXPECT_EQ(HDF_SUCCESS, g_status);
403 rc = wlanObj->unregisterEventCallback();
404 ASSERT_EQ(rc, HDF_SUCCESS);
405 rc = wlanObj->destroyFeature(feature);
406 ASSERT_EQ(rc, HDF_SUCCESS);
407 sleep(20);
408 }
409 };