1 // Copyright (c) 2011 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 "googleurl/src/gurl.h" 6 #include "net/base/registry_controlled_domain.h" 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 #include "effective_tld_names_unittest1.cc" 10 #undef TOTAL_KEYWORDS 11 #undef MIN_WORD_LENGTH 12 #undef MAX_WORD_LENGTH 13 #undef MIN_HASH_VALUE 14 #undef MAX_HASH_VALUE 15 #include "effective_tld_names_unittest2.cc" 16 17 namespace net { 18 namespace { 19 20 class TestRegistryControlledDomainService : 21 public RegistryControlledDomainService { 22 public: 23 // Sets the given data. 24 static void UseDomainData(FindDomainPtr function) { 25 RegistryControlledDomainService::UseFindDomainFunction(function); 26 } 27 28 // Creates a new dedicated instance to be used for testing, deleting any 29 // previously-set one. 30 static void UseDedicatedInstance() { 31 delete static_cast<TestRegistryControlledDomainService*>( 32 SetInstance(new TestRegistryControlledDomainService())); 33 } 34 35 // Restores RegistryControlledDomainService to using its default instance, 36 // deleting any previously-set test instance. 37 static void UseDefaultInstance() { 38 delete static_cast<TestRegistryControlledDomainService*>(SetInstance(NULL)); 39 } 40 }; 41 42 class RegistryControlledDomainTest : public testing::Test { 43 protected: 44 virtual void SetUp() { 45 TestRegistryControlledDomainService::UseDedicatedInstance(); 46 } 47 48 virtual void TearDown() { 49 TestRegistryControlledDomainService::UseDefaultInstance(); 50 } 51 }; 52 53 54 std::string GetDomainFromURL(const std::string& url) { 55 return TestRegistryControlledDomainService::GetDomainAndRegistry(GURL(url)); 56 } 57 58 std::string GetDomainFromHost(const std::wstring& host) { 59 return TestRegistryControlledDomainService::GetDomainAndRegistry(host); 60 } 61 62 size_t GetRegistryLengthFromURL(const std::string& url, 63 bool allow_unknown_registries) { 64 return TestRegistryControlledDomainService::GetRegistryLength(GURL(url), 65 allow_unknown_registries); 66 } 67 68 size_t GetRegistryLengthFromHost(const std::wstring& host, 69 bool allow_unknown_registries) { 70 return TestRegistryControlledDomainService::GetRegistryLength(host, 71 allow_unknown_registries); 72 } 73 74 bool CompareDomains(const std::string& url1, const std::string& url2) { 75 GURL g1 = GURL(url1); 76 GURL g2 = GURL(url2); 77 return TestRegistryControlledDomainService::SameDomainOrHost(g1, g2); 78 } 79 80 TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) { 81 TestRegistryControlledDomainService::UseDomainData( 82 Perfect_Hash_Test1::FindDomain); 83 84 // Test GURL version of GetDomainAndRegistry(). 85 EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html")); // 1 86 EXPECT_EQ("baz.jp.", GetDomainFromURL("http://a.baz.jp./file.html")); // 1 87 EXPECT_EQ("", GetDomainFromURL("http://ac.jp")); // 2 88 EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp")); // 3 89 EXPECT_EQ("", GetDomainFromURL("http://bar.jp")); // 3 90 EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp")); // 3 4 91 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp")); 92 // 4 93 EXPECT_EQ("pref.bar.jp", GetDomainFromURL("http://baz.pref.bar.jp")); // 5 94 EXPECT_EQ("b.bar.baz.com.", GetDomainFromURL("http://a.b.bar.baz.com.")); 95 // 6 96 EXPECT_EQ("a.d.c", GetDomainFromURL("http://a.d.c")); // 7 97 EXPECT_EQ("a.d.c", GetDomainFromURL("http://.a.d.c")); // 7 98 EXPECT_EQ("a.d.c", GetDomainFromURL("http://..a.d.c")); // 7 99 EXPECT_EQ("b.c", GetDomainFromURL("http://a.b.c")); // 7 8 100 EXPECT_EQ("baz.com", GetDomainFromURL("http://baz.com")); // none 101 EXPECT_EQ("baz.com.", GetDomainFromURL("http://baz.com.")); // none 102 103 EXPECT_EQ("", GetDomainFromURL("")); 104 EXPECT_EQ("", GetDomainFromURL("http://")); 105 EXPECT_EQ("", GetDomainFromURL("file:///C:/file.html")); 106 EXPECT_EQ("", GetDomainFromURL("http://foo.com..")); 107 EXPECT_EQ("", GetDomainFromURL("http://...")); 108 EXPECT_EQ("", GetDomainFromURL("http://192.168.0.1")); 109 EXPECT_EQ("", GetDomainFromURL("http://localhost")); 110 EXPECT_EQ("", GetDomainFromURL("http://localhost.")); 111 EXPECT_EQ("", GetDomainFromURL("http:////Comment")); 112 113 // Test std::wstring version of GetDomainAndRegistry(). Uses the same 114 // underpinnings as the GURL version, so this is really more of a check of 115 // CanonicalizeHost(). 116 EXPECT_EQ("baz.jp", GetDomainFromHost(L"a.baz.jp")); // 1 117 EXPECT_EQ("baz.jp.", GetDomainFromHost(L"a.baz.jp.")); // 1 118 EXPECT_EQ("", GetDomainFromHost(L"ac.jp")); // 2 119 EXPECT_EQ("", GetDomainFromHost(L"a.bar.jp")); // 3 120 EXPECT_EQ("", GetDomainFromHost(L"bar.jp")); // 3 121 EXPECT_EQ("", GetDomainFromHost(L"baz.bar.jp")); // 3 4 122 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromHost(L"a.b.baz.bar.jp")); // 3 4 123 EXPECT_EQ("pref.bar.jp", GetDomainFromHost(L"baz.pref.bar.jp")); // 5 124 EXPECT_EQ("b.bar.baz.com.", GetDomainFromHost(L"a.b.bar.baz.com.")); // 6 125 EXPECT_EQ("a.d.c", GetDomainFromHost(L"a.d.c")); // 7 126 EXPECT_EQ("a.d.c", GetDomainFromHost(L".a.d.c")); // 7 127 EXPECT_EQ("a.d.c", GetDomainFromHost(L"..a.d.c")); // 7 128 EXPECT_EQ("b.c", GetDomainFromHost(L"a.b.c")); // 7 8 129 EXPECT_EQ("baz.com", GetDomainFromHost(L"baz.com")); // none 130 EXPECT_EQ("baz.com.", GetDomainFromHost(L"baz.com.")); // none 131 132 EXPECT_EQ("", GetDomainFromHost(L"")); 133 EXPECT_EQ("", GetDomainFromHost(L"foo.com..")); 134 EXPECT_EQ("", GetDomainFromHost(L"...")); 135 EXPECT_EQ("", GetDomainFromHost(L"192.168.0.1")); 136 EXPECT_EQ("", GetDomainFromHost(L"localhost.")); 137 EXPECT_EQ("", GetDomainFromHost(L".localhost.")); 138 } 139 140 TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) { 141 TestRegistryControlledDomainService::UseDomainData( 142 Perfect_Hash_Test1::FindDomain); 143 144 // Test GURL version of GetRegistryLength(). 145 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", false)); 146 // 1 147 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html", false)); 148 // 1 149 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp", false)); // 2 150 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp", false)); // 3 151 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://bar.jp", false)); // 3 152 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.bar.jp", false)); // 3 4 153 EXPECT_EQ(12U, GetRegistryLengthFromURL("http://a.b.baz.bar.jp", false)); 154 // 4 155 EXPECT_EQ(6U, GetRegistryLengthFromURL("http://baz.pref.bar.jp", false)); 156 // 5 157 EXPECT_EQ(11U, GetRegistryLengthFromURL("http://a.b.bar.baz.com", false)); 158 // 6 159 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.d.c", false)); // 7 160 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://.a.d.c", false)); // 7 161 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://..a.d.c", false)); // 7 162 EXPECT_EQ(1U, GetRegistryLengthFromURL("http://a.b.c", false)); // 7 8 163 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.com", false)); // none 164 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.com.", false)); // none 165 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://baz.com", true)); // none 166 EXPECT_EQ(4U, GetRegistryLengthFromURL("http://baz.com.", true)); // none 167 168 EXPECT_EQ(std::string::npos, GetRegistryLengthFromURL("", false)); 169 EXPECT_EQ(std::string::npos, GetRegistryLengthFromURL("http://", false)); 170 EXPECT_EQ(std::string::npos, 171 GetRegistryLengthFromURL("file:///C:/file.html", false)); 172 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://foo.com..", false)); 173 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://...", false)); 174 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://192.168.0.1", false)); 175 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost", false)); 176 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost", true)); 177 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.", false)); 178 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://localhost.", true)); 179 EXPECT_EQ(0U, GetRegistryLengthFromURL("http:////Comment", false)); 180 181 // Test std::wstring version of GetRegistryLength(). Uses the same 182 // underpinnings as the GURL version, so this is really more of a check of 183 // CanonicalizeHost(). 184 EXPECT_EQ(2U, GetRegistryLengthFromHost(L"a.baz.jp", false)); // 1 185 EXPECT_EQ(3U, GetRegistryLengthFromHost(L"a.baz.jp.", false)); // 1 186 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"ac.jp", false)); // 2 187 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"a.bar.jp", false)); // 3 188 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"bar.jp", false)); // 3 189 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"baz.bar.jp", false)); // 3 4 190 EXPECT_EQ(12U, GetRegistryLengthFromHost(L"a.b.baz.bar.jp", false)); // 4 191 EXPECT_EQ(6U, GetRegistryLengthFromHost(L"baz.pref.bar.jp", false)); // 5 192 EXPECT_EQ(11U, GetRegistryLengthFromHost(L"a.b.bar.baz.com", false)); 193 // 6 194 EXPECT_EQ(3U, GetRegistryLengthFromHost(L"a.d.c", false)); // 7 195 EXPECT_EQ(3U, GetRegistryLengthFromHost(L".a.d.c", false)); // 7 196 EXPECT_EQ(3U, GetRegistryLengthFromHost(L"..a.d.c", false)); // 7 197 EXPECT_EQ(1U, GetRegistryLengthFromHost(L"a.b.c", false)); // 7 8 198 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"baz.com", false)); // none 199 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"baz.com.", false)); // none 200 EXPECT_EQ(3U, GetRegistryLengthFromHost(L"baz.com", true)); // none 201 EXPECT_EQ(4U, GetRegistryLengthFromHost(L"baz.com.", true)); // none 202 203 EXPECT_EQ(std::string::npos, GetRegistryLengthFromHost(L"", false)); 204 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"foo.com..", false)); 205 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"..", false)); 206 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"192.168.0.1", false)); 207 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"localhost", false)); 208 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"localhost", true)); 209 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"localhost.", false)); 210 EXPECT_EQ(0U, GetRegistryLengthFromHost(L"localhost.", true)); 211 } 212 213 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { 214 TestRegistryControlledDomainService::UseDomainData( 215 Perfect_Hash_Test2::FindDomain); 216 217 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", 218 "http://a.b.bar.jp/file.html")); // b.bar.jp 219 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", 220 "http://b.b.bar.jp/file.html")); // b.bar.jp 221 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp 222 "http://a.not.jp/file.html")); // not.jp 223 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp 224 "http://a.foo.jp./file.html")); // foo.jp. 225 EXPECT_FALSE(CompareDomains("http://a.com/file.html", // a.com 226 "http://b.com/file.html")); // b.com 227 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html", 228 "http://b.x.com/file.html")); // x.com 229 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html", 230 "http://.x.com/file.html")); // x.com 231 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html", 232 "http://..b.x.com/file.html")); // x.com 233 EXPECT_TRUE(CompareDomains("http://intranet/file.html", 234 "http://intranet/file.html")); // intranet 235 EXPECT_TRUE(CompareDomains("http://127.0.0.1/file.html", 236 "http://127.0.0.1/file.html")); // 127.0.0.1 237 EXPECT_FALSE(CompareDomains("http://192.168.0.1/file.html", // 192.168.0.1 238 "http://127.0.0.1/file.html")); // 127.0.0.1 239 EXPECT_FALSE(CompareDomains("file:///C:/file.html", 240 "file:///C:/file.html")); // no host 241 } 242 243 TEST_F(RegistryControlledDomainTest, TestDefaultData) { 244 TestRegistryControlledDomainService::UseDefaultInstance(); 245 246 // Note that no data is set: we're using the default rules. 247 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://google.com", false)); 248 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://stanford.edu", false)); 249 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ustreas.gov", false)); 250 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://icann.net", false)); 251 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org", false)); 252 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.foo", false)); 253 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", true)); 254 } 255 256 } // namespace 257 } // namespace net 258