| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/domain_reliability/config.h" | 5 #include "components/domain_reliability/config.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace domain_reliability { | 13 namespace domain_reliability { |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 scoped_ptr<DomainReliabilityConfig> MakeBaseConfig() { | 16 scoped_ptr<DomainReliabilityConfig> MakeBaseConfig() { |
| 17 DomainReliabilityConfig* config = new DomainReliabilityConfig(); | 17 DomainReliabilityConfig* config = new DomainReliabilityConfig(); |
| 18 config->domain = "example"; | 18 config->origin = GURL("https://example/"); |
| 19 config->version = "1"; | 19 config->include_subdomains = false; |
| 20 | 20 config->collectors.push_back(new GURL("https://example/upload")); |
| 21 DomainReliabilityConfig::Collector* collector = | 21 config->failure_sample_rate = 1.0; |
| 22 new DomainReliabilityConfig::Collector(); | 22 config->success_sample_rate = 0.0; |
| 23 collector->upload_url = GURL("https://example/upload"); | 23 EXPECT_TRUE(config->IsValid()); |
| 24 config->collectors.push_back(collector); | |
| 25 | |
| 26 return scoped_ptr<DomainReliabilityConfig>(config); | 24 return scoped_ptr<DomainReliabilityConfig>(config); |
| 27 } | 25 } |
| 28 | 26 |
| 29 scoped_ptr<DomainReliabilityConfig> MakeSampleConfig() { | 27 scoped_ptr<DomainReliabilityConfig> MakeSampleConfig() { |
| 30 scoped_ptr<DomainReliabilityConfig> config(MakeBaseConfig()); | 28 scoped_ptr<DomainReliabilityConfig> config(MakeBaseConfig()); |
| 31 | 29 config->path_prefixes.push_back(new std::string("/css/")); |
| 32 DomainReliabilityConfig::Resource* resource = | 30 config->path_prefixes.push_back(new std::string("/js/")); |
| 33 new DomainReliabilityConfig::Resource(); | |
| 34 resource->name = "home"; | |
| 35 resource->url_patterns.push_back( | |
| 36 new std::string("http://example/")); | |
| 37 resource->success_sample_rate = 0.0; | |
| 38 resource->failure_sample_rate = 1.0; | |
| 39 config->resources.push_back(resource); | |
| 40 | |
| 41 resource = new DomainReliabilityConfig::Resource(); | |
| 42 resource->name = "static"; | |
| 43 resource->url_patterns.push_back(new std::string("http://example/css/*")); | |
| 44 resource->url_patterns.push_back(new std::string("http://example/js/*")); | |
| 45 resource->success_sample_rate = 0.0; | |
| 46 resource->failure_sample_rate = 1.0; | |
| 47 config->resources.push_back(resource); | |
| 48 | |
| 49 resource = new DomainReliabilityConfig::Resource(); | |
| 50 resource->name = "html"; | |
| 51 resource->url_patterns.push_back( | |
| 52 new std::string("http://example/*.html")); | |
| 53 resource->success_sample_rate = 0.0; | |
| 54 resource->failure_sample_rate = 1.0; | |
| 55 config->resources.push_back(resource); | |
| 56 | |
| 57 EXPECT_TRUE(config->IsValid()); | 31 EXPECT_TRUE(config->IsValid()); |
| 58 return config.Pass(); | 32 return config.Pass(); |
| 59 } | 33 } |
| 60 | 34 |
| 61 scoped_ptr<DomainReliabilityConfig> MakeConfigWithResource( | |
| 62 const std::string& name, | |
| 63 const std::string& pattern) { | |
| 64 scoped_ptr<DomainReliabilityConfig> config(MakeBaseConfig()); | |
| 65 | |
| 66 DomainReliabilityConfig::Resource* resource = | |
| 67 new DomainReliabilityConfig::Resource(); | |
| 68 resource->name = name; | |
| 69 resource->url_patterns.push_back(new std::string(pattern)); | |
| 70 resource->success_sample_rate = 1.0; | |
| 71 resource->failure_sample_rate = 1.0; | |
| 72 config->resources.push_back(resource); | |
| 73 | |
| 74 EXPECT_TRUE(config->IsValid()); | |
| 75 return config.Pass(); | |
| 76 } | |
| 77 | |
| 78 int GetIndex(DomainReliabilityConfig* config, const char* url_string) { | |
| 79 return config->GetResourceIndexForUrl(GURL(url_string)); | |
| 80 } | |
| 81 | |
| 82 class DomainReliabilityConfigTest : public testing::Test { }; | 35 class DomainReliabilityConfigTest : public testing::Test { }; |
| 83 | 36 |
| 84 TEST_F(DomainReliabilityConfigTest, IsValid) { | 37 TEST_F(DomainReliabilityConfigTest, IsValid) { |
| 85 scoped_ptr<DomainReliabilityConfig> config; | 38 scoped_ptr<DomainReliabilityConfig> config; |
| 86 | 39 |
| 87 config = MakeSampleConfig(); | 40 config = MakeSampleConfig(); |
| 88 EXPECT_TRUE(config->IsValid()); | 41 EXPECT_TRUE(config->IsValid()); |
| 89 | 42 |
| 90 config = MakeSampleConfig(); | 43 config = MakeSampleConfig(); |
| 91 config->domain = ""; | 44 config->origin = GURL(); |
| 92 EXPECT_FALSE(config->IsValid()); | |
| 93 | |
| 94 // Version is optional. | |
| 95 config = MakeSampleConfig(); | |
| 96 config->version = ""; | |
| 97 EXPECT_TRUE(config->IsValid()); | |
| 98 | |
| 99 config = MakeSampleConfig(); | |
| 100 config->resources.clear(); | |
| 101 EXPECT_FALSE(config->IsValid()); | |
| 102 | |
| 103 config = MakeSampleConfig(); | |
| 104 config->resources[0]->name.clear(); | |
| 105 EXPECT_FALSE(config->IsValid()); | |
| 106 | |
| 107 config = MakeSampleConfig(); | |
| 108 config->resources[0]->url_patterns.clear(); | |
| 109 EXPECT_FALSE(config->IsValid()); | |
| 110 | |
| 111 config = MakeSampleConfig(); | |
| 112 config->resources[0]->success_sample_rate = 2.0; | |
| 113 EXPECT_FALSE(config->IsValid()); | |
| 114 | |
| 115 config = MakeSampleConfig(); | |
| 116 config->resources[0]->failure_sample_rate = 2.0; | |
| 117 EXPECT_FALSE(config->IsValid()); | 45 EXPECT_FALSE(config->IsValid()); |
| 118 | 46 |
| 119 config = MakeSampleConfig(); | 47 config = MakeSampleConfig(); |
| 120 config->collectors.clear(); | 48 config->collectors.clear(); |
| 121 EXPECT_FALSE(config->IsValid()); | 49 EXPECT_FALSE(config->IsValid()); |
| 122 | 50 |
| 123 config = MakeSampleConfig(); | 51 config = MakeSampleConfig(); |
| 124 config->collectors[0]->upload_url = GURL(); | 52 delete config->collectors[0]; |
| 53 config->collectors[0] = new GURL(); |
| 125 EXPECT_FALSE(config->IsValid()); | 54 EXPECT_FALSE(config->IsValid()); |
| 126 } | |
| 127 | 55 |
| 128 TEST_F(DomainReliabilityConfigTest, GetResourceIndexForUrl) { | 56 config = MakeSampleConfig(); |
| 129 scoped_ptr<DomainReliabilityConfig> config = MakeSampleConfig(); | 57 config->failure_sample_rate = 2.0; |
| 58 EXPECT_FALSE(config->IsValid()); |
| 130 | 59 |
| 131 EXPECT_EQ(0, GetIndex(config.get(), "http://example/")); | 60 config = MakeSampleConfig(); |
| 132 EXPECT_EQ(1, GetIndex(config.get(), "http://example/css/foo.css")); | 61 config->success_sample_rate = 2.0; |
| 133 EXPECT_EQ(1, GetIndex(config.get(), "http://example/js/bar.js")); | 62 EXPECT_FALSE(config->IsValid()); |
| 134 EXPECT_EQ(2, GetIndex(config.get(), "http://example/test.html")); | |
| 135 EXPECT_EQ(-1, GetIndex(config.get(), "http://example/no-resource")); | |
| 136 } | |
| 137 | |
| 138 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchUsername) { | |
| 139 scoped_ptr<DomainReliabilityConfig> config = | |
| 140 MakeConfigWithResource("username", "*username*"); | |
| 141 | |
| 142 EXPECT_EQ(-1, GetIndex(config.get(), "http://username:password@example/")); | |
| 143 } | |
| 144 | |
| 145 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchPassword) { | |
| 146 scoped_ptr<DomainReliabilityConfig> config = | |
| 147 MakeConfigWithResource("password", "*password*"); | |
| 148 | |
| 149 EXPECT_EQ(-1, GetIndex(config.get(), "http://username:password@example/")); | |
| 150 } | |
| 151 | |
| 152 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchFragment) { | |
| 153 scoped_ptr<DomainReliabilityConfig> config = | |
| 154 MakeConfigWithResource("fragment", "*fragment*"); | |
| 155 | |
| 156 EXPECT_EQ(-1, GetIndex(config.get(), "http://example/#fragment")); | |
| 157 } | 63 } |
| 158 | 64 |
| 159 TEST_F(DomainReliabilityConfigTest, FromJSON) { | 65 TEST_F(DomainReliabilityConfigTest, FromJSON) { |
| 160 std::string config_json = | 66 std::string config_json = |
| 161 "{ \"config_version\": \"1\"," | 67 "{ \"origin\": \"https://example/\"," |
| 162 " \"monitored_domain\": \"test.example\"," | 68 " \"include_subdomains\": false," |
| 163 " \"monitored_resources\": [ {" | 69 " \"collectors\": [ \"https://example/upload\" ]," |
| 164 " \"resource_name\": \"home\"," | 70 " \"path_prefixes\": [" |
| 165 " \"url_patterns\": [ \"http://test.example/\" ]," | 71 " \"/css/\"," |
| 166 " \"success_sample_rate\": 0.01," | 72 " \"/js/\"" |
| 167 " \"failure_sample_rate\": 0.10" | 73 " ]," |
| 168 " } ]," | 74 " \"failure_sample_rate\": 0.10," |
| 169 " \"collectors\": [ {" | 75 " \"success_sample_rate\": 0.01" |
| 170 " \"upload_url\": \"https://test.example/domrel/upload\"" | |
| 171 " } ]" | |
| 172 "}"; | 76 "}"; |
| 173 | 77 |
| 174 scoped_ptr<const DomainReliabilityConfig> config( | 78 scoped_ptr<const DomainReliabilityConfig> config( |
| 175 DomainReliabilityConfig::FromJSON(config_json)); | 79 DomainReliabilityConfig::FromJSON(config_json)); |
| 176 | 80 |
| 177 EXPECT_TRUE(config); | 81 EXPECT_TRUE(config); |
| 178 EXPECT_EQ("1", config->version); | 82 EXPECT_EQ("https://example/", config->origin.spec()); |
| 179 EXPECT_EQ("test.example", config->domain); | 83 EXPECT_FALSE(config->include_subdomains); |
| 180 EXPECT_EQ(1u, config->resources.size()); | |
| 181 EXPECT_EQ("home", config->resources[0]->name); | |
| 182 EXPECT_EQ(1u, config->resources[0]->url_patterns.size()); | |
| 183 EXPECT_EQ("http://test.example/", *(config->resources[0]->url_patterns[0])); | |
| 184 EXPECT_EQ(0.01, config->resources[0]->success_sample_rate); | |
| 185 EXPECT_EQ(0.10, config->resources[0]->failure_sample_rate); | |
| 186 EXPECT_EQ(1u, config->collectors.size()); | 84 EXPECT_EQ(1u, config->collectors.size()); |
| 187 EXPECT_EQ(GURL("https://test.example/domrel/upload"), | 85 EXPECT_EQ(GURL("https://example/upload"), *config->collectors[0]); |
| 188 config->collectors[0]->upload_url); | 86 EXPECT_EQ(2u, config->path_prefixes.size()); |
| 87 EXPECT_EQ("/css/", *config->path_prefixes[0]); |
| 88 EXPECT_EQ("/js/", *config->path_prefixes[1]); |
| 89 EXPECT_EQ(0.10, config->failure_sample_rate); |
| 90 EXPECT_EQ(0.01, config->success_sample_rate); |
| 189 } | 91 } |
| 190 | 92 |
| 191 } // namespace | 93 } // namespace |
| 192 } // namespace domain_reliability | 94 } // namespace domain_reliability |
| OLD | NEW |