OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 ListValue* https_scheme_list = new ListValue(); | 102 ListValue* https_scheme_list = new ListValue(); |
103 https_scheme_list->Append(Value::CreateStringValue("https")); | 103 https_scheme_list->Append(Value::CreateStringValue("https")); |
104 DictionaryValue* https_url_filter = new DictionaryValue(); | 104 DictionaryValue* https_url_filter = new DictionaryValue(); |
105 https_url_filter->SetString(keys2::kHostSuffixKey, "example.com"); | 105 https_url_filter->SetString(keys2::kHostSuffixKey, "example.com"); |
106 https_url_filter->SetString(keys2::kHostPrefixKey, "www"); | 106 https_url_filter->SetString(keys2::kHostPrefixKey, "www"); |
107 https_url_filter->Set(keys2::kSchemesKey, https_scheme_list); | 107 https_url_filter->Set(keys2::kSchemesKey, https_scheme_list); |
108 DictionaryValue https_condition; | 108 DictionaryValue https_condition; |
109 https_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 109 https_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
110 https_condition.Set(keys::kUrlKey, https_url_filter); | 110 https_condition.Set(keys::kUrlKey, https_url_filter); |
111 | 111 |
112 DictionaryValue empty_condition; | |
113 empty_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | |
114 | |
112 WebRequestConditionSet::AnyVector conditions; | 115 WebRequestConditionSet::AnyVector conditions; |
113 | 116 |
114 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 117 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
115 new json_schema_compiler::any::Any); | 118 new json_schema_compiler::any::Any); |
116 condition1->Init(http_condition); | 119 condition1->Init(http_condition); |
117 conditions.push_back(condition1); | 120 conditions.push_back(condition1); |
118 | 121 |
119 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( | 122 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( |
120 new json_schema_compiler::any::Any); | 123 new json_schema_compiler::any::Any); |
121 condition2->Init(https_condition); | 124 condition2->Init(https_condition); |
122 conditions.push_back(condition2); | 125 conditions.push_back(condition2); |
123 | 126 |
127 linked_ptr<json_schema_compiler::any::Any> condition3 = make_linked_ptr( | |
128 new json_schema_compiler::any::Any); | |
129 condition3->Init(empty_condition); | |
130 conditions.push_back(condition3); | |
131 | |
124 // Test insertion | 132 // Test insertion |
125 std::string error; | 133 std::string error; |
126 scoped_ptr<WebRequestConditionSet> result = | 134 scoped_ptr<WebRequestConditionSet> result = |
127 WebRequestConditionSet::Create(matcher.condition_factory(), | 135 WebRequestConditionSet::Create(matcher.condition_factory(), |
128 conditions, &error); | 136 conditions, &error); |
129 EXPECT_EQ("", error); | 137 EXPECT_EQ("", error); |
130 ASSERT_TRUE(result.get()); | 138 ASSERT_TRUE(result.get()); |
131 EXPECT_EQ(2u, result->conditions().size()); | 139 EXPECT_EQ(3u, result->conditions().size()); |
132 | 140 |
133 // Tell the URLMatcher about our shiny new patterns. | 141 // Tell the URLMatcher about our shiny new patterns. |
134 URLMatcherConditionSet::Vector url_matcher_condition_set; | 142 URLMatcherConditionSet::Vector url_matcher_condition_set; |
135 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 143 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
136 matcher.AddConditionSets(url_matcher_condition_set); | 144 matcher.AddConditionSets(url_matcher_condition_set); |
137 | 145 |
138 std::set<URLMatcherConditionSet::ID> url_match_ids; | 146 std::set<URLMatcherConditionSet::ID> url_match_ids; |
139 int number_matches = 0; | 147 int number_matches = 0; |
140 | 148 |
141 // Test that the result is correct and matches http://www.example.com and | 149 // Test that the result is correct and matches http://www.example.com and |
(...skipping 28 matching lines...) Expand all Loading... | |
170 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 178 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
171 number_matches = 0; | 179 number_matches = 0; |
172 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 180 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); |
173 i != url_match_ids.end(); ++i) { | 181 i != url_match_ids.end(); ++i) { |
174 if (result->IsFulfilled( | 182 if (result->IsFulfilled( |
175 *i, WebRequestRule::RequestData( | 183 *i, WebRequestRule::RequestData( |
176 &https_foo_request, ON_BEFORE_REQUEST))) | 184 &https_foo_request, ON_BEFORE_REQUEST))) |
177 ++number_matches; | 185 ++number_matches; |
178 } | 186 } |
179 EXPECT_EQ(0, number_matches); | 187 EXPECT_EQ(0, number_matches); |
188 | |
189 // Check that the empty condition reports success independently of the URL. | |
Jeffrey Yasskin
2012/12/18 02:24:32
Please check a non-empty condition that doesn't ha
vabr (Chromium)
2012/12/18 18:26:42
Done, as WebRequestConditionTest.IsFulfilledIndepe
| |
190 // We need to use IsFulfilledWithoutURLMatcher instead of IsFulfilled to | |
191 // ignore the URL-dependent conditions. | |
192 number_matches = 0; | |
193 if (result->IsFulfilledWithoutURLMatcher( | |
194 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST))) | |
195 ++number_matches; | |
196 if (result->IsFulfilledWithoutURLMatcher( | |
197 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))) | |
198 ++number_matches; | |
199 if (result->IsFulfilledWithoutURLMatcher( | |
200 WebRequestRule::RequestData(&https_foo_request, ON_BEFORE_REQUEST))) | |
201 ++number_matches; | |
202 EXPECT_EQ(3, number_matches); | |
180 } | 203 } |
181 | 204 |
182 TEST(WebRequestConditionTest, TestPortFilter) { | 205 TEST(WebRequestConditionTest, TestPortFilter) { |
183 // Necessary for TestURLRequest. | 206 // Necessary for TestURLRequest. |
184 MessageLoop message_loop(MessageLoop::TYPE_IO); | 207 MessageLoop message_loop(MessageLoop::TYPE_IO); |
185 URLMatcher matcher; | 208 URLMatcher matcher; |
186 | 209 |
187 // Allow 80;1000-1010. | 210 // Allow 80;1000-1010. |
188 ListValue* port_range = new ListValue(); | 211 ListValue* port_range = new ListValue(); |
189 port_range->Append(Value::CreateIntegerValue(1000)); | 212 port_range->Append(Value::CreateIntegerValue(1000)); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 | 296 |
274 // Test error on incompatible application stages for involved attributes. | 297 // Test error on incompatible application stages for involved attributes. |
275 error.clear(); | 298 error.clear(); |
276 result = WebRequestCondition::Create(matcher.condition_factory(), | 299 result = WebRequestCondition::Create(matcher.condition_factory(), |
277 condition_value, &error); | 300 condition_value, &error); |
278 EXPECT_FALSE(error.empty()); | 301 EXPECT_FALSE(error.empty()); |
279 EXPECT_FALSE(result.get()); | 302 EXPECT_FALSE(result.get()); |
280 } | 303 } |
281 | 304 |
282 } // namespace extensions | 305 } // namespace extensions |
OLD | NEW |