Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: net/base/ip_address_number_unittest.cc

Issue 1810183002: Migrate net/proxy/* to net::IPAddress. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments eroman Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/ip_address_number.cc ('k') | net/base/ip_address_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_number.h" 5 #include "net/base/ip_address_number.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 139
140 TEST(IpAddressNumberTest, ConvertIPv4MappedToIPv4) { 140 TEST(IpAddressNumberTest, ConvertIPv4MappedToIPv4) {
141 IPAddressNumber ipv4mapped_number; 141 IPAddressNumber ipv4mapped_number;
142 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number)); 142 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number));
143 IPAddressNumber expected; 143 IPAddressNumber expected;
144 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected)); 144 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected));
145 IPAddressNumber result = ConvertIPv4MappedToIPv4(ipv4mapped_number); 145 IPAddressNumber result = ConvertIPv4MappedToIPv4(ipv4mapped_number);
146 EXPECT_EQ(expected, result); 146 EXPECT_EQ(expected, result);
147 } 147 }
148 148
149 // Test parsing invalid CIDR notation literals.
150 TEST(IpAddressNumberTest, ParseCIDRBlock_Invalid) {
151 const char* const bad_literals[] = {
152 "foobar",
153 "",
154 "192.168.0.1",
155 "::1",
156 "/",
157 "/1",
158 "1",
159 "192.168.1.1/-1",
160 "192.168.1.1/33",
161 "::1/-3",
162 "a::3/129",
163 "::1/x",
164 "192.168.0.1//11"
165 };
166
167 for (size_t i = 0; i < arraysize(bad_literals); ++i) {
168 IPAddressNumber ip_number;
169 size_t prefix_length_in_bits;
170
171 EXPECT_FALSE(ParseCIDRBlock(bad_literals[i],
172 &ip_number,
173 &prefix_length_in_bits));
174 }
175 }
176
177 // Test parsing a valid CIDR notation literal.
178 TEST(IpAddressNumberTest, ParseCIDRBlock_Valid) {
179 IPAddressNumber ip_number;
180 size_t prefix_length_in_bits;
181
182 EXPECT_TRUE(ParseCIDRBlock("192.168.0.1/11",
183 &ip_number,
184 &prefix_length_in_bits));
185
186 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number));
187 EXPECT_EQ(11u, prefix_length_in_bits);
188 }
189
190 TEST(IpAddressNumberTest, IPNumberMatchesPrefix) { 149 TEST(IpAddressNumberTest, IPNumberMatchesPrefix) {
191 struct { 150 struct {
192 const char* const cidr_literal; 151 const char* const cidr_literal;
152 size_t prefix_length_in_bits;
193 const char* const ip_literal; 153 const char* const ip_literal;
194 bool expected_to_match; 154 bool expected_to_match;
195 } tests[] = { 155 } tests[] = {
196 // IPv4 prefix with IPv4 inputs. 156 // IPv4 prefix with IPv4 inputs.
197 { 157 {"10.10.1.32", 27, "10.10.1.44", true},
198 "10.10.1.32/27", 158 {"10.10.1.32", 27, "10.10.1.90", false},
199 "10.10.1.44", 159 {"10.10.1.32", 27, "10.10.1.90", false},
200 true
201 },
202 {
203 "10.10.1.32/27",
204 "10.10.1.90",
205 false
206 },
207 {
208 "10.10.1.32/27",
209 "10.10.1.90",
210 false
211 },
212 160
213 // IPv6 prefix with IPv6 inputs. 161 // IPv6 prefix with IPv6 inputs.
214 { 162 {"2001:db8::", 32, "2001:DB8:3:4::5", true},
215 "2001:db8::/32", 163 {"2001:db8::", 32, "2001:c8::", false},
216 "2001:DB8:3:4::5",
217 true
218 },
219 {
220 "2001:db8::/32",
221 "2001:c8::",
222 false
223 },
224 164
225 // IPv6 prefix with IPv4 inputs. 165 // IPv6 prefix with IPv4 inputs.
226 { 166 {"2001:db8::", 33, "192.168.0.1", false},
227 "2001:db8::/33", 167 {"::ffff:192.168.0.1", 112, "192.168.33.77", true},
228 "192.168.0.1",
229 false
230 },
231 {
232 "::ffff:192.168.0.1/112",
233 "192.168.33.77",
234 true
235 },
236 168
237 // IPv4 prefix with IPv6 inputs. 169 // IPv4 prefix with IPv6 inputs.
238 { 170 {"10.11.33.44", 16, "::ffff:0a0b:89", true},
239 "10.11.33.44/16", 171 {"10.11.33.44", 16, "::ffff:10.12.33.44", false},
240 "::ffff:0a0b:89",
241 true
242 },
243 {
244 "10.11.33.44/16",
245 "::ffff:10.12.33.44",
246 false
247 },
248 }; 172 };
249 for (size_t i = 0; i < arraysize(tests); ++i) { 173 for (size_t i = 0; i < arraysize(tests); ++i) {
250 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", i, 174 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", i,
251 tests[i].cidr_literal, 175 tests[i].cidr_literal,
252 tests[i].ip_literal)); 176 tests[i].ip_literal));
253 177
254 IPAddressNumber ip_number; 178 IPAddressNumber ip_number;
255 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].ip_literal, &ip_number)); 179 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].ip_literal, &ip_number));
256 180
257 IPAddressNumber ip_prefix; 181 IPAddressNumber ip_prefix;
258 size_t prefix_length_in_bits;
259 182
260 EXPECT_TRUE(ParseCIDRBlock(tests[i].cidr_literal, 183 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].cidr_literal, &ip_prefix));
261 &ip_prefix,
262 &prefix_length_in_bits));
263 184
264 EXPECT_EQ(tests[i].expected_to_match, 185 EXPECT_EQ(tests[i].expected_to_match,
265 IPNumberMatchesPrefix(ip_number, 186 IPNumberMatchesPrefix(ip_number, ip_prefix,
266 ip_prefix, 187 tests[i].prefix_length_in_bits));
267 prefix_length_in_bits));
268 } 188 }
269 } 189 }
270 190
271 } // anonymous namespace 191 } // anonymous namespace
272 192
273 } // namespace net 193 } // namespace net
OLDNEW
« no previous file with comments | « net/base/ip_address_number.cc ('k') | net/base/ip_address_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698