1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 #include <cstdint>
18 #include <limits>
19 #include <sstream>
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/macros.h>
24 #include <fmt/format.h>
25 #include <gtest/gtest.h>
26 
27 #include "netdutils/InternetAddresses.h"
28 
29 namespace android {
30 namespace netdutils {
31 namespace {
32 
33 enum Relation { EQ, LT };
34 
operator <<(std::ostream & os,Relation relation)35 std::ostream& operator<<(std::ostream& os, Relation relation) {
36     switch (relation) {
37         case EQ: os << "eq"; break;
38         case LT: os << "lt"; break;
39         default: os << "?!"; break;
40     }
41     return os;
42 }
43 
44 template <typename T>
45 struct OperatorExpectation {
46     const Relation relation;
47     const T obj1;
48     const T obj2;
49 
toStringandroid::netdutils::__anon996be6b80111::OperatorExpectation50     std::string toString() const {
51         std::stringstream output;
52         output << obj1 << " " << relation << " " << obj2;
53         return output.str();
54     }
55 };
56 
57 template <typename T>
testGamutOfOperators(const OperatorExpectation<T> & expectation)58 void testGamutOfOperators(const OperatorExpectation<T>& expectation) {
59     switch (expectation.relation) {
60         case EQ:
61             EXPECT_TRUE(expectation.obj1 == expectation.obj2);
62             EXPECT_TRUE(expectation.obj1 <= expectation.obj2);
63             EXPECT_TRUE(expectation.obj1 >= expectation.obj2);
64             EXPECT_FALSE(expectation.obj1 != expectation.obj2);
65             EXPECT_FALSE(expectation.obj1 < expectation.obj2);
66             EXPECT_FALSE(expectation.obj1 > expectation.obj2);
67             break;
68 
69         case LT:
70             EXPECT_TRUE(expectation.obj1 < expectation.obj2);
71             EXPECT_TRUE(expectation.obj1 <= expectation.obj2);
72             EXPECT_TRUE(expectation.obj1 != expectation.obj2);
73             EXPECT_FALSE(expectation.obj1 > expectation.obj2);
74             EXPECT_FALSE(expectation.obj1 >= expectation.obj2);
75             EXPECT_FALSE(expectation.obj1 == expectation.obj2);
76             break;
77 
78         default:
79             FAIL() << "Unknown relation given in test expectation";
80     }
81 }
82 
83 const in_addr IPV4_ANY{htonl(INADDR_ANY)};
84 const in_addr IPV4_LOOPBACK{htonl(INADDR_LOOPBACK)};
85 const in_addr IPV4_ONES{~0U};
86 const in6_addr IPV6_ANY = IN6ADDR_ANY_INIT;
87 const in6_addr IPV6_LOOPBACK = IN6ADDR_LOOPBACK_INIT;
88 const in6_addr FE80{{{0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}};
89 const in6_addr FE80_1{{{0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}};
90 const in6_addr FE80_2{{{0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,2}}};
91 const uint8_t ff = std::numeric_limits<uint8_t>::max();
92 const in6_addr IPV6_ONES{{{ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff}}};
93 
TEST(IPAddressTest,GamutOfOperators)94 TEST(IPAddressTest, GamutOfOperators) {
95     const std::vector<OperatorExpectation<IPAddress>> kExpectations{
96             {EQ, IPAddress(), IPAddress()},
97             {EQ, IPAddress(IPV4_ONES), IPAddress(IPV4_ONES)},
98             {EQ, IPAddress(IPV6_ONES), IPAddress(IPV6_ONES)},
99             {EQ, IPAddress(FE80_1), IPAddress(FE80_1)},
100             {EQ, IPAddress(FE80_2), IPAddress(FE80_2)},
101             {LT, IPAddress(), IPAddress(IPV4_ANY)},
102             {LT, IPAddress(), IPAddress(IPV4_ONES)},
103             {LT, IPAddress(), IPAddress(IPV6_ANY)},
104             {LT, IPAddress(), IPAddress(IPV6_ONES)},
105             {LT, IPAddress(IPV4_ANY), IPAddress(IPV4_ONES)},
106             {LT, IPAddress(IPV4_ANY), IPAddress(IPV6_ANY)},
107             {LT, IPAddress(IPV4_ONES), IPAddress(IPV6_ANY)},
108             {LT, IPAddress(IPV4_ONES), IPAddress(IPV6_ONES)},
109             {LT, IPAddress(IPV6_ANY), IPAddress(IPV6_LOOPBACK)},
110             {LT, IPAddress(IPV6_ANY), IPAddress(IPV6_ONES)},
111             {LT, IPAddress(IPV6_LOOPBACK), IPAddress(IPV6_ONES)},
112             {LT, IPAddress(FE80_1), IPAddress(FE80_2)},
113             {LT, IPAddress(FE80_1), IPAddress(IPV6_ONES)},
114             {LT, IPAddress(FE80_2), IPAddress(IPV6_ONES)},
115             // Sort by scoped_id within the same address.
116             {LT, IPAddress(FE80_1), IPAddress(FE80_1, 1)},
117             {LT, IPAddress(FE80_1, 1), IPAddress(FE80_1, 2)},
118             // Sort by address first, scope_id second.
119             {LT, IPAddress(FE80_1, 2), IPAddress(FE80_2, 1)},
120     };
121 
122     size_t tests_run = 0;
123     for (const auto& expectation : kExpectations) {
124         SCOPED_TRACE(expectation.toString());
125         EXPECT_NO_FATAL_FAILURE(testGamutOfOperators(expectation));
126         tests_run++;
127     }
128     EXPECT_EQ(kExpectations.size(), tests_run);
129 }
130 
TEST(IPAddressTest,ScopeIds)131 TEST(IPAddressTest, ScopeIds) {
132     // Scope IDs ignored for IPv4 addresses.
133     const IPAddress ones(IPV4_ONES);
134     EXPECT_EQ(0U, ones.scope_id());
135     const IPAddress ones22(ones, 22);
136     EXPECT_EQ(0U, ones22.scope_id());
137     EXPECT_EQ(ones, ones22);
138     const IPAddress ones23(ones, 23);
139     EXPECT_EQ(0U, ones23.scope_id());
140     EXPECT_EQ(ones22, ones23);
141 
142     EXPECT_EQ("fe80::1%22", IPAddress(FE80_1, 22).toString());
143     EXPECT_EQ("fe80::2%23", IPAddress(FE80_2, 23).toString());
144 
145     // Verify that given an IPAddress with a scope_id an address without a
146     // scope_id can be constructed (just in case it's useful).
147     const IPAddress fe80_intf22(FE80_1, 22);
148     EXPECT_EQ(22U, fe80_intf22.scope_id());
149     EXPECT_EQ(fe80_intf22, IPAddress(fe80_intf22));
150     EXPECT_EQ(IPAddress(FE80_1), IPAddress(fe80_intf22, 0));
151 }
152 
TEST(IPAddressTest,forString)153 TEST(IPAddressTest, forString) {
154     IPAddress ip;
155 
156     EXPECT_FALSE(IPAddress::forString("not_an_ip", &ip));
157     EXPECT_FALSE(IPAddress::forString("not_an_ip", nullptr));
158     EXPECT_EQ(IPAddress(), IPAddress::forString("not_an_ip"));
159 
160     EXPECT_EQ(IPAddress(IPV4_ANY), IPAddress::forString("0.0.0.0"));
161     EXPECT_EQ(IPAddress(IPV4_ONES), IPAddress::forString("255.255.255.255"));
162     EXPECT_EQ(IPAddress(IPV4_LOOPBACK), IPAddress::forString("127.0.0.1"));
163 
164     EXPECT_EQ(IPAddress(IPV6_ANY), IPAddress::forString("::"));
165     EXPECT_EQ(IPAddress(IPV6_ANY), IPAddress::forString("::0"));
166     EXPECT_EQ(IPAddress(IPV6_ANY), IPAddress::forString("0::"));
167     EXPECT_EQ(IPAddress(IPV6_LOOPBACK), IPAddress::forString("::1"));
168     EXPECT_EQ(IPAddress(IPV6_LOOPBACK), IPAddress::forString("0::1"));
169     EXPECT_EQ(IPAddress(FE80_1), IPAddress::forString("fe80::1"));
170     EXPECT_EQ(IPAddress(FE80_1, 22), IPAddress::forString("fe80::1%22"));
171     // This relies upon having a loopback interface named "lo" with ifindex 1.
172     EXPECT_EQ(IPAddress(FE80_1, 1), IPAddress::forString("fe80::1%lo"));
173 }
174 
TEST(IPPrefixTest,forString)175 TEST(IPPrefixTest, forString) {
176     IPPrefix prefix;
177 
178     EXPECT_FALSE(IPPrefix::forString("", &prefix));
179     EXPECT_FALSE(IPPrefix::forString("invalid", &prefix));
180     EXPECT_FALSE(IPPrefix::forString("192.0.2.0", &prefix));
181     EXPECT_FALSE(IPPrefix::forString("2001::db8::", &prefix));
182 
183     EXPECT_FALSE(IPPrefix::forString("2001:db8::/", &prefix));
184     EXPECT_FALSE(IPPrefix::forString("2001:db8:://32", &prefix));
185     EXPECT_FALSE(IPPrefix::forString("2001:db8::/32z", &prefix));
186     EXPECT_FALSE(IPPrefix::forString("2001:db8::/32/", &prefix));
187     EXPECT_FALSE(IPPrefix::forString("2001:db8::/0x20", &prefix));
188     EXPECT_FALSE(IPPrefix::forString("2001:db8:: /32", &prefix));
189     EXPECT_FALSE(IPPrefix::forString("2001:db8::/ 32", &prefix));
190     EXPECT_FALSE(IPPrefix::forString(" 2001:db8::/32", &prefix));
191     EXPECT_FALSE(IPPrefix::forString("2001:db8::/32 ", &prefix));
192     EXPECT_FALSE(IPPrefix::forString("2001:db8::/+32", &prefix));
193 
194     EXPECT_FALSE(IPPrefix::forString("192.0.2.0/33", &prefix));
195     EXPECT_FALSE(IPPrefix::forString("2001:db8::/129", &prefix));
196     EXPECT_FALSE(IPPrefix::forString("192.0.2.0/-1", &prefix));
197     EXPECT_FALSE(IPPrefix::forString("2001:db8::/-1", &prefix));
198 
199     EXPECT_TRUE(IPPrefix::forString("2001:db8::/32", &prefix));
200     EXPECT_EQ("2001:db8::/32", prefix.toString());
201     EXPECT_EQ(IPPrefix(IPAddress::forString("2001:db8::"), 32), prefix);
202 
203     EXPECT_EQ(IPPrefix(), IPPrefix::forString("invalid"));
204 
205     EXPECT_EQ("0.0.0.0/0", IPPrefix::forString("0.0.0.0/0").toString());
206     EXPECT_EQ("::/0", IPPrefix::forString("::/0").toString());
207     EXPECT_EQ("192.0.2.128/25", IPPrefix::forString("192.0.2.131/25").toString());
208     EXPECT_EQ("2001:db8:1:2:3:4:5:4/126",
209               IPPrefix::forString("2001:db8:1:2:3:4:5:6/126").toString());
210 }
211 
TEST(IPPrefixTest,IPv4Truncation)212 TEST(IPPrefixTest, IPv4Truncation) {
213     const auto prefixStr = [](int length) -> std::string {
214         return IPPrefix(IPAddress(IPV4_ONES), length).toString();
215     };
216 
217     EXPECT_EQ("0.0.0.0/0", prefixStr(0));
218 
219     EXPECT_EQ("128.0.0.0/1", prefixStr(1));
220     EXPECT_EQ("192.0.0.0/2", prefixStr(2));
221     EXPECT_EQ("224.0.0.0/3", prefixStr(3));
222     EXPECT_EQ("240.0.0.0/4", prefixStr(4));
223     EXPECT_EQ("248.0.0.0/5", prefixStr(5));
224     EXPECT_EQ("252.0.0.0/6", prefixStr(6));
225     EXPECT_EQ("254.0.0.0/7", prefixStr(7));
226     EXPECT_EQ("255.0.0.0/8", prefixStr(8));
227 
228     EXPECT_EQ("255.128.0.0/9", prefixStr(9));
229     EXPECT_EQ("255.192.0.0/10", prefixStr(10));
230     EXPECT_EQ("255.224.0.0/11", prefixStr(11));
231     EXPECT_EQ("255.240.0.0/12", prefixStr(12));
232     EXPECT_EQ("255.248.0.0/13", prefixStr(13));
233     EXPECT_EQ("255.252.0.0/14", prefixStr(14));
234     EXPECT_EQ("255.254.0.0/15", prefixStr(15));
235     EXPECT_EQ("255.255.0.0/16", prefixStr(16));
236 
237     EXPECT_EQ("255.255.128.0/17", prefixStr(17));
238     EXPECT_EQ("255.255.192.0/18", prefixStr(18));
239     EXPECT_EQ("255.255.224.0/19", prefixStr(19));
240     EXPECT_EQ("255.255.240.0/20", prefixStr(20));
241     EXPECT_EQ("255.255.248.0/21", prefixStr(21));
242     EXPECT_EQ("255.255.252.0/22", prefixStr(22));
243     EXPECT_EQ("255.255.254.0/23", prefixStr(23));
244     EXPECT_EQ("255.255.255.0/24", prefixStr(24));
245 
246     EXPECT_EQ("255.255.255.128/25", prefixStr(25));
247     EXPECT_EQ("255.255.255.192/26", prefixStr(26));
248     EXPECT_EQ("255.255.255.224/27", prefixStr(27));
249     EXPECT_EQ("255.255.255.240/28", prefixStr(28));
250     EXPECT_EQ("255.255.255.248/29", prefixStr(29));
251     EXPECT_EQ("255.255.255.252/30", prefixStr(30));
252     EXPECT_EQ("255.255.255.254/31", prefixStr(31));
253     EXPECT_EQ("255.255.255.255/32", prefixStr(32));
254 }
255 
TEST(IPPrefixTest,IPv6Truncation)256 TEST(IPPrefixTest, IPv6Truncation) {
257     const auto prefixStr = [](int length) -> std::string {
258         return IPPrefix(IPAddress(IPV6_ONES), length).toString();
259     };
260 
261     EXPECT_EQ("::/0", prefixStr(0));
262 
263     EXPECT_EQ("8000::/1", prefixStr(1));
264     EXPECT_EQ("c000::/2", prefixStr(2));
265     EXPECT_EQ("e000::/3", prefixStr(3));
266     EXPECT_EQ("f000::/4", prefixStr(4));
267     EXPECT_EQ("f800::/5", prefixStr(5));
268     EXPECT_EQ("fc00::/6", prefixStr(6));
269     EXPECT_EQ("fe00::/7", prefixStr(7));
270     EXPECT_EQ("ff00::/8", prefixStr(8));
271 
272     EXPECT_EQ("ff80::/9", prefixStr(9));
273     EXPECT_EQ("ffc0::/10", prefixStr(10));
274     EXPECT_EQ("ffe0::/11", prefixStr(11));
275     EXPECT_EQ("fff0::/12", prefixStr(12));
276     EXPECT_EQ("fff8::/13", prefixStr(13));
277     EXPECT_EQ("fffc::/14", prefixStr(14));
278     EXPECT_EQ("fffe::/15", prefixStr(15));
279     EXPECT_EQ("ffff::/16", prefixStr(16));
280 
281     EXPECT_EQ("ffff:8000::/17", prefixStr(17));
282     EXPECT_EQ("ffff:c000::/18", prefixStr(18));
283     EXPECT_EQ("ffff:e000::/19", prefixStr(19));
284     EXPECT_EQ("ffff:f000::/20", prefixStr(20));
285     EXPECT_EQ("ffff:f800::/21", prefixStr(21));
286     EXPECT_EQ("ffff:fc00::/22", prefixStr(22));
287     EXPECT_EQ("ffff:fe00::/23", prefixStr(23));
288     EXPECT_EQ("ffff:ff00::/24", prefixStr(24));
289 
290     EXPECT_EQ("ffff:ff80::/25", prefixStr(25));
291     EXPECT_EQ("ffff:ffc0::/26", prefixStr(26));
292     EXPECT_EQ("ffff:ffe0::/27", prefixStr(27));
293     EXPECT_EQ("ffff:fff0::/28", prefixStr(28));
294     EXPECT_EQ("ffff:fff8::/29", prefixStr(29));
295     EXPECT_EQ("ffff:fffc::/30", prefixStr(30));
296     EXPECT_EQ("ffff:fffe::/31", prefixStr(31));
297     EXPECT_EQ("ffff:ffff::/32", prefixStr(32));
298 
299     EXPECT_EQ("ffff:ffff:8000::/33", prefixStr(33));
300     EXPECT_EQ("ffff:ffff:c000::/34", prefixStr(34));
301     EXPECT_EQ("ffff:ffff:e000::/35", prefixStr(35));
302     EXPECT_EQ("ffff:ffff:f000::/36", prefixStr(36));
303     EXPECT_EQ("ffff:ffff:f800::/37", prefixStr(37));
304     EXPECT_EQ("ffff:ffff:fc00::/38", prefixStr(38));
305     EXPECT_EQ("ffff:ffff:fe00::/39", prefixStr(39));
306     EXPECT_EQ("ffff:ffff:ff00::/40", prefixStr(40));
307 
308     EXPECT_EQ("ffff:ffff:ff80::/41", prefixStr(41));
309     EXPECT_EQ("ffff:ffff:ffc0::/42", prefixStr(42));
310     EXPECT_EQ("ffff:ffff:ffe0::/43", prefixStr(43));
311     EXPECT_EQ("ffff:ffff:fff0::/44", prefixStr(44));
312     EXPECT_EQ("ffff:ffff:fff8::/45", prefixStr(45));
313     EXPECT_EQ("ffff:ffff:fffc::/46", prefixStr(46));
314     EXPECT_EQ("ffff:ffff:fffe::/47", prefixStr(47));
315     EXPECT_EQ("ffff:ffff:ffff::/48", prefixStr(48));
316 
317     EXPECT_EQ("ffff:ffff:ffff:8000::/49", prefixStr(49));
318     EXPECT_EQ("ffff:ffff:ffff:c000::/50", prefixStr(50));
319     EXPECT_EQ("ffff:ffff:ffff:e000::/51", prefixStr(51));
320     EXPECT_EQ("ffff:ffff:ffff:f000::/52", prefixStr(52));
321     EXPECT_EQ("ffff:ffff:ffff:f800::/53", prefixStr(53));
322     EXPECT_EQ("ffff:ffff:ffff:fc00::/54", prefixStr(54));
323     EXPECT_EQ("ffff:ffff:ffff:fe00::/55", prefixStr(55));
324     EXPECT_EQ("ffff:ffff:ffff:ff00::/56", prefixStr(56));
325 
326     EXPECT_EQ("ffff:ffff:ffff:ff80::/57", prefixStr(57));
327     EXPECT_EQ("ffff:ffff:ffff:ffc0::/58", prefixStr(58));
328     EXPECT_EQ("ffff:ffff:ffff:ffe0::/59", prefixStr(59));
329     EXPECT_EQ("ffff:ffff:ffff:fff0::/60", prefixStr(60));
330     EXPECT_EQ("ffff:ffff:ffff:fff8::/61", prefixStr(61));
331     EXPECT_EQ("ffff:ffff:ffff:fffc::/62", prefixStr(62));
332     EXPECT_EQ("ffff:ffff:ffff:fffe::/63", prefixStr(63));
333     EXPECT_EQ("ffff:ffff:ffff:ffff::/64", prefixStr(64));
334 
335     EXPECT_EQ("ffff:ffff:ffff:ffff:8000::/65", prefixStr(65));
336     EXPECT_EQ("ffff:ffff:ffff:ffff:c000::/66", prefixStr(66));
337     EXPECT_EQ("ffff:ffff:ffff:ffff:e000::/67", prefixStr(67));
338     EXPECT_EQ("ffff:ffff:ffff:ffff:f000::/68", prefixStr(68));
339     EXPECT_EQ("ffff:ffff:ffff:ffff:f800::/69", prefixStr(69));
340     EXPECT_EQ("ffff:ffff:ffff:ffff:fc00::/70", prefixStr(70));
341     EXPECT_EQ("ffff:ffff:ffff:ffff:fe00::/71", prefixStr(71));
342     EXPECT_EQ("ffff:ffff:ffff:ffff:ff00::/72", prefixStr(72));
343 
344     EXPECT_EQ("ffff:ffff:ffff:ffff:ff80::/73", prefixStr(73));
345     EXPECT_EQ("ffff:ffff:ffff:ffff:ffc0::/74", prefixStr(74));
346     EXPECT_EQ("ffff:ffff:ffff:ffff:ffe0::/75", prefixStr(75));
347     EXPECT_EQ("ffff:ffff:ffff:ffff:fff0::/76", prefixStr(76));
348     EXPECT_EQ("ffff:ffff:ffff:ffff:fff8::/77", prefixStr(77));
349     EXPECT_EQ("ffff:ffff:ffff:ffff:fffc::/78", prefixStr(78));
350     EXPECT_EQ("ffff:ffff:ffff:ffff:fffe::/79", prefixStr(79));
351     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff::/80", prefixStr(80));
352 
353     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:8000::/81", prefixStr(81));
354     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:c000::/82", prefixStr(82));
355     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:e000::/83", prefixStr(83));
356     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:f000::/84", prefixStr(84));
357     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:f800::/85", prefixStr(85));
358     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:fc00::/86", prefixStr(86));
359     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:fe00::/87", prefixStr(87));
360     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ff00::/88", prefixStr(88));
361 
362     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ff80::/89", prefixStr(89));
363     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffc0::/90", prefixStr(90));
364     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffe0::/91", prefixStr(91));
365     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:fff0::/92", prefixStr(92));
366     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:fff8::/93", prefixStr(93));
367     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:fffc::/94", prefixStr(94));
368     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:fffe::/95", prefixStr(95));
369     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff::/96", prefixStr(96));
370 
371     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:8000:0/97", prefixStr(97));
372     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:c000:0/98", prefixStr(98));
373     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:e000:0/99", prefixStr(99));
374     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:f000:0/100", prefixStr(100));
375     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:f800:0/101", prefixStr(101));
376     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:fc00:0/102", prefixStr(102));
377     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:fe00:0/103", prefixStr(103));
378     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ff00:0/104", prefixStr(104));
379 
380     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ff80:0/105", prefixStr(105));
381     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffc0:0/106", prefixStr(106));
382     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffe0:0/107", prefixStr(107));
383     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:fff0:0/108", prefixStr(108));
384     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:fff8:0/109", prefixStr(109));
385     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:fffc:0/110", prefixStr(110));
386     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:fffe:0/111", prefixStr(111));
387     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0/112", prefixStr(112));
388 
389     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:8000/113", prefixStr(113));
390     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:c000/114", prefixStr(114));
391     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:e000/115", prefixStr(115));
392     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:f000/116", prefixStr(116));
393     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:f800/117", prefixStr(117));
394     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fc00/118", prefixStr(118));
395     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fe00/119", prefixStr(119));
396     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00/120", prefixStr(120));
397 
398     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff80/121", prefixStr(121));
399     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffc0/122", prefixStr(122));
400     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0/123", prefixStr(123));
401     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0/124", prefixStr(124));
402     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff8/125", prefixStr(125));
403     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc/126", prefixStr(126));
404     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe/127", prefixStr(127));
405     EXPECT_EQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128", prefixStr(128));
406 }
407 
TEST(IPPrefixTest,TruncationOther)408 TEST(IPPrefixTest, TruncationOther) {
409     const struct {
410         const char* ip;
411         const int cidrLen;
412         const char* ipTruncated;
413     } testExpectations[] = {
414             {"192.0.2.0", 24, "192.0.2.0"},
415             {"192.0.2.0", 23, "192.0.2.0"},
416             {"192.0.2.0", 22, "192.0.0.0"},
417             {"192.0.2.0", 1, "128.0.0.0"},
418             {"2001:db8:cafe:d00d::", 56, "2001:db8:cafe:d000::"},
419             {"2001:db8:cafe:d00d::", 48, "2001:db8:cafe::"},
420             {"2001:db8:cafe:d00d::", 47, "2001:db8:cafe::"},
421             {"2001:db8:cafe:d00d::", 46, "2001:db8:cafc::"},
422     };
423 
424     for (const auto& expectation : testExpectations) {
425         IPAddress ip;
426         EXPECT_TRUE(IPAddress::forString(expectation.ip, &ip))
427                 << "Failed to parse IP address " << expectation.ip;
428 
429         IPAddress ipTruncated;
430         EXPECT_TRUE(IPAddress::forString(expectation.ipTruncated, &ipTruncated))
431                 << "Failed to parse IP address " << expectation.ipTruncated;
432 
433         IPPrefix prefix(ip, expectation.cidrLen);
434 
435         EXPECT_EQ(expectation.cidrLen, prefix.length())
436                 << "Unexpected cidrLen " << expectation.cidrLen;
437         EXPECT_EQ(ipTruncated, prefix.ip())
438                 << "Unexpected IP truncation: " << prefix.ip() << ", expected: " << ipTruncated;
439     }
440 }
441 
TEST(IPPrefixTest,containsPrefix)442 TEST(IPPrefixTest, containsPrefix) {
443     const struct {
444         const char* prefix;
445         const char* otherPrefix;
446         const bool expected;
447         std::string asParameters() const {
448             return fmt::format("prefix={}, other={}, expect={}", prefix, otherPrefix, expected);
449         }
450     } testExpectations[] = {
451             {"192.0.0.0/8", "192.0.0.0/8", true},
452             {"192.1.0.0/16", "192.1.0.0/16", true},
453             {"192.1.2.0/24", "192.1.2.0/24", true},
454             {"192.1.2.3/32", "192.1.2.3/32", true},
455             {"0.0.0.0/0", "192.0.0.0/8", true},
456             {"0.0.0.0/0", "192.1.0.0/16", true},
457             {"0.0.0.0/0", "192.1.2.0/24", true},
458             {"0.0.0.0/0", "192.1.2.3/32", true},
459             {"192.0.0.0/8", "192.1.0.0/16", true},
460             {"192.0.0.0/8", "192.1.2.0/24", true},
461             {"192.0.0.0/8", "192.1.2.5/32", true},
462             {"192.1.0.0/16", "192.1.2.0/24", true},
463             {"192.1.0.0/16", "192.1.3.6/32", true},
464             {"192.5.6.0/24", "192.5.6.7/32", true},
465             {"192.1.2.3/32", "192.1.2.0/24", false},
466             {"192.1.2.3/32", "192.1.0.0/16", false},
467             {"192.1.2.3/32", "192.0.0.0/8", false},
468             {"192.1.2.3/32", "0.0.0.0/0", false},
469             {"192.1.2.0/24", "192.1.0.0/16", false},
470             {"192.1.2.0/24", "192.0.0.0/8", false},
471             {"192.1.2.0/24", "0.0.0.0/0", false},
472             {"192.9.0.0/16", "192.0.0.0/8", false},
473             {"192.9.0.0/16", "0.0.0.0/0", false},
474             {"192.0.0.0/8", "0.0.0.0/0", false},
475             {"192.0.0.0/8", "191.0.0.0/8", false},
476             {"191.0.0.0/8", "192.0.0.0/8", false},
477             {"192.8.0.0/16", "192.7.0.0/16", false},
478             {"192.7.0.0/16", "192.8.0.0/16", false},
479             {"192.8.6.0/24", "192.7.5.0/24", false},
480             {"192.7.5.0/24", "192.8.6.0/24", false},
481             {"192.8.6.100/32", "192.8.6.200/32", false},
482             {"192.8.6.200/32", "192.8.6.100/32", false},
483             {"192.0.0.0/8", "192.0.0.0/12", true},
484             {"192.0.0.0/12", "192.0.0.0/8", false},
485             {"2001::/16", "2001::/16", true},
486             {"2001:db8::/32", "2001:db8::/32", true},
487             {"2001:db8:cafe::/48", "2001:db8:cafe::/48", true},
488             {"2001:db8:cafe:d00d::/64", "2001:db8:cafe:d00d::/64", true},
489             {"2001:db8:cafe:d00d:fec0::/80", "2001:db8:cafe:d00d:fec0::/80", true},
490             {"2001:db8:cafe:d00d:fec0:de::/96", "2001:db8:cafe:d00d:fec0:de::/96", true},
491             {"2001:db8:cafe:d00d:fec0:de:ac::/112", "2001:db8:cafe:d00d:fec0:de:ac::/112", true},
492             {"2001:db8::cafe:0:1/128", "2001:db8::cafe:0:1/128", true},
493             {"2001::/16", "2001:db8::/32", true},
494             {"2001::/16", "2001:db8:cafe::/48", true},
495             {"2001::/16", "2001:db8:cafe:d00d::/64", true},
496             {"2001::/16", "2001:db8:cafe:d00d:fec0::/80", true},
497             {"2001::/16", "2001:db8:cafe:d00d:fec0:de::/96", true},
498             {"2001::/16", "2001:db8:cafe:d00d:fec0:de:ac::/112", true},
499             {"2001::/16", "2001:db8:cafe:d00d:fec0:de:ac:dd/128", true},
500             {"::/0", "2001::/16", true},
501             {"::/0", "2001:db8::/32", true},
502             {"::/0", "2001:db8:cafe::/48", true},
503             {"::/0", "2001:db8:cafe:d00d::/64", true},
504             {"::/0", "2001:db8:cafe:d00d:fec0::/80", true},
505             {"::/0", "2001:db8:cafe:d00d:fec0:de::/96", true},
506             {"::/0", "2001:db8:cafe:d00d:fec0:de:ac::/112", true},
507             {"::/0", "2001:db8:cafe:d00d:fec0:de:ac:dd/128", true},
508             {"2001:db8::dd/128", "2001::/16", false},
509             {"2001:db8::dd/128", "2001:db8::/32", false},
510             {"2001:db8::dd/128", "2001:db8:cafe::/48", false},
511             {"2001:db8::dd/128", "2001:db8:cafe:d00d::/64", false},
512             {"2001:db8::dd/128", "2001:db8:cafe:d00d:fec0::/80", false},
513             {"2001:db8::dd/128", "2001:db8:cafe:d00d:fec0:de::/96", false},
514             {"2001:db8::dd/128", "2001:db8:cafe:d00d:fec0:de:ac::/112", false},
515             {"2001:db7::/32", "2001:db8::/32", false},
516             {"2001:db8::/32", "2001:db7::/32", false},
517             {"2001:db8:caff::/48", "2001:db8:cafe::/48", false},
518             {"2001:db8:cafe::/48", "2001:db8:caff::/48", false},
519             {"2001:db8:cafe:a00d::/64", "2001:db8:cafe:d00d::/64", false},
520             {"2001:db8:cafe:d00d::/64", "2001:db8:cafe:a00d::/64", false},
521             {"2001:db8:cafe:d00d:fec1::/80", "2001:db8:cafe:d00d:fec0::/80", false},
522             {"2001:db8:cafe:d00d:fec0::/80", "2001:db8:cafe:d00d:fec1::/80", false},
523             {"2001:db8:cafe:d00d:fec0:dd::/96", "2001:db8:cafe:d00d:fec0:ae::/96", false},
524             {"2001:db8:cafe:d00d:fec0:ae::/96", "2001:db8:cafe:d00d:fec0:dd::/96", false},
525             {"2001:db8:cafe:d00d:fec0:de:aa::/112", "2001:db8:cafe:d00d:fec0:de:ac::/112", false},
526             {"2001:db8:cafe:d00d:fec0:de:ac::/112", "2001:db8:cafe:d00d:fec0:de:aa::/112", false},
527             {"2001:db8::cafe:0:123/128", "2001:db8::cafe:0:456/128", false},
528             {"2001:db8::cafe:0:456/128", "2001:db8::cafe:0:123/128", false},
529             {"2001:db8::/32", "2001:db8::/64", true},
530             {"2001:db8::/64", "2001:db8::/32", false},
531             {"::/0", "0.0.0.0/0", false},
532             {"::/0", "1.0.0.0/8", false},
533             {"::/0", "1.2.0.0/16", false},
534             {"::/0", "1.2.3.0/24", false},
535             {"::/0", "1.2.3.4/32", false},
536             {"2001::/16", "1.2.3.4/32", false},
537             {"2001::db8::/32", "1.2.3.4/32", false},
538             {"2001:db8:cafe::/48", "1.2.3.4/32", false},
539             {"2001:db8:cafe:d00d::/64", "1.2.3.4/32", false},
540             {"2001:db8:cafe:d00d:fec0::/80", "1.2.3.4/32", false},
541             {"2001:db8:cafe:d00d:fec0:ae::/96", "1.2.3.4/32", false},
542             {"2001:db8:cafe:d00d:fec0:de:aa::/112", "1.2.3.4/32", false},
543             {"0.0.0.0/0", "::/0", false},
544             {"0.0.0.0/0", "2001::/16", false},
545             {"0.0.0.0/0", "2001::db8::/32", false},
546             {"0.0.0.0/0", "2001:db8:cafe::/48", false},
547             {"0.0.0.0/0", "2001:db8:cafe:d00d::/64", false},
548             {"0.0.0.0/0", "2001:db8:cafe:d00d:fec0::/80", false},
549             {"0.0.0.0/0", "2001:db8:cafe:d00d:fec0:ae::/96", false},
550             {"0.0.0.0/0", "2001:db8:cafe:d00d:fec0:de:aa::/112", false},
551             {"1.2.3.4/32", "2001:db8:cafe:d00d:fec0:de:aa::/112", false},
552             {"1.2.3.0/24", "2001:db8:cafe:d00d:fec0:de:aa::/112", false},
553             {"1.2.0.0/16", "2001:db8:cafe:d00d:fec0:de:aa::/112", false},
554             {"1.0.0.0/8", "2001:db8:cafe:d00d:fec0:de:aa::/112", false},
555     };
556 
557     for (const auto& expectation : testExpectations) {
558         SCOPED_TRACE(expectation.asParameters());
559         IPPrefix a = IPPrefix::forString(expectation.prefix);
560         IPPrefix b = IPPrefix::forString(expectation.otherPrefix);
561         EXPECT_EQ(expectation.expected, a.contains(b));
562     }
563 }
564 
TEST(IPPrefixTest,containsAddress)565 TEST(IPPrefixTest, containsAddress) {
566     const struct {
567         const char* prefix;
568         const char* address;
569         const bool expected;
570         std::string asParameters() const {
571             return fmt::format("prefix={}, address={}, expect={}", prefix, address, expected);
572         }
573     } testExpectations[] = {
574         {"0.0.0.0/0", "255.255.255.255", true},
575         {"0.0.0.0/0", "1.2.3.4", true},
576         {"0.0.0.0/0", "1.2.3.0", true},
577         {"0.0.0.0/0", "1.2.0.0", true},
578         {"0.0.0.0/0", "1.0.0.0", true},
579         {"0.0.0.0/0", "0.0.0.0", true},
580         {"0.0.0.0/0", "2001:4868:4860::8888", false},
581         {"0.0.0.0/0", "::/0", false},
582         {"192.0.2.0/23", "192.0.2.0", true},
583         {"192.0.2.0/23", "192.0.2.43", true},
584         {"192.0.2.0/23", "192.0.3.21", true},
585         {"192.0.2.0/23", "192.0.0.21", false},
586         {"192.0.2.0/23", "8.8.8.8", false},
587         {"192.0.2.0/23", "2001:4868:4860::8888", false},
588         {"192.0.2.0/23", "::/0", false},
589         {"1.2.3.4/32", "1.2.3.4", true},
590         {"1.2.3.4/32", "1.2.3.5", false},
591         {"10.0.0.0/8", "10.2.0.0", true},
592         {"10.0.0.0/8", "10.2.3.5", true},
593         {"10.0.0.0/8", "10.0.0.0", true},
594         {"10.0.0.0/8", "10.255.255.254", true},
595         {"10.0.0.0/8", "11.0.0.0", false},
596         {"::/0", "2001:db8:f000::ace:d00c", true},
597         {"::/0", "2002:db8:f00::ace:d00d", true},
598         {"::/0", "2001:db7:f00::ace:d00e", true},
599         {"::/0", "2001:db8:f01::bad:d00d", true},
600         {"::/0", "::", true},
601         {"::/0", "0.0.0.0", false},
602         {"::/0", "1.2.3.4", false},
603         {"2001:db8:f00::ace:d00d/127", "2001:db8:f00::ace:d00c", true},
604         {"2001:db8:f00::ace:d00d/127", "2001:db8:f00::ace:d00d", true},
605         {"2001:db8:f00::ace:d00d/127", "2001:db8:f00::ace:d00e", false},
606         {"2001:db8:f00::ace:d00d/127", "2001:db8:f00::bad:d00d", false},
607         {"2001:db8:f00::ace:d00d/127", "2001:4868:4860::8888", false},
608         {"2001:db8:f00::ace:d00d/127", "8.8.8.8", false},
609         {"2001:db8:f00::ace:d00d/127", "0.0.0.0", false},
610         {"2001:db8:f00::ace:d00d/128", "2001:db8:f00::ace:d00d", true},
611         {"2001:db8:f00::ace:d00d/128", "2001:db8:f00::ace:d00c", false},
612         {"2001::/16", "2001::", true},
613         {"2001::/16", "2001:db8:f00::ace:d00d", true},
614         {"2001::/16", "2001:db8:f00::bad:d00d", true},
615         {"2001::/16", "2001::abc", true},
616         {"2001::/16", "2001:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true},
617         {"2001::/16", "2000::", false},
618     };
619 
620     for (const auto& expectation : testExpectations) {
621         SCOPED_TRACE(expectation.asParameters());
622         IPPrefix a = IPPrefix::forString(expectation.prefix);
623         IPAddress b = IPAddress::forString(expectation.address);
624         EXPECT_EQ(expectation.expected, a.contains(b));
625     }
626 }
627 
TEST(IPPrefixTest,GamutOfOperators)628 TEST(IPPrefixTest, GamutOfOperators) {
629     const std::vector<OperatorExpectation<IPPrefix>> kExpectations{
630             {EQ, IPPrefix(), IPPrefix()},
631             {EQ, IPPrefix(IPAddress(IPV4_ANY), 0), IPPrefix(IPAddress(IPV4_ANY), 0)},
632             {EQ, IPPrefix(IPAddress(IPV4_ANY), IPV4_ADDR_BITS), IPPrefix(IPAddress(IPV4_ANY))},
633             {EQ, IPPrefix(IPAddress(IPV6_ANY), 0), IPPrefix(IPAddress(IPV6_ANY), 0)},
634             {EQ, IPPrefix(IPAddress(IPV6_ANY), IPV6_ADDR_BITS), IPPrefix(IPAddress(IPV6_ANY))},
635             // Needlessly fully-specified IPv6 link-local address.
636             {EQ, IPPrefix(IPAddress(FE80_1)), IPPrefix(IPAddress(FE80_1, 0), IPV6_ADDR_BITS)},
637             // Different IPv6 link-local addresses within the same /64, no scoped_id: same /64.
638             {EQ, IPPrefix(IPAddress(FE80_1), 64), IPPrefix(IPAddress(FE80_2), 64)},
639             // Different IPv6 link-local address within the same /64, same scoped_id: same /64.
640             {EQ, IPPrefix(IPAddress(FE80_1, 17), 64), IPPrefix(IPAddress(FE80_2, 17), 64)},
641             // Unspecified < IPv4.
642             {LT, IPPrefix(), IPPrefix(IPAddress(IPV4_ANY), 0)},
643             // Same IPv4 base address sorts by prefix length.
644             {LT, IPPrefix(IPAddress(IPV4_ANY), 0), IPPrefix(IPAddress(IPV4_ANY), 1)},
645             {LT, IPPrefix(IPAddress(IPV4_ANY), 1), IPPrefix(IPAddress(IPV4_ANY), IPV4_ADDR_BITS)},
646             // Truncation means each base IPv4 address is different.
647             {LT, IPPrefix(IPAddress(IPV4_ONES), 0), IPPrefix(IPAddress(IPV4_ONES), 1)},
648             {LT, IPPrefix(IPAddress(IPV4_ONES), 1), IPPrefix(IPAddress(IPV4_ONES), IPV4_ADDR_BITS)},
649             // Sort by base IPv4 addresses first.
650             {LT, IPPrefix(IPAddress(IPV4_ANY), 0), IPPrefix(IPAddress::forString("0.0.0.1"))},
651             {LT, IPPrefix(IPAddress(IPV4_ANY), 1), IPPrefix(IPAddress::forString("0.0.0.1"))},
652             {LT, IPPrefix(IPAddress(IPV4_ANY), 24), IPPrefix(IPAddress::forString("0.0.0.1"))},
653             // IPv4 < IPv6.
654             {LT, IPPrefix(IPAddress(IPV4_ANY), 0), IPPrefix(IPAddress(IPV6_ANY), 0)},
655             {LT, IPPrefix(IPAddress(IPV4_ONES)), IPPrefix(IPAddress(IPV6_ANY))},
656             // Unspecified < IPv6.
657             {LT, IPPrefix(), IPPrefix(IPAddress(IPV6_ANY), 0)},
658             // Same IPv6 base address sorts by prefix length.
659             {LT, IPPrefix(IPAddress(IPV6_ANY), 0), IPPrefix(IPAddress(IPV6_ANY), 1)},
660             {LT, IPPrefix(IPAddress(IPV6_ANY), 1), IPPrefix(IPAddress(IPV6_ANY), IPV6_ADDR_BITS)},
661             // Truncation means each base IPv6 address is different.
662             {LT, IPPrefix(IPAddress(IPV6_ONES), 0), IPPrefix(IPAddress(IPV6_ONES), 1)},
663             {LT, IPPrefix(IPAddress(IPV6_ONES), 1), IPPrefix(IPAddress(IPV6_ONES), IPV6_ADDR_BITS)},
664             // Different IPv6 link-local address in same /64, different scoped_id: different /64.
665             {LT, IPPrefix(IPAddress(FE80_1, 17), 64), IPPrefix(IPAddress(FE80_2, 22), 64)},
666             {LT, IPPrefix(IPAddress(FE80_1, 17), 64), IPPrefix(IPAddress(FE80_1, 18), 64)},
667             {LT, IPPrefix(IPAddress(FE80_1, 18), 64), IPPrefix(IPAddress(FE80_1, 19), 64)},
668     };
669 
670     size_t tests_run = 0;
671     for (const auto& expectation : kExpectations) {
672         SCOPED_TRACE(expectation.toString());
673         EXPECT_NO_FATAL_FAILURE(testGamutOfOperators(expectation));
674         tests_run++;
675     }
676     EXPECT_EQ(kExpectations.size(), tests_run);
677 }
678 
TEST(IPSockAddrTest,GamutOfOperators)679 TEST(IPSockAddrTest, GamutOfOperators) {
680     const std::vector<OperatorExpectation<IPSockAddr>> kExpectations{
681             {EQ, IPSockAddr(), IPSockAddr()},
682             {EQ, IPSockAddr(IPAddress(IPV4_ANY)), IPSockAddr(IPAddress(IPV4_ANY), 0)},
683             {EQ, IPSockAddr(IPAddress(IPV6_ANY)), IPSockAddr(IPAddress(IPV6_ANY), 0)},
684             {EQ, IPSockAddr(IPAddress(FE80_1), 80), IPSockAddr(IPAddress(FE80_1), 80)},
685             {EQ, IPSockAddr(IPAddress(FE80_1, 17)), IPSockAddr(IPAddress(FE80_1, 17), 0)},
686             {LT, IPSockAddr(IPAddress(IPV4_ANY), 0), IPSockAddr(IPAddress(IPV4_ANY), 1)},
687             {LT, IPSockAddr(IPAddress(IPV4_ANY), 53), IPSockAddr(IPAddress(IPV4_ANY), 123)},
688             {LT, IPSockAddr(IPAddress(IPV4_ONES), 123), IPSockAddr(IPAddress(IPV6_ANY), 53)},
689             {LT, IPSockAddr(IPAddress(IPV6_ANY), 0), IPSockAddr(IPAddress(IPV6_ANY), 1)},
690             {LT, IPSockAddr(IPAddress(IPV6_ANY), 53), IPSockAddr(IPAddress(IPV6_ANY), 123)},
691             {LT, IPSockAddr(IPAddress(FE80_1), 80), IPSockAddr(IPAddress(FE80_1, 17), 80)},
692             {LT, IPSockAddr(IPAddress(FE80_1, 17), 80), IPSockAddr(IPAddress(FE80_1, 22), 80)},
693     };
694 
695     size_t tests_run = 0;
696     for (const auto& expectation : kExpectations) {
697         SCOPED_TRACE(expectation.toString());
698         EXPECT_NO_FATAL_FAILURE(testGamutOfOperators(expectation));
699         tests_run++;
700     }
701     EXPECT_EQ(kExpectations.size(), tests_run);
702 }
703 
TEST(IPSockAddrTest,toString)704 TEST(IPSockAddrTest, toString) {
705     EXPECT_EQ("<unspecified>:0", IPSockAddr().toString());
706     EXPECT_EQ("0.0.0.0:0", IPSockAddr(IPAddress(IPV4_ANY)).toString());
707     EXPECT_EQ("255.255.255.255:67", IPSockAddr(IPAddress(IPV4_ONES), 67).toString());
708     EXPECT_EQ("[::]:0", IPSockAddr(IPAddress(IPV6_ANY)).toString());
709     EXPECT_EQ("[::1]:53", IPSockAddr(IPAddress(IPV6_LOOPBACK), 53).toString());
710     EXPECT_EQ("[fe80::1]:0", IPSockAddr(IPAddress(FE80_1)).toString());
711     EXPECT_EQ("[fe80::2%17]:123", IPSockAddr(IPAddress(FE80_2, 17), 123).toString());
712 }
713 
TEST(CompatIPDataTest,ConversionsClearUnneededValues)714 TEST(CompatIPDataTest, ConversionsClearUnneededValues) {
715     const uint32_t idx = 17;
716     const IPSockAddr linkLocalNtpSockaddr(IPAddress(FE80_2, idx), 123);
717     EXPECT_EQ(IPAddress(FE80_2, idx), linkLocalNtpSockaddr.ip());
718     // IPSockAddr(IPSockaddr.ip()) see the port cleared.
719     EXPECT_EQ(0, IPSockAddr(linkLocalNtpSockaddr.ip()).port());
720     const IPPrefix linkLocalPrefix(linkLocalNtpSockaddr.ip(), 64);
721     EXPECT_EQ(IPAddress(FE80, idx), linkLocalPrefix.ip());
722     // IPPrefix(IPPrefix.ip()) see the CIDR length cleared.
723     EXPECT_EQ(IPV6_ADDR_BITS, IPPrefix(linkLocalPrefix.ip()).length());
724 }
725 
726 }  // namespace
727 }  // namespace netdutils
728 }  // namespace android
729