google_configs_unittest.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2015 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.
  4. #include "components/domain_reliability/google_configs.h"
  5. #include "net/base/url_util.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace domain_reliability {
  8. namespace {
  9. // This really only checks domains, not origins.
  10. bool HasSameOriginCollector(const DomainReliabilityConfig* config) {
  11. for (const auto& collector : config->collectors) {
  12. if (collector->host() == config->origin.host())
  13. return true;
  14. }
  15. return false;
  16. }
  17. TEST(DomainReliabilityGoogleConfigsTest, ConfigsAreValid) {
  18. auto configs = GetAllGoogleConfigsForTesting();
  19. for (const auto& config : configs) {
  20. EXPECT_TRUE(config->IsValid());
  21. }
  22. }
  23. TEST(DomainReliabilityGoogleConfigsTest, MaybeGetGoogleConfig) {
  24. // Includes subdomains and includes same-origin collector.
  25. std::string host = "google.ac";
  26. auto config = MaybeGetGoogleConfig(host);
  27. EXPECT_EQ(host, config->origin.host());
  28. EXPECT_TRUE(config->include_subdomains);
  29. EXPECT_TRUE(HasSameOriginCollector(config.get()));
  30. // Includes subdomains and excludes same-origin collector.
  31. host = "2mdn.net";
  32. config = MaybeGetGoogleConfig(host);
  33. EXPECT_EQ(host, config->origin.host());
  34. EXPECT_TRUE(config->include_subdomains);
  35. EXPECT_FALSE(HasSameOriginCollector(config.get()));
  36. // Excludes subdomains and includes same-origin collector.
  37. host = "accounts.google.com";
  38. config = MaybeGetGoogleConfig(host);
  39. EXPECT_EQ(host, config->origin.host());
  40. EXPECT_FALSE(config->include_subdomains);
  41. EXPECT_TRUE(HasSameOriginCollector(config.get()));
  42. // Excludes subdomains and excludes same-origin collector.
  43. host = "ad.doubleclick.net";
  44. config = MaybeGetGoogleConfig(host);
  45. EXPECT_EQ(host, config->origin.host());
  46. EXPECT_FALSE(config->include_subdomains);
  47. EXPECT_FALSE(HasSameOriginCollector(config.get()));
  48. }
  49. TEST(DomainReliabilityGoogleConfigsTest, MaybeGetGoogleConfigSubdomains) {
  50. // google.ac is duplicated for the www. subdomain so it generates a
  51. // subdomain-specific config.
  52. std::string host = "www.google.ac";
  53. auto config = MaybeGetGoogleConfig(host);
  54. EXPECT_EQ(host, config->origin.host());
  55. // Subdomains are not included for the www version.
  56. EXPECT_FALSE(config->include_subdomains);
  57. // Other subdomains match the parent config.
  58. host = "subdomain.google.ac";
  59. config = MaybeGetGoogleConfig(host);
  60. EXPECT_EQ(net::GetSuperdomain(host), config->origin.host());
  61. EXPECT_TRUE(config->include_subdomains);
  62. // 2mdn.net is not duplicated for www, but it includes subdomains, so
  63. // www.2mdn.net is covered.
  64. host = "www.2mdn.net";
  65. config = MaybeGetGoogleConfig(host);
  66. EXPECT_EQ(net::GetSuperdomain(host), config->origin.host());
  67. EXPECT_TRUE(config->include_subdomains);
  68. // drive.google.com does not include subdomains and is not duplicated for www.
  69. host = "subdomain.drive.google.com";
  70. config = MaybeGetGoogleConfig(host);
  71. EXPECT_FALSE(config);
  72. host = "www.drive.google.com";
  73. config = MaybeGetGoogleConfig(host);
  74. EXPECT_FALSE(config);
  75. // accounts.google.com should get its own config, even though it is a
  76. // subdomain of google.com (which does include subdomains), because an exact
  77. // match takes priority.
  78. host = "accounts.google.com";
  79. config = MaybeGetGoogleConfig(host);
  80. EXPECT_EQ(host, config->origin.host());
  81. }
  82. } // namespace
  83. } // namespace domain_reliability