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 16 #include <gtest/gtest.h> 17 18 #include "dhcp_logger.h" 19 #include "dhcp_client_service_impl.h" 20 #include "dhcp_client_state_machine.h" 21 #include "dhcp_define.h" 22 #include "securec.h" 23 24 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientServiceImplTest"); 25 26 using namespace testing::ext; 27 using namespace ::testing; 28 namespace OHOS { 29 namespace DHCP { 30 constexpr int ZERO = 0; 31 constexpr int ADDRESS_ARRAY_SIZE = 12; 32 class DhcpClientServiceImplTest : public testing::Test { 33 public: SetUpTestCase()34 static void SetUpTestCase() 35 {} TearDownTestCase()36 static void TearDownTestCase() 37 {} SetUp()38 virtual void SetUp() 39 { 40 dhcpClientImpl = std::make_unique<OHOS::DHCP::DhcpClientServiceImpl>(); 41 } TearDown()42 virtual void TearDown() 43 { 44 if (dhcpClientImpl != nullptr) { 45 dhcpClientImpl.reset(nullptr); 46 } 47 } 48 public: 49 std::unique_ptr<OHOS::DHCP::DhcpClientServiceImpl> dhcpClientImpl; 50 }; 51 52 HWTEST_F(DhcpClientServiceImplTest, IsNativeProcessTest, TestSize.Level1) 53 { 54 ASSERT_TRUE(dhcpClientImpl != nullptr); 55 DHCP_LOGE("enter IsNativeProcess fail Test"); 56 57 RouterConfig config; 58 config.ifname = "wlan0"; 59 config.bIpv6 = false; 60 config.prohibitUseCacheIp = false; 61 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StartDhcpClient(config)); 62 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StopDhcpClient(config.ifname, false)); 63 } 64 65 HWTEST_F(DhcpClientServiceImplTest, OnStartTest, TestSize.Level1) 66 { 67 DHCP_LOGE("enter OnStartTest"); 68 dhcpClientImpl->OnStart(); 69 EXPECT_EQ(DHCP_E_SUCCESS, ZERO); 70 } 71 72 HWTEST_F(DhcpClientServiceImplTest, OnStopTest, TestSize.Level1) 73 { 74 DHCP_LOGE("enter OnStopTest"); 75 dhcpClientImpl->OnStop(); 76 EXPECT_EQ(DHCP_E_SUCCESS, ZERO); 77 } 78 79 HWTEST_F(DhcpClientServiceImplTest, InitTest, TestSize.Level1) 80 { 81 DHCP_LOGE("enter InitTest"); 82 dhcpClientImpl->Init(); 83 EXPECT_EQ(DHCP_E_SUCCESS, ZERO); 84 } 85 86 HWTEST_F(DhcpClientServiceImplTest, StartOldClientTest, TestSize.Level1) 87 { 88 DHCP_LOGE("enter StartOldClientTest"); 89 ASSERT_TRUE(dhcpClientImpl != nullptr); 90 91 std::string ifname = "wlan0"; 92 bool bIpv6 = true; 93 DhcpClient client; 94 client.ifName = ifname; 95 client.isIpv6 = bIpv6; 96 RouterCfg config; 97 config.ifname = "wlan0"; 98 config.bIpv6 = true; 99 config.prohibitUseCacheIp = false; 100 EXPECT_EQ(DHCP_E_FAILED, dhcpClientImpl->StartOldClient(config, client)); 101 102 client.pStaStateMachine = new DhcpClientStateMachine(client.ifName); 103 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StartOldClient(config, client)); 104 105 bIpv6 = false; 106 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StartOldClient(config, client)); 107 } 108 109 HWTEST_F(DhcpClientServiceImplTest, StartNewClientTest, TestSize.Level1) 110 { 111 DHCP_LOGE("enter StartNewClientTest"); 112 ASSERT_TRUE(dhcpClientImpl != nullptr); 113 114 RouterCfg cfg; 115 cfg.ifname = ""; 116 cfg.bIpv6 = false; 117 cfg.prohibitUseCacheIp = false; 118 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StartNewClient(cfg)); 119 } 120 121 HWTEST_F(DhcpClientServiceImplTest, IsRemoteDiedTest, TestSize.Level1) 122 { 123 DHCP_LOGE("enter IsRemoteDiedTest"); 124 ASSERT_TRUE(dhcpClientImpl != nullptr); 125 126 EXPECT_EQ(true, dhcpClientImpl->IsRemoteDied()); 127 } 128 129 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultSuccessTest, TestSize.Level1) 130 { 131 DHCP_LOGE("enter DhcpIpv4ResultSuccessTest"); 132 ASSERT_TRUE(dhcpClientImpl != nullptr); 133 struct DhcpIpResult ipResult; 134 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultSuccess(ipResult)); 135 136 ipResult.code = PUBLISH_CODE_SUCCESS; 137 dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr)); 138 EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpIpv4ResultSuccess(ipResult)); 139 140 ipResult.code = PUBLISH_CODE_TIMEOUT; 141 dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr)); 142 EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpIpv4ResultSuccess(ipResult)); 143 144 ipResult.code = PUBLISH_CODE_FAILED; 145 dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr)); 146 EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpIpv4ResultSuccess(ipResult)); 147 dhcpClientImpl->m_mapClientCallBack.clear(); 148 } 149 150 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultFailTest, TestSize.Level1) 151 { 152 DHCP_LOGE("enter DhcpIpv4ResultFailTest"); 153 ASSERT_TRUE(dhcpClientImpl != nullptr); 154 struct DhcpIpResult ipResult; 155 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultFail(ipResult)); 156 157 dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr)); 158 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultFail(ipResult)); 159 dhcpClientImpl->m_mapClientCallBack.clear(); 160 161 DhcpClient client; 162 dhcpClientImpl->m_mapClientService.emplace(std::make_pair("wlan0", client)); 163 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultFail(ipResult)); 164 dhcpClientImpl->m_mapClientService.clear(); 165 } 166 167 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultFailTest1, TestSize.Level1) 168 { 169 DHCP_LOGE("enter DhcpIpv4ResultFailTest1"); 170 ASSERT_TRUE(dhcpClientImpl != nullptr); 171 DhcpIpResult ipResult; 172 ipResult.ifname = "wlan0"; 173 ipResult.uAddTime = 123456789; 174 ActionMode action = ACTION_RENEW_T1; 175 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultFail(ipResult)); 176 } 177 178 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultTimeOutTest, TestSize.Level1) 179 { 180 DHCP_LOGE("enter DhcpIpv4ResultTimeOutTest"); 181 ASSERT_TRUE(dhcpClientImpl != nullptr); 182 std::string ifname; 183 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname)); 184 ifname = "wlan0"; 185 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname)); 186 187 dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr)); 188 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname)); 189 dhcpClientImpl->m_mapClientCallBack.clear(); 190 191 DhcpClient client; 192 dhcpClientImpl->m_mapClientService.emplace(std::make_pair("wlan0", client)); 193 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname)); 194 dhcpClientImpl->m_mapClientService.clear(); 195 } 196 197 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultExpiredTest, TestSize.Level1) 198 { 199 DHCP_LOGE("enter DhcpIpv4ResultExpiredTest"); 200 ASSERT_TRUE(dhcpClientImpl != nullptr); 201 std::string ifname; 202 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultExpired(ifname)); 203 ifname = "wlan0"; 204 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultExpired(ifname)); 205 206 dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr)); 207 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultExpired(ifname)); 208 dhcpClientImpl->m_mapClientCallBack.clear(); 209 210 DhcpClient client; 211 dhcpClientImpl->m_mapClientService.emplace(std::make_pair("wlan0", client)); 212 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultExpired(ifname)); 213 dhcpClientImpl->m_mapClientService.clear(); 214 } 215 216 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv6ResulCallbackTest, TestSize.Level1) 217 { 218 DHCP_LOGE("enter DhcpIpv6ResulCallbackTest"); 219 ASSERT_TRUE(dhcpClientImpl != nullptr); 220 std::string ifname; 221 DhcpIpv6Info info; 222 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 223 224 ASSERT_TRUE(strncpy_s(info.globalIpv6Addr, DHCP_INET6_ADDRSTRLEN, " 192.168.1.10", ADDRESS_ARRAY_SIZE) == EOK); 225 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 226 227 ASSERT_TRUE(strncpy_s(info.routeAddr, DHCP_INET6_ADDRSTRLEN, " 192.168.1.1", ADDRESS_ARRAY_SIZE) == EOK); 228 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 229 230 ASSERT_TRUE(strncpy_s(info.globalIpv6Addr, DHCP_INET6_ADDRSTRLEN, "292.168.1.10", ADDRESS_ARRAY_SIZE) == EOK); 231 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 232 233 dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr)); 234 ifname = "wlan0"; 235 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 236 dhcpClientImpl->m_mapClientCallBack.clear(); 237 238 sptr<IDhcpClientCallBack> mclientCallback; 239 dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", mclientCallback)); 240 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 241 dhcpClientImpl->m_mapClientCallBack.clear(); 242 } 243 244 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv6ResulCallbackTest1, TestSize.Level1) 245 { 246 DHCP_LOGE("enter DhcpIpv6ResulCallbackTest1"); 247 ASSERT_TRUE(dhcpClientImpl != nullptr); 248 DhcpIpv6Info info; 249 strncpy_s(info.globalIpv6Addr, DHCP_INET6_ADDRSTRLEN, "2001:0db8:85a3:0000:0000:8a2e:0370:7334", 250 DHCP_INET6_ADDRSTRLEN - 1); 251 dhcpClientImpl->DhcpIpv6ResulCallback("test_ifname", info); 252 } 253 254 HWTEST_F(DhcpClientServiceImplTest, PushDhcpResultTest, TestSize.Level1) 255 { 256 DHCP_LOGE("enter PushDhcpResultTest"); 257 ASSERT_TRUE(dhcpClientImpl != nullptr); 258 std::string ifname; 259 OHOS::DHCP::DhcpResult result; 260 result.iptype = 1; 261 result.isOptSuc = true; 262 dhcpClientImpl->PushDhcpResult(ifname, result); 263 264 ifname = "wlan"; 265 dhcpClientImpl->PushDhcpResult(ifname, result); 266 } 267 268 HWTEST_F(DhcpClientServiceImplTest, CheckDhcpResultExistTest, TestSize.Level1) 269 { 270 DHCP_LOGE("enter CheckDhcpResultExistTest"); 271 ASSERT_TRUE(dhcpClientImpl != nullptr); 272 std::string ifname; 273 OHOS::DHCP::DhcpResult result; 274 result.iptype = 1; 275 result.isOptSuc = true; 276 dhcpClientImpl->CheckDhcpResultExist(ifname, result); 277 } 278 279 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv6ResultTimeOutTest, TestSize.Level1) 280 { 281 DHCP_LOGI("DhcpIpv6ResultTimeOutTest enter!"); 282 std::string ifname = "wlan0"; 283 EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpIpv6ResultTimeOut(ifname)); 284 } 285 286 HWTEST_F(DhcpClientServiceImplTest, DhcpFreeIpv6Test, TestSize.Level1) 287 { 288 DHCP_LOGI("DhcpFreeIpv6Test enter!"); 289 std::string ifname = "wlan0"; 290 EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpFreeIpv6(ifname)); 291 } 292 293 HWTEST_F(DhcpClientServiceImplTest, IsGlobalIPv6AddressTest, TestSize.Level1) 294 { 295 DHCP_LOGI("IsGlobalIPv6AddressTest enter!"); 296 std::string ipAddress = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; 297 bool result = dhcpClientImpl->IsGlobalIPv6Address(ipAddress); 298 EXPECT_EQ(result, true); 299 300 ipAddress = "3001:0db8:85a3:0000:0000:8a2e:0370:7334"; 301 result = dhcpClientImpl->IsGlobalIPv6Address(ipAddress); 302 EXPECT_EQ(result, true); 303 304 ipAddress = "1001:0db8:85a3:0000:0000:8a2e:0370:7334"; 305 result = dhcpClientImpl->IsGlobalIPv6Address(ipAddress); 306 EXPECT_EQ(result, false); 307 308 ipAddress = ""; 309 result = dhcpClientImpl->IsGlobalIPv6Address(ipAddress); 310 EXPECT_EQ(result, false); 311 } 312 313 HWTEST_F(DhcpClientServiceImplTest, DhcpOfferResultSuccessTest, TestSize.Level1) 314 { 315 DHCP_LOGI("DhcpOfferResultSuccessTest enter!"); 316 struct DhcpIpResult ipResult; 317 ipResult.ifname = "wlan1"; 318 int result = dhcpClientImpl->DhcpOfferResultSuccess(ipResult); 319 EXPECT_EQ(result, DHCP_OPT_FAILED); 320 } 321 322 HWTEST_F(DhcpClientServiceImplTest, DealWifiDhcpCacheTest, TestSize.Level1) 323 { 324 DHCP_LOGI("DealWifiDhcpCacheTest enter!"); 325 IpCacheInfo ipCacheInfo; 326 ipCacheInfo.ssid = "123"; 327 ipCacheInfo.bssid = "123"; 328 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->DealWifiDhcpCache(WIFI_DHCP_CACHE_ADD, ipCacheInfo)); 329 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->DealWifiDhcpCache(WIFI_DHCP_CACHE_REMOVE, ipCacheInfo)); 330 } 331 } 332 } 333