1 /* 2 * Copyright (C) 2022 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 <gtest/gtest.h> 16 #include <gmock/gmock.h> 17 #include <cstddef> 18 #include <cstdint> 19 #include "securec.h" 20 #include "kits/c/wifi_device.h" 21 #include "wifi_logger.h" 22 #include "mock_wifi_c_device.h" 23 24 using ::testing::_; 25 using ::testing::AtLeast; 26 using ::testing::DoAll; 27 using ::testing::Eq; 28 using ::testing::Ref; 29 using ::testing::Return; 30 using ::testing::SetArgReferee; 31 using ::testing::StrEq; 32 using ::testing::TypedEq; 33 using ::testing::ext::TestSize; 34 DEFINE_WIFILOG_LABEL("WifiCDeviceStubTest"); 35 36 namespace OHOS { 37 namespace Wifi { 38 39 constexpr int NETWORK_ID = 15; 40 constexpr int FREQUENCY = 2437; 41 constexpr int TIMESTAMP = -750366468; 42 constexpr int RSSI = 2; 43 constexpr int TYPE_OPEN = 0; 44 constexpr unsigned char BSSID[WIFI_MAC_LEN] = "test1"; 45 constexpr int BAND = 2; 46 constexpr int ZERO = 0; 47 48 class WifiCDeviceTest : public testing::Test { 49 public: SetUpTestCase()50 static void SetUpTestCase() {} TearDownTestCase()51 static void TearDownTestCase() {} SetUp()52 virtual void SetUp() {} TearDown()53 virtual void TearDown() {} 54 EnableWifiSuccess()55 void EnableWifiSuccess() 56 { 57 EXPECT_FALSE(EnableWifi() == WIFI_SUCCESS); 58 } 59 DisableWifiSuccess()60 void DisableWifiSuccess() 61 { 62 EXPECT_FALSE(DisableWifi() == WIFI_SUCCESS); 63 } 64 EnableSemiWifiSuccess()65 void EnableSemiWifiSuccess() 66 { 67 EXPECT_FALSE(EnableSemiWifi() == WIFI_SUCCESS); 68 } 69 IsWifiActiveEnable()70 void IsWifiActiveEnable() 71 { 72 EXPECT_FALSE(IsWifiActive() == true); 73 } 74 ScanSuccess()75 void ScanSuccess() 76 { 77 EXPECT_FALSE(Scan() == WIFI_SUCCESS); 78 } 79 GetScanInfoListSucess()80 void GetScanInfoListSucess() 81 { 82 WifiScanInfo result; 83 if (strcpy_s(result.ssid, sizeof(result.ssid), "networkId") != EOK) { 84 return; 85 } 86 87 if (memcpy_s(result.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 88 return; 89 } 90 result.securityType = TYPE_OPEN; 91 result.rssi = RSSI; 92 result.frequency = FREQUENCY; 93 result.timestamp = TIMESTAMP; 94 unsigned int mSize = 0; 95 EXPECT_TRUE(GetScanInfoList(&result, &mSize) != WIFI_SUCCESS); 96 } 97 GetWifiDetailStateSucess()98 void GetWifiDetailStateSucess() 99 { 100 WifiDetailState state; 101 EXPECT_TRUE(GetWifiDetailState(&state) != WIFI_SUCCESS); 102 } 103 GetScanInfoListFail()104 void GetScanInfoListFail() 105 { 106 WifiScanInfo* result = nullptr; 107 unsigned int mSize = 0; 108 EXPECT_TRUE(GetScanInfoList(result, &mSize) != WIFI_SUCCESS); 109 } 110 AddDeviceConfigSuccess()111 void AddDeviceConfigSuccess() 112 { 113 int result = 0; 114 WifiDeviceConfig config; 115 if (strcpy_s(config.ssid, sizeof(config.ssid), "networkId") != EOK) { 116 return; 117 } 118 119 if (memcpy_s(config.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 120 return; 121 } 122 123 if (strcpy_s(config.preSharedKey, sizeof(config.preSharedKey), "12345678") != EOK) { 124 return; 125 } 126 config.securityType = TYPE_OPEN; 127 config.netId = NETWORK_ID; 128 config.freq = FREQUENCY; 129 EXPECT_TRUE(AddDeviceConfig(&config, &result) != WIFI_SUCCESS); 130 } 131 AddDeviceConfigFail2()132 void AddDeviceConfigFail2() 133 { 134 WIFI_LOGI("AddDeviceConfigFail2 enter"); 135 int result = 0; 136 WifiDeviceConfig config; 137 if (strcpy_s(config.ssid, sizeof(config.ssid), "1networkId1networkId1networkId12") != EOK) { 138 WIFI_LOGE("AddDeviceConfigFail2 strcpy_s ssid Fail!"); 139 return; 140 } 141 142 if (memcpy_s(config.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 143 WIFI_LOGE("AddDeviceConfigFail2 memcpy_s bssid Fail!"); 144 return; 145 } 146 147 if (strcpy_s(config.preSharedKey, sizeof(config.preSharedKey), "12345678") != EOK) { 148 WIFI_LOGE("AddDeviceConfigFail2 strcpy_s preSharedKey Fail!"); 149 return; 150 } 151 config.securityType = TYPE_OPEN; 152 config.netId = NETWORK_ID; 153 config.freq = FREQUENCY; 154 EXPECT_TRUE(AddDeviceConfig(&config, &result) != WIFI_SUCCESS); 155 } 156 AddDeviceConfigFail3()157 void AddDeviceConfigFail3() 158 { 159 WIFI_LOGI("AddDeviceConfigFail3 enter"); 160 int result = 0; 161 WifiDeviceConfig config; 162 if (strcpy_s(config.ssid, sizeof(config.ssid), "networkId") != EOK) { 163 WIFI_LOGE("AddDeviceConfigFail3 strcpy_s ssid Fail!"); 164 return; 165 } 166 167 if (memcpy_s(config.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 168 WIFI_LOGE("AddDeviceConfigFail3 memcpy_s bssid Fail!"); 169 return; 170 } 171 172 if (strcpy_s(config.preSharedKey, sizeof(config.preSharedKey), 173 "1234567892123456789212345678921234567892123456789212345678921234") != EOK) { 174 WIFI_LOGE("AddDeviceConfigFail3 strcpy_s preSharedKey Fail!"); 175 return; 176 } 177 config.securityType = TYPE_OPEN; 178 config.netId = NETWORK_ID; 179 config.freq = FREQUENCY; 180 EXPECT_TRUE(AddDeviceConfig(&config, &result) != WIFI_SUCCESS); 181 } 182 GetDeviceConfigsSuccess()183 void GetDeviceConfigsSuccess() 184 { 185 unsigned int mSize = 0; 186 WifiDeviceConfig result; 187 if (strcpy_s(result.ssid, sizeof(result.ssid), "networkId") != EOK) { 188 return; 189 } 190 191 if (memcpy_s(result.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 192 return; 193 } 194 195 if (strcpy_s(result.preSharedKey, sizeof(result.preSharedKey), "12345678") != EOK) { 196 return; 197 } 198 result.securityType = TYPE_OPEN; 199 result.netId = NETWORK_ID; 200 result.freq = FREQUENCY; 201 EXPECT_TRUE(GetDeviceConfigs(&result, &mSize) != WIFI_SUCCESS); 202 } 203 GetDeviceConfigsFail()204 void GetDeviceConfigsFail() 205 { 206 WIFI_LOGI("GetDeviceConfigsFail enter"); 207 unsigned int mSize = 0; 208 WifiDeviceConfig result; 209 if (strcpy_s(result.ssid, sizeof(result.ssid), "networkId") != EOK) { 210 WIFI_LOGE("GetDeviceConfigsFail strcpy_s ssid Fail!"); 211 return; 212 } 213 214 if (memcpy_s(result.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 215 WIFI_LOGE("GetDeviceConfigsFail memcpy_s bssid Fail!"); 216 return; 217 } 218 219 if (strcpy_s(result.preSharedKey, sizeof(result.preSharedKey), "12345678") != EOK) { 220 WIFI_LOGE("GetDeviceConfigsFail strcpy_s preSharedKey Fail!"); 221 return; 222 } 223 result.securityType = TYPE_OPEN; 224 result.netId = NETWORK_ID; 225 result.freq = FREQUENCY; 226 EXPECT_CALL(WifiCDevice::GetInstance(), GetDeviceConfigs(_, _)).WillRepeatedly(Return(WIFI_OPT_SUCCESS)); 227 EXPECT_TRUE(GetDeviceConfigs(&result, &mSize) != WIFI_SUCCESS); 228 } 229 RemoveDeviceSuccess()230 void RemoveDeviceSuccess() 231 { 232 int networkId = NETWORK_ID; 233 EXPECT_TRUE(RemoveDevice(networkId) != WIFI_SUCCESS); 234 } 235 DisableDeviceConfigSuccess()236 void DisableDeviceConfigSuccess() 237 { 238 int networkId = NETWORK_ID; 239 EXPECT_TRUE(DisableDeviceConfig(networkId) != WIFI_SUCCESS); 240 } 241 EnableDeviceConfigSuccess()242 void EnableDeviceConfigSuccess() 243 { 244 int networkId = NETWORK_ID; 245 EXPECT_TRUE(EnableDeviceConfig(networkId) != WIFI_SUCCESS); 246 } 247 ConnectToSuccess()248 void ConnectToSuccess() 249 { 250 int networkId = NETWORK_ID; 251 EXPECT_TRUE(ConnectTo(networkId) != WIFI_SUCCESS); 252 } 253 ConnectToDeviceSuccess()254 void ConnectToDeviceSuccess() 255 { 256 WifiDeviceConfig config; 257 if (strcpy_s(config.ssid, sizeof(config.ssid), "networkId") != EOK) { 258 return; 259 } 260 261 if (memcpy_s(config.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 262 return; 263 } 264 265 if (strcpy_s(config.preSharedKey, sizeof(config.preSharedKey), "12345678") != EOK) { 266 return; 267 } 268 config.netId = NETWORK_ID; 269 config.freq = FREQUENCY; 270 EXPECT_TRUE(ConnectToDevice(&config) != WIFI_SUCCESS); 271 } 272 ConnectToDeviceFail()273 void ConnectToDeviceFail() 274 { 275 WIFI_LOGI("ConnectToDeviceFail enter"); 276 WifiDeviceConfig config; 277 if (strcpy_s(config.ssid, sizeof(config.ssid), "1networkId1networkId1networkId12") != EOK) { 278 WIFI_LOGE("ConnectToDeviceFail strcpy_s ssid Fail!"); 279 return; 280 } 281 282 if (memcpy_s(config.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 283 WIFI_LOGE("ConnectToDeviceFail memcpy_s bssid Fail!"); 284 return; 285 } 286 287 if (strcpy_s(config.preSharedKey, sizeof(config.preSharedKey), "12345678") != EOK) { 288 WIFI_LOGE("ConnectToDeviceFail strcpy_s preSharedKey Fail!"); 289 return; 290 } 291 config.netId = NETWORK_ID; 292 config.freq = FREQUENCY; 293 EXPECT_TRUE(ConnectToDevice(&config) != WIFI_SUCCESS); 294 } 295 DisconnectSuccess()296 void DisconnectSuccess() 297 { 298 EXPECT_TRUE(Disconnect() != WIFI_SUCCESS); 299 } 300 GetLinkedInfoSuccess()301 void GetLinkedInfoSuccess() 302 { 303 WifiLinkedInfo result; 304 if (strcpy_s(result.ssid, sizeof(result.ssid), "networkId") != EOK) { 305 return; 306 } 307 308 if (memcpy_s(result.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 309 return; 310 } 311 result.frequency = FREQUENCY; 312 result.connState = WIFI_CONNECTED; 313 EXPECT_TRUE(GetLinkedInfo(&result) != WIFI_SUCCESS); 314 } 315 GetDeviceMacAddressSuccess()316 void GetDeviceMacAddressSuccess() 317 { 318 unsigned char result[WIFI_MAC_LEN] = {0}; 319 EXPECT_TRUE(GetDeviceMacAddress(result) != WIFI_SUCCESS); 320 } 321 AdvanceScanSuccess()322 void AdvanceScanSuccess() 323 { 324 WifiScanParams params; 325 if (strcpy_s(params.ssid, sizeof(params.ssid), "networkId") != EOK) { 326 return; 327 } 328 329 if (memcpy_s(params.bssid, WIFI_MAC_LEN, BSSID, WIFI_MAC_LEN - 1) != EOK) { 330 return; 331 } 332 params.scanType = WIFI_FREQ_SCAN; 333 params.freqs = FREQUENCY; 334 params.band = BAND; 335 params.ssidLen = strlen(params.ssid); 336 EXPECT_TRUE(AdvanceScan(¶ms) != WIFI_SUCCESS); 337 } 338 GetIpInfoSuccess()339 void GetIpInfoSuccess() 340 { 341 IpInfo info; 342 EXPECT_TRUE(GetIpInfo(&info) != WIFI_SUCCESS); 343 } 344 GetSignalLevelSuccess()345 void GetSignalLevelSuccess() 346 { 347 int rssi = RSSI; 348 int band = BAND; 349 EXPECT_EQ(GetSignalLevel(rssi, band), -1); 350 } 351 SetLowLatencyModeTest()352 void SetLowLatencyModeTest() 353 { 354 EXPECT_TRUE(SetLowLatencyMode(true) == WIFI_SUCCESS); 355 EXPECT_TRUE(SetLowLatencyMode(false) == WIFI_SUCCESS); 356 } 357 IsBandTypeSupportedTest()358 void IsBandTypeSupportedTest() 359 { 360 bool supported = false; 361 EXPECT_TRUE(IsBandTypeSupported(BAND, &supported) != WIFI_SUCCESS); 362 } 363 Get5GHzChannelListTest()364 void Get5GHzChannelListTest() 365 { 366 int result = ZERO; 367 int size = ZERO; 368 EXPECT_TRUE(Get5GHzChannelList(&result, &size) != WIFI_SUCCESS); 369 } 370 StartPortalCertificationTest()371 void StartPortalCertificationTest() 372 { 373 EXPECT_TRUE(StartPortalCertification() != WIFI_SUCCESS); 374 } 375 }; 376 377 HWTEST_F(WifiCDeviceTest, EnableWifiSuccess, TestSize.Level1) 378 { 379 EnableWifiSuccess(); 380 } 381 382 HWTEST_F(WifiCDeviceTest, DisableWifiSuccess, TestSize.Level1) 383 { 384 DisableWifiSuccess(); 385 } 386 387 HWTEST_F(WifiCDeviceTest, EnableSemiWifiSuccess, TestSize.Level1) 388 { 389 EnableSemiWifiSuccess(); 390 } 391 392 HWTEST_F(WifiCDeviceTest, IsWifiActiveEnable, TestSize.Level1) 393 { 394 IsWifiActiveEnable(); 395 } 396 397 HWTEST_F(WifiCDeviceTest, ScanSuccess, TestSize.Level1) 398 { 399 ScanSuccess(); 400 } 401 402 HWTEST_F(WifiCDeviceTest, GetScanInfoListSucess, TestSize.Level1) 403 { 404 GetScanInfoListSucess(); 405 } 406 407 HWTEST_F(WifiCDeviceTest, GetWifiDetailStateSucess, TestSize.Level1) 408 { 409 GetWifiDetailStateSucess(); 410 } 411 412 HWTEST_F(WifiCDeviceTest, GetScanInfoListFail, TestSize.Level1) 413 { 414 GetScanInfoListFail(); 415 } 416 417 HWTEST_F(WifiCDeviceTest, AddDeviceConfigSuccess, TestSize.Level1) 418 { 419 AddDeviceConfigSuccess(); 420 } 421 422 HWTEST_F(WifiCDeviceTest, AddDeviceConfigFail2, TestSize.Level1) 423 { 424 AddDeviceConfigFail2(); 425 } 426 427 HWTEST_F(WifiCDeviceTest, AddDeviceConfigFail3, TestSize.Level1) 428 { 429 AddDeviceConfigFail3(); 430 } 431 432 HWTEST_F(WifiCDeviceTest, GetDeviceConfigsSuccess, TestSize.Level1) 433 { 434 GetDeviceConfigsSuccess(); 435 } 436 437 HWTEST_F(WifiCDeviceTest, GetDeviceConfigsFail, TestSize.Level1) 438 { 439 GetDeviceConfigsFail(); 440 } 441 442 HWTEST_F(WifiCDeviceTest, RemoveDeviceSuccess, TestSize.Level1) 443 { 444 RemoveDeviceSuccess(); 445 } 446 447 HWTEST_F(WifiCDeviceTest, DisableDeviceConfigSuccess, TestSize.Level1) 448 { 449 DisableDeviceConfigSuccess(); 450 } 451 452 HWTEST_F(WifiCDeviceTest, EnableDeviceConfigSuccess, TestSize.Level1) 453 { 454 EnableDeviceConfigSuccess(); 455 } 456 457 HWTEST_F(WifiCDeviceTest, ConnectToSuccess, TestSize.Level1) 458 { 459 ConnectToSuccess(); 460 } 461 462 HWTEST_F(WifiCDeviceTest, ConnectToDeviceSuccess, TestSize.Level1) 463 { 464 ConnectToDeviceSuccess(); 465 } 466 467 HWTEST_F(WifiCDeviceTest, ConnectToDeviceFail, TestSize.Level1) 468 { 469 ConnectToDeviceFail(); 470 } 471 472 HWTEST_F(WifiCDeviceTest, DisconnectSuccess, TestSize.Level1) 473 { 474 DisconnectSuccess(); 475 } 476 477 HWTEST_F(WifiCDeviceTest, GetLinkedInfoSuccess, TestSize.Level1) 478 { 479 GetLinkedInfoSuccess(); 480 } 481 482 HWTEST_F(WifiCDeviceTest, GetDeviceMacAddressSuccess, TestSize.Level1) 483 { 484 GetDeviceMacAddressSuccess(); 485 } 486 487 HWTEST_F(WifiCDeviceTest, AdvanceScanSuccess, TestSize.Level1) 488 { 489 AdvanceScanSuccess(); 490 } 491 492 HWTEST_F(WifiCDeviceTest, GetIpInfoSuccess, TestSize.Level1) 493 { 494 GetIpInfoSuccess(); 495 } 496 497 HWTEST_F(WifiCDeviceTest, GetSignalLevelSuccess, TestSize.Level1) 498 { 499 GetSignalLevelSuccess(); 500 } 501 502 HWTEST_F(WifiCDeviceTest, SetLowLatencyModeTest, TestSize.Level1) 503 { 504 SetLowLatencyModeTest(); 505 } 506 507 HWTEST_F(WifiCDeviceTest, IsBandTypeSupportedTest, TestSize.Level1) 508 { 509 IsBandTypeSupportedTest(); 510 } 511 512 HWTEST_F(WifiCDeviceTest, Get5GHzChannelListTest, TestSize.Level1) 513 { 514 Get5GHzChannelListTest(); 515 } 516 } // namespace Wifi 517 } // namespace OHOS