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