/Users/deen/code/yugabyte-db/src/yb/util/net/inetaddress-test.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/util/net/inetaddress.h" |
15 | | |
16 | | #include "yb/util/net/net_fwd.h" |
17 | | #include "yb/util/net/net_util.h" |
18 | | #include "yb/util/result.h" |
19 | | #include "yb/util/test_macros.h" |
20 | | #include "yb/util/test_util.h" |
21 | | |
22 | | namespace yb { |
23 | | |
24 | | class InetAddressTest : public YBTest { |
25 | | protected: |
26 | 6 | void RunRoundTrip(const std::string& strval) { |
27 | 6 | InetAddress addr_orig(ASSERT_RESULT(ParseIpAddress(strval))); |
28 | 6 | std::string bytes; |
29 | 6 | ASSERT_OK(addr_orig.ToBytes(&bytes)); |
30 | 6 | InetAddress addr_new; |
31 | 6 | ASSERT_OK(addr_new.FromBytes(bytes)); |
32 | 6 | std::string strval_new; |
33 | 6 | ASSERT_OK(addr_new.ToString(&strval_new)); |
34 | 6 | ASSERT_EQ(strval, strval_new); |
35 | 6 | ASSERT_EQ(addr_orig, addr_new); |
36 | 6 | } |
37 | | }; |
38 | | |
39 | 1 | TEST_F(InetAddressTest, TestRoundTrip) { |
40 | 1 | for (const std::string& strval : { |
41 | 1 | "1.2.3.4", |
42 | 1 | "2001:db8:a0b:12f0::1", |
43 | 1 | "0.0.0.0", |
44 | 1 | "2607:f0d0:1002:51::4", |
45 | 1 | "::1", |
46 | 6 | "255.255.255.255"}) { |
47 | 6 | RunRoundTrip(strval); |
48 | 6 | } |
49 | 1 | } |
50 | | |
51 | 1 | TEST_F(InetAddressTest, TestOperators) { |
52 | | // Assignment. |
53 | 1 | InetAddress addr1(ASSERT_RESULT(ParseIpAddress("1.2.3.4"))); |
54 | 1 | InetAddress addr2 = addr1; |
55 | 1 | std::string strval; |
56 | 1 | ASSERT_OK(addr2.ToString(&strval)); |
57 | 1 | ASSERT_EQ("1.2.3.4", strval); |
58 | | |
59 | | // InEquality. |
60 | 1 | addr1 = InetAddress(ASSERT_RESULT(ParseIpAddress("1.2.3.4"))); |
61 | 1 | addr2 = InetAddress(ASSERT_RESULT(ParseIpAddress("1.2.3.5"))); |
62 | 1 | ASSERT_NE(addr1, addr2); |
63 | | |
64 | | // Comparison. |
65 | 1 | addr1 = InetAddress(ASSERT_RESULT(ParseIpAddress("1.2.3.4"))); |
66 | 1 | addr2 = InetAddress(ASSERT_RESULT(ParseIpAddress("2001:db8:a0b:12f0::1"))); |
67 | | |
68 | | // v4 < v6 |
69 | 1 | ASSERT_LT(addr1, addr2); |
70 | 1 | ASSERT_GT(addr2, addr1); |
71 | | |
72 | 1 | addr1 = InetAddress(ASSERT_RESULT(ParseIpAddress("1.2.3.4"))); |
73 | 1 | addr2 = InetAddress(ASSERT_RESULT(ParseIpAddress("1.2.3.5"))); |
74 | 1 | ASSERT_LT(addr1, addr2); |
75 | 1 | ASSERT_LE(addr1, addr2); |
76 | | |
77 | 1 | addr1 = InetAddress(ASSERT_RESULT(ParseIpAddress("1.2.3.4"))); |
78 | 1 | addr2 = InetAddress(ASSERT_RESULT(ParseIpAddress("1.2.3.4"))); |
79 | 1 | ASSERT_LE(addr1, addr2); |
80 | 1 | ASSERT_GE(addr1, addr2); |
81 | 1 | } |
82 | | |
83 | 1 | TEST_F(InetAddressTest, TestErrors) { |
84 | 1 | InetAddress addr; |
85 | 1 | ASSERT_FALSE(ParseIpAddress("1.2.3.256").ok()); |
86 | 1 | ASSERT_FALSE(ParseIpAddress("1:2:3:f").ok()); |
87 | 1 | ASSERT_FALSE(ParseIpAddress("2607:g0d0:1002:51::4").ok()); |
88 | | |
89 | 1 | std::string bytes; |
90 | 1 | ASSERT_FALSE(addr.FromBytes(bytes).ok()); |
91 | 1 | bytes = "0"; |
92 | 1 | ASSERT_FALSE(addr.FromBytes(bytes).ok()); |
93 | 1 | bytes = "012345"; |
94 | 1 | ASSERT_FALSE(addr.FromBytes(bytes).ok()); |
95 | 1 | bytes = "111111111111111111"; // 17 bytes. |
96 | 1 | ASSERT_FALSE(addr.FromBytes(bytes).ok()); |
97 | 1 | } |
98 | | |
99 | 1 | TEST_F(InetAddressTest, FilterAddresses) { |
100 | | |
101 | | // Create a list of ipv6 and ipv4 addresses |
102 | 1 | const vector<string> address_strs = { |
103 | 1 | "::1", "10.150.0.148", |
104 | 1 | "2600:1f18:1094:c832:36e6:43b9:e6c8:f02", "127.0.0.1", |
105 | 1 | "127.0.0.2", "0.0.0.0", |
106 | 1 | "::", "fe80::4001:aff:fe96:94", |
107 | 1 | "fe80::2%lo" |
108 | 1 | }; |
109 | 1 | vector<IpAddress> addresses; |
110 | 9 | for (const auto &address_str : address_strs) { |
111 | 9 | LOG(INFO) << address_str; |
112 | 9 | addresses.push_back(IpAddress::from_string(address_str)); |
113 | 9 | } |
114 | 1 | LOG(INFO) << "Starting test"; |
115 | 1 | auto test_addresses = addresses; |
116 | 1 | FilterAddresses("ipv4_all", &test_addresses); |
117 | 1 | ASSERT_EQ(test_addresses.size(), 4); |
118 | | |
119 | 1 | test_addresses = addresses; |
120 | 1 | FilterAddresses("ipv6_all", &test_addresses); |
121 | 1 | ASSERT_EQ(test_addresses.size(), 5); |
122 | | |
123 | 1 | test_addresses = addresses; |
124 | 1 | FilterAddresses("ipv6_all,ipv4_all", &test_addresses); |
125 | 1 | ASSERT_EQ(test_addresses.size(), 9); |
126 | 1 | ASSERT_TRUE(test_addresses[0].is_v6()); |
127 | 1 | ASSERT_TRUE(test_addresses[test_addresses.size() - 1].is_v4()); |
128 | | |
129 | 1 | test_addresses = addresses; |
130 | 1 | FilterAddresses("ipv6_external", &test_addresses); |
131 | 1 | ASSERT_EQ(test_addresses.size(), 1); |
132 | | |
133 | 1 | test_addresses = addresses; |
134 | 1 | FilterAddresses("ipv6_non_link_local", &test_addresses); |
135 | 1 | ASSERT_EQ(test_addresses.size(), 2); |
136 | | |
137 | 1 | test_addresses = addresses; |
138 | 1 | FilterAddresses("ipv4_external", &test_addresses); |
139 | 1 | ASSERT_EQ(test_addresses.size(), 1); |
140 | | |
141 | 1 | test_addresses = addresses; |
142 | 1 | FilterAddresses("ipv4_external,ipv6_external", &test_addresses); |
143 | 1 | ASSERT_EQ(test_addresses.size(), 2); |
144 | 1 | ASSERT_TRUE(test_addresses[0].is_v4()); |
145 | 1 | ASSERT_TRUE(test_addresses[test_addresses.size() - 1].is_v6()); |
146 | | |
147 | 1 | test_addresses = addresses; |
148 | 1 | FilterAddresses("ipv6_external,ipv4_external", &test_addresses); |
149 | 1 | ASSERT_EQ(test_addresses.size(), 2); |
150 | 1 | ASSERT_TRUE(test_addresses[0].is_v6()); |
151 | 1 | ASSERT_TRUE(test_addresses[test_addresses.size() - 1].is_v4()); |
152 | 1 | } |
153 | | |
154 | | } // namespace yb |