Move common/network/IPAddress.h and related to folly/
[folly.git] / folly / test / IPAddressTest.h
1 /*
2  * Copyright 2014 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 <gtest/gtest.h>
22
23 extern "C" {
24 #include <sys/types.h>
25 #include <arpa/inet.h>
26 #include <sys/socket.h>
27 }
28
29 #include "folly/IPAddress.h"
30
31 namespace folly {
32
33 class IPAddress;
34
35 typedef std::vector<uint8_t> ByteVector;
36
37 struct AddressData {
38   std::string address;
39   ByteVector bytes;
40   uint8_t version;
41
42   AddressData(const std::string& address, const ByteVector& bytes,
43               uint8_t version)
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) {}
50
51   static in_addr parseAddress4(const std::string& src) {
52     in_addr addr;
53     inet_pton(AF_INET, src.c_str(), &addr);
54     return addr;
55   }
56
57   static in6_addr parseAddress6(const std::string& src) {
58     in6_addr addr;
59     inet_pton(AF_INET6, src.c_str(), &addr);
60     return addr;
61   }
62 };
63
64 struct AddressFlags {
65   std::string address;
66   uint8_t flags;
67   uint8_t version;
68
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;
76
77   AddressFlags(const std::string& addr, uint8_t version, uint8_t flags)
78     : address(addr)
79     , flags(flags)
80     , version(version)
81   {}
82
83   bool isLoopback() const {
84     return (flags & IS_LOCAL);
85   }
86   bool isNonroutable() const {
87     return (flags & IS_NONROUTABLE);
88   }
89   bool isPrivate() const {
90     return (flags & IS_PRIVATE);
91   }
92   bool isZero() const {
93     return (flags & IS_ZERO);
94   }
95   bool isLinkLocal() const {
96     return (flags & IS_LINK_LOCAL);
97   }
98   bool isLinkLocalBroadcast() const {
99     return (flags & IS_LINK_LOCAL_BROADCAST);
100   }
101 };
102
103 struct MaskData {
104   std::string address;
105   uint8_t mask;
106   std::string subnet;
107   MaskData(const std::string& addr, uint8_t mask,
108            const std::string& subnet)
109     : address(addr)
110     , mask(mask)
111     , subnet(subnet)
112   {}
113 };
114
115 struct MaskBoundaryData : MaskData {
116   bool inSubnet;
117   MaskBoundaryData(const std::string& addr, uint8_t mask,
118                    const std::string& subnet, bool inSubnet)
119     : MaskData(addr, mask, subnet)
120     , inSubnet(inSubnet)
121   {}
122 };
123
124 struct SerializeData {
125   std::string address;
126   ByteVector bytes;
127
128   SerializeData(const std::string& addr, const ByteVector& bytes)
129     : address(addr)
130     , bytes(bytes)
131   {}
132 };
133
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());
144     } else {
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());
149     }
150   }
151 };
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> {};
166 } // folly