2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <gtest/gtest.h>
23 #include <sys/types.h>
25 #include <folly/IPAddress.h>
26 #include <folly/portability/Sockets.h>
32 typedef std::vector<uint8_t> ByteVector;
39 AddressData(const std::string& address, const ByteVector& bytes,
41 : address(address), bytes(bytes), version(version) {}
42 AddressData(const std::string& address, uint8_t version)
43 : address(address), bytes(), version(version) {}
44 explicit AddressData(const std::string& address)
45 : address(address), bytes(), version(0) {}
46 AddressData(): address(""), bytes(), version(0) {}
48 static in_addr parseAddress4(const std::string& src) {
50 inet_pton(AF_INET, src.c_str(), &addr);
54 static in6_addr parseAddress6(const std::string& src) {
56 inet_pton(AF_INET6, src.c_str(), &addr);
66 static const uint8_t IS_LOCAL = 1 << 0;
67 static const uint8_t IS_NONROUTABLE = 1 << 1;
68 static const uint8_t IS_PRIVATE = 1 << 2;
69 static const uint8_t IS_ZERO = 1 << 3;
70 static const uint8_t IS_LINK_LOCAL = 1 << 4;
71 static const uint8_t IS_MULTICAST = 1 << 5;
72 static const uint8_t IS_LINK_LOCAL_BROADCAST = 1 << 6;
74 AddressFlags(const std::string& addr, uint8_t version, uint8_t flags)
80 bool isLoopback() const {
81 return (flags & IS_LOCAL);
83 bool isNonroutable() const {
84 return (flags & IS_NONROUTABLE);
86 bool isPrivate() const {
87 return (flags & IS_PRIVATE);
90 return (flags & IS_ZERO);
92 bool isLinkLocal() const {
93 return (flags & IS_LINK_LOCAL);
95 bool isLinkLocalBroadcast() const {
96 return (flags & IS_LINK_LOCAL_BROADCAST);
104 MaskData(const std::string& addr, uint8_t mask,
105 const std::string& subnet)
112 struct MaskBoundaryData : MaskData {
114 MaskBoundaryData(const std::string& addr, uint8_t mask,
115 const std::string& subnet, bool inSubnet)
116 : MaskData(addr, mask, subnet)
121 struct SerializeData {
125 SerializeData(const std::string& addr, const ByteVector& bytes)
131 struct IPAddressTest : public ::testing::TestWithParam<AddressData> {
132 void ExpectIsValid(const IPAddress& addr) {
133 AddressData param = GetParam();
134 EXPECT_EQ(param.version, addr.version());
135 EXPECT_EQ(param.address, addr.str());
136 if (param.version == 4) {
137 in_addr v4addr = AddressData::parseAddress4(param.address);
138 EXPECT_EQ(0, memcmp(&v4addr, addr.asV4().toByteArray().data(), 4));
139 EXPECT_TRUE(addr.isV4());
140 EXPECT_FALSE(addr.isV6());
142 in6_addr v6addr = AddressData::parseAddress6(param.address);
143 EXPECT_EQ(0, memcmp(&v6addr, addr.asV6().toByteArray().data(), 16));
144 EXPECT_TRUE(addr.isV6());
145 EXPECT_FALSE(addr.isV4());
149 struct IPAddressFlagTest : public ::testing::TestWithParam<AddressFlags> {};
150 struct IPAddressCtorTest : public ::testing::TestWithParam<std::string> {};
151 struct IPAddressCtorBinaryTest : public ::testing::TestWithParam<ByteVector> {};
152 struct IPAddressMappedTest :
153 public ::testing::TestWithParam<std::pair<std::string,std::string> > {};
154 struct IPAddressMaskTest : public ::testing::TestWithParam<MaskData> {};
155 struct IPAddressMaskBoundaryTest :
156 public ::testing::TestWithParam<MaskBoundaryData> {};
157 struct IPAddressSerializeTest :
158 public ::testing::TestWithParam<SerializeData> {};
159 struct IPAddressByteAccessorTest:
160 public ::testing::TestWithParam<AddressData> {};
161 struct IPAddressBitAccessorTest:
162 public ::testing::TestWithParam<AddressData> {};