[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / google / google_util_unittest.cc
blob83794e227a56442b41b74b4f841035992dcd4803
1 // Copyright (c) 2012 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.
5 #include "base/command_line.h"
6 #include "chrome/browser/google/google_url_tracker.h"
7 #include "chrome/browser/google/google_util.h"
8 #include "chrome/common/chrome_switches.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using google_util::IsGoogleDomainUrl;
14 // Helpers --------------------------------------------------------------------
16 namespace {
18 // These functions merely provide brevity in the callers.
20 bool IsHomePage(const std::string& url) {
21 return google_util::IsGoogleHomePageUrl(GURL(url));
24 bool IsSearch(const std::string& url) {
25 return google_util::IsGoogleSearchUrl(GURL(url));
28 bool StartsWithBaseURL(const std::string& url) {
29 return google_util::StartsWithCommandLineGoogleBaseURL(GURL(url));
32 } // namespace
35 // Actual tests ---------------------------------------------------------------
37 TEST(GoogleUtilTest, GoodHomePagesNonSecure) {
38 // Valid home page hosts.
39 EXPECT_TRUE(IsHomePage(GoogleURLTracker::kDefaultGoogleHomepage));
40 EXPECT_TRUE(IsHomePage("http://google.com"));
41 EXPECT_TRUE(IsHomePage("http://www.google.com"));
42 EXPECT_TRUE(IsHomePage("http://www.google.ca"));
43 EXPECT_TRUE(IsHomePage("http://www.google.co.uk"));
44 EXPECT_TRUE(IsHomePage("http://www.google.com:80/"));
46 // Only the paths /, /webhp, and /ig.* are valid. Query parameters are
47 // ignored.
48 EXPECT_TRUE(IsHomePage("http://www.google.com/"));
49 EXPECT_TRUE(IsHomePage("http://www.google.com/webhp"));
50 EXPECT_TRUE(IsHomePage("http://www.google.com/webhp?rlz=TEST"));
51 EXPECT_TRUE(IsHomePage("http://www.google.com/ig"));
52 EXPECT_TRUE(IsHomePage("http://www.google.com/ig/foo"));
53 EXPECT_TRUE(IsHomePage("http://www.google.com/ig?rlz=TEST"));
54 EXPECT_TRUE(IsHomePage("http://www.google.com/ig/foo?rlz=TEST"));
57 TEST(GoogleUtilTest, GoodHomePagesSecure) {
58 // Valid home page hosts.
59 EXPECT_TRUE(IsHomePage("https://google.com"));
60 EXPECT_TRUE(IsHomePage("https://www.google.com"));
61 EXPECT_TRUE(IsHomePage("https://www.google.ca"));
62 EXPECT_TRUE(IsHomePage("https://www.google.co.uk"));
63 EXPECT_TRUE(IsHomePage("https://www.google.com:443/"));
65 // Only the paths /, /webhp, and /ig.* are valid. Query parameters are
66 // ignored.
67 EXPECT_TRUE(IsHomePage("https://www.google.com/"));
68 EXPECT_TRUE(IsHomePage("https://www.google.com/webhp"));
69 EXPECT_TRUE(IsHomePage("https://www.google.com/webhp?rlz=TEST"));
70 EXPECT_TRUE(IsHomePage("https://www.google.com/ig"));
71 EXPECT_TRUE(IsHomePage("https://www.google.com/ig/foo"));
72 EXPECT_TRUE(IsHomePage("https://www.google.com/ig?rlz=TEST"));
73 EXPECT_TRUE(IsHomePage("https://www.google.com/ig/foo?rlz=TEST"));
76 TEST(GoogleUtilTest, BadHomePages) {
77 EXPECT_FALSE(IsHomePage(std::string()));
79 // If specified, only the "www" subdomain is OK.
80 EXPECT_FALSE(IsHomePage("http://maps.google.com"));
81 EXPECT_FALSE(IsHomePage("http://foo.google.com"));
83 // No non-standard port numbers.
84 EXPECT_FALSE(IsHomePage("http://www.google.com:1234"));
85 EXPECT_FALSE(IsHomePage("https://www.google.com:5678"));
87 // Invalid TLDs.
88 EXPECT_FALSE(IsHomePage("http://www.google.abc"));
89 EXPECT_FALSE(IsHomePage("http://www.google.com.abc"));
90 EXPECT_FALSE(IsHomePage("http://www.google.abc.com"));
91 EXPECT_FALSE(IsHomePage("http://www.google.ab.cd"));
92 EXPECT_FALSE(IsHomePage("http://www.google.uk.qq"));
94 // Must be http or https.
95 EXPECT_FALSE(IsHomePage("ftp://www.google.com"));
96 EXPECT_FALSE(IsHomePage("file://does/not/exist"));
97 EXPECT_FALSE(IsHomePage("bad://www.google.com"));
98 EXPECT_FALSE(IsHomePage("www.google.com"));
100 // Only the paths /, /webhp, and /ig.* are valid.
101 EXPECT_FALSE(IsHomePage("http://www.google.com/abc"));
102 EXPECT_FALSE(IsHomePage("http://www.google.com/webhpabc"));
103 EXPECT_FALSE(IsHomePage("http://www.google.com/webhp/abc"));
104 EXPECT_FALSE(IsHomePage("http://www.google.com/abcig"));
105 EXPECT_FALSE(IsHomePage("http://www.google.com/webhp/ig"));
107 // A search URL should not be identified as a home page URL.
108 EXPECT_FALSE(IsHomePage("http://www.google.com/search?q=something"));
110 // Path is case sensitive.
111 EXPECT_FALSE(IsHomePage("https://www.google.com/WEBHP"));
114 TEST(GoogleUtilTest, GoodSearchPagesNonSecure) {
115 // Queries with path "/search" need to have the query parameter in either
116 // the url parameter or the hash fragment.
117 EXPECT_TRUE(IsSearch("http://www.google.com/search?q=something"));
118 EXPECT_TRUE(IsSearch("http://www.google.com/search#q=something"));
119 EXPECT_TRUE(IsSearch("http://www.google.com/search?name=bob&q=something"));
120 EXPECT_TRUE(IsSearch("http://www.google.com/search?name=bob#q=something"));
121 EXPECT_TRUE(IsSearch("http://www.google.com/search?name=bob#age=24&q=thing"));
122 EXPECT_TRUE(IsSearch("http://www.google.co.uk/search?q=something"));
123 // It's actually valid for both to have the query parameter.
124 EXPECT_TRUE(IsSearch("http://www.google.com/search?q=something#q=other"));
126 // Queries with path "/webhp", "/" or "" need to have the query parameter in
127 // the hash fragment.
128 EXPECT_TRUE(IsSearch("http://www.google.com/webhp#q=something"));
129 EXPECT_TRUE(IsSearch("http://www.google.com/webhp#name=bob&q=something"));
130 EXPECT_TRUE(IsSearch("http://www.google.com/webhp?name=bob#q=something"));
131 EXPECT_TRUE(IsSearch("http://www.google.com/webhp?name=bob#age=24&q=thing"));
133 EXPECT_TRUE(IsSearch("http://www.google.com/#q=something"));
134 EXPECT_TRUE(IsSearch("http://www.google.com/#name=bob&q=something"));
135 EXPECT_TRUE(IsSearch("http://www.google.com/?name=bob#q=something"));
136 EXPECT_TRUE(IsSearch("http://www.google.com/?name=bob#age=24&q=something"));
138 EXPECT_TRUE(IsSearch("http://www.google.com#q=something"));
139 EXPECT_TRUE(IsSearch("http://www.google.com#name=bob&q=something"));
140 EXPECT_TRUE(IsSearch("http://www.google.com?name=bob#q=something"));
141 EXPECT_TRUE(IsSearch("http://www.google.com?name=bob#age=24&q=something"));
144 TEST(GoogleUtilTest, GoodSearchPagesSecure) {
145 // Queries with path "/search" need to have the query parameter in either
146 // the url parameter or the hash fragment.
147 EXPECT_TRUE(IsSearch("https://www.google.com/search?q=something"));
148 EXPECT_TRUE(IsSearch("https://www.google.com/search#q=something"));
149 EXPECT_TRUE(IsSearch("https://www.google.com/search?name=bob&q=something"));
150 EXPECT_TRUE(IsSearch("https://www.google.com/search?name=bob#q=something"));
151 EXPECT_TRUE(IsSearch("https://www.google.com/search?name=bob#age=24&q=q"));
152 EXPECT_TRUE(IsSearch("https://www.google.co.uk/search?q=something"));
153 // It's actually valid for both to have the query parameter.
154 EXPECT_TRUE(IsSearch("https://www.google.com/search?q=something#q=other"));
156 // Queries with path "/webhp", "/" or "" need to have the query parameter in
157 // the hash fragment.
158 EXPECT_TRUE(IsSearch("https://www.google.com/webhp#q=something"));
159 EXPECT_TRUE(IsSearch("https://www.google.com/webhp#name=bob&q=something"));
160 EXPECT_TRUE(IsSearch("https://www.google.com/webhp?name=bob#q=something"));
161 EXPECT_TRUE(IsSearch("https://www.google.com/webhp?name=bob#age=24&q=thing"));
163 EXPECT_TRUE(IsSearch("https://www.google.com/#q=something"));
164 EXPECT_TRUE(IsSearch("https://www.google.com/#name=bob&q=something"));
165 EXPECT_TRUE(IsSearch("https://www.google.com/?name=bob#q=something"));
166 EXPECT_TRUE(IsSearch("https://www.google.com/?name=bob#age=24&q=something"));
168 EXPECT_TRUE(IsSearch("https://www.google.com#q=something"));
169 EXPECT_TRUE(IsSearch("https://www.google.com#name=bob&q=something"));
170 EXPECT_TRUE(IsSearch("https://www.google.com?name=bob#q=something"));
171 EXPECT_TRUE(IsSearch("https://www.google.com?name=bob#age=24&q=something"));
174 TEST(GoogleUtilTest, BadSearches) {
175 // A home page URL should not be identified as a search URL.
176 EXPECT_FALSE(IsSearch(GoogleURLTracker::kDefaultGoogleHomepage));
177 EXPECT_FALSE(IsSearch("http://google.com"));
178 EXPECT_FALSE(IsSearch("http://www.google.com"));
179 EXPECT_FALSE(IsSearch("http://www.google.com/search"));
180 EXPECT_FALSE(IsSearch("http://www.google.com/search?"));
182 // Must be http or https
183 EXPECT_FALSE(IsSearch("ftp://www.google.com/search?q=something"));
184 EXPECT_FALSE(IsSearch("file://does/not/exist/search?q=something"));
185 EXPECT_FALSE(IsSearch("bad://www.google.com/search?q=something"));
186 EXPECT_FALSE(IsSearch("www.google.com/search?q=something"));
188 // Can't have an empty query parameter.
189 EXPECT_FALSE(IsSearch("http://www.google.com/search?q="));
190 EXPECT_FALSE(IsSearch("http://www.google.com/search?name=bob&q="));
191 EXPECT_FALSE(IsSearch("http://www.google.com/webhp#q="));
192 EXPECT_FALSE(IsSearch("http://www.google.com/webhp#name=bob&q="));
194 // Home page searches without a hash fragment query parameter are invalid.
195 EXPECT_FALSE(IsSearch("http://www.google.com/webhp?q=something"));
196 EXPECT_FALSE(IsSearch("http://www.google.com/webhp?q=something#no=good"));
197 EXPECT_FALSE(IsSearch("http://www.google.com/webhp?name=bob&q=something"));
198 EXPECT_FALSE(IsSearch("http://www.google.com/?q=something"));
199 EXPECT_FALSE(IsSearch("http://www.google.com?q=something"));
201 // Some paths are outright invalid as searches.
202 EXPECT_FALSE(IsSearch("http://www.google.com/notreal?q=something"));
203 EXPECT_FALSE(IsSearch("http://www.google.com/chrome?q=something"));
204 EXPECT_FALSE(IsSearch("http://www.google.com/search/nogood?q=something"));
205 EXPECT_FALSE(IsSearch("http://www.google.com/webhp/nogood#q=something"));
206 EXPECT_FALSE(IsSearch(std::string()));
208 // Case sensitive paths.
209 EXPECT_FALSE(IsSearch("http://www.google.com/SEARCH?q=something"));
210 EXPECT_FALSE(IsSearch("http://www.google.com/WEBHP#q=something"));
213 TEST(GoogleUtilTest, GoogleDomains) {
214 // Test some good Google domains (valid TLDs).
215 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com"),
216 google_util::ALLOW_SUBDOMAIN,
217 google_util::DISALLOW_NON_STANDARD_PORTS));
218 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://google.com"),
219 google_util::ALLOW_SUBDOMAIN,
220 google_util::DISALLOW_NON_STANDARD_PORTS));
221 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.ca"),
222 google_util::ALLOW_SUBDOMAIN,
223 google_util::DISALLOW_NON_STANDARD_PORTS));
224 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.biz.tj"),
225 google_util::ALLOW_SUBDOMAIN,
226 google_util::DISALLOW_NON_STANDARD_PORTS));
227 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com/search?q=thing"),
228 google_util::ALLOW_SUBDOMAIN,
229 google_util::DISALLOW_NON_STANDARD_PORTS));
230 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com/webhp"),
231 google_util::ALLOW_SUBDOMAIN,
232 google_util::DISALLOW_NON_STANDARD_PORTS));
234 // Test some bad Google domains (invalid TLDs).
235 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.notrealtld"),
236 google_util::ALLOW_SUBDOMAIN,
237 google_util::DISALLOW_NON_STANDARD_PORTS));
238 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.faketld/search?q=q"),
239 google_util::ALLOW_SUBDOMAIN,
240 google_util::DISALLOW_NON_STANDARD_PORTS));
241 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.yahoo.com"),
242 google_util::ALLOW_SUBDOMAIN,
243 google_util::DISALLOW_NON_STANDARD_PORTS));
245 // Test subdomain checks.
246 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://images.google.com"),
247 google_util::ALLOW_SUBDOMAIN,
248 google_util::DISALLOW_NON_STANDARD_PORTS));
249 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://images.google.com"),
250 google_util::DISALLOW_SUBDOMAIN,
251 google_util::DISALLOW_NON_STANDARD_PORTS));
252 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://google.com"),
253 google_util::DISALLOW_SUBDOMAIN,
254 google_util::DISALLOW_NON_STANDARD_PORTS));
255 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com"),
256 google_util::DISALLOW_SUBDOMAIN,
257 google_util::DISALLOW_NON_STANDARD_PORTS));
259 // Port and scheme checks.
260 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:80"),
261 google_util::DISALLOW_SUBDOMAIN,
262 google_util::DISALLOW_NON_STANDARD_PORTS));
263 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
264 google_util::DISALLOW_SUBDOMAIN,
265 google_util::DISALLOW_NON_STANDARD_PORTS));
266 EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:443"),
267 google_util::DISALLOW_SUBDOMAIN,
268 google_util::DISALLOW_NON_STANDARD_PORTS));
269 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
270 google_util::DISALLOW_SUBDOMAIN,
271 google_util::DISALLOW_NON_STANDARD_PORTS));
272 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
273 google_util::DISALLOW_SUBDOMAIN,
274 google_util::ALLOW_NON_STANDARD_PORTS));
275 EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:123"),
276 google_util::DISALLOW_SUBDOMAIN,
277 google_util::ALLOW_NON_STANDARD_PORTS));
278 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:80"),
279 google_util::DISALLOW_SUBDOMAIN,
280 google_util::ALLOW_NON_STANDARD_PORTS));
281 EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:443"),
282 google_util::DISALLOW_SUBDOMAIN,
283 google_util::ALLOW_NON_STANDARD_PORTS));
284 EXPECT_FALSE(IsGoogleDomainUrl(GURL("file://www.google.com"),
285 google_util::DISALLOW_SUBDOMAIN,
286 google_util::DISALLOW_NON_STANDARD_PORTS));
287 EXPECT_FALSE(IsGoogleDomainUrl(GURL("doesnotexist://www.google.com"),
288 google_util::DISALLOW_SUBDOMAIN,
289 google_util::DISALLOW_NON_STANDARD_PORTS));
292 TEST(GoogleUtilTest, GoogleBaseURLNotSpecified) {
293 // When no command-line flag is specified, no input to
294 // StartsWithCommandLineGoogleBaseURL() should return true.
295 EXPECT_FALSE(StartsWithBaseURL(std::string()));
296 EXPECT_FALSE(StartsWithBaseURL("http://www.foo.com/"));
297 EXPECT_FALSE(StartsWithBaseURL("http://www.google.com/"));
299 // By default, none of the IsGoogleXXX functions should return true for a
300 // "foo.com" URL.
301 EXPECT_FALSE(IsGoogleHostname("www.foo.com",
302 google_util::DISALLOW_SUBDOMAIN));
303 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.foo.com/xyz"),
304 google_util::DISALLOW_SUBDOMAIN,
305 google_util::DISALLOW_NON_STANDARD_PORTS));
306 EXPECT_FALSE(IsGoogleDomainUrl(GURL("https://www.foo.com/"),
307 google_util::DISALLOW_SUBDOMAIN,
308 google_util::DISALLOW_NON_STANDARD_PORTS));
309 EXPECT_FALSE(IsHomePage("https://www.foo.com/webhp"));
310 EXPECT_FALSE(IsSearch("http://www.foo.com/search?q=a"));
312 // Override the Google base URL on the command line.
313 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL,
314 "http://www.foo.com/");
316 // Only URLs which start with exactly the string on the command line should
317 // cause StartsWithCommandLineGoogleBaseURL() to return true.
318 EXPECT_FALSE(StartsWithBaseURL(std::string()));
319 EXPECT_TRUE(StartsWithBaseURL("http://www.foo.com/"));
320 EXPECT_TRUE(StartsWithBaseURL("http://www.foo.com/abc"));
321 EXPECT_FALSE(StartsWithBaseURL("https://www.foo.com/"));
322 EXPECT_FALSE(StartsWithBaseURL("http://www.google.com/"));
324 // The various IsGoogleXXX functions should respect the command-line flag.
325 EXPECT_TRUE(IsGoogleHostname("www.foo.com", google_util::DISALLOW_SUBDOMAIN));
326 EXPECT_FALSE(IsGoogleHostname("foo.com", google_util::ALLOW_SUBDOMAIN));
327 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.foo.com/xyz"),
328 google_util::DISALLOW_SUBDOMAIN,
329 google_util::DISALLOW_NON_STANDARD_PORTS));
330 EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.foo.com/"),
331 google_util::DISALLOW_SUBDOMAIN,
332 google_util::DISALLOW_NON_STANDARD_PORTS));
333 EXPECT_TRUE(IsHomePage("https://www.foo.com/webhp"));
334 EXPECT_FALSE(IsHomePage("http://www.foo.com/xyz"));
335 EXPECT_TRUE(IsSearch("http://www.foo.com/search?q=a"));
338 TEST(GoogleUtilTest, GoogleBaseURLDisallowQuery) {
339 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL,
340 "http://www.foo.com/?q=");
341 EXPECT_FALSE(google_util::CommandLineGoogleBaseURL().is_valid());
344 TEST(GoogleUtilTest, GoogleBaseURLDisallowRef) {
345 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL,
346 "http://www.foo.com/#q=");
347 EXPECT_FALSE(google_util::CommandLineGoogleBaseURL().is_valid());
350 TEST(GoogleUtilTest, GoogleBaseURLFixup) {
351 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL,
352 "www.foo.com");
353 ASSERT_TRUE(google_util::CommandLineGoogleBaseURL().is_valid());
354 EXPECT_EQ("http://www.foo.com/",
355 google_util::CommandLineGoogleBaseURL().spec());