2 * Copyright 2015 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>
24 #include <sys/types.h>
25 #include <arpa/inet.h>
26 #include <sys/socket.h>
29 #include <folly/IPAddress.h>
35 typedef std::vector<uint8_t> ByteVector;
42 AddressData(const std::string& address, const ByteVector& bytes,
44 : address(address), bytes(bytes), version(version) {}
45 AddressData(const std::string& address, uint8_t version)
46 : address(address), bytes(), version(version) {}
47 explicit AddressData(const std::string& address)
48 : address(address), bytes(), version(0) {}
49 AddressData(): address(""), bytes(), version(0) {}
51 static in_addr parseAddress4(const std::string& src) {
53 inet_pton(AF_INET, src.c_str(), &addr);
57 static in6_addr parseAddress6(const std::string& src) {
59 inet_pton(AF_INET6, src.c_str(), &addr);
69 static const uint8_t IS_LOCAL = 1 << 0;
70 static const uint8_t IS_NONROUTABLE = 1 << 1;
71 static const uint8_t IS_PRIVATE = 1 << 2;
72 static const uint8_t IS_ZERO = 1 << 3;
73 static const uint8_t IS_LINK_LOCAL = 1 << 4;
74 static const uint8_t IS_MULTICAST = 1 << 5;
75 static const uint8_t IS_LINK_LOCAL_BROADCAST = 1 << 6;
77 AddressFlags(const std::string& addr, uint8_t version, uint8_t flags)
83 bool isLoopback() const {
84 return (flags & IS_LOCAL);
86 bool isNonroutable() const {
87 return (flags & IS_NONROUTABLE);
89 bool isPrivate() const {
90 return (flags & IS_PRIVATE);
93 return (flags & IS_ZERO);
95 bool isLinkLocal() const {
96 return (flags & IS_LINK_LOCAL);
98 bool isLinkLocalBroadcast() const {
99 return (flags & IS_LINK_LOCAL_BROADCAST);
107 MaskData(const std::string& addr, uint8_t mask,
108 const std::string& subnet)
115 struct MaskBoundaryData : MaskData {
117 MaskBoundaryData(const std::string& addr, uint8_t mask,
118 const std::string& subnet, bool inSubnet)
119 : MaskData(addr, mask, subnet)
124 struct SerializeData {
128 SerializeData(const std::string& addr, const ByteVector& bytes)
134 struct IPAddressTest : public ::testing::TestWithParam<AddressData> {
135 void ExpectIsValid(const IPAddress& addr) {
136 AddressData param = GetParam();
137 EXPECT_EQ(param.version, addr.version());
138 EXPECT_EQ(param.address, addr.str());
139 if (param.version == 4) {
140 in_addr v4addr = AddressData::parseAddress4(param.address);
141 EXPECT_EQ(0, memcmp(&v4addr, addr.asV4().toByteArray().data(), 4));
142 EXPECT_TRUE(addr.isV4());
143 EXPECT_FALSE(addr.isV6());
145 in6_addr v6addr = AddressData::parseAddress6(param.address);
146 EXPECT_EQ(0, memcmp(&v6addr, addr.asV6().toByteArray().data(), 16));
147 EXPECT_TRUE(addr.isV6());
148 EXPECT_FALSE(addr.isV4());
152 struct IPAddressFlagTest : public ::testing::TestWithParam<AddressFlags> {};
153 struct IPAddressCtorTest : public ::testing::TestWithParam<std::string> {};
154 struct IPAddressCtorBinaryTest : public ::testing::TestWithParam<ByteVector> {};
155 struct IPAddressMappedTest :
156 public ::testing::TestWithParam<std::pair<std::string,std::string> > {};
157 struct IPAddressMaskTest : public ::testing::TestWithParam<MaskData> {};
158 struct IPAddressMaskBoundaryTest :
159 public ::testing::TestWithParam<MaskBoundaryData> {};
160 struct IPAddressSerializeTest :
161 public ::testing::TestWithParam<SerializeData> {};
162 struct IPAddressByteAccessorTest:
163 public ::testing::TestWithParam<AddressData> {};
164 struct IPAddressBitAccessorTest:
165 public ::testing::TestWithParam<AddressData> {};