1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/history/url_utils.h" 6 7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "url/gurl.h" 9 10 namespace history { 11 12 namespace { 13 14 TEST(HistoryUrlUtilsTest, CanonicalURLStringCompare) { 15 // Comprehensive test by comparing each pair in sorted list. O(n^2). 16 const char* sorted_list[] = { 17 "http://www.gogle.com/redirects_to_google", 18 "http://www.google.com", 19 "http://www.google.com/", 20 "http://www.google.com/?q", 21 "http://www.google.com/A", 22 "http://www.google.com/index.html", 23 "http://www.google.com/test", 24 "http://www.google.com/test?query", 25 "http://www.google.com/test?r=3", 26 "http://www.google.com/test#hash", 27 "http://www.google.com/test/?query", 28 "http://www.google.com/test/#hash", 29 "http://www.google.com/test/zzzzz", 30 "http://www.google.com/test$dollar", 31 "http://www.google.com/test%E9%9B%80", 32 "http://www.google.com/test-case", 33 "http://www.google.com:80/", 34 "https://www.google.com", 35 }; 36 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(sorted_list); ++i) { 37 EXPECT_FALSE(CanonicalURLStringCompare(sorted_list[i], sorted_list[i])) 38 << " for \"" << sorted_list[i] << "\" < \"" << sorted_list[i] << "\""; 39 // Every disjoint pair-wise comparison. 40 for (size_t j = i + 1; j < ARRAYSIZE_UNSAFE(sorted_list); ++j) { 41 EXPECT_TRUE(CanonicalURLStringCompare(sorted_list[i], sorted_list[j])) 42 << " for \"" << sorted_list[i] << "\" < \"" << sorted_list[j] << "\""; 43 EXPECT_FALSE(CanonicalURLStringCompare(sorted_list[j], sorted_list[i])) 44 << " for \"" << sorted_list[j] << "\" < \"" << sorted_list[i] << "\""; 45 } 46 } 47 } 48 49 TEST(HistoryUrlUtilsTest, HaveSameSchemeHostAndPort) { 50 struct { 51 const char* s1; 52 const char* s2; 53 } true_cases[] = { 54 {"http://www.google.com", "http://www.google.com"}, 55 {"http://www.google.com/a/b", "http://www.google.com/a/b"}, 56 {"http://www.google.com?test=3", "http://www.google.com/"}, 57 {"http://www.google.com/#hash", "http://www.google.com/?q"}, 58 {"http://www.google.com/", "http://www.google.com/test/with/dir/"}, 59 {"http://www.google.com:360", "http://www.google.com:360/?q=1234"}, 60 {"http://www.google.com:80", "http://www.google.com/gurl/is/smart"}, 61 {"http://www.google.com/test", "http://www.google.com/test/with/dir/"}, 62 {"http://www.google.com/test?", "http://www.google.com/test/with/dir/"}, 63 }; 64 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(true_cases); ++i) { 65 EXPECT_TRUE(HaveSameSchemeHostAndPort(GURL(true_cases[i].s1), 66 GURL(true_cases[i].s2))) 67 << " for true_cases[" << i << "]"; 68 } 69 struct { 70 const char* s1; 71 const char* s2; 72 } false_cases[] = { 73 {"http://www.google.co", "http://www.google.com"}, 74 {"http://google.com", "http://www.google.com"}, 75 {"http://www.google.com", "https://www.google.com"}, 76 {"http://www.google.com/path", "http://www.google.com:137/path"}, 77 {"http://www.google.com/same/dir", "http://www.youtube.com/same/dir"}, 78 }; 79 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(false_cases); ++i) { 80 EXPECT_FALSE(HaveSameSchemeHostAndPort(GURL(false_cases[i].s1), 81 GURL(false_cases[i].s2))) 82 << " for false_cases[" << i << "]"; 83 } 84 } 85 86 TEST(HistoryUrlUtilsTest, IsPathPrefix) { 87 struct { 88 const char* p1; 89 const char* p2; 90 } true_cases[] = { 91 {"", ""}, 92 {"", "/"}, 93 {"/", "/"}, 94 {"/a/b", "/a/b"}, 95 {"/", "/test/with/dir/"}, 96 {"/test", "/test/with/dir/"}, 97 {"/test/", "/test/with/dir"}, 98 }; 99 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(true_cases); ++i) { 100 EXPECT_TRUE(IsPathPrefix(true_cases[i].p1, true_cases[i].p2)) 101 << " for true_cases[" << i << "]"; 102 } 103 struct { 104 const char* p1; 105 const char* p2; 106 } false_cases[] = { 107 {"/test", ""}, 108 {"/", ""}, // Arguable. 109 {"/a/b/", "/a/b"}, // Arguable. 110 {"/te", "/test"}, 111 {"/test", "/test-bed"}, 112 {"/test-", "/test"}, 113 }; 114 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(false_cases); ++i) { 115 EXPECT_FALSE(IsPathPrefix(false_cases[i].p1, false_cases[i].p2)) 116 << " for false_cases[" << i << "]"; 117 } 118 } 119 120 TEST(HistoryUrlUtilsTest, ToggleHTTPAndHTTPS) { 121 EXPECT_EQ(GURL("http://www.google.com/test?q#r"), 122 ToggleHTTPAndHTTPS(GURL("https://www.google.com/test?q#r"))); 123 EXPECT_EQ(GURL("https://www.google.com:137/"), 124 ToggleHTTPAndHTTPS(GURL("http://www.google.com:137/"))); 125 EXPECT_EQ(GURL::EmptyGURL(), 126 ToggleHTTPAndHTTPS(GURL("ftp://www.google.com/"))); 127 } 128 129 } // namespace 130 131 } // namespace history 132