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 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::DHCP::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::DHCP::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 }
62 
63 HWTEST_F(DhcpClientServiceImplTest, OnStartTest, TestSize.Level1)
64 {
65     DHCP_LOGE("enter OnStartTest");
66     dhcpClientImpl->OnStart();
67 }
68 
69 HWTEST_F(DhcpClientServiceImplTest, OnStopTest, TestSize.Level1)
70 {
71     DHCP_LOGE("enter OnStopTest");
72     dhcpClientImpl->OnStop();
73 }
74 
75 HWTEST_F(DhcpClientServiceImplTest, InitTest, TestSize.Level1)
76 {
77     DHCP_LOGE("enter InitTest");
78     dhcpClientImpl->Init();
79 }
80 
81 HWTEST_F(DhcpClientServiceImplTest, StartOldClientTest, TestSize.Level1)
82 {
83     DHCP_LOGE("enter StartOldClientTest");
84     ASSERT_TRUE(dhcpClientImpl != nullptr);
85 
86     std::string ifname = "wlan0";
87     bool bIpv6 = true;
88     DhcpClient client;
89     client.ifName = ifname;
90     client.isIpv6 = bIpv6;
91     EXPECT_EQ(DHCP_E_FAILED, dhcpClientImpl->StartOldClient(ifname, bIpv6, client));
92 
93     client.pStaStateMachine = new DhcpClientStateMachine(client.ifName);
94     EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StartOldClient(ifname, bIpv6, client));
95 }
96 
97 HWTEST_F(DhcpClientServiceImplTest, StartNewClientTest, TestSize.Level1)
98 {
99     DHCP_LOGE("enter StartNewClientTest");
100     ASSERT_TRUE(dhcpClientImpl != nullptr);
101 
102     std::string ifname = "";
103     bool bIpv6 = false;
104     EXPECT_EQ(DHCP_E_SUCCESS, dhcpClientImpl->StartNewClient(ifname, bIpv6));
105 }
106 
107 HWTEST_F(DhcpClientServiceImplTest, IsRemoteDiedTest, TestSize.Level1)
108 {
109     DHCP_LOGE("enter IsRemoteDiedTest");
110     ASSERT_TRUE(dhcpClientImpl != nullptr);
111 
112     EXPECT_EQ(true, dhcpClientImpl->IsRemoteDied());
113 }
114 
115 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultSuccessTest, TestSize.Level1)
116 {
117     DHCP_LOGE("enter DhcpIpv4ResultSuccessTest");
118     ASSERT_TRUE(dhcpClientImpl != nullptr);
119     struct DhcpIpResult ipResult;
120     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultSuccess(ipResult));
121 
122     ipResult.code = PUBLISH_CODE_SUCCESS;
123     dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr));
124     EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpIpv4ResultSuccess(ipResult));
125 
126     ipResult.code = PUBLISH_CODE_TIMEOUT;
127     dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr));
128     EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpIpv4ResultSuccess(ipResult));
129 
130     ipResult.code = PUBLISH_CODE_FAILED;
131     dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr));
132     EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpIpv4ResultSuccess(ipResult));
133     dhcpClientImpl->m_mapClientCallBack.clear();
134 }
135 
136 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultFailTest, TestSize.Level1)
137 {
138     DHCP_LOGE("enter DhcpIpv4ResultFailTest");
139     ASSERT_TRUE(dhcpClientImpl != nullptr);
140     struct DhcpIpResult ipResult;
141     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultFail(ipResult));
142 
143     dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr));
144     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultFail(ipResult));
145     dhcpClientImpl->m_mapClientCallBack.clear();
146 
147     DhcpClient client;
148     dhcpClientImpl->m_mapClientService.emplace(std::make_pair("wlan0", client));
149     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultFail(ipResult));
150     dhcpClientImpl->m_mapClientService.clear();
151 }
152 
153 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultTimeOutTest, TestSize.Level1)
154 {
155     DHCP_LOGE("enter DhcpIpv4ResultTimeOutTest");
156     ASSERT_TRUE(dhcpClientImpl != nullptr);
157     std::string ifname;
158     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname));
159     ifname = "wlan0";
160     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname));
161 
162     dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr));
163     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname));
164     dhcpClientImpl->m_mapClientCallBack.clear();
165 
166     DhcpClient client;
167     dhcpClientImpl->m_mapClientService.emplace(std::make_pair("wlan0", client));
168     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultTimeOut(ifname));
169     dhcpClientImpl->m_mapClientService.clear();
170 }
171 
172 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv4ResultExpiredTest, TestSize.Level1)
173 {
174     DHCP_LOGE("enter DhcpIpv4ResultExpiredTest");
175     ASSERT_TRUE(dhcpClientImpl != nullptr);
176     std::string ifname;
177     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultExpired(ifname));
178     ifname = "wlan0";
179     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultExpired(ifname));
180 
181     dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr));
182     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultExpired(ifname));
183     dhcpClientImpl->m_mapClientCallBack.clear();
184 
185     DhcpClient client;
186     dhcpClientImpl->m_mapClientService.emplace(std::make_pair("wlan0", client));
187     EXPECT_EQ(DHCP_OPT_FAILED, dhcpClientImpl->DhcpIpv4ResultExpired(ifname));
188     dhcpClientImpl->m_mapClientService.clear();
189 }
190 
191 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv6ResulCallbackTest, TestSize.Level1)
192 {
193     DHCP_LOGE("enter DhcpIpv6ResulCallbackTest");
194     ASSERT_TRUE(dhcpClientImpl != nullptr);
195     std::string ifname;
196     DhcpIpv6Info info;
197     dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info);
198 
199     ASSERT_TRUE(strncpy_s(info.globalIpv6Addr, DHCP_INET6_ADDRSTRLEN, " 192.168.1.10", ADDRESS_ARRAY_SIZE) == EOK);
200     dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info);
201 
202     ASSERT_TRUE(strncpy_s(info.routeAddr, DHCP_INET6_ADDRSTRLEN, " 192.168.1.1", ADDRESS_ARRAY_SIZE) == EOK);
203     dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info);
204 
205     ASSERT_TRUE(strncpy_s(info.globalIpv6Addr, DHCP_INET6_ADDRSTRLEN, "292.168.1.10", ADDRESS_ARRAY_SIZE) == EOK);
206     dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info);
207 
208     dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", nullptr));
209     ifname = "wlan0";
210     dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info);
211     dhcpClientImpl->m_mapClientCallBack.clear();
212 
213     sptr<IDhcpClientCallBack> mclientCallback;
214     dhcpClientImpl->m_mapClientCallBack.emplace(std::make_pair("wlan0", mclientCallback));
215     dhcpClientImpl->DhcpIpv6ResulCallback(ifname, info);
216     dhcpClientImpl->m_mapClientCallBack.clear();
217 }
218 
219 HWTEST_F(DhcpClientServiceImplTest, PushDhcpResultTest, TestSize.Level1)
220 {
221     DHCP_LOGE("enter PushDhcpResultTest");
222     ASSERT_TRUE(dhcpClientImpl != nullptr);
223     std::string ifname;
224     OHOS::DHCP::DhcpResult result;
225     result.iptype = 1;
226     result.isOptSuc = true;
227     dhcpClientImpl->PushDhcpResult(ifname, result);
228 
229     ifname = "wlan";
230     dhcpClientImpl->PushDhcpResult(ifname, result);
231 }
232 
233 HWTEST_F(DhcpClientServiceImplTest, CheckDhcpResultExistTest, TestSize.Level1)
234 {
235     DHCP_LOGE("enter CheckDhcpResultExistTest");
236     ASSERT_TRUE(dhcpClientImpl != nullptr);
237     std::string ifname;
238     OHOS::DHCP::DhcpResult result;
239     result.iptype = 1;
240     result.isOptSuc = true;
241     dhcpClientImpl->CheckDhcpResultExist(ifname, result);
242 }
243 
244 HWTEST_F(DhcpClientServiceImplTest, DhcpIpv6ResultTimeOutTest, TestSize.Level1)
245 {
246     DHCP_LOGI("DhcpIpv6ResultTimeOutTest enter!");
247     std::string ifname = "wlan0";
248     EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpIpv6ResultTimeOut(ifname));
249 }
250 
251 HWTEST_F(DhcpClientServiceImplTest, DhcpFreeIpv6Test, TestSize.Level1)
252 {
253     DHCP_LOGI("DhcpFreeIpv6Test enter!");
254     std::string ifname = "wlan0";
255     EXPECT_EQ(DHCP_OPT_SUCCESS, dhcpClientImpl->DhcpFreeIpv6(ifname));
256 }
257 }
258 }
259