[Eraser strings] Remove unused Supervised User infobar and corresponding strings
[chromium-blink-merge.git] / components / domain_reliability / config_unittest.cc
blobcc6ea6452722b5ca4be8a9e6e5f1dbe7c65c2a2d
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 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();
92 config->domain = "";
93 EXPECT_FALSE(config->IsValid());
95 config = MakeSampleConfig();
96 config->valid_until = 0.0;
97 EXPECT_FALSE(config->IsValid());
99 // Version is optional.
100 config = MakeSampleConfig();
101 config->version = "";
102 EXPECT_TRUE(config->IsValid());
104 config = MakeSampleConfig();
105 config->resources.clear();
106 EXPECT_FALSE(config->IsValid());
108 config = MakeSampleConfig();
109 config->resources[0]->name.clear();
110 EXPECT_FALSE(config->IsValid());
112 config = MakeSampleConfig();
113 config->resources[0]->url_patterns.clear();
114 EXPECT_FALSE(config->IsValid());
116 config = MakeSampleConfig();
117 config->resources[0]->success_sample_rate = 2.0;
118 EXPECT_FALSE(config->IsValid());
120 config = MakeSampleConfig();
121 config->resources[0]->failure_sample_rate = 2.0;
122 EXPECT_FALSE(config->IsValid());
124 config = MakeSampleConfig();
125 config->collectors.clear();
126 EXPECT_FALSE(config->IsValid());
128 config = MakeSampleConfig();
129 config->collectors[0]->upload_url = GURL();
130 EXPECT_FALSE(config->IsValid());
133 TEST_F(DomainReliabilityConfigTest, IsExpired) {
134 base::Time now = base::Time::Now();
135 base::TimeDelta one_day = base::TimeDelta::FromDays(1);
137 DomainReliabilityConfig unexpired_config;
138 unexpired_config.valid_until = (now + one_day).ToDoubleT();
139 EXPECT_FALSE(unexpired_config.IsExpired(now));
141 DomainReliabilityConfig expired_config;
142 expired_config.valid_until = (now - one_day).ToDoubleT();
143 EXPECT_TRUE(expired_config.IsExpired(now));
146 TEST_F(DomainReliabilityConfigTest, GetResourceIndexForUrl) {
147 scoped_ptr<DomainReliabilityConfig> config = MakeSampleConfig();
149 EXPECT_EQ(0, GetIndex(config.get(), "http://example/"));
150 EXPECT_EQ(1, GetIndex(config.get(), "http://example/css/foo.css"));
151 EXPECT_EQ(1, GetIndex(config.get(), "http://example/js/bar.js"));
152 EXPECT_EQ(2, GetIndex(config.get(), "http://example/test.html"));
153 EXPECT_EQ(-1, GetIndex(config.get(), "http://example/no-resource"));
156 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchUsername) {
157 scoped_ptr<DomainReliabilityConfig> config =
158 MakeConfigWithResource("username", "*username*");
160 EXPECT_EQ(-1, GetIndex(config.get(), "http://username:password@example/"));
163 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchPassword) {
164 scoped_ptr<DomainReliabilityConfig> config =
165 MakeConfigWithResource("password", "*password*");
167 EXPECT_EQ(-1, GetIndex(config.get(), "http://username:password@example/"));
170 TEST_F(DomainReliabilityConfigTest, UrlPatternCantMatchFragment) {
171 scoped_ptr<DomainReliabilityConfig> config =
172 MakeConfigWithResource("fragment", "*fragment*");
174 EXPECT_EQ(-1, GetIndex(config.get(), "http://example/#fragment"));
177 TEST_F(DomainReliabilityConfigTest, FromJSON) {
178 std::string config_json =
179 "{ \"config_version\": \"1\","
180 " \"config_valid_until\": 1234567890.0,"
181 " \"monitored_domain\": \"test.example\","
182 " \"monitored_resources\": [ {"
183 " \"resource_name\": \"home\","
184 " \"url_patterns\": [ \"http://test.example/\" ],"
185 " \"success_sample_rate\": 0.01,"
186 " \"failure_sample_rate\": 0.10"
187 " } ],"
188 " \"collectors\": [ {"
189 " \"upload_url\": \"https://test.example/domrel/upload\""
190 " } ]"
191 "}";
193 scoped_ptr<const DomainReliabilityConfig> config(
194 DomainReliabilityConfig::FromJSON(config_json));
196 EXPECT_TRUE(config);
197 EXPECT_EQ("1", config->version);
198 EXPECT_EQ(1234567890.0, config->valid_until);
199 EXPECT_EQ("test.example", config->domain);
200 EXPECT_EQ(1u, config->resources.size());
201 EXPECT_EQ("home", config->resources[0]->name);
202 EXPECT_EQ(1u, config->resources[0]->url_patterns.size());
203 EXPECT_EQ("http://test.example/", *(config->resources[0]->url_patterns[0]));
204 EXPECT_EQ(0.01, config->resources[0]->success_sample_rate);
205 EXPECT_EQ(0.10, config->resources[0]->failure_sample_rate);
206 EXPECT_EQ(1u, config->collectors.size());
207 EXPECT_EQ(GURL("https://test.example/domrel/upload"),
208 config->collectors[0]->upload_url);
211 } // namespace
212 } // namespace domain_reliability