Fix SimpleBarrier
[folly.git] / folly / test / IPAddressTest.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <string>
20
21 #include <sys/types.h>
22
23 #include <folly/IPAddress.h>
24 #include <folly/portability/GTest.h>
25 #include <folly/portability/Sockets.h>
26
27 namespace folly {
28
29 class IPAddress;
30
31 typedef std::vector<uint8_t> ByteVector;
32
33 struct AddressData {
34   std::string address;
35   ByteVector bytes;
36   uint8_t version;
37
38   AddressData(const std::string& address, const ByteVector& bytes,
39               uint8_t version)
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) {}
46
47   static in_addr parseAddress4(const std::string& src) {
48     in_addr addr;
49     inet_pton(AF_INET, src.c_str(), &addr);
50     return addr;
51   }
52
53   static in6_addr parseAddress6(const std::string& src) {
54     in6_addr addr;
55     inet_pton(AF_INET6, src.c_str(), &addr);
56     return addr;
57   }
58 };
59
60 struct AddressFlags {
61   std::string address;
62   uint8_t flags;
63   uint8_t version;
64
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;
72
73   AddressFlags(const std::string& addr, uint8_t version, uint8_t flags)
74     : address(addr)
75     , flags(flags)
76     , version(version)
77   {}
78
79   bool isLoopback() const {
80     return (flags & IS_LOCAL);
81   }
82   bool isNonroutable() const {
83     return (flags & IS_NONROUTABLE);
84   }
85   bool isPrivate() const {
86     return (flags & IS_PRIVATE);
87   }
88   bool isZero() const {
89     return (flags & IS_ZERO);
90   }
91   bool isLinkLocal() const {
92     return (flags & IS_LINK_LOCAL);
93   }
94   bool isLinkLocalBroadcast() const {
95     return (flags & IS_LINK_LOCAL_BROADCAST);
96   }
97 };
98
99 struct MaskData {
100   std::string address;
101   uint8_t mask;
102   std::string subnet;
103   MaskData(const std::string& addr, uint8_t mask,
104            const std::string& subnet)
105     : address(addr)
106     , mask(mask)
107     , subnet(subnet)
108   {}
109 };
110
111 struct MaskBoundaryData : MaskData {
112   bool inSubnet;
113   MaskBoundaryData(const std::string& addr, uint8_t mask,
114                    const std::string& subnet, bool inSubnet)
115     : MaskData(addr, mask, subnet)
116     , inSubnet(inSubnet)
117   {}
118 };
119
120 struct SerializeData {
121   std::string address;
122   ByteVector bytes;
123
124   SerializeData(const std::string& addr, const ByteVector& bytes)
125     : address(addr)
126     , bytes(bytes)
127   {}
128 };
129
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());
140     } else {
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());
145     }
146   }
147 };
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> {};
162 } // folly