Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / domain_reliability / config_unittest.cc
blob11aa39a935e2a6f529d43280cfa05791f2fc4283
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
3 // found in the LICENSE file.
5 #include "components/domain_reliability/config.h"
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace domain_reliability {
14 namespace {
16 scoped_ptr<DomainReliabilityConfig> MakeBaseConfig() {
17 DomainReliabilityConfig* config = new DomainReliabilityConfig();
18 config->domain = "example";
19 config->valid_until = 1234567890.0;
20 config->version = "1";
22 DomainReliabilityConfig::Collector* collector =
23 new DomainReliabilityConfig::Collector();
24 collector->upload_url = GURL("https://example/upload");
25 config->collectors.push_back(collector);
27 return scoped_ptr<DomainReliabilityConfig>(config);
30 scoped_ptr<DomainReliabilityConfig> MakeSampleConfig() {
31 scoped_ptr<DomainReliabilityConfig> config(MakeBaseConfig());
33 DomainReliabilityConfig::Resource* resource =
34 new DomainReliabilityConfig::Resource();
35 resource->name = "home";
36 resource->url_patterns.push_back(
37 new std::string("http://example/"));
38 resource->success_sample_rate = 0.0;
39 resource->failure_sample_rate = 1.0;
40 config->resources.push_back(resource);
42 resource = new DomainReliabilityConfig::Resource();
43 resource->name = "static";
44 resource->url_patterns.push_back(new std::string("http://example/css/*"));
45 resource->url_patterns.push_back(new std::string("http://example/js/*"));
46 resource->success_sample_rate = 0.0;
47 resource->failure_sample_rate = 1.0;
48 config->resources.push_back(resource);
50 resource = new DomainReliabilityConfig::Resource();
51 resource->name = "html";
52 resource->url_patterns.push_back(
53 new std::string("http://example/*.html"));
54 resource->success_sample_rate = 0.0;
55 resource->failure_sample_rate = 1.0;
56 config->resources.push_back(resource);
58 EXPECT_TRUE(config->IsValid());
59 return config.Pass();
62 scoped_ptr<DomainReliabilityConfig> MakeConfigWithResource(
63 const std::string& name,
64 const std::string& pattern) {
65 scoped_ptr<DomainReliabilityConfig> config(MakeBaseConfig());
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);
75 EXPECT_TRUE(config->IsValid());
76 return config.Pass();
79 int GetIndex(DomainReliabilityConfig* config, const char* url_string) {
80 return config->GetResourceIndexForUrl(GURL(url_string));
83 } // namespace
85 class DomainReliabilityConfigTest : public testing::Test { };
87 TEST_F(DomainReliabilityConfigTest, IsValid) {
88 scoped_ptr<DomainReliabilityConfig> config;
90 config = MakeSampleConfig();
91 EXPECT_TRUE(config->IsValid());
93 config = MakeSampleConfig();
94 config->domain = "";
95 EXPECT_FALSE(config->IsValid());
97 config = MakeSampleConfig();
98 config->valid_until = 0.0;
99 EXPECT_FALSE(config->IsValid());
101 config = MakeSampleConfig();
102 config->version = "";
103 EXPECT_FALSE(config->IsValid());
105 config = MakeSampleConfig();
106 config->resources.clear();
107 EXPECT_FALSE(config->IsValid());
109 config = MakeSampleConfig();
110 config->resources[0]->name.clear();
111 EXPECT_FALSE(config->IsValid());
113 config = MakeSampleConfig();
114 config->resources[0]->url_patterns.clear();
115 EXPECT_FALSE(config->IsValid());
117 config = MakeSampleConfig();
118 config->resources[0]->success_sample_rate = 2.0;
119 EXPECT_FALSE(config->IsValid());
121 config = MakeSampleConfig();
122 config->resources[0]->failure_sample_rate = 2.0;
123 EXPECT_FALSE(config->IsValid());
125 config = MakeSampleConfig();
126 config->collectors.clear();
127 EXPECT_FALSE(config->IsValid());
129 config = MakeSampleConfig();
130 config->collectors[0]->upload_url = GURL();
131 EXPECT_FALSE(config->IsValid());
134 TEST_F(DomainReliabilityConfigTest, IsExpired) {
135 base::Time now = base::Time::Now();
136 base::TimeDelta one_day = base::TimeDelta::FromDays(1);
138 DomainReliabilityConfig unexpired_config;
139 unexpired_config.valid_until = (now + one_day).ToDoubleT();
140 EXPECT_FALSE(unexpired_config.IsExpired(now));
142 DomainReliabilityConfig expired_config;
143 expired_config.valid_until = (now - one_day).ToDoubleT();
144 EXPECT_TRUE(expired_config.IsExpired(now));
147 TEST_F(DomainReliabilityConfigTest, GetResourceIndexForUrl) {
148 scoped_ptr<DomainReliabilityConfig> config = MakeSampleConfig();
150 EXPECT_EQ(0, GetIndex(config.get(), "http://example/"));
151 EXPECT_EQ(1, GetIndex(config.get(), "http://example/css/foo.css"));
152 EXPECT_EQ(1, GetIndex(config.get(), "http://example/js/bar.js"));
153 EXPECT_EQ(2, GetIndex(config.get(), "http://example/test.html"));
154 EXPECT_EQ(-1, GetIndex(config.get(), "http://example/no-resource"));
157 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchUsername) {
158 scoped_ptr<DomainReliabilityConfig> config =
159 MakeConfigWithResource("username", "*username*");
161 EXPECT_EQ(-1, GetIndex(config.get(), "http://username:password@example/"));
164 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchPassword) {
165 scoped_ptr<DomainReliabilityConfig> config =
166 MakeConfigWithResource("password", "*password*");
168 EXPECT_EQ(-1, GetIndex(config.get(), "http://username:password@example/"));
171 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchFragment) {
172 scoped_ptr<DomainReliabilityConfig> config =
173 MakeConfigWithResource("fragment", "*fragment*");
175 EXPECT_EQ(-1, GetIndex(config.get(), "http://example/#fragment"));
178 TEST_F(DomainReliabilityConfigTest, FromJSON) {
179 std::string config_json =
180 "{ \"config_version\": \"1\","
181 " \"config_valid_until\": 1234567890.0,"
182 " \"monitored_domain\": \"test.example\","
183 " \"monitored_resources\": [ {"
184 " \"resource_name\": \"home\","
185 " \"url_patterns\": [ \"http://test.example/\" ],"
186 " \"success_sample_rate\": 0.01,"
187 " \"failure_sample_rate\": 0.10"
188 " } ],"
189 " \"collectors\": [ {"
190 " \"upload_url\": \"https://test.example/domrel/upload\""
191 " } ]"
192 "}";
194 scoped_ptr<const DomainReliabilityConfig> config(
195 DomainReliabilityConfig::FromJSON(config_json));
197 EXPECT_TRUE(config);
198 EXPECT_EQ("1", config->version);
199 EXPECT_EQ(1234567890.0, config->valid_until);
200 EXPECT_EQ("test.example", config->domain);
201 EXPECT_EQ(1u, config->resources.size());
202 EXPECT_EQ("home", config->resources[0]->name);
203 EXPECT_EQ(1u, config->resources[0]->url_patterns.size());
204 EXPECT_EQ("http://test.example/", *(config->resources[0]->url_patterns[0]));
205 EXPECT_EQ(0.01, config->resources[0]->success_sample_rate);
206 EXPECT_EQ(0.10, config->resources[0]->failure_sample_rate);
207 EXPECT_EQ(1u, config->collectors.size());
208 EXPECT_EQ(GURL("https://test.example/domrel/upload"),
209 config->collectors[0]->upload_url);
212 } // namespace domain_reliability