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 "base/strings/stringprintf.h"
7 #include "components/google/core/browser/google_switches.h"
8 #include "components/google/core/browser/google_url_tracker.h"
9 #include "components/google/core/browser/google_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using google_util::IsGoogleDomainUrl
;
15 // Helpers --------------------------------------------------------------------
19 const std::string kValidSearchSchemes
[] = {
24 const std::string kValidSearchQueryParams
[] = {
26 "as_q" // Advanced search uses "as_q" instead of "q" as the query param.
29 // These functions merely provide brevity in the callers.
31 bool IsHomePage(const std::string
& url
) {
32 return google_util::IsGoogleHomePageUrl(GURL(url
));
35 bool IsSearch(const std::string
& url
) {
36 return google_util::IsGoogleSearchUrl(GURL(url
));
39 bool StartsWithBaseURL(const std::string
& url
) {
40 return google_util::StartsWithCommandLineGoogleBaseURL(GURL(url
));
46 // Actual tests ---------------------------------------------------------------
48 TEST(GoogleUtilTest
, GoodHomePagesNonSecure
) {
49 // Valid home page hosts.
50 EXPECT_TRUE(IsHomePage(GoogleURLTracker::kDefaultGoogleHomepage
));
51 EXPECT_TRUE(IsHomePage("http://google.com"));
52 EXPECT_TRUE(IsHomePage("http://www.google.com"));
53 EXPECT_TRUE(IsHomePage("http://www.google.ca"));
54 EXPECT_TRUE(IsHomePage("http://www.google.co.uk"));
55 EXPECT_TRUE(IsHomePage("http://www.google.com:80/"));
57 // Only the paths /, /webhp, and /ig.* are valid. Query parameters are
59 EXPECT_TRUE(IsHomePage("http://www.google.com/"));
60 EXPECT_TRUE(IsHomePage("http://www.google.com/webhp"));
61 EXPECT_TRUE(IsHomePage("http://www.google.com/webhp?rlz=TEST"));
62 EXPECT_TRUE(IsHomePage("http://www.google.com/ig"));
63 EXPECT_TRUE(IsHomePage("http://www.google.com/ig/foo"));
64 EXPECT_TRUE(IsHomePage("http://www.google.com/ig?rlz=TEST"));
65 EXPECT_TRUE(IsHomePage("http://www.google.com/ig/foo?rlz=TEST"));
68 TEST(GoogleUtilTest
, GoodHomePagesSecure
) {
69 // Valid home page hosts.
70 EXPECT_TRUE(IsHomePage("https://google.com"));
71 EXPECT_TRUE(IsHomePage("https://www.google.com"));
72 EXPECT_TRUE(IsHomePage("https://www.google.ca"));
73 EXPECT_TRUE(IsHomePage("https://www.google.co.uk"));
74 EXPECT_TRUE(IsHomePage("https://www.google.com:443/"));
76 // Only the paths /, /webhp, and /ig.* are valid. Query parameters are
78 EXPECT_TRUE(IsHomePage("https://www.google.com/"));
79 EXPECT_TRUE(IsHomePage("https://www.google.com/webhp"));
80 EXPECT_TRUE(IsHomePage("https://www.google.com/webhp?rlz=TEST"));
81 EXPECT_TRUE(IsHomePage("https://www.google.com/ig"));
82 EXPECT_TRUE(IsHomePage("https://www.google.com/ig/foo"));
83 EXPECT_TRUE(IsHomePage("https://www.google.com/ig?rlz=TEST"));
84 EXPECT_TRUE(IsHomePage("https://www.google.com/ig/foo?rlz=TEST"));
87 TEST(GoogleUtilTest
, BadHomePages
) {
88 EXPECT_FALSE(IsHomePage(std::string()));
90 // If specified, only the "www" subdomain is OK.
91 EXPECT_FALSE(IsHomePage("http://maps.google.com"));
92 EXPECT_FALSE(IsHomePage("http://foo.google.com"));
94 // No non-standard port numbers.
95 EXPECT_FALSE(IsHomePage("http://www.google.com:1234"));
96 EXPECT_FALSE(IsHomePage("https://www.google.com:5678"));
99 EXPECT_FALSE(IsHomePage("http://www.google.example"));
100 EXPECT_FALSE(IsHomePage("http://www.google.com.example"));
101 EXPECT_FALSE(IsHomePage("http://www.google.example.com"));
102 EXPECT_FALSE(IsHomePage("http://www.google.ab.cd"));
103 EXPECT_FALSE(IsHomePage("http://www.google.uk.qq"));
105 // Must be http or https.
106 EXPECT_FALSE(IsHomePage("ftp://www.google.com"));
107 EXPECT_FALSE(IsHomePage("file://does/not/exist"));
108 EXPECT_FALSE(IsHomePage("bad://www.google.com"));
109 EXPECT_FALSE(IsHomePage("www.google.com"));
111 // Only the paths /, /webhp, and /ig.* are valid.
112 EXPECT_FALSE(IsHomePage("http://www.google.com/abc"));
113 EXPECT_FALSE(IsHomePage("http://www.google.com/webhpabc"));
114 EXPECT_FALSE(IsHomePage("http://www.google.com/webhp/abc"));
115 EXPECT_FALSE(IsHomePage("http://www.google.com/abcig"));
116 EXPECT_FALSE(IsHomePage("http://www.google.com/webhp/ig"));
118 // A search URL should not be identified as a home page URL.
119 EXPECT_FALSE(IsHomePage("http://www.google.com/search?q=something"));
121 // Path is case sensitive.
122 EXPECT_FALSE(IsHomePage("https://www.google.com/WEBHP"));
125 TEST(GoogleUtilTest
, GoodSearches
) {
126 const std::string patterns
[] = {
127 // Queries with path "/search" need to have the query parameter in either
128 // the url parameter or the hash fragment.
129 "%s://www.google.com/search?%s=something",
130 "%s://www.google.com/search#%s=something",
131 "%s://www.google.com/search?name=bob&%s=something",
132 "%s://www.google.com/search?name=bob#%s=something",
133 "%s://www.google.com/search?name=bob#age=24&%s=thng",
134 "%s://www.google.co.uk/search?%s=something",
135 // It's actually valid for both to have the query parameter.
136 "%s://www.google.com/search?%s=something#q=other",
138 // Queries with path "/webhp", "/" or "" need to have the query parameter in
139 // the hash fragment.
140 "%s://www.google.com/webhp#%s=something",
141 "%s://www.google.com/webhp#name=bob&%s=something",
142 "%s://www.google.com/webhp?name=bob#%s=something",
143 "%s://www.google.com/webhp?name=bob#age=24&%s=thing",
145 "%s://www.google.com/#%s=something",
146 "%s://www.google.com/#name=bob&%s=something",
147 "%s://www.google.com/?name=bob#%s=something",
148 "%s://www.google.com/?name=bob#age=24&%s=something",
150 "%s://www.google.com#%s=something",
151 "%s://www.google.com#name=bob&%s=something",
152 "%s://www.google.com?name=bob#%s=something",
153 "%s://www.google.com?name=bob#age=24&%s=something"
156 for (const std::string
& pattern
: patterns
) {
157 for (const std::string
& scheme
: kValidSearchSchemes
) {
158 for (const std::string
& query_param
: kValidSearchQueryParams
) {
159 EXPECT_TRUE(IsSearch(base::StringPrintf(pattern
.c_str(),
161 query_param
.c_str())));
167 TEST(GoogleUtilTest
, BadSearches
) {
168 // A home page URL should not be identified as a search URL.
169 EXPECT_FALSE(IsSearch(GoogleURLTracker::kDefaultGoogleHomepage
));
171 // Must be http or https.
172 EXPECT_FALSE(IsSearch("ftp://www.google.com/search?q=something"));
173 EXPECT_FALSE(IsSearch("file://does/not/exist/search?q=something"));
174 EXPECT_FALSE(IsSearch("bad://www.google.com/search?q=something"));
175 EXPECT_FALSE(IsSearch("www.google.com/search?q=something"));
177 // Empty URL is invalid.
178 EXPECT_FALSE(IsSearch(std::string()));
180 const std::string patterns
[] = {
182 "%s://www.google.com",
183 "%s://www.google.com/search",
184 "%s://www.google.com/search?"
187 for (const std::string
& pattern
: patterns
) {
188 for (const std::string
& scheme
: kValidSearchSchemes
) {
189 EXPECT_FALSE(IsSearch(base::StringPrintf(pattern
.c_str(),
194 const std::string patterns_q
[] = {
195 // Can't have an empty query parameter.
196 "%s://www.google.com/search?%s=",
197 "%s://www.google.com/search?name=bob&%s=",
198 "%s://www.google.com/webhp#%s=",
199 "%s://www.google.com/webhp#name=bob&%s=",
201 // Home page searches without a hash fragment query parameter are invalid.
202 "%s://www.google.com/webhp?%s=something",
203 "%s://www.google.com/webhp?%s=something#no=good",
204 "%s://www.google.com/webhp?name=bob&%s=something",
205 "%s://www.google.com/?%s=something",
206 "%s://www.google.com?%s=something",
208 // Some paths are outright invalid as searches.
209 "%s://www.google.com/notreal?%s=something",
210 "%s://www.google.com/chrome?%s=something",
211 "%s://www.google.com/search/nogood?%s=something",
212 "%s://www.google.com/webhp/nogood#%s=something",
214 // Case sensitive paths.
215 "%s://www.google.com/SEARCH?%s=something",
216 "%s://www.google.com/WEBHP#%s=something"
219 for (const std::string
& pattern
: patterns_q
) {
220 for (const std::string
& scheme
: kValidSearchSchemes
) {
221 for (const std::string
& query_param
: kValidSearchQueryParams
) {
222 EXPECT_FALSE(IsSearch(base::StringPrintf(pattern
.c_str(),
224 query_param
.c_str())));
230 TEST(GoogleUtilTest
, GoogleDomains
) {
231 // Test some good Google domains (valid TLDs).
232 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com"),
233 google_util::ALLOW_SUBDOMAIN
,
234 google_util::DISALLOW_NON_STANDARD_PORTS
));
235 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://google.com"),
236 google_util::ALLOW_SUBDOMAIN
,
237 google_util::DISALLOW_NON_STANDARD_PORTS
));
238 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.ca"),
239 google_util::ALLOW_SUBDOMAIN
,
240 google_util::DISALLOW_NON_STANDARD_PORTS
));
241 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.biz.tj"),
242 google_util::ALLOW_SUBDOMAIN
,
243 google_util::DISALLOW_NON_STANDARD_PORTS
));
244 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com/search?q=thing"),
245 google_util::ALLOW_SUBDOMAIN
,
246 google_util::DISALLOW_NON_STANDARD_PORTS
));
247 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com/webhp"),
248 google_util::ALLOW_SUBDOMAIN
,
249 google_util::DISALLOW_NON_STANDARD_PORTS
));
251 // Test some bad Google domains (invalid TLDs).
252 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.notrealtld"),
253 google_util::ALLOW_SUBDOMAIN
,
254 google_util::DISALLOW_NON_STANDARD_PORTS
));
255 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.faketld/search?q=q"),
256 google_util::ALLOW_SUBDOMAIN
,
257 google_util::DISALLOW_NON_STANDARD_PORTS
));
258 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.yahoo.com"),
259 google_util::ALLOW_SUBDOMAIN
,
260 google_util::DISALLOW_NON_STANDARD_PORTS
));
262 // Test subdomain checks.
263 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://images.google.com"),
264 google_util::ALLOW_SUBDOMAIN
,
265 google_util::DISALLOW_NON_STANDARD_PORTS
));
266 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://images.google.com"),
267 google_util::DISALLOW_SUBDOMAIN
,
268 google_util::DISALLOW_NON_STANDARD_PORTS
));
269 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://google.com"),
270 google_util::DISALLOW_SUBDOMAIN
,
271 google_util::DISALLOW_NON_STANDARD_PORTS
));
272 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com"),
273 google_util::DISALLOW_SUBDOMAIN
,
274 google_util::DISALLOW_NON_STANDARD_PORTS
));
276 // Port and scheme checks.
277 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:80"),
278 google_util::DISALLOW_SUBDOMAIN
,
279 google_util::DISALLOW_NON_STANDARD_PORTS
));
280 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
281 google_util::DISALLOW_SUBDOMAIN
,
282 google_util::DISALLOW_NON_STANDARD_PORTS
));
283 EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:443"),
284 google_util::DISALLOW_SUBDOMAIN
,
285 google_util::DISALLOW_NON_STANDARD_PORTS
));
286 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
287 google_util::DISALLOW_SUBDOMAIN
,
288 google_util::DISALLOW_NON_STANDARD_PORTS
));
289 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
290 google_util::DISALLOW_SUBDOMAIN
,
291 google_util::ALLOW_NON_STANDARD_PORTS
));
292 EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:123"),
293 google_util::DISALLOW_SUBDOMAIN
,
294 google_util::ALLOW_NON_STANDARD_PORTS
));
295 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:80"),
296 google_util::DISALLOW_SUBDOMAIN
,
297 google_util::ALLOW_NON_STANDARD_PORTS
));
298 EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:443"),
299 google_util::DISALLOW_SUBDOMAIN
,
300 google_util::ALLOW_NON_STANDARD_PORTS
));
301 EXPECT_FALSE(IsGoogleDomainUrl(GURL("file://www.google.com"),
302 google_util::DISALLOW_SUBDOMAIN
,
303 google_util::DISALLOW_NON_STANDARD_PORTS
));
304 EXPECT_FALSE(IsGoogleDomainUrl(GURL("doesnotexist://www.google.com"),
305 google_util::DISALLOW_SUBDOMAIN
,
306 google_util::DISALLOW_NON_STANDARD_PORTS
));
309 TEST(GoogleUtilTest
, GoogleBaseURLNotSpecified
) {
310 // When no command-line flag is specified, no input to
311 // StartsWithCommandLineGoogleBaseURL() should return true.
312 EXPECT_FALSE(StartsWithBaseURL(std::string()));
313 EXPECT_FALSE(StartsWithBaseURL("http://www.foo.com/"));
314 EXPECT_FALSE(StartsWithBaseURL("http://www.google.com/"));
316 // By default, none of the IsGoogleXXX functions should return true for a
318 EXPECT_FALSE(IsGoogleHostname("www.foo.com",
319 google_util::DISALLOW_SUBDOMAIN
));
320 EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.foo.com/xyz"),
321 google_util::DISALLOW_SUBDOMAIN
,
322 google_util::DISALLOW_NON_STANDARD_PORTS
));
323 EXPECT_FALSE(IsGoogleDomainUrl(GURL("https://www.foo.com/"),
324 google_util::DISALLOW_SUBDOMAIN
,
325 google_util::DISALLOW_NON_STANDARD_PORTS
));
326 EXPECT_FALSE(IsHomePage("https://www.foo.com/webhp"));
327 EXPECT_FALSE(IsSearch("http://www.foo.com/search?q=a"));
329 // Override the Google base URL on the command line.
330 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
331 switches::kGoogleBaseURL
, "http://www.foo.com/");
333 // Only URLs which start with exactly the string on the command line should
334 // cause StartsWithCommandLineGoogleBaseURL() to return true.
335 EXPECT_FALSE(StartsWithBaseURL(std::string()));
336 EXPECT_TRUE(StartsWithBaseURL("http://www.foo.com/"));
337 EXPECT_TRUE(StartsWithBaseURL("http://www.foo.com/abc"));
338 EXPECT_FALSE(StartsWithBaseURL("https://www.foo.com/"));
339 EXPECT_FALSE(StartsWithBaseURL("http://www.google.com/"));
341 // The various IsGoogleXXX functions should respect the command-line flag.
342 EXPECT_TRUE(IsGoogleHostname("www.foo.com", google_util::DISALLOW_SUBDOMAIN
));
343 EXPECT_FALSE(IsGoogleHostname("foo.com", google_util::ALLOW_SUBDOMAIN
));
344 EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.foo.com/xyz"),
345 google_util::DISALLOW_SUBDOMAIN
,
346 google_util::DISALLOW_NON_STANDARD_PORTS
));
347 EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.foo.com/"),
348 google_util::DISALLOW_SUBDOMAIN
,
349 google_util::DISALLOW_NON_STANDARD_PORTS
));
350 EXPECT_TRUE(IsHomePage("https://www.foo.com/webhp"));
351 EXPECT_FALSE(IsHomePage("http://www.foo.com/xyz"));
352 EXPECT_TRUE(IsSearch("http://www.foo.com/search?q=a"));
355 TEST(GoogleUtilTest
, GoogleBaseURLDisallowQuery
) {
356 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
357 switches::kGoogleBaseURL
, "http://www.foo.com/?q=");
358 EXPECT_FALSE(google_util::CommandLineGoogleBaseURL().is_valid());
361 TEST(GoogleUtilTest
, GoogleBaseURLDisallowRef
) {
362 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
363 switches::kGoogleBaseURL
, "http://www.foo.com/#q=");
364 EXPECT_FALSE(google_util::CommandLineGoogleBaseURL().is_valid());
367 TEST(GoogleUtilTest
, GoogleBaseURLFixup
) {
368 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
369 switches::kGoogleBaseURL
, "www.foo.com");
370 ASSERT_TRUE(google_util::CommandLineGoogleBaseURL().is_valid());
371 EXPECT_EQ("http://www.foo.com/",
372 google_util::CommandLineGoogleBaseURL().spec());
375 TEST(GoogleUtilTest
, YoutubeDomains
) {
376 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.com"),
377 google_util::ALLOW_SUBDOMAIN
,
378 google_util::DISALLOW_NON_STANDARD_PORTS
));
379 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://youtube.com"),
380 google_util::ALLOW_SUBDOMAIN
,
381 google_util::DISALLOW_NON_STANDARD_PORTS
));
382 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://youtube.com/path/main.html"),
383 google_util::ALLOW_SUBDOMAIN
,
384 google_util::DISALLOW_NON_STANDARD_PORTS
));
385 EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://notyoutube.com"),
386 google_util::ALLOW_SUBDOMAIN
,
387 google_util::DISALLOW_NON_STANDARD_PORTS
));
390 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.ca"),
391 google_util::ALLOW_SUBDOMAIN
,
392 google_util::DISALLOW_NON_STANDARD_PORTS
));
393 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.co.uk"),
394 google_util::ALLOW_SUBDOMAIN
,
395 google_util::DISALLOW_NON_STANDARD_PORTS
));
396 EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://www.youtube.notrealtld"),
397 google_util::ALLOW_SUBDOMAIN
,
398 google_util::DISALLOW_NON_STANDARD_PORTS
));
401 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://images.youtube.com"),
402 google_util::ALLOW_SUBDOMAIN
,
403 google_util::DISALLOW_NON_STANDARD_PORTS
));
404 EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://images.youtube.com"),
405 google_util::DISALLOW_SUBDOMAIN
,
406 google_util::DISALLOW_NON_STANDARD_PORTS
));
408 // Port and scheme checks.
409 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.com:80"),
410 google_util::DISALLOW_SUBDOMAIN
,
411 google_util::DISALLOW_NON_STANDARD_PORTS
));
412 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("https://www.youtube.com:443"),
413 google_util::DISALLOW_SUBDOMAIN
,
414 google_util::DISALLOW_NON_STANDARD_PORTS
));
415 EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://www.youtube.com:123"),
416 google_util::DISALLOW_SUBDOMAIN
,
417 google_util::DISALLOW_NON_STANDARD_PORTS
));
418 EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.com:123"),
419 google_util::DISALLOW_SUBDOMAIN
,
420 google_util::ALLOW_NON_STANDARD_PORTS
));
421 EXPECT_FALSE(IsYoutubeDomainUrl(GURL("file://www.youtube.com"),
422 google_util::DISALLOW_SUBDOMAIN
,
423 google_util::DISALLOW_NON_STANDARD_PORTS
));