OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 using base::ASCIIToUTF16; | 18 using base::ASCIIToUTF16; |
19 using base::WideToUTF16; | 19 using base::WideToUTF16; |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 static const size_t kNpos = base::string16::npos; | 25 const size_t kNpos = base::string16::npos; |
26 | 26 |
27 const char* kLanguages[] = { | 27 const char* const kLanguages[] = { |
28 "", "en", "zh-CN", "ja", "ko", | 28 "", "en", "zh-CN", "ja", "ko", |
29 "he", "ar", "ru", "el", "fr", | 29 "he", "ar", "ru", "el", "fr", |
30 "de", "pt", "sv", "th", "hi", | 30 "de", "pt", "sv", "th", "hi", |
31 "de,en", "el,en", "zh-TW,en", "ko,ja", "he,ru,en", | 31 "de,en", "el,en", "zh-TW,en", "ko,ja", "he,ru,en", |
32 "zh,ru,en" | 32 "zh,ru,en" |
33 }; | 33 }; |
34 | 34 |
35 struct IDNTestCase { | 35 struct IDNTestCase { |
36 const char* input; | 36 const char* const input; |
37 const wchar_t* unicode_output; | 37 const wchar_t* unicode_output; |
38 const bool unicode_allowed[arraysize(kLanguages)]; | 38 const bool unicode_allowed[arraysize(kLanguages)]; |
39 }; | 39 }; |
40 | 40 |
41 // TODO(jungshik) This is just a random sample of languages and is far | 41 // TODO(jungshik) This is just a random sample of languages and is far |
42 // from exhaustive. We may have to generate all the combinations | 42 // from exhaustive. We may have to generate all the combinations |
43 // of languages (powerset of a set of all the languages). | 43 // of languages (powerset of a set of all the languages). |
44 const IDNTestCase idn_cases[] = { | 44 const IDNTestCase idn_cases[] = { |
45 // No IDN | 45 // No IDN |
46 {"www.google.com", L"www.google.com", | 46 {"www.google.com", L"www.google.com", |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 true}}, | 347 true}}, |
348 #endif | 348 #endif |
349 }; | 349 }; |
350 | 350 |
351 struct AdjustOffsetCase { | 351 struct AdjustOffsetCase { |
352 size_t input_offset; | 352 size_t input_offset; |
353 size_t output_offset; | 353 size_t output_offset; |
354 }; | 354 }; |
355 | 355 |
356 struct UrlTestData { | 356 struct UrlTestData { |
357 const char* description; | 357 const char* const description; |
358 const char* input; | 358 const char* const input; |
359 const char* languages; | 359 const char* const languages; |
360 FormatUrlTypes format_types; | 360 FormatUrlTypes format_types; |
361 UnescapeRule::Type escape_rules; | 361 UnescapeRule::Type escape_rules; |
362 const wchar_t* output; // Use |wchar_t| to handle Unicode constants easily. | 362 const wchar_t* output; // Use |wchar_t| to handle Unicode constants easily. |
363 size_t prefix_len; | 363 size_t prefix_len; |
364 }; | 364 }; |
365 | 365 |
366 // A helper for IDN*{Fast,Slow}. | 366 // A helper for IDN*{Fast,Slow}. |
367 // Append "::<language list>" to |expected| and |actual| to make it | 367 // Append "::<language list>" to |expected| and |actual| to make it |
368 // easy to tell which sub-case fails without debugging. | 368 // easy to tell which sub-case fails without debugging. |
369 void AppendLanguagesToOutputs(const char* languages, | 369 void AppendLanguagesToOutputs(const char* languages, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("www.blah"))); | 448 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("www.blah"))); |
449 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("blah"))); | 449 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("blah"))); |
450 } | 450 } |
451 | 451 |
452 // This is currently a windows specific function. | 452 // This is currently a windows specific function. |
453 #if defined(OS_WIN) | 453 #if defined(OS_WIN) |
454 namespace { | 454 namespace { |
455 | 455 |
456 struct GetDirectoryListingEntryCase { | 456 struct GetDirectoryListingEntryCase { |
457 const wchar_t* name; | 457 const wchar_t* name; |
458 const char* raw_bytes; | 458 const char* const raw_bytes; |
459 bool is_dir; | 459 bool is_dir; |
460 int64 filesize; | 460 int64 filesize; |
461 base::Time time; | 461 base::Time time; |
462 const char* expected; | 462 const char* const expected; |
463 }; | 463 }; |
464 | 464 |
465 } // namespace | 465 } // namespace |
466 | 466 |
467 TEST(NetUtilTest, GetDirectoryListingEntry) { | 467 TEST(NetUtilTest, GetDirectoryListingEntry) { |
468 const GetDirectoryListingEntryCase test_cases[] = { | 468 const GetDirectoryListingEntryCase test_cases[] = { |
469 {L"Foo", | 469 {L"Foo", |
470 "", | 470 "", |
471 false, | 471 false, |
472 10000, | 472 10000, |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 NULL); | 923 NULL); |
924 EXPECT_EQ(url.spec(), GURL(formatted).spec()); | 924 EXPECT_EQ(url.spec(), GURL(formatted).spec()); |
925 } | 925 } |
926 } | 926 } |
927 | 927 |
928 // Make sure that calling FormatUrl on a GURL and then converting back to a GURL | 928 // Make sure that calling FormatUrl on a GURL and then converting back to a GURL |
929 // only results in a different GURL for certain characters. | 929 // only results in a different GURL for certain characters. |
930 TEST(NetUtilTest, FormatUrlRoundTripQueryEscaped) { | 930 TEST(NetUtilTest, FormatUrlRoundTripQueryEscaped) { |
931 // A full list of characters which FormatURL should unescape and GURL should | 931 // A full list of characters which FormatURL should unescape and GURL should |
932 // not escape again, when they appear in a query string. | 932 // not escape again, when they appear in a query string. |
933 const char* kUnescapedCharacters = | 933 const char kUnescapedCharacters[] = |
934 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_~"; | 934 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_~"; |
935 for (unsigned char test_char = 0; test_char < 128; ++test_char) { | 935 for (unsigned char test_char = 0; test_char < 128; ++test_char) { |
936 std::string original_url("http://www.google.com/?"); | 936 std::string original_url("http://www.google.com/?"); |
937 original_url.push_back('%'); | 937 original_url.push_back('%'); |
938 original_url.append(base::HexEncode(&test_char, 1)); | 938 original_url.append(base::HexEncode(&test_char, 1)); |
939 | 939 |
940 GURL url(original_url); | 940 GURL url(original_url); |
941 size_t prefix_len; | 941 size_t prefix_len; |
942 base::string16 formatted = FormatUrl(url, | 942 base::string16 formatted = FormatUrl(url, |
943 std::string(), | 943 std::string(), |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 | 1062 |
1063 const size_t omit_all_offsets[] = { | 1063 const size_t omit_all_offsets[] = { |
1064 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, kNpos, kNpos, kNpos, kNpos, | 1064 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, kNpos, kNpos, kNpos, kNpos, |
1065 0, 1, 2, 3, 4, 5, 6, 7 | 1065 0, 1, 2, 3, 4, 5, 6, 7 |
1066 }; | 1066 }; |
1067 CheckAdjustedOffsets("http://user@foo.com/", "en", kFormatUrlOmitAll, | 1067 CheckAdjustedOffsets("http://user@foo.com/", "en", kFormatUrlOmitAll, |
1068 UnescapeRule::NORMAL, omit_all_offsets); | 1068 UnescapeRule::NORMAL, omit_all_offsets); |
1069 } | 1069 } |
1070 | 1070 |
1071 } // namespace net | 1071 } // namespace net |
OLD | NEW |