• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2012 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 "shill/net/ip_address.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <arpa/inet.h>
22 
23 #include "shill/net/byte_string.h"
24 
25 using std::string;
26 using testing::Test;
27 
28 namespace shill {
29 
30 namespace {
31 const char kV4String1[] = "192.168.10.1";
32 const unsigned char kV4Address1[] = { 192, 168, 10, 1 };
33 const char kV4String2[] = "192.168.10";
34 const unsigned char kV4Address2[] = { 192, 168, 10 };
35 const char kV6String1[] = "fe80::1aa9:5ff:7ebf:14c5";
36 const unsigned char kV6Address1[] = { 0xfe, 0x80, 0x00, 0x00,
37                                       0x00, 0x00, 0x00, 0x00,
38                                       0x1a, 0xa9, 0x05, 0xff,
39                                       0x7e, 0xbf, 0x14, 0xc5 };
40 const char kV6String2[] = "1980:0:1000:1b02:1aa9:5ff:7ebf";
41 const unsigned char kV6Address2[] = { 0x19, 0x80, 0x00, 0x00,
42                                       0x10, 0x00, 0x1b, 0x02,
43                                       0x1a, 0xa9, 0x05, 0xff,
44                                       0x7e, 0xbf };
45 }  // namespace
46 
47 class IPAddressTest : public Test {
48  protected:
TestAddress(IPAddress::Family family,const string & good_string,const ByteString & good_bytes,const string & bad_string,const ByteString & bad_bytes)49   void TestAddress(IPAddress::Family family,
50                    const string& good_string,
51                    const ByteString& good_bytes,
52                    const string& bad_string,
53                    const ByteString& bad_bytes) {
54     IPAddress good_addr(family);
55 
56     EXPECT_TRUE(good_addr.SetAddressFromString(good_string));
57     EXPECT_EQ(IPAddress::GetAddressLength(family), good_addr.GetLength());
58     EXPECT_EQ(family, good_addr.family());
59     EXPECT_FALSE(good_addr.IsDefault());
60     EXPECT_EQ(0, memcmp(good_addr.GetConstData(), good_bytes.GetConstData(),
61                         good_bytes.GetLength()));
62     EXPECT_TRUE(good_addr.address().Equals(good_bytes));
63     string address_string;
64     EXPECT_TRUE(good_addr.IntoString(&address_string));
65     EXPECT_EQ(good_string, address_string);
66 
67     IPAddress good_addr_from_bytes(family, good_bytes);
68     EXPECT_TRUE(good_addr.Equals(good_addr_from_bytes));
69 
70     IPAddress good_addr_from_string(good_string);
71     EXPECT_EQ(family, good_addr_from_string.family());
72 
73     IPAddress bad_addr(family);
74     EXPECT_FALSE(bad_addr.SetAddressFromString(bad_string));
75     EXPECT_FALSE(good_addr.Equals(bad_addr));
76 
77     EXPECT_FALSE(bad_addr.IsValid());
78 
79     IPAddress bad_addr_from_bytes(family, bad_bytes);
80     EXPECT_EQ(family, bad_addr_from_bytes.family());
81     EXPECT_FALSE(bad_addr_from_bytes.IsValid());
82 
83     IPAddress bad_addr_from_string(bad_string);
84     EXPECT_EQ(IPAddress::kFamilyUnknown, bad_addr_from_string.family());
85 
86     EXPECT_FALSE(bad_addr.Equals(bad_addr_from_bytes));
87     EXPECT_FALSE(bad_addr.IntoString(&address_string));
88 
89     sockaddr_storage storage = {};
90     auto addr = reinterpret_cast<sockaddr*>(&storage);
91     addr->sa_family = family;
92     ssize_t addr_size;
93     if (family == IPAddress::kFamilyIPv6) {
94       auto sin6 = reinterpret_cast<sockaddr_in6*>(addr);
95       inet_pton(AF_INET6, good_string.c_str(), &sin6->sin6_addr.s6_addr);
96       addr_size = sizeof(sockaddr_in6);
97     } else {
98       auto sin = reinterpret_cast<sockaddr_in*>(addr);
99       inet_pton(AF_INET, good_string.c_str(), &sin->sin_addr.s_addr);
100       addr_size = sizeof(sockaddr_in);
101     }
102     IPAddress from_short_sockaddr(addr, addr_size - 1);
103     EXPECT_FALSE(from_short_sockaddr.IsValid());
104     IPAddress from_sockaddr(addr, addr_size);
105     EXPECT_TRUE(from_sockaddr.IsValid());
106     EXPECT_EQ(family, from_sockaddr.family());
107     EXPECT_TRUE(from_sockaddr.IntoString(&address_string));
108     EXPECT_EQ(good_string, address_string);
109 
110     sockaddr_storage storage_empty = {};
111     sockaddr_storage storage2 = {};
112     auto addr2 = reinterpret_cast<sockaddr*>(&storage2);
113     EXPECT_FALSE(from_short_sockaddr.IntoSockAddr(addr2, addr_size));
114     EXPECT_EQ(0, memcmp(&storage2, &storage_empty, sizeof(storage2)));
115     EXPECT_FALSE(from_sockaddr.IntoSockAddr(addr2, addr_size - 1));
116     EXPECT_EQ(0, memcmp(&storage2, &storage_empty, sizeof(storage2)));
117     EXPECT_TRUE(from_sockaddr.IntoSockAddr(addr2, addr_size));
118     EXPECT_EQ(0, memcmp(&storage2, &storage, sizeof(storage2)));
119   }
120 };
121 
TEST_F(IPAddressTest,Statics)122 TEST_F(IPAddressTest, Statics) {
123   EXPECT_EQ(4, IPAddress::GetAddressLength(IPAddress::kFamilyIPv4));
124   EXPECT_EQ(16, IPAddress::GetAddressLength(IPAddress::kFamilyIPv6));
125 
126   EXPECT_EQ(0, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4,
127                                                   "0.0.0.0"));
128   EXPECT_EQ(20, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4,
129                                                    "255.255.240.0"));
130   EXPECT_EQ(32, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4,
131                                                    "255.255.255.255"));
132   EXPECT_EQ(32, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4,
133                                                    ""));
134   EXPECT_EQ(32, IPAddress::GetPrefixLengthFromMask(IPAddress::kFamilyIPv4,
135                                                    "foo"));
136 
137   IPAddress addr4(IPAddress::kFamilyIPv4);
138   addr4.SetAddressToDefault();
139 
140   EXPECT_EQ(4, addr4.GetLength());
141   EXPECT_EQ(IPAddress::kFamilyIPv4, addr4.family());
142   EXPECT_TRUE(addr4.IsDefault());
143   EXPECT_TRUE(addr4.address().IsZero());
144   EXPECT_TRUE(addr4.address().Equals(ByteString(4)));
145 
146 
147   IPAddress addr6(IPAddress::kFamilyIPv6);
148   addr6.SetAddressToDefault();
149 
150   EXPECT_EQ(16, addr6.GetLength());
151   EXPECT_EQ(addr6.family(), IPAddress::kFamilyIPv6);
152   EXPECT_TRUE(addr6.IsDefault());
153   EXPECT_TRUE(addr6.address().IsZero());
154   EXPECT_TRUE(addr6.address().Equals(ByteString(16)));
155 
156   EXPECT_FALSE(addr4.Equals(addr6));
157 }
158 
TEST_F(IPAddressTest,IPv4)159 TEST_F(IPAddressTest, IPv4) {
160   TestAddress(IPAddress::kFamilyIPv4,
161               kV4String1, ByteString(kV4Address1, sizeof(kV4Address1)),
162               kV4String2, ByteString(kV4Address2, sizeof(kV4Address2)));
163 }
164 
165 
TEST_F(IPAddressTest,IPv6)166 TEST_F(IPAddressTest, IPv6) {
167   TestAddress(IPAddress::kFamilyIPv6,
168               kV6String1, ByteString(kV6Address1, sizeof(kV6Address1)),
169               kV6String2, ByteString(kV6Address2, sizeof(kV6Address2)));
170 }
171 
TEST_F(IPAddressTest,SetAddressAndPrefixFromString)172 TEST_F(IPAddressTest, SetAddressAndPrefixFromString) {
173   IPAddress address(IPAddress::kFamilyIPv4);
174   const string kString1(kV4String1);
175   const string kString2(kV4String2);
176   EXPECT_FALSE(address.SetAddressAndPrefixFromString(""));
177   EXPECT_FALSE(address.SetAddressAndPrefixFromString(kString1));
178   EXPECT_FALSE(address.SetAddressAndPrefixFromString(kString1 + "/"));
179   EXPECT_FALSE(address.SetAddressAndPrefixFromString(kString1 + "/10x"));
180   EXPECT_FALSE(address.SetAddressAndPrefixFromString(kString2 + "/10"));
181   EXPECT_TRUE(address.SetAddressAndPrefixFromString(kString1 + "/0"));
182   EXPECT_EQ(0, address.prefix());
183   EXPECT_TRUE(address.SetAddressAndPrefixFromString(kString1 + "/32"));
184   EXPECT_EQ(32, address.prefix());
185   EXPECT_FALSE(address.SetAddressAndPrefixFromString(kString1 + "/33"));
186   EXPECT_FALSE(address.SetAddressAndPrefixFromString(kString1 + "/-1"));
187   EXPECT_TRUE(address.SetAddressAndPrefixFromString(kString1 + "/10"));
188   EXPECT_EQ(10, address.prefix());
189   ByteString kAddress1(kV4Address1, sizeof(kV4Address1));
190   EXPECT_TRUE(kAddress1.Equals(address.address()));
191 }
192 
TEST_F(IPAddressTest,HasSameAddressAs)193 TEST_F(IPAddressTest, HasSameAddressAs) {
194   const string kString1(kV4String1);
195   IPAddress address0(IPAddress::kFamilyIPv4);
196   EXPECT_TRUE(address0.SetAddressAndPrefixFromString(kString1 + "/0"));
197   IPAddress address1(IPAddress::kFamilyIPv4);
198   EXPECT_TRUE(address1.SetAddressAndPrefixFromString(kString1 + "/10"));
199   IPAddress address2(IPAddress::kFamilyIPv4);
200   EXPECT_TRUE(address2.SetAddressAndPrefixFromString(kString1 + "/0"));
201 
202   EXPECT_FALSE(address0.Equals(address1));
203   EXPECT_TRUE(address0.Equals(address2));
204   EXPECT_TRUE(address0.HasSameAddressAs(address1));
205   EXPECT_TRUE(address0.HasSameAddressAs(address2));
206 }
207 
208 struct PrefixMapping {
PrefixMappingshill::PrefixMapping209   PrefixMapping() : family(IPAddress::kFamilyUnknown), prefix(0) {}
PrefixMappingshill::PrefixMapping210   PrefixMapping(IPAddress::Family family_in,
211                 size_t prefix_in,
212                 const string& expected_address_in)
213       : family(family_in),
214         prefix(prefix_in),
215         expected_address(expected_address_in) {}
216   IPAddress::Family family;
217   size_t prefix;
218   string expected_address;
219 };
220 
221 class IPAddressPrefixMappingTest
222     : public testing::TestWithParam<PrefixMapping> {};
223 
TEST_P(IPAddressPrefixMappingTest,TestPrefixMapping)224 TEST_P(IPAddressPrefixMappingTest, TestPrefixMapping) {
225   IPAddress address = IPAddress::GetAddressMaskFromPrefix(GetParam().family,
226                                                           GetParam().prefix);
227   IPAddress expected_address(GetParam().family);
228   EXPECT_TRUE(expected_address.SetAddressFromString(
229       GetParam().expected_address));
230   EXPECT_TRUE(expected_address.Equals(address));
231 }
232 
233 INSTANTIATE_TEST_CASE_P(
234     IPAddressPrefixMappingTestRun,
235     IPAddressPrefixMappingTest,
236     ::testing::Values(
237         PrefixMapping(IPAddress::kFamilyIPv4, 0, "0.0.0.0"),
238         PrefixMapping(IPAddress::kFamilyIPv4, 1, "128.0.0.0"),
239         PrefixMapping(IPAddress::kFamilyIPv4, 4, "240.0.0.0"),
240         PrefixMapping(IPAddress::kFamilyIPv4, 7, "254.0.0.0"),
241         PrefixMapping(IPAddress::kFamilyIPv4, 10, "255.192.0.0"),
242         PrefixMapping(IPAddress::kFamilyIPv4, 13, "255.248.0.0"),
243         PrefixMapping(IPAddress::kFamilyIPv4, 16, "255.255.0.0"),
244         PrefixMapping(IPAddress::kFamilyIPv4, 19, "255.255.224.0"),
245         PrefixMapping(IPAddress::kFamilyIPv4, 22, "255.255.252.0"),
246         PrefixMapping(IPAddress::kFamilyIPv4, 25, "255.255.255.128"),
247         PrefixMapping(IPAddress::kFamilyIPv4, 28, "255.255.255.240"),
248         PrefixMapping(IPAddress::kFamilyIPv4, 31, "255.255.255.254"),
249         PrefixMapping(IPAddress::kFamilyIPv4, 32, "255.255.255.255"),
250         PrefixMapping(IPAddress::kFamilyIPv4, 33, "255.255.255.255"),
251         PrefixMapping(IPAddress::kFamilyIPv4, 34, "255.255.255.255"),
252         PrefixMapping(IPAddress::kFamilyIPv6, 0, "0::"),
253         PrefixMapping(IPAddress::kFamilyIPv6, 1, "8000::"),
254         PrefixMapping(IPAddress::kFamilyIPv6, 17, "ffff:8000::"),
255         PrefixMapping(IPAddress::kFamilyIPv6, 34, "ffff:ffff:c000::"),
256         PrefixMapping(IPAddress::kFamilyIPv6, 51, "ffff:ffff:ffff:e000::"),
257         PrefixMapping(IPAddress::kFamilyIPv6, 68,
258                       "ffff:ffff:ffff:ffff:f000::"),
259         PrefixMapping(IPAddress::kFamilyIPv6, 85,
260                       "ffff:ffff:ffff:ffff:ffff:f800::"),
261         PrefixMapping(IPAddress::kFamilyIPv6, 102,
262                       "ffff:ffff:ffff:ffff:ffff:ffff:fc00::"),
263         PrefixMapping(IPAddress::kFamilyIPv6, 119,
264                       "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fe00"),
265         PrefixMapping(IPAddress::kFamilyIPv6, 128,
266                       "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
267         PrefixMapping(IPAddress::kFamilyIPv6, 136,
268                       "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
269 
270 struct BitOperationMapping {
BitOperationMappingshill::BitOperationMapping271   BitOperationMapping() : family(IPAddress::kFamilyUnknown) {}
BitOperationMappingshill::BitOperationMapping272   BitOperationMapping(IPAddress::Family family_in,
273               const string& address_a_in,
274               const string& address_b_in,
275               const string& expected_anded_in,
276               const string& expected_orred_in)
277       : family(family_in),
278         address_a(address_a_in),
279         address_b(address_b_in),
280         expected_anded(expected_anded_in),
281         expected_orred(expected_orred_in) {}
282   IPAddress::Family family;
283   string address_a;
284   string address_b;
285   string expected_anded;
286   string expected_orred;
287 };
288 
289 class IPAddressBitOperationMappingTest
290     : public testing::TestWithParam<BitOperationMapping> {};
291 
TEST_P(IPAddressBitOperationMappingTest,TestBitOperationMapping)292 TEST_P(IPAddressBitOperationMappingTest, TestBitOperationMapping) {
293   IPAddress address_a(GetParam().family);
294   EXPECT_TRUE(address_a.SetAddressFromString(GetParam().address_a));
295   IPAddress address_b(GetParam().family);
296   EXPECT_TRUE(address_b.SetAddressFromString(GetParam().address_b));
297   IPAddress expected_anded(GetParam().family);
298   EXPECT_TRUE(expected_anded.SetAddressFromString(
299       GetParam().expected_anded));
300   EXPECT_TRUE(expected_anded.Equals(address_a.MaskWith(address_b)));
301   IPAddress expected_orred(GetParam().family);
302   EXPECT_TRUE(expected_orred.SetAddressFromString(
303       GetParam().expected_orred));
304   EXPECT_TRUE(expected_orred.Equals(address_a.MergeWith(address_b)));
305 }
306 
307 INSTANTIATE_TEST_CASE_P(
308     IPAddressBitOperationMappingTestRun,
309     IPAddressBitOperationMappingTest,
310     ::testing::Values(
311         BitOperationMapping(IPAddress::kFamilyIPv4,
312                             "255.255.255.255", "0.0.0.0",
313                             "0.0.0.0", "255.255.255.255"),
314         BitOperationMapping(IPAddress::kFamilyIPv4,
315                             "0.0.0.0", "255.255.255.255",
316                             "0.0.0.0", "255.255.255.255"),
317         BitOperationMapping(IPAddress::kFamilyIPv4,
318                             "170.170.170.170", "85.85.85.85",
319                             "0.0.0.0", "255.255.255.255"),
320         BitOperationMapping(IPAddress::kFamilyIPv4,
321                             "238.187.119.221", "119.221.238.187",
322                             "102.153.102.153", "255.255.255.255"),
323         BitOperationMapping(IPAddress::kFamilyIPv4,
324                             "17.68.136.34", "119.221.238.187",
325                             "17.68.136.34", "119.221.238.187"),
326         BitOperationMapping(IPAddress::kFamilyIPv4,
327                             "192.168.1.10", "255.255.255.0",
328                             "192.168.1.0", "255.255.255.10")));
329 
330 struct NetworkPartMapping {
NetworkPartMappingshill::NetworkPartMapping331   NetworkPartMapping() : family(IPAddress::kFamilyUnknown) {}
NetworkPartMappingshill::NetworkPartMapping332   NetworkPartMapping(IPAddress::Family family_in,
333                      const string& address_in,
334                      size_t prefix_in,
335                      const string& expected_network_in,
336                      const string& expected_broadcast_in)
337       : family(family_in),
338         address(address_in),
339         prefix(prefix_in),
340         expected_network(expected_network_in),
341         expected_broadcast(expected_broadcast_in) {}
342   IPAddress::Family family;
343   string address;
344   size_t prefix;
345   string expected_network;
346   string expected_broadcast;
347 };
348 
349 class IPAddressNetworkPartMappingTest
350     : public testing::TestWithParam<NetworkPartMapping> {};
351 
TEST_P(IPAddressNetworkPartMappingTest,TestNetworkPartMapping)352 TEST_P(IPAddressNetworkPartMappingTest, TestNetworkPartMapping) {
353   IPAddress address(GetParam().family);
354   EXPECT_TRUE(address.SetAddressFromString(GetParam().address));
355   IPAddress expected_network(GetParam().family);
356   EXPECT_TRUE(expected_network.SetAddressFromString(
357       GetParam().expected_network));
358   address.set_prefix(GetParam().prefix);
359   EXPECT_TRUE(expected_network.Equals(address.GetNetworkPart()));
360   IPAddress expected_broadcast(GetParam().family);
361   EXPECT_TRUE(expected_broadcast.SetAddressFromString(
362       GetParam().expected_broadcast));
363   EXPECT_TRUE(expected_broadcast.Equals(address.GetDefaultBroadcast()));
364 }
365 
366 INSTANTIATE_TEST_CASE_P(
367     IPAddressNetworkPartMappingTestRun,
368     IPAddressNetworkPartMappingTest,
369     ::testing::Values(
370         NetworkPartMapping(IPAddress::kFamilyIPv4,
371                            "255.255.255.255", 0, "0.0.0.0", "255.255.255.255"),
372         NetworkPartMapping(IPAddress::kFamilyIPv4,
373                            "255.255.255.255", 32,
374                            "255.255.255.255", "255.255.255.255"),
375         NetworkPartMapping(IPAddress::kFamilyIPv4,
376                            "255.255.255.255", 24,
377                            "255.255.255.0", "255.255.255.255"),
378         NetworkPartMapping(IPAddress::kFamilyIPv4,
379                            "255.255.255.255", 16,
380                            "255.255.0.0", "255.255.255.255"),
381         NetworkPartMapping(IPAddress::kFamilyIPv4,
382                            "0.0.0.0", 0, "0.0.0.0", "255.255.255.255"),
383         NetworkPartMapping(IPAddress::kFamilyIPv4,
384                            "0.0.0.0", 32, "0.0.0.0", "0.0.0.0"),
385         NetworkPartMapping(IPAddress::kFamilyIPv4,
386                            "0.0.0.0", 24, "0.0.0.0", "0.0.0.255"),
387         NetworkPartMapping(IPAddress::kFamilyIPv4,
388                            "0.0.0.0", 16, "0.0.0.0", "0.0.255.255"),
389         NetworkPartMapping(IPAddress::kFamilyIPv4,
390                            "192.168.1.1", 24, "192.168.1.0", "192.168.1.255"),
391         NetworkPartMapping(IPAddress::kFamilyIPv4,
392                            "10.1.0.1", 8, "10.0.0.0", "10.255.255.255")));
393 
394 struct MinPrefixLengthMapping {
MinPrefixLengthMappingshill::MinPrefixLengthMapping395   MinPrefixLengthMapping() : family(IPAddress::kFamilyUnknown) {}
MinPrefixLengthMappingshill::MinPrefixLengthMapping396   MinPrefixLengthMapping(IPAddress::Family family_in,
397                          const string& address_in,
398                          size_t expected_min_prefix_in)
399       : family(family_in),
400         address(address_in),
401         expected_min_prefix(expected_min_prefix_in) {}
402   IPAddress::Family family;
403   string address;
404   size_t expected_min_prefix;
405 };
406 
407 class IPAddressMinPrefixLengthMappingTest
408     : public testing::TestWithParam<MinPrefixLengthMapping> {};
409 
TEST_P(IPAddressMinPrefixLengthMappingTest,TestMinPrefixLengthMapping)410 TEST_P(IPAddressMinPrefixLengthMappingTest, TestMinPrefixLengthMapping) {
411   IPAddress address(GetParam().family);
412   EXPECT_TRUE(address.SetAddressFromString(GetParam().address));
413   EXPECT_EQ(GetParam().expected_min_prefix, address.GetMinPrefixLength());
414 }
415 
416 INSTANTIATE_TEST_CASE_P(
417     IPAddressMinPrefixLengthMappingTestRun,
418     IPAddressMinPrefixLengthMappingTest,
419     ::testing::Values(
420         MinPrefixLengthMapping(IPAddress::kFamilyIPv6, "fe80::", 128),
421         MinPrefixLengthMapping(IPAddress::kFamilyIPv4, "255.255.255.255", 32),
422         MinPrefixLengthMapping(IPAddress::kFamilyIPv4, "224.0.0.0", 32),
423         MinPrefixLengthMapping(IPAddress::kFamilyIPv4, "192.168.0.0", 24),
424         MinPrefixLengthMapping(IPAddress::kFamilyIPv4, "172.16.0.0", 16),
425         MinPrefixLengthMapping(IPAddress::kFamilyIPv4, "10.10.10.10", 8)));
426 
427 struct CanReachAddressMapping {
CanReachAddressMappingshill::CanReachAddressMapping428   CanReachAddressMapping() : family(IPAddress::kFamilyUnknown) {}
CanReachAddressMappingshill::CanReachAddressMapping429   CanReachAddressMapping(IPAddress::Family family_in,
430                          const string& address_a_in,
431                          const string& address_b_in,
432                          bool expected_result_in)
433       : family(family_in),
434         address_a(address_a_in),
435         address_b(address_b_in),
436         expected_result(expected_result_in) {}
437   IPAddress::Family family;
438   string address_a;
439   string address_b;
440   size_t expected_result;
441 };
442 
443 class IPAddressCanReachAddressMappingTest
444     : public testing::TestWithParam<CanReachAddressMapping> {};
445 
TEST_P(IPAddressCanReachAddressMappingTest,TestCanReachAddressMapping)446 TEST_P(IPAddressCanReachAddressMappingTest, TestCanReachAddressMapping) {
447   IPAddress address_a(GetParam().family);
448   EXPECT_TRUE(address_a.SetAddressAndPrefixFromString(GetParam().address_a));
449   IPAddress address_b(GetParam().family);
450   EXPECT_TRUE(address_b.SetAddressAndPrefixFromString(GetParam().address_b));
451   EXPECT_EQ(GetParam().expected_result, address_a.CanReachAddress(address_b));
452 }
453 
454 INSTANTIATE_TEST_CASE_P(
455     IPAddressCanReachAddressMappingTestRun,
456     IPAddressCanReachAddressMappingTest,
457     ::testing::Values(
458         CanReachAddressMapping(IPAddress::kFamilyIPv6,
459                                "fe80:1000::/16", "fe80:2000::/16", true),
460         CanReachAddressMapping(IPAddress::kFamilyIPv6,
461                                "fe80:1000::/16", "fe80:2000::/32", true),
462         CanReachAddressMapping(IPAddress::kFamilyIPv6,
463                                "fe80:1000::/32", "fe80:2000::/16", false),
464         CanReachAddressMapping(IPAddress::kFamilyIPv4,
465                                "192.168.1.1/24", "192.168.1.2/24", true),
466         CanReachAddressMapping(IPAddress::kFamilyIPv4,
467                                "192.168.1.1/24", "192.168.2.2/24", false),
468         CanReachAddressMapping(IPAddress::kFamilyIPv4,
469                                "192.168.1.1/16", "192.168.2.2/24", true),
470         CanReachAddressMapping(IPAddress::kFamilyIPv4,
471                                "192.168.1.1/24", "192.168.2.2/16", false)));
472 
473 }  // namespace shill
474