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

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

Issue 10309002: Reimplements net::AddressList without struct addrinfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added missing NET_EXPORT to *PortOnAddressList. Created 8 years, 7 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 | Annotate | Revision Log
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/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/stringprintf.h" 15 #include "base/stringprintf.h"
16 #include "base/sys_byteorder.h" 16 #include "base/sys_byteorder.h"
17 #include "base/sys_string_conversions.h" 17 #include "base/sys_string_conversions.h"
18 #include "base/test/test_file_util.h" 18 #include "base/test/test_file_util.h"
19 #include "base/time.h" 19 #include "base/time.h"
20 #include "base/utf_string_conversions.h" 20 #include "base/utf_string_conversions.h"
21 #include "googleurl/src/gurl.h" 21 #include "googleurl/src/gurl.h"
22 #include "net/base/sys_addrinfo.h"
23 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
24 23
25 namespace net { 24 namespace net {
26 25
27 namespace { 26 namespace {
28 27
29 static const size_t kNpos = string16::npos; 28 static const size_t kNpos = string16::npos;
30 29
31 struct FileCase { 30 struct FileCase {
32 const wchar_t* file; 31 const wchar_t* file;
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 struct UrlTestData { 397 struct UrlTestData {
399 const char* description; 398 const char* description;
400 const char* input; 399 const char* input;
401 const char* languages; 400 const char* languages;
402 FormatUrlTypes format_types; 401 FormatUrlTypes format_types;
403 UnescapeRule::Type escape_rules; 402 UnescapeRule::Type escape_rules;
404 const wchar_t* output; // Use |wchar_t| to handle Unicode constants easily. 403 const wchar_t* output; // Use |wchar_t| to handle Unicode constants easily.
405 size_t prefix_len; 404 size_t prefix_len;
406 }; 405 };
407 406
408 // Returns an addrinfo for the given 32-bit address (IPv4.)
409 // The result lives in static storage, so don't delete it.
410 // |bytes| should be an array of length 4.
411 const struct addrinfo* GetIPv4Address(const uint8* bytes, int port) {
412 static struct addrinfo static_ai;
413 static struct sockaddr_in static_addr4;
414
415 struct addrinfo* ai = &static_ai;
416 ai->ai_socktype = SOCK_STREAM;
417 memset(ai, 0, sizeof(static_ai));
418
419 ai->ai_family = AF_INET;
420 ai->ai_addrlen = sizeof(static_addr4);
421
422 struct sockaddr_in* addr4 = &static_addr4;
423 memset(addr4, 0, sizeof(static_addr4));
424 addr4->sin_port = base::HostToNet16(port);
425 addr4->sin_family = ai->ai_family;
426 memcpy(&addr4->sin_addr, bytes, 4);
427
428 ai->ai_addr = (sockaddr*)addr4;
429 return ai;
430 }
431
432 // Returns a addrinfo for the given 128-bit address (IPv6.)
433 // The result lives in static storage, so don't delete it.
434 // |bytes| should be an array of length 16.
435 const struct addrinfo* GetIPv6Address(const uint8* bytes, int port) {
436 static struct addrinfo static_ai;
437 static struct sockaddr_in6 static_addr6;
438
439 struct addrinfo* ai = &static_ai;
440 ai->ai_socktype = SOCK_STREAM;
441 memset(ai, 0, sizeof(static_ai));
442
443 ai->ai_family = AF_INET6;
444 ai->ai_addrlen = sizeof(static_addr6);
445
446 struct sockaddr_in6* addr6 = &static_addr6;
447 memset(addr6, 0, sizeof(static_addr6));
448 addr6->sin6_port = base::HostToNet16(port);
449 addr6->sin6_family = ai->ai_family;
450 memcpy(&addr6->sin6_addr, bytes, 16);
451
452 ai->ai_addr = (sockaddr*)addr6;
453 return ai;
454 }
455
456 // A helper for IDN*{Fast,Slow}. 407 // A helper for IDN*{Fast,Slow}.
457 // Append "::<language list>" to |expected| and |actual| to make it 408 // Append "::<language list>" to |expected| and |actual| to make it
458 // easy to tell which sub-case fails without debugging. 409 // easy to tell which sub-case fails without debugging.
459 void AppendLanguagesToOutputs(const char* languages, 410 void AppendLanguagesToOutputs(const char* languages,
460 string16* expected, 411 string16* expected,
461 string16* actual) { 412 string16* actual) {
462 string16 to_append = ASCIIToUTF16("::") + ASCIIToUTF16(languages); 413 string16 to_append = ASCIIToUTF16("::") + ASCIIToUTF16(languages);
463 expected->append(to_append); 414 expected->append(to_append);
464 actual->append(to_append); 415 actual->append(to_append);
465 } 416 }
(...skipping 1785 matching lines...) Expand 10 before | Expand all | Expand 10 after
2251 // For IPv6 literals should always include the brackets. 2202 // For IPv6 literals should always include the brackets.
2252 { GURL("http://[1::2]/x"), "[1::2]"}, 2203 { GURL("http://[1::2]/x"), "[1::2]"},
2253 { GURL("http://[::a]:33/x"), "[::a]:33"}, 2204 { GURL("http://[::a]:33/x"), "[::a]:33"},
2254 }; 2205 };
2255 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 2206 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
2256 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); 2207 std::string host_and_port = GetHostAndOptionalPort(tests[i].url);
2257 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); 2208 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port);
2258 } 2209 }
2259 } 2210 }
2260 2211
2261
2262 TEST(NetUtilTest, NetAddressToString_IPv4) {
eroman 2012/05/04 01:08:41 IMPORTANT: Why are all these tests being removed?
szym 2012/05/04 02:38:29 Agreed. I didn't notice that NetAddressToString(st
2263 const struct {
2264 uint8 addr[4];
2265 const char* result;
2266 } tests[] = {
2267 {{0, 0, 0, 0}, "0.0.0.0"},
2268 {{127, 0, 0, 1}, "127.0.0.1"},
2269 {{192, 168, 0, 1}, "192.168.0.1"},
2270 };
2271
2272 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
2273 const addrinfo* ai = GetIPv4Address(tests[i].addr, 80);
2274 std::string result = NetAddressToString(ai);
2275 EXPECT_EQ(std::string(tests[i].result), result);
2276 }
2277 }
2278
2279 TEST(NetUtilTest, NetAddressToString_IPv6) {
2280 const struct {
2281 uint8 addr[16];
2282 const char* result;
2283 } tests[] = {
2284 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
2285 0x98, 0x76, 0x54, 0x32, 0x10},
2286 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"},
2287 };
2288
2289 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
2290 const addrinfo* ai = GetIPv6Address(tests[i].addr, 80);
2291 std::string result = NetAddressToString(ai);
2292 // Allow NetAddressToString() to fail, in case the system doesn't
2293 // support IPv6.
2294 if (!result.empty())
2295 EXPECT_EQ(std::string(tests[i].result), result);
2296 }
2297 }
2298
2299 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) {
2300 uint8 addr[] = {127, 0, 0, 1};
2301 const addrinfo* ai = GetIPv4Address(addr, 166);
2302 std::string result = NetAddressToStringWithPort(ai);
2303 EXPECT_EQ("127.0.0.1:166", result);
2304 }
2305
2306 TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) {
2307 uint8 addr[] = {
2308 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA,
2309 0x98, 0x76, 0x54, 0x32, 0x10
2310 };
2311 const addrinfo* ai = GetIPv6Address(addr, 361);
2312 std::string result = NetAddressToStringWithPort(ai);
2313
2314 // May fail on systems that don't support IPv6.
2315 if (!result.empty())
2316 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result);
2317 }
2318
2319 TEST(NetUtilTest, GetHostName) { 2212 TEST(NetUtilTest, GetHostName) {
2320 // We can't check the result of GetHostName() directly, since the result 2213 // We can't check the result of GetHostName() directly, since the result
2321 // will differ across machines. Our goal here is to simply exercise the 2214 // will differ across machines. Our goal here is to simply exercise the
2322 // code path, and check that things "look about right". 2215 // code path, and check that things "look about right".
2323 std::string hostname = GetHostName(); 2216 std::string hostname = GetHostName();
2324 EXPECT_FALSE(hostname.empty()); 2217 EXPECT_FALSE(hostname.empty());
2325 } 2218 }
2326 2219
2327 TEST(NetUtilTest, FormatUrl) { 2220 TEST(NetUtilTest, FormatUrl) {
2328 FormatUrlTypes default_format_type = kFormatUrlOmitUsernamePassword; 2221 FormatUrlTypes default_format_type = kFormatUrlOmitUsernamePassword;
(...skipping 887 matching lines...) Expand 10 before | Expand all | Expand 10 after
3216 if (it->address[i] != 0) { 3109 if (it->address[i] != 0) {
3217 all_zeroes = false; 3110 all_zeroes = false;
3218 break; 3111 break;
3219 } 3112 }
3220 } 3113 }
3221 EXPECT_FALSE(all_zeroes); 3114 EXPECT_FALSE(all_zeroes);
3222 } 3115 }
3223 } 3116 }
3224 3117
3225 } // namespace net 3118 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698