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"
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
{
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());
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());
79 int GetIndex(DomainReliabilityConfig
* config
, const char* url_string
) {
80 return config
->GetResourceIndexForUrl(GURL(url_string
));
83 class DomainReliabilityConfigTest
: public testing::Test
{ };
85 TEST_F(DomainReliabilityConfigTest
, IsValid
) {
86 scoped_ptr
<DomainReliabilityConfig
> config
;
88 config
= MakeSampleConfig();
89 EXPECT_TRUE(config
->IsValid());
91 config
= MakeSampleConfig();
93 EXPECT_FALSE(config
->IsValid());
95 config
= MakeSampleConfig();
96 config
->valid_until
= 0.0;
97 EXPECT_FALSE(config
->IsValid());
99 config
= MakeSampleConfig();
100 config
->version
= "";
101 EXPECT_FALSE(config
->IsValid());
103 config
= MakeSampleConfig();
104 config
->resources
.clear();
105 EXPECT_FALSE(config
->IsValid());
107 config
= MakeSampleConfig();
108 config
->resources
[0]->name
.clear();
109 EXPECT_FALSE(config
->IsValid());
111 config
= MakeSampleConfig();
112 config
->resources
[0]->url_patterns
.clear();
113 EXPECT_FALSE(config
->IsValid());
115 config
= MakeSampleConfig();
116 config
->resources
[0]->success_sample_rate
= 2.0;
117 EXPECT_FALSE(config
->IsValid());
119 config
= MakeSampleConfig();
120 config
->resources
[0]->failure_sample_rate
= 2.0;
121 EXPECT_FALSE(config
->IsValid());
123 config
= MakeSampleConfig();
124 config
->collectors
.clear();
125 EXPECT_FALSE(config
->IsValid());
127 config
= MakeSampleConfig();
128 config
->collectors
[0]->upload_url
= GURL();
129 EXPECT_FALSE(config
->IsValid());
132 TEST_F(DomainReliabilityConfigTest
, IsExpired
) {
133 base::Time now
= base::Time::Now();
134 base::TimeDelta one_day
= base::TimeDelta::FromDays(1);
136 DomainReliabilityConfig unexpired_config
;
137 unexpired_config
.valid_until
= (now
+ one_day
).ToDoubleT();
138 EXPECT_FALSE(unexpired_config
.IsExpired(now
));
140 DomainReliabilityConfig expired_config
;
141 expired_config
.valid_until
= (now
- one_day
).ToDoubleT();
142 EXPECT_TRUE(expired_config
.IsExpired(now
));
145 TEST_F(DomainReliabilityConfigTest
, GetResourceIndexForUrl
) {
146 scoped_ptr
<DomainReliabilityConfig
> config
= MakeSampleConfig();
148 EXPECT_EQ(0, GetIndex(config
.get(), "http://example/"));
149 EXPECT_EQ(1, GetIndex(config
.get(), "http://example/css/foo.css"));
150 EXPECT_EQ(1, GetIndex(config
.get(), "http://example/js/bar.js"));
151 EXPECT_EQ(2, GetIndex(config
.get(), "http://example/test.html"));
152 EXPECT_EQ(-1, GetIndex(config
.get(), "http://example/no-resource"));
155 TEST_F(DomainReliabilityConfigTest
, UrlPatternCantMatchUsername
) {
156 scoped_ptr
<DomainReliabilityConfig
> config
=
157 MakeConfigWithResource("username", "*username*");
159 EXPECT_EQ(-1, GetIndex(config
.get(), "http://username:password@example/"));
162 TEST_F(DomainReliabilityConfigTest
, UrlPatternCantMatchPassword
) {
163 scoped_ptr
<DomainReliabilityConfig
> config
=
164 MakeConfigWithResource("password", "*password*");
166 EXPECT_EQ(-1, GetIndex(config
.get(), "http://username:password@example/"));
169 TEST_F(DomainReliabilityConfigTest
, UrlPatternCantMatchFragment
) {
170 scoped_ptr
<DomainReliabilityConfig
> config
=
171 MakeConfigWithResource("fragment", "*fragment*");
173 EXPECT_EQ(-1, GetIndex(config
.get(), "http://example/#fragment"));
176 TEST_F(DomainReliabilityConfigTest
, FromJSON
) {
177 std::string config_json
=
178 "{ \"config_version\": \"1\","
179 " \"config_valid_until\": 1234567890.0,"
180 " \"monitored_domain\": \"test.example\","
181 " \"monitored_resources\": [ {"
182 " \"resource_name\": \"home\","
183 " \"url_patterns\": [ \"http://test.example/\" ],"
184 " \"success_sample_rate\": 0.01,"
185 " \"failure_sample_rate\": 0.10"
187 " \"collectors\": [ {"
188 " \"upload_url\": \"https://test.example/domrel/upload\""
192 scoped_ptr
<const DomainReliabilityConfig
> config(
193 DomainReliabilityConfig::FromJSON(config_json
));
196 EXPECT_EQ("1", config
->version
);
197 EXPECT_EQ(1234567890.0, config
->valid_until
);
198 EXPECT_EQ("test.example", config
->domain
);
199 EXPECT_EQ(1u, config
->resources
.size());
200 EXPECT_EQ("home", config
->resources
[0]->name
);
201 EXPECT_EQ(1u, config
->resources
[0]->url_patterns
.size());
202 EXPECT_EQ("http://test.example/", *(config
->resources
[0]->url_patterns
[0]));
203 EXPECT_EQ(0.01, config
->resources
[0]->success_sample_rate
);
204 EXPECT_EQ(0.10, config
->resources
[0]->failure_sample_rate
);
205 EXPECT_EQ(1u, config
->collectors
.size());
206 EXPECT_EQ(GURL("https://test.example/domrel/upload"),
207 config
->collectors
[0]->upload_url
);
211 } // namespace domain_reliability