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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/google/core/common/google_util.h"
#include "base/command_line.h"
#include "base/strings/strcat.h"
#include "components/google/core/common/google_switches.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_constants.h"
using google_util::IsGoogleDomainUrl;
// Helpers --------------------------------------------------------------------
namespace {
constexpr const char* kValidSearchSchemes[] = {"http", "https"};
constexpr const char* kValidSearchQueryParams[] = {
"q",
"as_q" // Advanced search uses "as_q" instead of "q" as the query param.
};
// These functions merely provide brevity in the callers.
bool IsHomePage(const std::string& url) {
return google_util::IsGoogleHomePageUrl(GURL(url));
}
bool IsSearch(const std::string& url) {
return google_util::IsGoogleSearchUrl(GURL(url));
}
bool StartsWithBaseURL(const std::string& url) {
return google_util::StartsWithCommandLineGoogleBaseURL(GURL(url));
}
} // namespace
// Actual tests ---------------------------------------------------------------
TEST(GoogleUtilTest, GoodHomePagesNonSecure) {
// Valid home page hosts.
EXPECT_TRUE(IsHomePage(google_util::kGoogleHomepageURL));
EXPECT_TRUE(IsHomePage("http://google.com"));
EXPECT_TRUE(IsHomePage("http://www.google.com"));
EXPECT_TRUE(IsHomePage("http://www.google.ca"));
EXPECT_TRUE(IsHomePage("http://www.google.co.uk"));
EXPECT_TRUE(IsHomePage("http://www.google.com:80/"));
// Only the paths /, /webhp, and /ig.* are valid. Query parameters are
// ignored.
EXPECT_TRUE(IsHomePage("http://www.google.com/"));
EXPECT_TRUE(IsHomePage("http://www.google.com/webhp"));
EXPECT_TRUE(IsHomePage("http://www.google.com/webhp?rlz=TEST"));
EXPECT_TRUE(IsHomePage("http://www.google.com/ig"));
EXPECT_TRUE(IsHomePage("http://www.google.com/ig/foo"));
EXPECT_TRUE(IsHomePage("http://www.google.com/ig?rlz=TEST"));
EXPECT_TRUE(IsHomePage("http://www.google.com/ig/foo?rlz=TEST"));
// Accepted subdomains.
EXPECT_TRUE(IsHomePage("http://ipv4.google.com/"));
EXPECT_TRUE(IsHomePage("http://ipv6.google.com/"));
// Trailing dots.
EXPECT_TRUE(IsHomePage("http://ipv4.google.com./"));
EXPECT_TRUE(IsHomePage("http://google.com./"));
}
TEST(GoogleUtilTest, GoodHomePagesSecure) {
// Valid home page hosts.
EXPECT_TRUE(IsHomePage("https://google.com"));
EXPECT_TRUE(IsHomePage("https://www.google.com"));
EXPECT_TRUE(IsHomePage("https://www.google.ca"));
EXPECT_TRUE(IsHomePage("https://www.google.co.uk"));
EXPECT_TRUE(IsHomePage("https://www.google.com:443/"));
// Only the paths /, /webhp, and /ig.* are valid. Query parameters are
// ignored.
EXPECT_TRUE(IsHomePage("https://www.google.com/"));
EXPECT_TRUE(IsHomePage("https://www.google.com/webhp"));
EXPECT_TRUE(IsHomePage("https://www.google.com/webhp?rlz=TEST"));
EXPECT_TRUE(IsHomePage("https://www.google.com/ig"));
EXPECT_TRUE(IsHomePage("https://www.google.com/ig/foo"));
EXPECT_TRUE(IsHomePage("https://www.google.com/ig?rlz=TEST"));
EXPECT_TRUE(IsHomePage("https://www.google.com/ig/foo?rlz=TEST"));
}
TEST(GoogleUtilTest, BadHomePages) {
EXPECT_FALSE(IsHomePage(std::string()));
// If specified, only the "www" subdomain is OK.
EXPECT_FALSE(IsHomePage("http://maps.google.com"));
EXPECT_FALSE(IsHomePage("http://foo.google.com"));
// No non-standard port numbers.
EXPECT_FALSE(IsHomePage("http://www.google.com:1234"));
EXPECT_FALSE(IsHomePage("https://www.google.com:5678"));
// Invalid TLDs.
EXPECT_FALSE(IsHomePage("http://www.google.example"));
EXPECT_FALSE(IsHomePage("http://www.google.com.example"));
EXPECT_FALSE(IsHomePage("http://www.google.example.com"));
EXPECT_FALSE(IsHomePage("http://www.google.ab.cd"));
EXPECT_FALSE(IsHomePage("http://www.google.uk.qq"));
// Must be http or https.
EXPECT_FALSE(IsHomePage("ftp://www.google.com"));
EXPECT_FALSE(IsHomePage("file://does/not/exist"));
EXPECT_FALSE(IsHomePage("bad://www.google.com"));
EXPECT_FALSE(IsHomePage("www.google.com"));
// Only the paths /, /webhp, and /ig.* are valid.
EXPECT_FALSE(IsHomePage("http://www.google.com/abc"));
EXPECT_FALSE(IsHomePage("http://www.google.com/webhpabc"));
EXPECT_FALSE(IsHomePage("http://www.google.com/webhp/abc"));
EXPECT_FALSE(IsHomePage("http://www.google.com/abcig"));
EXPECT_FALSE(IsHomePage("http://www.google.com/webhp/ig"));
// A search URL should not be identified as a home page URL.
EXPECT_FALSE(IsHomePage("http://www.google.com/search?q=something"));
// Path is case sensitive.
EXPECT_FALSE(IsHomePage("https://www.google.com/WEBHP"));
// Only .com subdomain and no www.
EXPECT_FALSE(IsHomePage("http://ipv4.google.co.uk"));
EXPECT_FALSE(IsHomePage("http://www.ipv4.google.com"));
}
TEST(GoogleUtilTest, GoodSearches) {
constexpr struct {
const char* before_query_param;
const char* after_query_param;
} kPatterns[] = {
// Queries with path "/search" need to have the query parameter in either
// the url parameter or the hash fragment.
{"www.google.com/search?", "=something"},
{"www.google.com/search#", "=something"},
{"www.google.com/search?name=bob&", "=something"},
{"www.google.com/search?name=bob#", "=something"},
{"www.google.com/search?name=bob#age=24&", "=thng"},
{"www.google.co.uk/search?", "=something"},
// It's actually valid for both to have the query parameter.
{"www.google.com/search?", "=something#q=other"},
// Also valid to have an empty query parameter
{"www.google.com/search?", "="},
// Queries with path "/webhp", "/" or "" need to have the query parameter
// in the hash fragment.
{"www.google.com/webhp#", "=something"},
{"www.google.com/webhp#name=bob&", "=something"},
{"www.google.com/webhp?name=bob#", "=something"},
{"www.google.com/webhp?name=bob#age=24&", "=thing"},
{"www.google.com/#", "=something"},
{"www.google.com/#name=bob&", "=something"},
{"www.google.com/?name=bob#", "=something"},
{"www.google.com/?name=bob#age=24&", "=something"},
{"www.google.com#", "=something"},
{"www.google.com#name=bob&", "=something"},
{"www.google.com?name=bob#", "=something"},
{"www.google.com?name=bob#age=24&", "=something"},
// Google subdomain queries.
{"ipv4.google.com/search?", "=something"},
{"ipv4.google.com#name=bob&", "=something"},
{"ipv6.google.com?name=bob#", "=something"},
{"ipv6.google.com?name=bob#age=24&", "=something"},
// Trailing dots in the hosts.
{"www.google.com./#", "=something"},
{"www.google.de./#", "=something"},
{"ipv4.google.com./#", "=something"},
{"ipv6.google.com./#", "=something"}};
for (const auto& pattern : kPatterns) {
for (const char* scheme : kValidSearchSchemes) {
for (const char* query_param : kValidSearchQueryParams) {
EXPECT_TRUE(IsSearch(base::StrCat(
{scheme, url::kStandardSchemeSeparator, pattern.before_query_param,
query_param, pattern.after_query_param})));
}
}
}
}
TEST(GoogleUtilTest, BadSearches) {
// A home page URL should not be identified as a search URL.
EXPECT_FALSE(IsSearch(google_util::kGoogleHomepageURL));
// Must be http or https.
EXPECT_FALSE(IsSearch("ftp://www.google.com/search?q=something"));
EXPECT_FALSE(IsSearch("file://does/not/exist/search?q=something"));
EXPECT_FALSE(IsSearch("bad://www.google.com/search?q=something"));
EXPECT_FALSE(IsSearch("www.google.com/search?q=something"));
// Empty URL is invalid.
EXPECT_FALSE(IsSearch(std::string()));
constexpr const char* kPatterns[] = {"google.com", "www.google.com",
"www.google.com/search",
"www.google.com/search?"};
for (const char* pattern : kPatterns) {
for (const char* scheme : kValidSearchSchemes) {
EXPECT_FALSE(IsSearch(
base::StrCat({scheme, url::kStandardSchemeSeparator, pattern})));
}
}
constexpr struct {
const char* before_query_param;
const char* after_query_param;
} kPatternsQ[] = {
// Home page searches without a hash fragment query parameter are invalid.
{"www.google.com/webhp?", "=something"},
{"www.google.com/webhp?", "=something#no=good"},
{"www.google.com/webhp?name=bob&", "=something"},
{"www.google.com/?", "=something"},
{"www.google.com?", "=something"},
// Some paths are outright invalid as searches.
{"www.google.com/notreal?", "=something"},
{"www.google.com/chrome?", "=something"},
{"www.google.com/search/nogood?", "=something"},
{"www.google.com/webhp/nogood#", "=something"},
// Case sensitive paths.
{"www.google.com/SEARCH?", "=something"},
{"www.google.com/WEBHP#", "=something"}};
for (const auto& pattern : kPatternsQ) {
for (const char* scheme : kValidSearchSchemes) {
for (const char* query_param : kValidSearchQueryParams) {
EXPECT_FALSE(IsSearch(base::StrCat(
{scheme, url::kStandardSchemeSeparator, pattern.before_query_param,
query_param, pattern.after_query_param})));
}
}
}
}
TEST(GoogleUtilTest, GoogleDomains) {
// Test some good Google domains (valid TLDs).
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://google.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.ca"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.off.ai"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com/search?q=thing"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com/webhp"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
// Test some bad Google domains (invalid/non-Google TLDs).
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.notrealtld"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.sd"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.faketld/search?q=q"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.yahoo.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.biz.tj"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
// Test subdomain checks.
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://images.google.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://images.google.com"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://google.com"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
// Port and scheme checks.
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:80"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:443"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:123"),
google_util::DISALLOW_SUBDOMAIN,
google_util::ALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:123"),
google_util::DISALLOW_SUBDOMAIN,
google_util::ALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.google.com:80"),
google_util::DISALLOW_SUBDOMAIN,
google_util::ALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.google.com:443"),
google_util::DISALLOW_SUBDOMAIN,
google_util::ALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("file://www.google.com"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("doesnotexist://www.google.com"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
}
TEST(GoogleUtilTest, GoogleBaseURLNotSpecified) {
// When no command-line flag is specified, no input to
// StartsWithCommandLineGoogleBaseURL() should return true.
EXPECT_FALSE(StartsWithBaseURL(std::string()));
EXPECT_FALSE(StartsWithBaseURL("http://www.foo.com/"));
EXPECT_FALSE(StartsWithBaseURL("http://www.google.com/"));
// By default, none of the IsGoogleXXX functions should return true for a
// "foo.com" URL.
EXPECT_FALSE(
IsGoogleHostname("www.foo.com", google_util::DISALLOW_SUBDOMAIN));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("http://www.foo.com/xyz"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsGoogleDomainUrl(GURL("https://www.foo.com/"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsHomePage("https://www.foo.com/webhp"));
EXPECT_FALSE(IsSearch("http://www.foo.com/search?q=a"));
// Override the Google base URL on the command line.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kGoogleBaseURL, "http://www.foo.com/");
// Only URLs which start with exactly the string on the command line should
// cause StartsWithCommandLineGoogleBaseURL() to return true.
EXPECT_FALSE(StartsWithBaseURL(std::string()));
EXPECT_TRUE(StartsWithBaseURL("http://www.foo.com/"));
EXPECT_TRUE(StartsWithBaseURL("http://www.foo.com/abc"));
EXPECT_FALSE(StartsWithBaseURL("https://www.foo.com/"));
EXPECT_FALSE(StartsWithBaseURL("http://www.google.com/"));
// The various IsGoogleXXX functions should respect the command-line flag.
EXPECT_TRUE(IsGoogleHostname("www.foo.com", google_util::DISALLOW_SUBDOMAIN));
EXPECT_FALSE(IsGoogleHostname("foo.com", google_util::ALLOW_SUBDOMAIN));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("http://www.foo.com/xyz"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsGoogleDomainUrl(GURL("https://www.foo.com/"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsHomePage("https://www.foo.com/webhp"));
EXPECT_FALSE(IsHomePage("http://www.foo.com/xyz"));
EXPECT_TRUE(IsSearch("http://www.foo.com/search?q=a"));
}
TEST(GoogleUtilTest, GoogleBaseURLDisallowQuery) {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kGoogleBaseURL, "http://www.foo.com/?q=");
EXPECT_FALSE(google_util::CommandLineGoogleBaseURL().is_valid());
}
TEST(GoogleUtilTest, GoogleBaseURLDisallowRef) {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kGoogleBaseURL, "http://www.foo.com/#q=");
EXPECT_FALSE(google_util::CommandLineGoogleBaseURL().is_valid());
}
TEST(GoogleUtilTest, GoogleBaseURLFixup) {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kGoogleBaseURL, "www.foo.com");
ASSERT_TRUE(google_util::CommandLineGoogleBaseURL().is_valid());
EXPECT_EQ("http://www.foo.com/",
google_util::CommandLineGoogleBaseURL().spec());
}
TEST(GoogleUtilTest, YoutubeDomains) {
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://youtube.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://youtube.com/path/main.html"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://notyoutube.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
// YouTube Kids is not a youtube domain as it does not use the standard Google
// auth stack.
//
// Regression test for b/247647476
EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://www.youtubekids.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
// TLD checks.
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.ca"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.co.uk"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://www.youtube.notrealtld"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
// Non-YouTube TLD.
EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://www.youtube.biz.tj"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
// Subdomain checks.
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://images.youtube.com"),
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://images.youtube.com"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
// Port and scheme checks.
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.com:80"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("https://www.youtube.com:443"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsYoutubeDomainUrl(GURL("http://www.youtube.com:123"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
EXPECT_TRUE(IsYoutubeDomainUrl(GURL("http://www.youtube.com:123"),
google_util::DISALLOW_SUBDOMAIN,
google_util::ALLOW_NON_STANDARD_PORTS));
EXPECT_FALSE(IsYoutubeDomainUrl(GURL("file://www.youtube.com"),
google_util::DISALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS));
}
TEST(GoogleUtilTest, GoogleAssociatedDomains) {
EXPECT_FALSE(google_util::IsGoogleAssociatedDomainUrl(GURL()));
EXPECT_FALSE(google_util::IsGoogleAssociatedDomainUrl(GURL("invalid")));
EXPECT_FALSE(
google_util::IsGoogleAssociatedDomainUrl(GURL("https://myblog.com.au")));
// A typical URL for a Google production API.
EXPECT_TRUE(google_util::IsGoogleAssociatedDomainUrl(
GURL("https://myapi-pa.googleapis.com/v1/myservice")));
// A typical URL for a test instance of a Google API.
EXPECT_TRUE(google_util::IsGoogleAssociatedDomainUrl(
GURL("https://daily0-myapi-pa.sandbox.googleapis.com/v1/myservice")));
// A Google production API with parameters.
EXPECT_TRUE(google_util::IsGoogleAssociatedDomainUrl(
GURL("https://myapi-pa.googleapis.com/v1/myservice?k1=v1&k2=v2")));
// A Google test API with parameters.
EXPECT_TRUE(google_util::IsGoogleAssociatedDomainUrl(
GURL("https://daily0-myapi-pa.sandbox.googleapis.com/v1/"
"myservice?k1=v1&k2=v2")));
}
TEST(GoogleUtilTest, AppendToAsyncQueryParam) {
// Append to plain URL.
EXPECT_EQ(GURL("https://foo.com?async=bar:baz"),
google_util::AppendToAsyncQueryParam(GURL("https://foo.com"), "bar",
"baz"));
// Append to async param.
EXPECT_EQ(GURL("https://foo.com?async=bar:baz,hello:world"),
google_util::AppendToAsyncQueryParam(
GURL("https://foo.com?async=bar:baz"), "hello", "world"));
// Append to same async param.
EXPECT_EQ(GURL("https://foo.com?async=bar:baz,bar:buz"),
google_util::AppendToAsyncQueryParam(
GURL("https://foo.com?async=bar:baz"), "bar", "buz"));
}
TEST(GoogleUtilTest, GoogleSearchMode) {
EXPECT_EQ(
google_util::GoogleSearchModeFromUrl(GURL("https://www.google.com/")),
google_util::GoogleSearchMode::kWeb);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo")),
google_util::GoogleSearchMode::kWeb);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=web")),
google_util::GoogleSearchMode::kWeb);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=isch")),
google_util::GoogleSearchMode::kImages);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=vid")),
google_util::GoogleSearchMode::kVideos);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=shop")),
google_util::GoogleSearchMode::kShopping);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=flm")),
google_util::GoogleSearchMode::kFlights);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=bks")),
google_util::GoogleSearchMode::kBooks);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=nws")),
google_util::GoogleSearchMode::kNews);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=lcl")),
google_util::GoogleSearchMode::kLocal);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=invalid")),
google_util::GoogleSearchMode::kUnknown);
EXPECT_EQ(google_util::GoogleSearchModeFromUrl(
GURL("https://www.google.com/search?q=foo&tbm=lcl&tbm=nws")),
google_util::GoogleSearchMode::kUnknown);
}
|