| 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 | |
| 15 namespace { | 14 namespace { |
| 16 | 15 |
| 17 scoped_ptr<DomainReliabilityConfig> MakeValidConfig() { | 16 scoped_ptr<DomainReliabilityConfig> MakeBaseConfig() { |
| 18 DomainReliabilityConfig* config = new DomainReliabilityConfig(); | 17 DomainReliabilityConfig* config = new DomainReliabilityConfig(); |
| 19 config->domain = "example"; | 18 config->domain = "example"; |
| 20 config->valid_until = 1234567890.0; | 19 config->valid_until = 1234567890.0; |
| 21 config->version = "1"; | 20 config->version = "1"; |
| 22 | 21 |
| 22 DomainReliabilityConfig::Collector* collector = |
| 23 new DomainReliabilityConfig::Collector(); |
| 24 collector->upload_url = GURL("https://example/upload"); |
| 25 config->collectors.push_back(collector); |
| 26 |
| 27 return scoped_ptr<DomainReliabilityConfig>(config); |
| 28 } |
| 29 |
| 30 scoped_ptr<DomainReliabilityConfig> MakeSampleConfig() { |
| 31 scoped_ptr<DomainReliabilityConfig> config(MakeBaseConfig()); |
| 32 |
| 23 DomainReliabilityConfig::Resource* resource = | 33 DomainReliabilityConfig::Resource* resource = |
| 24 new DomainReliabilityConfig::Resource(); | 34 new DomainReliabilityConfig::Resource(); |
| 25 resource->name = "home"; | 35 resource->name = "home"; |
| 26 resource->url_patterns.push_back( | 36 resource->url_patterns.push_back( |
| 27 new std::string("http://example/")); | 37 new std::string("http://example/")); |
| 28 resource->success_sample_rate = 0.0; | 38 resource->success_sample_rate = 0.0; |
| 29 resource->failure_sample_rate = 1.0; | 39 resource->failure_sample_rate = 1.0; |
| 30 config->resources.push_back(resource); | 40 config->resources.push_back(resource); |
| 31 | 41 |
| 32 resource = new DomainReliabilityConfig::Resource(); | 42 resource = new DomainReliabilityConfig::Resource(); |
| 33 resource->name = "static"; | 43 resource->name = "static"; |
| 34 resource->url_patterns.push_back(new std::string("http://example/css/*")); | 44 resource->url_patterns.push_back(new std::string("http://example/css/*")); |
| 35 resource->url_patterns.push_back(new std::string("http://example/js/*")); | 45 resource->url_patterns.push_back(new std::string("http://example/js/*")); |
| 36 resource->success_sample_rate = 0.0; | 46 resource->success_sample_rate = 0.0; |
| 37 resource->failure_sample_rate = 1.0; | 47 resource->failure_sample_rate = 1.0; |
| 38 config->resources.push_back(resource); | 48 config->resources.push_back(resource); |
| 39 | 49 |
| 40 resource = new DomainReliabilityConfig::Resource(); | 50 resource = new DomainReliabilityConfig::Resource(); |
| 41 resource->name = "html"; | 51 resource->name = "html"; |
| 42 resource->url_patterns.push_back( | 52 resource->url_patterns.push_back( |
| 43 new std::string("http://example/*.html")); | 53 new std::string("http://example/*.html")); |
| 44 resource->success_sample_rate = 0.0; | 54 resource->success_sample_rate = 0.0; |
| 45 resource->failure_sample_rate = 1.0; | 55 resource->failure_sample_rate = 1.0; |
| 46 config->resources.push_back(resource); | 56 config->resources.push_back(resource); |
| 47 | 57 |
| 48 DomainReliabilityConfig::Collector* collector = | 58 EXPECT_TRUE(config->IsValid()); |
| 49 new DomainReliabilityConfig::Collector(); | 59 return config.Pass(); |
| 50 collector->upload_url = GURL("https://example/upload"); | 60 } |
| 51 config->collectors.push_back(collector); | 61 |
| 62 scoped_ptr<DomainReliabilityConfig> MakeConfigWithResource( |
| 63 const std::string& name, |
| 64 const std::string& pattern) { |
| 65 scoped_ptr<DomainReliabilityConfig> config(MakeBaseConfig()); |
| 66 |
| 67 DomainReliabilityConfig::Resource* resource = |
| 68 new DomainReliabilityConfig::Resource(); |
| 69 resource->name = name; |
| 70 resource->url_patterns.push_back(new std::string(pattern)); |
| 71 resource->success_sample_rate = 1.0; |
| 72 resource->failure_sample_rate = 1.0; |
| 73 config->resources.push_back(resource); |
| 52 | 74 |
| 53 EXPECT_TRUE(config->IsValid()); | 75 EXPECT_TRUE(config->IsValid()); |
| 54 return scoped_ptr<DomainReliabilityConfig>(config); | 76 return config.Pass(); |
| 55 } | 77 } |
| 56 | 78 |
| 57 int GetIndex(DomainReliabilityConfig* config, const char* url_string) { | 79 int GetIndex(DomainReliabilityConfig* config, const char* url_string) { |
| 58 return config->GetResourceIndexForUrl(GURL(url_string)); | 80 return config->GetResourceIndexForUrl(GURL(url_string)); |
| 59 } | 81 } |
| 60 | 82 |
| 61 } // namespace | 83 } // namespace |
| 62 | 84 |
| 63 class DomainReliabilityConfigTest : public testing::Test { }; | 85 class DomainReliabilityConfigTest : public testing::Test { }; |
| 64 | 86 |
| 65 TEST_F(DomainReliabilityConfigTest, IsValid) { | 87 TEST_F(DomainReliabilityConfigTest, IsValid) { |
| 66 scoped_ptr<DomainReliabilityConfig> config; | 88 scoped_ptr<DomainReliabilityConfig> config; |
| 67 | 89 |
| 68 config = MakeValidConfig(); | 90 config = MakeSampleConfig(); |
| 69 EXPECT_TRUE(config->IsValid()); | 91 EXPECT_TRUE(config->IsValid()); |
| 70 | 92 |
| 71 config = MakeValidConfig(); | 93 config = MakeSampleConfig(); |
| 72 config->domain = ""; | 94 config->domain = ""; |
| 73 EXPECT_FALSE(config->IsValid()); | 95 EXPECT_FALSE(config->IsValid()); |
| 74 | 96 |
| 75 config = MakeValidConfig(); | 97 config = MakeSampleConfig(); |
| 76 config->valid_until = 0.0; | 98 config->valid_until = 0.0; |
| 77 EXPECT_FALSE(config->IsValid()); | 99 EXPECT_FALSE(config->IsValid()); |
| 78 | 100 |
| 79 config = MakeValidConfig(); | 101 config = MakeSampleConfig(); |
| 80 config->version = ""; | 102 config->version = ""; |
| 81 EXPECT_FALSE(config->IsValid()); | 103 EXPECT_FALSE(config->IsValid()); |
| 82 | 104 |
| 83 config = MakeValidConfig(); | 105 config = MakeSampleConfig(); |
| 84 config->resources.clear(); | 106 config->resources.clear(); |
| 85 EXPECT_FALSE(config->IsValid()); | 107 EXPECT_FALSE(config->IsValid()); |
| 86 | 108 |
| 87 config = MakeValidConfig(); | 109 config = MakeSampleConfig(); |
| 88 config->resources[0]->name.clear(); | 110 config->resources[0]->name.clear(); |
| 89 EXPECT_FALSE(config->IsValid()); | 111 EXPECT_FALSE(config->IsValid()); |
| 90 | 112 |
| 91 config = MakeValidConfig(); | 113 config = MakeSampleConfig(); |
| 92 config->resources[0]->url_patterns.clear(); | 114 config->resources[0]->url_patterns.clear(); |
| 93 EXPECT_FALSE(config->IsValid()); | 115 EXPECT_FALSE(config->IsValid()); |
| 94 | 116 |
| 95 config = MakeValidConfig(); | 117 config = MakeSampleConfig(); |
| 96 config->resources[0]->success_sample_rate = 2.0; | 118 config->resources[0]->success_sample_rate = 2.0; |
| 97 EXPECT_FALSE(config->IsValid()); | 119 EXPECT_FALSE(config->IsValid()); |
| 98 | 120 |
| 99 config = MakeValidConfig(); | 121 config = MakeSampleConfig(); |
| 100 config->resources[0]->failure_sample_rate = 2.0; | 122 config->resources[0]->failure_sample_rate = 2.0; |
| 101 EXPECT_FALSE(config->IsValid()); | 123 EXPECT_FALSE(config->IsValid()); |
| 102 | 124 |
| 103 config = MakeValidConfig(); | 125 config = MakeSampleConfig(); |
| 104 config->collectors.clear(); | 126 config->collectors.clear(); |
| 105 EXPECT_FALSE(config->IsValid()); | 127 EXPECT_FALSE(config->IsValid()); |
| 106 | 128 |
| 107 config = MakeValidConfig(); | 129 config = MakeSampleConfig(); |
| 108 config->collectors[0]->upload_url = GURL(); | 130 config->collectors[0]->upload_url = GURL(); |
| 109 EXPECT_FALSE(config->IsValid()); | 131 EXPECT_FALSE(config->IsValid()); |
| 110 } | 132 } |
| 111 | 133 |
| 112 TEST_F(DomainReliabilityConfigTest, IsExpired) { | 134 TEST_F(DomainReliabilityConfigTest, IsExpired) { |
| 113 base::Time now = base::Time::Now(); | 135 base::Time now = base::Time::Now(); |
| 114 base::TimeDelta one_day = base::TimeDelta::FromDays(1); | 136 base::TimeDelta one_day = base::TimeDelta::FromDays(1); |
| 115 | 137 |
| 116 DomainReliabilityConfig unexpired_config; | 138 DomainReliabilityConfig unexpired_config; |
| 117 unexpired_config.valid_until = (now + one_day).ToDoubleT(); | 139 unexpired_config.valid_until = (now + one_day).ToDoubleT(); |
| 118 EXPECT_FALSE(unexpired_config.IsExpired(now)); | 140 EXPECT_FALSE(unexpired_config.IsExpired(now)); |
| 119 | 141 |
| 120 DomainReliabilityConfig expired_config; | 142 DomainReliabilityConfig expired_config; |
| 121 expired_config.valid_until = (now - one_day).ToDoubleT(); | 143 expired_config.valid_until = (now - one_day).ToDoubleT(); |
| 122 EXPECT_TRUE(expired_config.IsExpired(now)); | 144 EXPECT_TRUE(expired_config.IsExpired(now)); |
| 123 } | 145 } |
| 124 | 146 |
| 125 TEST_F(DomainReliabilityConfigTest, GetResourceIndexForUrl) { | 147 TEST_F(DomainReliabilityConfigTest, GetResourceIndexForUrl) { |
| 126 scoped_ptr<DomainReliabilityConfig> config = MakeValidConfig(); | 148 scoped_ptr<DomainReliabilityConfig> config = MakeSampleConfig(); |
| 127 | 149 |
| 128 EXPECT_EQ(0, GetIndex(&*config, "http://example/")); | 150 EXPECT_EQ(0, GetIndex(&*config, "http://example/")); |
| 129 EXPECT_EQ(1, GetIndex(&*config, "http://example/css/foo.css")); | 151 EXPECT_EQ(1, GetIndex(&*config, "http://example/css/foo.css")); |
| 130 EXPECT_EQ(1, GetIndex(&*config, "http://example/js/bar.js")); | 152 EXPECT_EQ(1, GetIndex(&*config, "http://example/js/bar.js")); |
| 131 EXPECT_EQ(2, GetIndex(&*config, "http://example/test.html")); | 153 EXPECT_EQ(2, GetIndex(&*config, "http://example/test.html")); |
| 132 EXPECT_EQ(-1, GetIndex(&*config, "http://example/no-resource")); | 154 EXPECT_EQ(-1, GetIndex(&*config, "http://example/no-resource")); |
| 133 } | 155 } |
| 134 | 156 |
| 157 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchUsername) { |
| 158 scoped_ptr<DomainReliabilityConfig> config = |
| 159 MakeConfigWithResource("username", "*username*"); |
| 160 |
| 161 EXPECT_EQ(-1, GetIndex(&*config, "http://username:password@example/")); |
| 162 } |
| 163 |
| 164 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchPassword) { |
| 165 scoped_ptr<DomainReliabilityConfig> config = |
| 166 MakeConfigWithResource("password", "*password*"); |
| 167 |
| 168 EXPECT_EQ(-1, GetIndex(&*config, "http://username:password@example/")); |
| 169 } |
| 170 |
| 135 TEST_F(DomainReliabilityConfigTest, FromJSON) { | 171 TEST_F(DomainReliabilityConfigTest, FromJSON) { |
| 136 std::string config_json = | 172 std::string config_json = |
| 137 "{ \"config_version\": \"1\"," | 173 "{ \"config_version\": \"1\"," |
| 138 " \"config_valid_until\": 1234567890.0," | 174 " \"config_valid_until\": 1234567890.0," |
| 139 " \"monitored_domain\": \"test.example\"," | 175 " \"monitored_domain\": \"test.example\"," |
| 140 " \"monitored_resources\": [ {" | 176 " \"monitored_resources\": [ {" |
| 141 " \"resource_name\": \"home\"," | 177 " \"resource_name\": \"home\"," |
| 142 " \"url_patterns\": [ \"http://test.example/\" ]," | 178 " \"url_patterns\": [ \"http://test.example/\" ]," |
| 143 " \"success_sample_rate\": 0.01," | 179 " \"success_sample_rate\": 0.01," |
| 144 " \"failure_sample_rate\": 0.10" | 180 " \"failure_sample_rate\": 0.10" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 160 EXPECT_EQ(1u, config->resources[0]->url_patterns.size()); | 196 EXPECT_EQ(1u, config->resources[0]->url_patterns.size()); |
| 161 EXPECT_EQ("http://test.example/", *(config->resources[0]->url_patterns[0])); | 197 EXPECT_EQ("http://test.example/", *(config->resources[0]->url_patterns[0])); |
| 162 EXPECT_EQ(0.01, config->resources[0]->success_sample_rate); | 198 EXPECT_EQ(0.01, config->resources[0]->success_sample_rate); |
| 163 EXPECT_EQ(0.10, config->resources[0]->failure_sample_rate); | 199 EXPECT_EQ(0.10, config->resources[0]->failure_sample_rate); |
| 164 EXPECT_EQ(1u, config->collectors.size()); | 200 EXPECT_EQ(1u, config->collectors.size()); |
| 165 EXPECT_EQ(GURL("https://test.example/domrel/upload"), | 201 EXPECT_EQ(GURL("https://test.example/domrel/upload"), |
| 166 config->collectors[0]->upload_url); | 202 config->collectors[0]->upload_url); |
| 167 } | 203 } |
| 168 | 204 |
| 169 } // namespace domain_reliability | 205 } // namespace domain_reliability |
| OLD | NEW |