Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/ip_address.h" | 5 #include "net/base/ip_address.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 uint8_t addr3[5] = {0xFE, 0xDC, 0xBA, 0x98}; | 70 uint8_t addr3[5] = {0xFE, 0xDC, 0xBA, 0x98}; |
| 71 IPAddress ip_address3(addr3); | 71 IPAddress ip_address3(addr3); |
| 72 EXPECT_FALSE(ip_address3.IsValid()); | 72 EXPECT_FALSE(ip_address3.IsValid()); |
| 73 EXPECT_FALSE(ip_address3.empty()); | 73 EXPECT_FALSE(ip_address3.empty()); |
| 74 | 74 |
| 75 IPAddress ip_address4; | 75 IPAddress ip_address4; |
| 76 EXPECT_FALSE(ip_address4.IsValid()); | 76 EXPECT_FALSE(ip_address4.IsValid()); |
| 77 EXPECT_TRUE(ip_address4.empty()); | 77 EXPECT_TRUE(ip_address4.empty()); |
| 78 } | 78 } |
| 79 | 79 |
| 80 TEST(IPAddressTest, IsReserved) { | 80 enum IPAddressReservedResult : bool { UNRESERVED = false, RESERVED = true }; |
|
eroman
2016/04/18 22:52:39
nit: How about NOT_RESERVED rather than "UNRESERVE
martijnc
2016/04/19 18:30:42
Done.
| |
| 81 | |
| 82 // Tests for the reserved IPv4 ranges and the (unreserved) blocks in between. | |
| 83 // The reserved ranges are tested by checking the first and last address of each | |
| 84 // range. The unreserved blocks are tested similarly. These tests cover the | |
| 85 // entire IPv4 address range. | |
| 86 TEST(IPAddressTest, IsReserved_IPv4) { | |
|
eroman
2016/04/18 22:52:39
nit: Remove the underscore (_) from the name.
*Te
martijnc
2016/04/19 18:30:42
Oh, didn't know that, thanks. Removed.
| |
| 81 struct { | 87 struct { |
| 82 const char* const address; | 88 const char* const address; |
| 83 bool is_reserved; | 89 IPAddressReservedResult is_reserved; |
| 84 } tests[] = {{"10.10.10.10", true}, | 90 } tests[] = {// 0.0.0.0/8 |
| 85 {"9.9.255.255", false}, | 91 {"0.0.0.0", RESERVED}, |
| 86 {"127.0.0.1", true}, | 92 {"0.255.255.255", RESERVED}, |
| 87 {"128.0.0.1", false}, | 93 // Unreserved block(s) |
| 88 {"198.18.0.0", true}, | 94 {"1.0.0.0", UNRESERVED}, |
| 89 {"198.18.255.255", true}, | 95 {"9.255.255.255", UNRESERVED}, |
| 90 {"198.17.255.255", false}, | 96 // 10.0.0.0/8 |
| 91 {"198.19.255.255", true}, | 97 {"10.0.0.0", RESERVED}, |
| 92 {"198.20.0.0", false}, | 98 {"10.255.255.255", RESERVED}, |
| 93 {"0000::", true}, | 99 // Unreserved block(s) |
| 94 {"FFC0:ba98:7654:3210:FEDC:BA98:7654:3210", false}, | 100 {"11.0.0.0", UNRESERVED}, |
| 95 {"2000:ba98:7654:2301:EFCD:BA98:7654:3210", false}, | 101 {"100.63.255.255", UNRESERVED}, |
| 96 {"::192.9.5.5", true}, | 102 // 100.64.0.0/10 |
| 97 {"FEED::BEEF", true}}; | 103 {"100.64.0.0", RESERVED}, |
| 104 {"100.127.255.255", RESERVED}, | |
| 105 // Unreserved block(s) | |
| 106 {"100.128.0.0", UNRESERVED}, | |
| 107 {"126.255.255.255", UNRESERVED}, | |
| 108 // 127.0.0.0/8 | |
| 109 {"127.0.0.0", RESERVED}, | |
| 110 {"127.255.255.255", RESERVED}, | |
| 111 // Unreserved block(s) | |
| 112 {"128.0.0.0", UNRESERVED}, | |
| 113 {"169.253.255.255", UNRESERVED}, | |
| 114 // 169.254.0.0/16 | |
| 115 {"169.254.0.0", RESERVED}, | |
| 116 {"169.254.255.255", RESERVED}, | |
| 117 // Unreserved block(s) | |
| 118 {"169.255.0.0", UNRESERVED}, | |
| 119 {"172.15.255.255", UNRESERVED}, | |
| 120 // 172.16.0.0/12 | |
| 121 {"172.16.0.0", RESERVED}, | |
| 122 {"172.31.255.255", RESERVED}, | |
| 123 // Unreserved block(s) | |
| 124 {"172.32.0.0", UNRESERVED}, | |
| 125 {"191.255.255.255", UNRESERVED}, | |
| 126 // 192.0.0.0/24 (including sub ranges) | |
| 127 {"192.0.0.0", UNRESERVED}, | |
| 128 {"192.0.0.255", UNRESERVED}, | |
| 129 // Unreserved block(s) | |
| 130 {"192.0.1.0", UNRESERVED}, | |
| 131 {"192.0.1.255", UNRESERVED}, | |
| 132 // 192.0.2.0/24 | |
| 133 {"192.0.2.0", RESERVED}, | |
| 134 {"192.0.2.255", RESERVED}, | |
| 135 // Unreserved block(s) | |
| 136 {"192.0.3.0", UNRESERVED}, | |
| 137 {"192.31.195.255", UNRESERVED}, | |
| 138 // 192.31.196.0/24 | |
| 139 {"192.31.196.0", UNRESERVED}, | |
| 140 {"192.31.196.255", UNRESERVED}, | |
| 141 // Unreserved block(s) | |
| 142 {"192.32.197.0", UNRESERVED}, | |
| 143 {"192.52.192.255", UNRESERVED}, | |
| 144 // 192.52.193.0/24 | |
| 145 {"192.52.193.0", UNRESERVED}, | |
| 146 {"192.52.193.255", UNRESERVED}, | |
| 147 // Unreserved block(s) | |
| 148 {"192.52.194.0", UNRESERVED}, | |
| 149 {"192.88.98.255", UNRESERVED}, | |
| 150 // 192.88.99.0/24 | |
| 151 {"192.88.99.0", RESERVED}, | |
| 152 {"192.88.99.255", RESERVED}, | |
| 153 // Unreserved block(s) | |
| 154 {"192.88.100.0", UNRESERVED}, | |
| 155 {"192.167.255.255", UNRESERVED}, | |
| 156 // 192.168.0.0/16 | |
| 157 {"192.168.0.0", RESERVED}, | |
| 158 {"192.168.255.255", RESERVED}, | |
| 159 // Unreserved block(s) | |
| 160 {"192.169.0.0", UNRESERVED}, | |
| 161 {"192.175.47.255", UNRESERVED}, | |
| 162 // 192.175.48.0/24 | |
| 163 {"192.175.48.0", UNRESERVED}, | |
| 164 {"192.175.48.255", UNRESERVED}, | |
| 165 // Unreserved block(s) | |
| 166 {"192.175.49.0", UNRESERVED}, | |
| 167 {"198.17.255.255", UNRESERVED}, | |
| 168 // 198.18.0.0/15 | |
| 169 {"198.18.0.0", RESERVED}, | |
| 170 {"198.19.255.255", RESERVED}, | |
| 171 // Unreserved block(s) | |
| 172 {"198.20.0.0", UNRESERVED}, | |
| 173 {"198.51.99.255", UNRESERVED}, | |
| 174 // 198.51.100.0/24 | |
| 175 {"198.51.100.0", RESERVED}, | |
| 176 {"198.51.100.255", RESERVED}, | |
| 177 // Unreserved block(s) | |
| 178 {"198.51.101.0", UNRESERVED}, | |
| 179 {"203.0.112.255", UNRESERVED}, | |
| 180 // 203.0.113.0/24 | |
| 181 {"203.0.113.0", RESERVED}, | |
| 182 {"203.0.113.255", RESERVED}, | |
| 183 // Unreserved block(s) | |
| 184 {"203.0.114.0", UNRESERVED}, | |
| 185 {"223.255.255.255", UNRESERVED}, | |
| 186 // 224.0.0.0/8 - 255.0.0.0/8 | |
| 187 {"224.0.0.0", RESERVED}, | |
| 188 {"255.255.255.255", RESERVED}}; | |
| 98 | 189 |
| 99 IPAddress address; | 190 IPAddress address; |
| 100 for (const auto& test : tests) { | 191 for (const auto& test : tests) { |
| 101 EXPECT_TRUE(address.AssignFromIPLiteral(test.address)); | 192 EXPECT_TRUE(address.AssignFromIPLiteral(test.address)); |
| 102 EXPECT_EQ(test.is_reserved, address.IsReserved()); | 193 EXPECT_EQ(!!test.is_reserved, address.IsReserved()); |
| 103 } | 194 } |
| 104 } | 195 } |
| 105 | 196 |
| 197 // Tests for the reserved IPv6 ranges and the (unreserved) blocks in between. | |
| 198 // The reserved ranges are tested by checking the first and last address of each | |
| 199 // range. The unreserved blocks are tested similarly. These tests cover the | |
| 200 // entire IPv6 address range. | |
| 201 TEST(IPAddressTest, IsReserved_IPv6) { | |
| 202 struct { | |
| 203 const char* const address; | |
| 204 IPAddressReservedResult is_reserved; | |
| 205 } tests[] = {// 0000::/8 | |
| 206 {"0:0:0:0:0:0:0:0", RESERVED}, | |
| 207 {"ff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 208 // 0100::/8 | |
| 209 {"100:0:0:0:0:0:0:0", RESERVED}, | |
| 210 {"1ff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 211 // 0200::/7 | |
| 212 {"200:0:0:0:0:0:0:0", RESERVED}, | |
| 213 {"3ff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 214 // 0400::/6 | |
| 215 {"400:0:0:0:0:0:0:0", RESERVED}, | |
| 216 {"7ff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 217 // 0800::/5 | |
| 218 {"800:0:0:0:0:0:0:0", RESERVED}, | |
| 219 {"fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 220 // 1000::/4 | |
| 221 {"1000:0:0:0:0:0:0:0", RESERVED}, | |
| 222 {"1fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 223 // 2000::/3 (Global Unicast) | |
| 224 {"2000:0:0:0:0:0:0:0", UNRESERVED}, | |
| 225 {"3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", UNRESERVED}, | |
| 226 // 4000::/3 | |
| 227 {"4000:0:0:0:0:0:0:0", RESERVED}, | |
| 228 {"5fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 229 // 6000::/3 | |
| 230 {"6000:0:0:0:0:0:0:0", RESERVED}, | |
| 231 {"7fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 232 // 8000::/3 | |
| 233 {"8000:0:0:0:0:0:0:0", RESERVED}, | |
| 234 {"9fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 235 // c000::/3 | |
| 236 {"c000:0:0:0:0:0:0:0", RESERVED}, | |
| 237 {"dfff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 238 // e000::/4 | |
| 239 {"e000:0:0:0:0:0:0:0", RESERVED}, | |
| 240 {"efff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 241 // f000::/5 | |
| 242 {"f000:0:0:0:0:0:0:0", RESERVED}, | |
| 243 {"f7ff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 244 // f800::/6 | |
| 245 {"f800:0:0:0:0:0:0:0", RESERVED}, | |
| 246 {"fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 247 // fc00::/7 | |
| 248 {"fc00:0:0:0:0:0:0:0", RESERVED}, | |
| 249 {"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 250 // fe00::/9 | |
| 251 {"fe00:0:0:0:0:0:0:0", RESERVED}, | |
| 252 {"fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 253 // fe80::/10 | |
| 254 {"fe80:0:0:0:0:0:0:0", RESERVED}, | |
| 255 {"febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 256 // fec0::/10 | |
| 257 {"fec0:0:0:0:0:0:0:0", RESERVED}, | |
| 258 {"feff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", RESERVED}, | |
| 259 // ff00::/8 (Multicast) | |
| 260 {"ff00:0:0:0:0:0:0:0", UNRESERVED}, | |
| 261 {"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", UNRESERVED}}; | |
| 262 | |
| 263 IPAddress address; | |
| 264 for (const auto& test : tests) { | |
| 265 EXPECT_TRUE(address.AssignFromIPLiteral(test.address)); | |
| 266 EXPECT_EQ(!!test.is_reserved, address.IsReserved()); | |
| 267 } | |
| 268 } | |
| 269 | |
| 106 TEST(IPAddressTest, IsZero) { | 270 TEST(IPAddressTest, IsZero) { |
| 107 uint8_t address1[4] = {}; | 271 uint8_t address1[4] = {}; |
| 108 IPAddress zero_ipv4_address(address1); | 272 IPAddress zero_ipv4_address(address1); |
| 109 EXPECT_TRUE(zero_ipv4_address.IsZero()); | 273 EXPECT_TRUE(zero_ipv4_address.IsZero()); |
| 110 | 274 |
| 111 uint8_t address2[4] = {10}; | 275 uint8_t address2[4] = {10}; |
| 112 IPAddress non_zero_ipv4_address(address2); | 276 IPAddress non_zero_ipv4_address(address2); |
| 113 EXPECT_FALSE(non_zero_ipv4_address.IsZero()); | 277 EXPECT_FALSE(non_zero_ipv4_address.IsZero()); |
| 114 | 278 |
| 115 uint8_t address3[16] = {}; | 279 uint8_t address3[16] = {}; |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 429 | 593 |
| 430 // Prefix is longer than the address. | 594 // Prefix is longer than the address. |
| 431 uint8_t ipv6_prefix5[] = {42, 0, 20, 80, 64, 12, 12, 9, 0, | 595 uint8_t ipv6_prefix5[] = {42, 0, 20, 80, 64, 12, 12, 9, 0, |
| 432 0, 0, 0, 0, 0, 0, 0, 10}; | 596 0, 0, 0, 0, 0, 0, 0, 10}; |
| 433 EXPECT_FALSE(IPAddressStartsWith(ipv6_address, ipv6_prefix5)); | 597 EXPECT_FALSE(IPAddressStartsWith(ipv6_address, ipv6_prefix5)); |
| 434 } | 598 } |
| 435 | 599 |
| 436 } // anonymous namespace | 600 } // anonymous namespace |
| 437 | 601 |
| 438 } // namespace net | 602 } // namespace net |
| OLD | NEW |