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