summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google/google_util_unittest.cc
blob: 75b706773b31708c103bafdcb65f1765052b3147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/google/google_url_tracker.h"
#include "chrome/browser/google/google_util.h"
#include "chrome/common/chrome_switches.h"
#include "testing/gtest/include/gtest/gtest.h"

using google_util::IsGoogleDomainUrl;
using google_util::IsGoogleHomePageUrl;
using google_util::IsGoogleSearchUrl;

TEST(GoogleUtilTest, GoodHomePagesNonSecure) {
  // Valid home page hosts.
  EXPECT_TRUE(IsGoogleHomePageUrl(GoogleURLTracker::kDefaultGoogleHomepage));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://google.com"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.ca"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.co.uk"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com:80/"));

  // Only the paths /, /webhp, and /ig.* are valid.  Query parameters are
  // ignored.
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com/"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com/webhp"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com/webhp?rlz=TEST"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com/ig"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com/ig/foo"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com/ig?rlz=TEST"));
  EXPECT_TRUE(IsGoogleHomePageUrl("http://www.google.com/ig/foo?rlz=TEST"));
}

TEST(GoogleUtilTest, GoodHomePagesSecure) {
  // Valid home page hosts.
  EXPECT_TRUE(IsGoogleHomePageUrl("https://google.com"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.ca"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.co.uk"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com:443/"));

  // Only the paths /, /webhp, and /ig.* are valid.  Query parameters are
  // ignored.
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com/"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com/webhp"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com/webhp?rlz=TEST"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com/ig"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com/ig/foo"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com/ig?rlz=TEST"));
  EXPECT_TRUE(IsGoogleHomePageUrl("https://www.google.com/ig/foo?rlz=TEST"));
}

TEST(GoogleUtilTest, BadHomePages) {
  EXPECT_FALSE(IsGoogleHomePageUrl(std::string()));

  // If specified, only the "www" subdomain is OK.
  EXPECT_FALSE(IsGoogleHomePageUrl("http://maps.google.com"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://foo.google.com"));

  // No non-standard port numbers.
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.com:1234"));
  EXPECT_FALSE(IsGoogleHomePageUrl("https://www.google.com:5678"));

  // Invalid TLDs.
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.abc"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.com.abc"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.abc.com"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.ab.cd"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.uk.qq"));

  // Must be http or https.
  EXPECT_FALSE(IsGoogleHomePageUrl("ftp://www.google.com"));
  EXPECT_FALSE(IsGoogleHomePageUrl("file://does/not/exist"));
  EXPECT_FALSE(IsGoogleHomePageUrl("bad://www.google.com"));
  EXPECT_FALSE(IsGoogleHomePageUrl("www.google.com"));

  // Only the paths /, /webhp, and /ig.* are valid.
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.com/abc"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.com/webhpabc"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.com/webhp/abc"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.com/abcig"));
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.com/webhp/ig"));

  // A search URL should not be identified as a home page URL.
  EXPECT_FALSE(IsGoogleHomePageUrl("http://www.google.com/search?q=something"));

  // Path is case sensitive.
  EXPECT_FALSE(IsGoogleHomePageUrl("https://www.google.com/WEBHP"));
}

TEST(GoogleUtilTest, GoodSearchPagesNonSecure) {
  // Queries with path "/search" need to have the query parameter in either
  // the url parameter or the hash fragment.
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/search?q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/search#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/search?name=bob&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/search?name=bob#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/search?name=bob#age=24&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.co.uk/search?q=something"));
  // It's actually valid for both to have the query parameter.
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/search?q=something#q=other"));

  // Queries with path "/webhp", "/" or "" need to have the query parameter in
  // the hash fragment.
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/webhp#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/webhp#name=bob&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/webhp?name=bob#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/webhp?name=bob#age=24&q=something"));

  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/#name=bob&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/?name=bob#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com/?name=bob#age=24&q=something"));

  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com#name=bob&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com?name=bob#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "http://www.google.com?name=bob#age=24&q=something"));
}

