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 Wifi { 30 constexpr int ADDRESS_ARRAY_SIZE = 12; 31 class DhcpClientServiceImplTest : public testing::Test { 32 public: SetUpTestCase()33 static void SetUpTestCase() 34 {} TearDownTestCase()35 static void TearDownTestCase() 36 {} SetUp()37 virtual void SetUp() 38 { 39 dhcpClientImpl = std::make_unique<OHOS::Wifi::DhcpClientServiceImpl>(); 40 } TearDown()41 virtual void TearDown() 42 { 43 if (dhcpClientImpl != nullptr) { 44 dhcpClientImpl.reset(nullptr); 45 } 46 } 47 public: 48 std::unique_ptr<OHOS::Wifi::DhcpClientServiceImpl> dhcpClientImpl; 49 }; 50 51 HWTEST_F(DhcpClientServiceImplTest, IsNativeProcessTest, TestSize.Level1) 52 { 53 ASSERT_TRUE(dhcpClientImpl != nullptr); 54 DHCP_LOGE("enter IsNativeProcess fail Test"); 55 56 const std::string& ifname = "wlan0"; 57 bool bIpv6 = true; 58 EXPECT_EQ(DHCP_E_PERMISSION_DENIED, dhcpClientImpl->StartDhcpClient(ifname, bIpv6)); 59 bIpv6 = false; 60 EXPECT_EQ(DHCP_E_PERMISSION_DENIED, dhcpClientImpl->StopDhcpClient(ifname, bIpv6)); 61 EXPECT_EQ(DHCP_E_PERMISSION_DENIED, dhcpClientImpl->RenewDhcpClient(ifname)); 62 } 63 64 HWTEST_F(DhcpClientServiceImplTest, OnStartTest, TestSize.Level1) 65 { 66 DHCP_LOGE("enter OnStartTest"); 67 dhcpClientImpl->OnStart(); 68 } 69 70 HWTEST_F(DhcpClientServiceImplTest, OnStopTest, TestSize.Level1) 71 { 72 DHCP_LOGE("enter OnStopTest"); 73 dhcpClientImpl->OnStop(); 74 } 75 76 HWTEST_F(DhcpClientServiceImplTest, InitTest, TestSize.Level1) 77 { 78 DHCP_LOGE("enter InitTest"); 79 dhcpClientImpl->Init(); 80 } 81 82 HWTEST_F(DhcpClientServiceImplTest, StartOldClientTest, TestSize.Level1) 83 { 84 DHCP_LOGE("enter StartOldClientTest"); 85 ASSERT_TRUE(dhcpClientImpl != nullptr); 86 87 std::string ifname = "wlan0"; 88 bool bIpv6 = true; 89 DhcpClient client; 90 client.ifName = ifname; 91 client.isIpv6 = bIpv6; 92 EXPECT_EQ(DHCP_E_FAILED, dhcpClientImpl->StartOldClient(ifname, bIpv6, client)); 93 94 client.pStaStateMachine = new DhcpClientStateMachine(client.ifName); 95 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StartOldClient(ifname, bIpv6, client)); 96 } 97 98 HWTEST_F(DhcpClientServiceImplTest, StartNewClientTest, TestSize.Level1) 99 { 100 DHCP_LOGE("enter StartNewClientTest"); 101 ASSERT_TRUE(dhcpClientImpl != nullptr); 102 103 std::string ifname = ""; 104 bool bIpv6 = false; 105 EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StartNewClient(ifname, bIpv6)); 106 } 107 108 HWTEST_F(DhcpClientServiceImplTest, IsRemoteDiedTest, TestSize.Level1) 109 { 110 DHCP_LOGE("enter IsRemoteDiedTest"); 111 ASSERT_TRUE(dhcpClientImpl != nullptr); 112 113 EXPECT_EQ(true, dhcpClientImpl->IsRemoteDied()); 114 } 115 116 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultSuccessTest, TestSize.Level1) 117 { 118 DHCP_LOGE("enter DhcpIpv4ResultSuccessTest"); 119 ASSERT_TRUE(dhcpClientImpl != nullptr); 120 std::vector<std::string> splits {}; 121 splits.push_back("wlan0"); 122 splits.push_back("12"); 123 splits.push_back("*"); 124 splits.push_back("wlan4"); 125 splits.push_back("wlan5"); 126 splits.push_back("wlan6"); 127 splits.push_back("wlan7"); 128 splits.push_back("wlan8"); 129 splits.push_back("wlan9"); 130 splits.push_back("wlan10"); 131 splits.push_back("wlan11"); 132 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultSuccess(splits)); 133 } 134 135 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultFailTest, TestSize.Level1) 136 { 137 DHCP_LOGE("enter DhcpIpv4ResultFailTest"); 138 ASSERT_TRUE(dhcpClientImpl != nullptr); 139 std::vector<std::string> splits {}; 140 splits.push_back("wlan0"); 141 splits.push_back("12"); 142 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultFail(splits)); 143 } 144 145 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultTimeOutTest, TestSize.Level1) 146 { 147 DHCP_LOGE("enter DhcpIpv4ResultTimeOutTest"); 148 ASSERT_TRUE(dhcpClientImpl != nullptr); 149 std::string ifname; 150 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname)); 151 ifname = "wlan0"; 152 EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname)); 153 } 154 155 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv6ResulCallbackTest, TestSize.Level1) 156 { 157 DHCP_LOGE("enter DhcpIpv6ResulCallbackTest"); 158 ASSERT_TRUE(dhcpClientImpl != nullptr); 159 std::string ifname; 160 DhcpIpv6Info info; 161 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 162 163 ASSERT_TRUE(strncpy_s(info.globalIpv6Addr, DHCP_INET6_ADDRSTRLEN, " 192.168.1.10", ADDRESS_ARRAY_SIZE) == EOK); 164 ASSERT_TRUE(strncpy_s(info.routeAddr, DHCP_INET6_ADDRSTRLEN, " 192.168.1.1", ADDRESS_ARRAY_SIZE) == EOK); 165 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 166 167 ifname = "wlan0"; 168 dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info); 169 } 170 171 HWTEST_F(DhcpClientServiceImplTest, PushDhcpResultTest, TestSize.Level1) 172 { 173 DHCP_LOGE("enter PushDhcpResultTest"); 174 ASSERT_TRUE(dhcpClientImpl != nullptr); 175 std::string ifname; 176 OHOS::Wifi::DhcpResult result; 177 result.iptype = 1; 178 result.isOptSuc = true; 179 dhcpClientImpl->PushDhcpResult(ifname, result); 180 181 ifname = "wlan"; 182 dhcpClientImpl->PushDhcpResult(ifname, result); 183 } 184 185 HWTEST_F(DhcpClientServiceImplTest, CheckDhcpResultExistTest, TestSize.Level1) 186 { 187 DHCP_LOGE("enter CheckDhcpResultExistTest"); 188 ASSERT_TRUE(dhcpClientImpl != nullptr); 189 std::string ifname; 190 OHOS::Wifi::DhcpResult result; 191 result.iptype = 1; 192 result.isOptSuc = true; 193 dhcpClientImpl->CheckDhcpResultExist(ifname, result); 194 } 195 196 HWTEST_F(DhcpClientServiceImplTest, StartServiceAbilityTest, TestSize.Level1) 197 { 198 DHCP_LOGI("enter StartServiceAbilityTest"); 199 ASSERT_TRUE(dhcpClientImpl != nullptr); 200 int sleeps = 1; 201 dhcpClientImpl->StartServiceAbility(sleeps); 202 sleeps = 0; 203 dhcpClientImpl->StartServiceAbility(sleeps); 204 sleeps = -1; 205 dhcpClientImpl->StartServiceAbility(sleeps); 206 } 207 } 208 } 209