2 * Copyright 2017 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 <sys/types.h>
23 #include <folly/IPAddress.h>
24 #include <folly/portability/GTest.h>
25 #include <folly/portability/Sockets.h>
31 typedef std::vector<uint8_t> ByteVector;
38 AddressData(const std::string& address, const ByteVector& bytes,
40 : address(address), bytes(bytes), version(version) {}
41 AddressData(const std::string& address, uint8_t version)
42 : address(address), bytes(), version(version) {}
43 explicit AddressData(const std::string& address)
44 : address(address), bytes(), version(0) {}
45 AddressData(): address(""), bytes(), version(0) {}
47 static in_addr parseAddress4(const std::string& src) {
49 inet_pton(AF_INET, src.c_str(), &addr);
53 static in6_addr parseAddress6(const std::string& src) {
55 inet_pton(AF_INET6, src.c_str(), &addr);
65 static const uint8_t IS_LOCAL = 1 << 0;
66 static const uint8_t IS_NONROUTABLE = 1 << 1;
67 static const uint8_t IS_PRIVATE = 1 << 2;
68 static const uint8_t IS_ZERO = 1 << 3;
69 static const uint8_t IS_LINK_LOCAL = 1 << 4;
70 static const uint8_t IS_MULTICAST = 1 << 5;
71 static const uint8_t IS_LINK_LOCAL_BROADCAST = 1 << 6;
73 AddressFlags(const std::string& addr, uint8_t version, uint8_t flags)
79 bool isLoopback() const {
80 return (flags & IS_LOCAL);
82 bool isNonroutable() const {
83 return (flags & IS_NONROUTABLE);
85 bool isPrivate() const {
86 return (flags & IS_PRIVATE);
89 return (flags & IS_ZERO);
91 bool isLinkLocal() const {
92 return (flags & IS_LINK_LOCAL);
94 bool isLinkLocalBroadcast() const {
95 return (flags & IS_LINK_LOCAL_BROADCAST);
103 MaskData(const std::string& addr, uint8_t mask,
104 const std::string& subnet)
111 struct MaskBoundaryData : MaskData {
113 MaskBoundaryData(const std::string& addr, uint8_t mask,
114 const std::string& subnet, bool inSubnet)
115 : MaskData(addr, mask, subnet)
120 struct SerializeData {
124 SerializeData(const std::string& addr, const ByteVector& bytes)
130 struct IPAddressTest : public ::testing::TestWithParam<AddressData> {
131 void ExpectIsValid(const IPAddress& addr) {
132 AddressData param = GetParam();
133 EXPECT_EQ(param.version, addr.version());
134 EXPECT_EQ(param.address, addr.str());
135 if (param.version == 4) {
136 in_addr v4addr = AddressData::parseAddress4(param.address);
137 EXPECT_EQ(0, memcmp(&v4addr, addr.asV4().toByteArray().data(), 4));
138 EXPECT_TRUE(addr.isV4());
139 EXPECT_FALSE(addr.isV6());
141 in6_addr v6addr = AddressData::parseAddress6(param.address);
142 EXPECT_EQ(0, memcmp(&v6addr, addr.asV6().toByteArray().data(), 16));
143 EXPECT_TRUE(addr.isV6());
144 EXPECT_FALSE(addr.isV4());
148 struct IPAddressFlagTest : public ::testing::TestWithParam<AddressFlags> {};
149 struct IPAddressCtorTest : public ::testing::TestWithParam<std::string> {};
150 struct IPAddressCtorBinaryTest : public ::testing::TestWithParam<ByteVector> {};
151 struct IPAddressMappedTest :
152 public ::testing::TestWithParam<std::pair<std::string,std::string> > {};
153 struct IPAddressMaskTest : public ::testing::TestWithParam<MaskData> {};
154 struct IPAddressMaskBoundaryTest :
155 public ::testing::TestWithParam<MaskBoundaryData> {};
156 struct IPAddressSerializeTest :
157 public ::testing::TestWithParam<SerializeData> {};
158 struct IPAddressByteAccessorTest:
159 public ::testing::TestWithParam<AddressData> {};
160 struct IPAddressBitAccessorTest:
161 public ::testing::TestWithParam<AddressData> {};