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 #include <gtest/gtest.h> 16 #include <gmock/gmock.h> 17 #include <unistd.h> 18 #include <fcntl.h> 19 #include <cstddef> 20 #include <cstdint> 21 #include <sys/stat.h> 22 #include <sys/socket.h> 23 #include <linux/rtnetlink.h> 24 #include <netinet/in.h> 25 #include "securec.h" 26 #include "dhcp_logger.h" 27 #include "dhcp_function.h" 28 #include "dhcp_ipv6_client.h" 29 30 DEFINE_DHCPLOG_DHCP_LABEL("DhcpIpv6ClientTest"); 31 32 using ::testing::_; 33 using ::testing::AtLeast; 34 using ::testing::DoAll; 35 using ::testing::Eq; 36 using ::testing::Ref; 37 using ::testing::Return; 38 using ::testing::SetArgReferee; 39 using ::testing::StrEq; 40 using ::testing::TypedEq; 41 using ::testing::ext::TestSize; 42 43 namespace OHOS { 44 namespace DHCP { 45 const int KERNEL_SOCKET_IFA_FAMILY = 10; 46 constexpr unsigned int IPV6_SCOPE_NODELOCAL = 0x01; 47 constexpr unsigned int IPV6_SCOPE_LINKLOCAL = 0x02; 48 constexpr unsigned int IPV6_SCOPE_SITELOCAL = 0x05; 49 constexpr unsigned int IPV6_SCOPE_OTHER = 0x08; 50 constexpr unsigned int PRE_FIX_LEN = 12; 51 constexpr unsigned int PRE_FIX_ADDRSTRLEN = 128; 52 constexpr int IPV6_RETURN_ZERO = 0x00000; 53 constexpr int IPV6_RETURN_65552 = 0x10010; 54 constexpr int IPV6_RETURN_131101 = 0x20020; 55 constexpr int IPV6_RETURN_1327744 = 0x50040; 56 constexpr int IPV6_RETURN_524288 = 0x80000; 57 constexpr int IPV6_RETURN_32 = 0x0020U; 58 constexpr int IPV6_OPT_FAILED = -1; 59 constexpr int IPV6_LEN_134 = 134; 60 constexpr int IPV6_LEN_24 = 24; 61 constexpr int IPV6_LEN_16 = 16; 62 constexpr int IPV6_LEN_10 = 10; 63 constexpr int BUF_LEN = 1024; 64 constexpr int BUF_LEN_2 = 2; 65 constexpr int DATA_SIZE = 8; 66 class DhcpIpv6ClientTest : public testing::Test { 67 public: SetUpTestCase()68 static void SetUpTestCase() 69 { 70 } TearDownTestCase()71 static void TearDownTestCase() 72 { 73 } SetUp()74 virtual void SetUp() 75 { 76 std::string ifname = "wlan0"; 77 ipv6Client = std::make_unique<OHOS::DHCP::DhcpIpv6Client>(ifname); 78 } TearDown()79 virtual void TearDown() 80 { 81 if (ipv6Client != nullptr) { 82 ipv6Client.reset(nullptr); 83 } 84 } 85 public: 86 std::unique_ptr<OHOS::DHCP::DhcpIpv6Client> ipv6Client; 87 }; 88 89 HWTEST_F(DhcpIpv6ClientTest, IsRunningTest, TestSize.Level1) 90 { 91 ASSERT_TRUE(ipv6Client != nullptr); 92 DHCP_LOGE("IsRunningTest enter!"); 93 ipv6Client->IsRunning(); 94 } 95 96 HWTEST_F(DhcpIpv6ClientTest, DhcpIPV6StopTest, TestSize.Level1) 97 { 98 ASSERT_TRUE(ipv6Client != nullptr); 99 DHCP_LOGE("DhcpIPV6StopTest enter!"); 100 ipv6Client->DhcpIPV6Stop(); 101 } 102 103 HWTEST_F(DhcpIpv6ClientTest, ResetTest, TestSize.Level1) 104 { 105 ASSERT_TRUE(ipv6Client != nullptr); 106 DHCP_LOGE("ResetTest enter!"); 107 ipv6Client->Reset(); 108 } 109 110 HWTEST_F(DhcpIpv6ClientTest, ipv6AddrScope2TypeTest, TestSize.Level1) 111 { 112 ASSERT_TRUE(ipv6Client != nullptr); 113 DHCP_LOGE("ipv6AddrScope2TypeTest enter!"); 114 EXPECT_EQ(IPV6_RETURN_65552, ipv6Client->ipv6AddrScope2Type(IPV6_SCOPE_NODELOCAL)); 115 EXPECT_EQ(IPV6_RETURN_131101, ipv6Client->ipv6AddrScope2Type(IPV6_SCOPE_LINKLOCAL)); 116 EXPECT_EQ(IPV6_RETURN_1327744, ipv6Client->ipv6AddrScope2Type(IPV6_SCOPE_SITELOCAL)); 117 EXPECT_EQ(IPV6_RETURN_524288, ipv6Client->ipv6AddrScope2Type(IPV6_SCOPE_OTHER)); 118 } 119 120 HWTEST_F(DhcpIpv6ClientTest, getAddrTypeTest, TestSize.Level1) 121 { 122 ASSERT_TRUE(ipv6Client != nullptr); 123 DHCP_LOGE("getAddrTypeTest enter!"); 124 struct in6_addr addr; 125 EXPECT_EQ(IPV6_RETURN_32, ipv6Client->getAddrType(nullptr)); 126 EXPECT_EQ(IPV6_RETURN_ZERO, ipv6Client->getAddrType(&addr)); 127 } 128 129 HWTEST_F(DhcpIpv6ClientTest, getAddrScopeTest, TestSize.Level1) 130 { 131 ASSERT_TRUE(ipv6Client != nullptr); 132 DHCP_LOGE("getAddrScopeTest enter!"); 133 struct in6_addr addr; 134 EXPECT_EQ(IPV6_RETURN_32, ipv6Client->getAddrScope(nullptr)); 135 EXPECT_EQ(IPV6_RETURN_ZERO, ipv6Client->getAddrScope(&addr)); 136 } 137 138 HWTEST_F(DhcpIpv6ClientTest, createKernelSocketTest, TestSize.Level1) 139 { 140 ASSERT_TRUE(ipv6Client != nullptr); 141 DHCP_LOGE("createKernelSocketTest enter!"); 142 ipv6Client->createKernelSocket(); 143 } 144 145 HWTEST_F(DhcpIpv6ClientTest, GetIpv6PrefixTest, TestSize.Level1) 146 { 147 ASSERT_TRUE(ipv6Client != nullptr); 148 DHCP_LOGE("GetIpv6PrefixTest enter!"); 149 char ipv6Addr[] = "192.168.1.10"; 150 char ipv6PrefixBuf[] = "192.168.1.12"; 151 ipv6Client->GetIpv6Prefix(nullptr, ipv6PrefixBuf, PRE_FIX_LEN); 152 ipv6Client->GetIpv6Prefix(ipv6Addr, nullptr, PRE_FIX_LEN); 153 ipv6Client->GetIpv6Prefix(ipv6Addr, ipv6PrefixBuf, PRE_FIX_ADDRSTRLEN); 154 ipv6Client->GetIpv6Prefix(ipv6Addr, ipv6PrefixBuf, PRE_FIX_LEN); 155 } 156 157 HWTEST_F(DhcpIpv6ClientTest, GetIpFromS6AddressTest, TestSize.Level1) 158 { 159 ASSERT_TRUE(ipv6Client != nullptr); 160 DHCP_LOGE("GetIpFromS6AddressTest enter!"); 161 char addr[] = "192.168.1.12"; 162 char buf[BUF_LEN] = {0}; 163 EXPECT_EQ(ipv6Client->GetIpFromS6Address(addr, 1, buf, BUF_LEN), IPV6_OPT_FAILED); 164 EXPECT_EQ(ipv6Client->GetIpFromS6Address(addr, 1, buf, BUF_LEN_2), IPV6_OPT_FAILED); 165 } 166 167 HWTEST_F(DhcpIpv6ClientTest, onIpv6AddressAddEventTest, TestSize.Level1) 168 { 169 ASSERT_TRUE(ipv6Client != nullptr); 170 DHCP_LOGE("onIpv6AddressAddEventTest enter!"); 171 char data[] = "192.168.1.12"; 172 ipv6Client->onIpv6AddressAddEvent(static_cast<void *>(data), PRE_FIX_LEN, 1); 173 ipv6Client->onIpv6AddressAddEvent(nullptr, PRE_FIX_LEN, 0); 174 ipv6Client->onIpv6AddressAddEvent(nullptr, PRE_FIX_LEN, 41); 175 } 176 177 HWTEST_F(DhcpIpv6ClientTest, onIpv6DnsAddEventTest, TestSize.Level1) 178 { 179 ASSERT_TRUE(ipv6Client != nullptr); 180 DHCP_LOGE("onIpv6DnsAddEventTest enter!"); 181 char data[] = "192.168.1.12"; 182 ipv6Client->onIpv6DnsAddEvent(static_cast<void *>(data), PRE_FIX_LEN, 1); 183 ipv6Client->onIpv6DnsAddEvent(nullptr, PRE_FIX_LEN, 0); 184 ipv6Client->onIpv6DnsAddEvent(nullptr, PRE_FIX_LEN, 41); 185 ipv6Client->onIpv6DnsAddEvent(static_cast<void *>(data), PRE_FIX_LEN, 41); 186 } 187 188 HWTEST_F(DhcpIpv6ClientTest, onIpv6RouteAddEventTest, TestSize.Level1) 189 { 190 ASSERT_TRUE(ipv6Client != nullptr); 191 DHCP_LOGE("onIpv6RouteAddEventTest enter!"); 192 char gateway[] = "192.168.1.1"; 193 char dst[] = "192.168.1.2"; 194 ipv6Client->onIpv6RouteAddEvent(gateway, dst, 1); 195 ipv6Client->onIpv6RouteAddEvent(nullptr, dst, 41); 196 ipv6Client->onIpv6RouteAddEvent(gateway, nullptr, 41); 197 ipv6Client->onIpv6RouteAddEvent(gateway, dst, 41); 198 } 199 200 HWTEST_F(DhcpIpv6ClientTest, getIpv6RouteAddrTest, TestSize.Level1) 201 { 202 ASSERT_TRUE(ipv6Client != nullptr); 203 DHCP_LOGE("getIpv6RouteAddrTest enter!"); 204 ipv6Client->getIpv6RouteAddr(); 205 } 206 207 HWTEST_F(DhcpIpv6ClientTest, setSocketFilterTest, TestSize.Level1) 208 { 209 ASSERT_TRUE(ipv6Client != nullptr); 210 DHCP_LOGE("setSocketFilterTest enter!"); 211 char addr[] = "192.168.1.2"; 212 ipv6Client->setSocketFilter(nullptr); 213 ipv6Client->setSocketFilter(static_cast<void *>(addr)); 214 } 215 216 HWTEST_F(DhcpIpv6ClientTest, parseNdUserOptMessageTest, TestSize.Level1) 217 { 218 ASSERT_TRUE(ipv6Client != nullptr); 219 DHCP_LOGE("parseNdUserOptMessageTest enter!"); 220 struct nduseroptmsg data; 221 data.nduseropt_opts_len = IPV6_LEN_16; 222 data.nduseropt_family = IPV6_LEN_10; 223 data.nduseropt_icmp_type = IPV6_LEN_134; 224 data.nduseropt_icmp_code = IPV6_RETURN_ZERO; 225 ipv6Client->parseNdUserOptMessage(nullptr, IPV6_LEN_24); 226 ipv6Client->parseNdUserOptMessage(static_cast<void *>(&data), IPV6_LEN_24); 227 228 data.nduseropt_opts_len = IPV6_LEN_24; 229 ipv6Client->parseNdUserOptMessage(static_cast<void *>(&data), IPV6_LEN_16); 230 231 data.nduseropt_opts_len = IPV6_LEN_16; 232 data.nduseropt_family = IPV6_LEN_16; 233 ipv6Client->parseNdUserOptMessage(static_cast<void *>(&data), IPV6_LEN_24); 234 235 data.nduseropt_opts_len = IPV6_LEN_16; 236 data.nduseropt_family = IPV6_LEN_10; 237 data.nduseropt_icmp_type = IPV6_LEN_16; 238 data.nduseropt_icmp_code = IPV6_LEN_16; 239 ipv6Client->parseNdUserOptMessage(static_cast<void *>(&data), IPV6_LEN_24); 240 241 data.nduseropt_opts_len = IPV6_LEN_16; 242 data.nduseropt_family = IPV6_LEN_10; 243 data.nduseropt_icmp_type = IPV6_LEN_134; 244 data.nduseropt_icmp_code = IPV6_LEN_16; 245 ipv6Client->parseNdUserOptMessage(static_cast<void *>(&data), IPV6_LEN_24); 246 } 247 248 HWTEST_F(DhcpIpv6ClientTest, parseNDRouteMessageTest, TestSize.Level1) 249 { 250 ASSERT_TRUE(ipv6Client != nullptr); 251 DHCP_LOGE("parseNDRouteMessageTest enter!"); 252 struct nlmsghdr msg; 253 ipv6Client->parseNDRouteMessage(nullptr); 254 ipv6Client->parseNDRouteMessage(static_cast<void *>(&msg)); 255 } 256 257 HWTEST_F(DhcpIpv6ClientTest, ParseNDRouteMessage_TEST1, TestSize.Level1) 258 { 259 ASSERT_TRUE(ipv6Client != nullptr); 260 DHCP_LOGE("ParseNDRouteMessage_TEST1 enter!"); 261 nlmsghdr hdrMsg; 262 rtmsg rtMsg; 263 hdrMsg.nlmsg_len = sizeof(rtmsg); 264 rtMsg.rtm_protocol = RTPROT_BOOT; 265 rtMsg.rtm_scope = RT_SCOPE_SITE; 266 rtMsg.rtm_type = RTN_MULTICAST; 267 rtMsg.rtm_src_len = 1; 268 rtMsg.rtm_flags = RTM_F_PREFIX; 269 ipv6Client->parseNDRouteMessage(&hdrMsg); 270 } 271 272 HWTEST_F(DhcpIpv6ClientTest, ParseNDRouteMessage_TEST2, TestSize.Level1) 273 { 274 ASSERT_TRUE(ipv6Client != nullptr); 275 DHCP_LOGE("ParseNDRouteMessage_TEST2 enter!"); 276 nlmsghdr hdrMsg; 277 rtmsg rtMsg; 278 hdrMsg.nlmsg_len = sizeof(rtmsg); 279 rtMsg.rtm_protocol = RTPROT_KERNEL; 280 rtMsg.rtm_scope = RT_SCOPE_UNIVERSE; 281 rtMsg.rtm_type = RTN_UNICAST; 282 rtMsg.rtm_src_len = 0; 283 rtMsg.rtm_flags = 0; 284 ipv6Client->parseNDRouteMessage(&hdrMsg); 285 } 286 287 HWTEST_F(DhcpIpv6ClientTest, parseNewneighMessageTest, TestSize.Level1) 288 { 289 ASSERT_TRUE(ipv6Client != nullptr); 290 DHCP_LOGE("parseNewneighMessageTest enter!"); 291 struct nlmsghdr msg; 292 ipv6Client->parseNewneighMessage(nullptr); 293 ipv6Client->parseNewneighMessage(static_cast<void *>(&msg)); 294 } 295 296 HWTEST_F(DhcpIpv6ClientTest, ParseNewneighMessage_Test, TestSize.Level1) 297 { 298 ASSERT_TRUE(ipv6Client != nullptr); 299 DHCP_LOGE("ParseNewneighMessage_Test enter!"); 300 void* msg = new char[sizeof(nlmsghdr) + sizeof(ndmsg) + sizeof(rtattr) + sizeof(in6_addr)]; 301 nlmsghdr* nlh = reinterpret_cast<nlmsghdr*>(msg); 302 ndmsg* ndm = reinterpret_cast<ndmsg*>((char*)msg + sizeof(nlmsghdr)); 303 rtattr* rta = reinterpret_cast<rtattr*>((char*)msg + sizeof(nlmsghdr) + sizeof(ndmsg)); 304 in6_addr* addr = reinterpret_cast<in6_addr*>((char*)msg + sizeof(nlmsghdr) + sizeof(ndmsg) + sizeof(rtattr)); 305 nlh->nlmsg_type = RTM_NEWNEIGH; 306 ndm->ndm_family = KERNEL_SOCKET_IFA_FAMILY; 307 ndm->ndm_state = NUD_REACHABLE; 308 rta->rta_type = NDA_DST; 309 rta->rta_len = sizeof(rtattr) + sizeof(in6_addr); 310 ipv6Client->parseNewneighMessage(msg); 311 } 312 313 HWTEST_F(DhcpIpv6ClientTest, fillRouteDataTest, TestSize.Level1) 314 { 315 ASSERT_TRUE(ipv6Client != nullptr); 316 DHCP_LOGE("fillRouteDataTest enter!"); 317 int len = 0; 318 char buf[BUF_LEN] = {0}; 319 ipv6Client->fillRouteData(nullptr, len); 320 ipv6Client->fillRouteData(buf, len); 321 } 322 323 HWTEST_F(DhcpIpv6ClientTest, handleKernelEventTest, TestSize.Level1) 324 { 325 ASSERT_TRUE(ipv6Client != nullptr); 326 DHCP_LOGE("handleKernelEventTest enter!"); 327 uint8_t data[] = "testcode"; 328 ipv6Client->handleKernelEvent(nullptr, 0); 329 ipv6Client->handleKernelEvent(data, 1); 330 ipv6Client->handleKernelEvent(data, DATA_SIZE); 331 } 332 333 HWTEST_F(DhcpIpv6ClientTest, AddIpv6AddressTest, TestSize.Level1) 334 { 335 DHCP_LOGI("AddIpv6Address enter!"); 336 ipv6Client->AddIpv6Address(nullptr, 0); 337 } 338 } 339 } 340