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

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

Issue 8395025: Make RegistryControlledDomainService a static-function container class, rather than a Singleton (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update licenses Created 9 years, 1 month 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) 2011 The Chromium Authors. All rights reserved. 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 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 "googleurl/src/gurl.h" 5 #include "googleurl/src/gurl.h"
6 #include "net/base/registry_controlled_domain.h" 6 #include "net/base/registry_controlled_domain.h"
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 8
9 #include "effective_tld_names_unittest1.cc" 9 #include "effective_tld_names_unittest1.cc"
10 #undef TOTAL_KEYWORDS 10 #undef TOTAL_KEYWORDS
11 #undef MIN_WORD_LENGTH 11 #undef MIN_WORD_LENGTH
12 #undef MAX_WORD_LENGTH 12 #undef MAX_WORD_LENGTH
13 #undef MIN_HASH_VALUE 13 #undef MIN_HASH_VALUE
14 #undef MAX_HASH_VALUE 14 #undef MAX_HASH_VALUE
15 #include "effective_tld_names_unittest2.cc" 15 #include "effective_tld_names_unittest2.cc"
16 16
17 namespace net { 17 namespace net {
18 namespace { 18 namespace {
19 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) { 20 std::string GetDomainFromURL(const std::string& url) {
55 return TestRegistryControlledDomainService::GetDomainAndRegistry(GURL(url)); 21 return RegistryControlledDomainService::GetDomainAndRegistry(GURL(url));
56 } 22 }
57 23
58 std::string GetDomainFromHost(const std::string& host) { 24 std::string GetDomainFromHost(const std::string& host) {
59 return TestRegistryControlledDomainService::GetDomainAndRegistry(host); 25 return RegistryControlledDomainService::GetDomainAndRegistry(host);
60 } 26 }
61 27
62 size_t GetRegistryLengthFromURL(const std::string& url, 28 size_t GetRegistryLengthFromURL(const std::string& url,
63 bool allow_unknown_registries) { 29 bool allow_unknown_registries) {
64 return TestRegistryControlledDomainService::GetRegistryLength(GURL(url), 30 return RegistryControlledDomainService::GetRegistryLength(GURL(url),
65 allow_unknown_registries); 31 allow_unknown_registries);
66 } 32 }
67 33
68 size_t GetRegistryLengthFromHost(const std::string& host, 34 size_t GetRegistryLengthFromHost(const std::string& host,
69 bool allow_unknown_registries) { 35 bool allow_unknown_registries) {
70 return TestRegistryControlledDomainService::GetRegistryLength(host, 36 return RegistryControlledDomainService::GetRegistryLength(host,
71 allow_unknown_registries); 37 allow_unknown_registries);
72 } 38 }
73 39
74 bool CompareDomains(const std::string& url1, const std::string& url2) { 40 bool CompareDomains(const std::string& url1, const std::string& url2) {
75 GURL g1 = GURL(url1); 41 GURL g1 = GURL(url1);
76 GURL g2 = GURL(url2); 42 GURL g2 = GURL(url2);
77 return TestRegistryControlledDomainService::SameDomainOrHost(g1, g2); 43 return RegistryControlledDomainService::SameDomainOrHost(g1, g2);
78 } 44 }
79 45
46 } // namespace
47
48 class RegistryControlledDomainTest : public testing::Test {
49 protected:
50 typedef RegistryControlledDomainService::FindDomainPtr FindDomainPtr;
51 void UseDomainData(FindDomainPtr function) {
52 RegistryControlledDomainService::UseFindDomainFunction(function);
53 }
54
55 virtual void TearDown() {
56 RegistryControlledDomainService::UseFindDomainFunction(NULL);
57 }
58 };
59
80 TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) { 60 TEST_F(RegistryControlledDomainTest, TestGetDomainAndRegistry) {
81 TestRegistryControlledDomainService::UseDomainData( 61 UseDomainData(Perfect_Hash_Test1::FindDomain);
82 Perfect_Hash_Test1::FindDomain);
83 62
84 // Test GURL version of GetDomainAndRegistry(). 63 // Test GURL version of GetDomainAndRegistry().
85 EXPECT_EQ("baz.jp", GetDomainFromURL("http://a.baz.jp/file.html")); // 1 64 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 65 EXPECT_EQ("baz.jp.", GetDomainFromURL("http://a.baz.jp./file.html")); // 1
87 EXPECT_EQ("", GetDomainFromURL("http://ac.jp")); // 2 66 EXPECT_EQ("", GetDomainFromURL("http://ac.jp")); // 2
88 EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp")); // 3 67 EXPECT_EQ("", GetDomainFromURL("http://a.bar.jp")); // 3
89 EXPECT_EQ("", GetDomainFromURL("http://bar.jp")); // 3 68 EXPECT_EQ("", GetDomainFromURL("http://bar.jp")); // 3
90 EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp")); // 3 4 69 EXPECT_EQ("", GetDomainFromURL("http://baz.bar.jp")); // 3 4
91 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp")); 70 EXPECT_EQ("a.b.baz.bar.jp", GetDomainFromURL("http://a.b.baz.bar.jp"));
92 // 4 71 // 4
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 110
132 EXPECT_EQ("", GetDomainFromHost("")); 111 EXPECT_EQ("", GetDomainFromHost(""));
133 EXPECT_EQ("", GetDomainFromHost("foo.com..")); 112 EXPECT_EQ("", GetDomainFromHost("foo.com.."));
134 EXPECT_EQ("", GetDomainFromHost("...")); 113 EXPECT_EQ("", GetDomainFromHost("..."));
135 EXPECT_EQ("", GetDomainFromHost("192.168.0.1")); 114 EXPECT_EQ("", GetDomainFromHost("192.168.0.1"));
136 EXPECT_EQ("", GetDomainFromHost("localhost.")); 115 EXPECT_EQ("", GetDomainFromHost("localhost."));
137 EXPECT_EQ("", GetDomainFromHost(".localhost.")); 116 EXPECT_EQ("", GetDomainFromHost(".localhost."));
138 } 117 }
139 118
140 TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) { 119 TEST_F(RegistryControlledDomainTest, TestGetRegistryLength) {
141 TestRegistryControlledDomainService::UseDomainData( 120 UseDomainData(Perfect_Hash_Test1::FindDomain);
142 Perfect_Hash_Test1::FindDomain);
143 121
144 // Test GURL version of GetRegistryLength(). 122 // Test GURL version of GetRegistryLength().
145 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", false)); 123 EXPECT_EQ(2U, GetRegistryLengthFromURL("http://a.baz.jp/file.html", false));
146 // 1 124 // 1
147 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html", false)); 125 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://a.baz.jp./file.html", false));
148 // 1 126 // 1
149 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp", false)); // 2 127 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://ac.jp", false)); // 2
150 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp", false)); // 3 128 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://a.bar.jp", false)); // 3
151 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://bar.jp", false)); // 3 129 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://bar.jp", false)); // 3
152 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.bar.jp", false)); // 3 4 130 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://baz.bar.jp", false)); // 3 4
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 EXPECT_EQ(0U, GetRegistryLengthFromHost("foo.com..", false)); 181 EXPECT_EQ(0U, GetRegistryLengthFromHost("foo.com..", false));
204 EXPECT_EQ(0U, GetRegistryLengthFromHost("..", false)); 182 EXPECT_EQ(0U, GetRegistryLengthFromHost("..", false));
205 EXPECT_EQ(0U, GetRegistryLengthFromHost("192.168.0.1", false)); 183 EXPECT_EQ(0U, GetRegistryLengthFromHost("192.168.0.1", false));
206 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", false)); 184 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", false));
207 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", true)); 185 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost", true));
208 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", false)); 186 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", false));
209 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", true)); 187 EXPECT_EQ(0U, GetRegistryLengthFromHost("localhost.", true));
210 } 188 }
211 189
212 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) { 190 TEST_F(RegistryControlledDomainTest, TestSameDomainOrHost) {
213 TestRegistryControlledDomainService::UseDomainData( 191 UseDomainData(Perfect_Hash_Test2::FindDomain);
214 Perfect_Hash_Test2::FindDomain);
215 192
216 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", 193 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
217 "http://a.b.bar.jp/file.html")); // b.bar.jp 194 "http://a.b.bar.jp/file.html")); // b.bar.jp
218 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html", 195 EXPECT_TRUE(CompareDomains("http://a.b.bar.jp/file.html",
219 "http://b.b.bar.jp/file.html")); // b.bar.jp 196 "http://b.b.bar.jp/file.html")); // b.bar.jp
220 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp 197 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp
221 "http://a.not.jp/file.html")); // not.jp 198 "http://a.not.jp/file.html")); // not.jp
222 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp 199 EXPECT_FALSE(CompareDomains("http://a.foo.jp/file.html", // foo.jp
223 "http://a.foo.jp./file.html")); // foo.jp. 200 "http://a.foo.jp./file.html")); // foo.jp.
224 EXPECT_FALSE(CompareDomains("http://a.com/file.html", // a.com 201 EXPECT_FALSE(CompareDomains("http://a.com/file.html", // a.com
225 "http://b.com/file.html")); // b.com 202 "http://b.com/file.html")); // b.com
226 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html", 203 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
227 "http://b.x.com/file.html")); // x.com 204 "http://b.x.com/file.html")); // x.com
228 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html", 205 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
229 "http://.x.com/file.html")); // x.com 206 "http://.x.com/file.html")); // x.com
230 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html", 207 EXPECT_TRUE(CompareDomains("http://a.x.com/file.html",
231 "http://..b.x.com/file.html")); // x.com 208 "http://..b.x.com/file.html")); // x.com
232 EXPECT_TRUE(CompareDomains("http://intranet/file.html", 209 EXPECT_TRUE(CompareDomains("http://intranet/file.html",
233 "http://intranet/file.html")); // intranet 210 "http://intranet/file.html")); // intranet
234 EXPECT_TRUE(CompareDomains("http://127.0.0.1/file.html", 211 EXPECT_TRUE(CompareDomains("http://127.0.0.1/file.html",
235 "http://127.0.0.1/file.html")); // 127.0.0.1 212 "http://127.0.0.1/file.html")); // 127.0.0.1
236 EXPECT_FALSE(CompareDomains("http://192.168.0.1/file.html", // 192.168.0.1 213 EXPECT_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 214 "http://127.0.0.1/file.html")); // 127.0.0.1
238 EXPECT_FALSE(CompareDomains("file:///C:/file.html", 215 EXPECT_FALSE(CompareDomains("file:///C:/file.html",
239 "file:///C:/file.html")); // no host 216 "file:///C:/file.html")); // no host
240 } 217 }
241 218
242 TEST_F(RegistryControlledDomainTest, TestDefaultData) { 219 TEST_F(RegistryControlledDomainTest, TestDefaultData) {
243 TestRegistryControlledDomainService::UseDefaultInstance();
244
245 // Note that no data is set: we're using the default rules. 220 // Note that no data is set: we're using the default rules.
246 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://google.com", false)); 221 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://google.com", false));
247 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://stanford.edu", false)); 222 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://stanford.edu", false));
248 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ustreas.gov", false)); 223 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ustreas.gov", false));
249 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://icann.net", false)); 224 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://icann.net", false));
250 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org", false)); 225 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://ferretcentral.org", false));
251 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.foo", false)); 226 EXPECT_EQ(0U, GetRegistryLengthFromURL("http://nowhere.foo", false));
252 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", true)); 227 EXPECT_EQ(3U, GetRegistryLengthFromURL("http://nowhere.foo", true));
253 } 228 }
254 229
255 } // namespace
wtc 2011/10/27 22:44:09 Why did you need to reduce the unnamed namespace?
Ryan Sleevi 2011/10/27 22:48:04 friend scope. Because RegistryControlledDomainServ
256 } // namespace net 230 } // namespace net
OLDNEW
« net/base/registry_controlled_domain.cc ('K') | « net/base/registry_controlled_domain.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698