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