TEST(GoogleUtilTest, GoodSearchPagesSecure) {
  // Queries with path "/search" need to have the query parameter in either
  // the url parameter or the hash fragment.
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/search?q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/search#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/search?name=bob&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/search?name=bob#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/search?name=bob#age=24&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.co.uk/search?q=something"));
  // It's actually valid for both to have the query parameter.
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/search?q=something#q=other"));

  // Queries with path "/webhp", "/" or "" need to have the query parameter in
  // the hash fragment.
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/webhp#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/webhp#name=bob&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/webhp?name=bob#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/webhp?name=bob#age=24&q=something"));

  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/#name=bob&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/?name=bob#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com/?name=bob#age=24&q=something"));

  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com#name=bob&q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com?name=bob#q=something"));
  EXPECT_TRUE(IsGoogleSearchUrl(
      "https://www.google.com?name=bob#age=24&q=something"));
}

TEST(GoogleUtilTest, BadSearches) {
  // A home page URL should not be identified as a search URL.
  EXPECT_FALSE(IsGoogleSearchUrl(GoogleURLTracker::kDefaultGoogleHomepage));
  EXPECT_FALSE(IsGoogleSearchUrl("http://google.com"));
  EXPECT_FALSE(IsGoogleSearchUrl("http://www.google.com"));
  EXPECT_FALSE(IsGoogleSearchUrl("http://www.google.com/search"));
  EXPECT_FALSE(IsGoogleSearchUrl("http://www.google.com/search?"));

  // Must be http or https
  EXPECT_FALSE(IsGoogleSearchUrl(
      "ftp://www.google.com/search?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "file://does/not/exist/search?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "bad://www.google.com/search?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "www.google.com/search?q=something"));

  // Can't have an empty query parameter.
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/search?q="));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/search?name=bob&q="));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/webhp#q="));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/webhp#name=bob&q="));

  // Home page searches without a hash fragment query parameter are invalid.
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/webhp?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/webhp?q=something#no=good"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/webhp?name=bob&q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com?q=something"));

  // Some paths are outright invalid as searches.
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/notreal?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/chrome?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/search/nogood?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/webhp/nogood#q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(std::string()));

  // Case sensitive paths.
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/SEARCH?q=something"));
  EXPECT_FALSE(IsGoogleSearchUrl(
      "http://www.google.com/WEBHP#q=something"));
}

TEST(GoogleUtilTest, GoogleDomains) {
  // Test some good Google domains (valid TLDs).
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.com",
                                google_util::ALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://google.com",
                                google_util::ALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.ca",
                                google_util::ALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.biz.tj",
                                google_util::ALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.com/search?q=something",
                                google_util::ALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.com/webhp",
                                google_util::ALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));

  // Test some bad Google domains (invalid TLDs).
  EXPECT_FALSE(IsGoogleDomainUrl("http://www.google.notrealtld",
                                 google_util::ALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("http://www.google.faketld/search?q=something",
                                 google_util::ALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("http://www.yahoo.com",
                                 google_util::ALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));

  // Test subdomain checks.
  EXPECT_TRUE(IsGoogleDomainUrl("http://images.google.com",
                                google_util::ALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("http://images.google.com",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://google.com",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.com",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));

  // Port and scheme checks.
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.com:80",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("http://www.google.com:123",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("https://www.google.com:443",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("http://www.google.com:123",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.com:123",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::ALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("https://www.google.com:123",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::ALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.com:80",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::ALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("https://www.google.com:443",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::ALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("file://www.google.com",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("doesnotexist://www.google.com",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));

  // Test overriding with --instant-url works.
  EXPECT_FALSE(IsGoogleDomainUrl("http://test.foo.com",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("http://test.foo.com:1234",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
      switches::kInstantURL, "http://test.foo.com:1234/bar");
  EXPECT_FALSE(IsGoogleDomainUrl("http://test.foo.com",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://test.foo.com:1234",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_FALSE(IsGoogleDomainUrl("file://test.foo.com:1234",
                                 google_util::DISALLOW_SUBDOMAIN,
                                 google_util::DISALLOW_NON_STANDARD_PORTS));
  EXPECT_TRUE(IsGoogleDomainUrl("http://www.google.com",
                                google_util::DISALLOW_SUBDOMAIN,
                                google_util::DISALLOW_NON_STANDARD_PORTS));
}