Chromium Code Reviews| 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" |
| 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " |
| 13 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 13 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
| 14 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
| 15 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 namespace keys = declarative_webrequest_constants; | 20 namespace keys = declarative_webrequest_constants; |
| 21 namespace keys2 = url_matcher_constants; | 21 namespace keys2 = url_matcher_constants; |
| 22 | 22 |
| 23 TEST(WebRequestConditionTest, CreateCondition) { | 23 TEST(WebRequestConditionTest, CreateCondition) { |
| 24 // Necessary for TestURLRequest. | 24 // Necessary for TestURLRequest. |
| 25 MessageLoop message_loop(MessageLoop::TYPE_IO); | 25 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 26 URLMatcher matcher; | 26 URLMatcher matcher; |
| 27 URLMatcher first_party_matcher; | |
|
battre
2012/12/06 19:53:26
I would suggest using a single URLMatcher to incre
| |
| 27 | 28 |
| 28 std::string error; | 29 std::string error; |
| 29 scoped_ptr<WebRequestCondition> result; | 30 scoped_ptr<WebRequestCondition> result; |
| 30 | 31 |
| 31 DictionaryValue invalid_condition; | 32 DictionaryValue invalid_condition; |
| 32 invalid_condition.SetString("invalid", "foobar"); | 33 invalid_condition.SetString("invalid", "foobar"); |
| 33 invalid_condition.SetString(keys::kInstanceTypeKey, | 34 invalid_condition.SetString(keys::kInstanceTypeKey, |
| 34 keys::kRequestMatcherType); | 35 keys::kRequestMatcherType); |
| 35 | 36 |
| 36 DictionaryValue invalid_condition2; | 37 DictionaryValue invalid_condition2; |
| 37 invalid_condition2.Set(keys::kUrlKey, new ListValue); | 38 invalid_condition2.Set(keys::kUrlKey, new ListValue); |
| 38 invalid_condition2.SetString(keys::kInstanceTypeKey, | 39 invalid_condition2.SetString(keys::kInstanceTypeKey, |
| 39 keys::kRequestMatcherType); | 40 keys::kRequestMatcherType); |
| 40 | 41 |
| 41 ListValue* resource_type_list = new ListValue(); | 42 ListValue* resource_type_list = new ListValue(); |
| 42 resource_type_list->Append(Value::CreateStringValue("main_frame")); | 43 resource_type_list->Append(Value::CreateStringValue("main_frame")); |
| 43 DictionaryValue* url_filter = new DictionaryValue(); | 44 DictionaryValue* url_filter = new DictionaryValue(); |
| 44 url_filter->SetString(keys2::kHostSuffixKey, "example.com"); | 45 url_filter->SetString(keys2::kHostSuffixKey, "example.com"); |
| 46 DictionaryValue* first_party_url_filter = new DictionaryValue(); | |
| 45 DictionaryValue valid_condition; | 47 DictionaryValue valid_condition; |
| 46 valid_condition.Set(keys::kResourceTypeKey, resource_type_list); | 48 valid_condition.Set(keys::kResourceTypeKey, resource_type_list); |
| 47 valid_condition.Set(keys::kUrlKey, url_filter); | 49 valid_condition.Set(keys::kUrlKey, url_filter); |
| 50 valid_condition.Set(keys::kFirstPartyForCookiesUrlKey, | |
| 51 first_party_url_filter); | |
| 48 valid_condition.SetString(keys::kInstanceTypeKey, | 52 valid_condition.SetString(keys::kInstanceTypeKey, |
| 49 keys::kRequestMatcherType); | 53 keys::kRequestMatcherType); |
| 50 | 54 |
| 51 // Test wrong condition name passed. | 55 // Test wrong condition name passed. |
| 52 error.clear(); | 56 error.clear(); |
| 53 result = WebRequestCondition::Create(matcher.condition_factory(), | 57 result = WebRequestCondition::Create(matcher.condition_factory(), |
| 58 first_party_matcher.condition_factory(), | |
| 54 invalid_condition, &error); | 59 invalid_condition, &error); |
| 55 EXPECT_FALSE(error.empty()); | 60 EXPECT_FALSE(error.empty()); |
| 56 EXPECT_FALSE(result.get()); | 61 EXPECT_FALSE(result.get()); |
| 57 | 62 |
| 58 // Test wrong datatype in host_suffix. | 63 // Test wrong datatype in host_suffix. |
| 59 error.clear(); | 64 error.clear(); |
| 60 result = WebRequestCondition::Create(matcher.condition_factory(), | 65 result = WebRequestCondition::Create(matcher.condition_factory(), |
| 66 first_party_matcher.condition_factory(), | |
| 61 invalid_condition2, &error); | 67 invalid_condition2, &error); |
| 62 EXPECT_FALSE(error.empty()); | 68 EXPECT_FALSE(error.empty()); |
| 63 EXPECT_FALSE(result.get()); | 69 EXPECT_FALSE(result.get()); |
| 64 | 70 |
| 65 // Test success (can we support multiple criteria?) | 71 // Test success (can we support multiple criteria?) |
| 66 error.clear(); | 72 error.clear(); |
| 67 result = WebRequestCondition::Create(matcher.condition_factory(), | 73 result = WebRequestCondition::Create(matcher.condition_factory(), |
| 74 first_party_matcher.condition_factory(), | |
| 68 valid_condition, &error); | 75 valid_condition, &error); |
| 69 EXPECT_EQ("", error); | 76 EXPECT_EQ("", error); |
| 70 ASSERT_TRUE(result.get()); | 77 ASSERT_TRUE(result.get()); |
| 71 | 78 |
| 72 net::TestURLRequestContext context; | 79 net::TestURLRequestContext context; |
| 73 net::TestURLRequest match_request( | 80 net::TestURLRequest match_request( |
| 74 GURL("http://www.example.com"), NULL, &context); | 81 GURL("http://www.example.com"), NULL, &context); |
| 75 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 82 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
| 76 ResourceType::MAIN_FRAME, NULL, -1, -1); | 83 ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 77 EXPECT_TRUE(result->IsFulfilled( | 84 EXPECT_TRUE(result->IsFulfilled( |
| 78 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); | 85 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); |
| 79 | 86 |
| 80 net::TestURLRequest wrong_resource_type( | 87 net::TestURLRequest wrong_resource_type( |
| 81 GURL("https://www.example.com"), NULL, &context); | 88 GURL("https://www.example.com"), NULL, &context); |
| 82 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 89 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
| 83 ResourceType::SUB_FRAME, NULL, -1, -1); | 90 ResourceType::SUB_FRAME, NULL, -1, -1); |
| 84 EXPECT_FALSE(result->IsFulfilled( | 91 EXPECT_FALSE(result->IsFulfilled( |
| 85 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | 92 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); |
| 86 } | 93 } |
| 87 | 94 |
| 88 TEST(WebRequestConditionTest, CreateConditionSet) { | 95 TEST(WebRequestConditionTest, CreateConditionSet) { |
| 89 // Necessary for TestURLRequest. | 96 // Necessary for TestURLRequest. |
| 90 MessageLoop message_loop(MessageLoop::TYPE_IO); | 97 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 91 URLMatcher matcher; | 98 URLMatcher matcher; |
| 99 URLMatcher first_party_matcher; | |
| 92 | 100 |
| 93 ListValue* http_scheme_list = new ListValue(); | 101 ListValue* http_scheme_list = new ListValue(); |
| 94 http_scheme_list->Append(Value::CreateStringValue("http")); | 102 http_scheme_list->Append(Value::CreateStringValue("http")); |
| 95 DictionaryValue* http_url_filter = new DictionaryValue(); | 103 DictionaryValue* http_url_filter = new DictionaryValue(); |
| 96 http_url_filter->SetString(keys2::kHostSuffixKey, "example.com"); | 104 http_url_filter->SetString(keys2::kHostSuffixKey, "example.com"); |
| 97 http_url_filter->Set(keys2::kSchemesKey, http_scheme_list); | 105 http_url_filter->Set(keys2::kSchemesKey, http_scheme_list); |
| 98 DictionaryValue http_condition; | 106 DictionaryValue http_condition; |
| 99 http_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 107 http_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 100 http_condition.Set(keys::kUrlKey, http_url_filter); | 108 http_condition.Set(keys::kUrlKey, http_url_filter); |
| 101 | 109 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 118 | 126 |
| 119 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( | 127 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( |
| 120 new json_schema_compiler::any::Any); | 128 new json_schema_compiler::any::Any); |
| 121 condition2->Init(https_condition); | 129 condition2->Init(https_condition); |
| 122 conditions.push_back(condition2); | 130 conditions.push_back(condition2); |
| 123 | 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(), |
| 136 first_party_matcher.condition_factory(), | |
| 128 conditions, &error); | 137 conditions, &error); |
| 129 EXPECT_EQ("", error); | 138 EXPECT_EQ("", error); |
| 130 ASSERT_TRUE(result.get()); | 139 ASSERT_TRUE(result.get()); |
| 131 EXPECT_EQ(2u, result->conditions().size()); | 140 EXPECT_EQ(2u, result->conditions().size()); |
| 132 | 141 |
| 133 // Tell the URLMatcher about our shiny new patterns. | 142 // Tell the URLMatcher about our shiny new patterns. |
| 134 URLMatcherConditionSet::Vector url_matcher_condition_set; | 143 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 135 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 144 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 136 matcher.AddConditionSets(url_matcher_condition_set); | 145 matcher.AddConditionSets(url_matcher_condition_set); |
| 137 | 146 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 &https_foo_request, ON_BEFORE_REQUEST))) | 185 &https_foo_request, ON_BEFORE_REQUEST))) |
| 177 ++number_matches; | 186 ++number_matches; |
| 178 } | 187 } |
| 179 EXPECT_EQ(0, number_matches); | 188 EXPECT_EQ(0, number_matches); |
| 180 } | 189 } |
| 181 | 190 |
| 182 TEST(WebRequestConditionTest, TestPortFilter) { | 191 TEST(WebRequestConditionTest, TestPortFilter) { |
| 183 // Necessary for TestURLRequest. | 192 // Necessary for TestURLRequest. |
| 184 MessageLoop message_loop(MessageLoop::TYPE_IO); | 193 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 185 URLMatcher matcher; | 194 URLMatcher matcher; |
| 195 URLMatcher first_party_matcher; | |
| 186 | 196 |
| 187 // Allow 80;1000-1010. | 197 // Allow 80;1000-1010. |
| 188 ListValue* port_range = new ListValue(); | 198 ListValue* port_range = new ListValue(); |
| 189 port_range->Append(Value::CreateIntegerValue(1000)); | 199 port_range->Append(Value::CreateIntegerValue(1000)); |
| 190 port_range->Append(Value::CreateIntegerValue(1010)); | 200 port_range->Append(Value::CreateIntegerValue(1010)); |
| 191 ListValue* port_ranges = new ListValue(); | 201 ListValue* port_ranges = new ListValue(); |
| 192 port_ranges->Append(Value::CreateIntegerValue(80)); | 202 port_ranges->Append(Value::CreateIntegerValue(80)); |
| 193 port_ranges->Append(port_range); | 203 port_ranges->Append(port_range); |
| 194 | 204 |
| 195 DictionaryValue* url_filter = new DictionaryValue(); | 205 DictionaryValue* url_filter = new DictionaryValue(); |
| 196 url_filter->Set(keys2::kPortsKey, port_ranges); | 206 url_filter->Set(keys2::kPortsKey, port_ranges); |
| 197 url_filter->SetString(keys2::kHostSuffixKey, "example.com"); | 207 url_filter->SetString(keys2::kHostSuffixKey, "example.com"); |
| 198 | 208 |
| 199 DictionaryValue condition; | 209 DictionaryValue condition; |
| 200 condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 210 condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 201 condition.Set(keys::kUrlKey, url_filter); | 211 condition.Set(keys::kUrlKey, url_filter); |
| 202 | 212 |
| 203 linked_ptr<json_schema_compiler::any::Any> any_condition = | 213 linked_ptr<json_schema_compiler::any::Any> any_condition = |
| 204 make_linked_ptr(new json_schema_compiler::any::Any); | 214 make_linked_ptr(new json_schema_compiler::any::Any); |
| 205 any_condition->Init(condition); | 215 any_condition->Init(condition); |
| 206 WebRequestConditionSet::AnyVector conditions; | 216 WebRequestConditionSet::AnyVector conditions; |
| 207 conditions.push_back(any_condition); | 217 conditions.push_back(any_condition); |
| 208 | 218 |
| 209 // Test insertion | 219 // Test insertion |
| 210 std::string error; | 220 std::string error; |
| 211 scoped_ptr<WebRequestConditionSet> result = | 221 scoped_ptr<WebRequestConditionSet> result = |
| 212 WebRequestConditionSet::Create(matcher.condition_factory(), | 222 WebRequestConditionSet::Create(matcher.condition_factory(), |
| 223 first_party_matcher.condition_factory(), | |
| 213 conditions, &error); | 224 conditions, &error); |
| 214 EXPECT_EQ("", error); | 225 EXPECT_EQ("", error); |
| 215 ASSERT_TRUE(result.get()); | 226 ASSERT_TRUE(result.get()); |
| 216 EXPECT_EQ(1u, result->conditions().size()); | 227 EXPECT_EQ(1u, result->conditions().size()); |
| 217 | 228 |
| 218 // Tell the URLMatcher about our shiny new patterns. | 229 // Tell the URLMatcher about our shiny new patterns. |
| 219 URLMatcherConditionSet::Vector url_matcher_condition_set; | 230 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 220 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 231 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 221 matcher.AddConditionSets(url_matcher_condition_set); | 232 matcher.AddConditionSets(url_matcher_condition_set); |
| 222 | 233 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 245 ASSERT_EQ(0u, url_match_ids.size()); | 256 ASSERT_EQ(0u, url_match_ids.size()); |
| 246 } | 257 } |
| 247 | 258 |
| 248 // Create a condition with two attributes: one on the request header and one on | 259 // Create a condition with two attributes: one on the request header and one on |
| 249 // the response header. The Create() method should fail and complain that it is | 260 // the response header. The Create() method should fail and complain that it is |
| 250 // impossible that both conditions are fulfilled at the same time. | 261 // impossible that both conditions are fulfilled at the same time. |
| 251 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 262 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
| 252 // Necessary for TestURLRequest. | 263 // Necessary for TestURLRequest. |
| 253 MessageLoop message_loop(MessageLoop::TYPE_IO); | 264 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 254 URLMatcher matcher; | 265 URLMatcher matcher; |
| 266 URLMatcher first_party_matcher; | |
| 255 | 267 |
| 256 std::string error; | 268 std::string error; |
| 257 scoped_ptr<WebRequestCondition> result; | 269 scoped_ptr<WebRequestCondition> result; |
| 258 | 270 |
| 259 DictionaryValue condition_value; | 271 DictionaryValue condition_value; |
| 260 condition_value.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 272 condition_value.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 261 | 273 |
| 262 // Create two JS arrays, each with one empty object... | 274 // Create two JS arrays, each with one empty object... |
| 263 scoped_ptr<ListValue> request_header_filters(new ListValue()); | 275 scoped_ptr<ListValue> request_header_filters(new ListValue()); |
| 264 request_header_filters->Append(new DictionaryValue()); | 276 request_header_filters->Append(new DictionaryValue()); |
| 265 scoped_ptr<ListValue> response_header_filters(new ListValue()); | 277 scoped_ptr<ListValue> response_header_filters(new ListValue()); |
| 266 response_header_filters->Append(new DictionaryValue()); | 278 response_header_filters->Append(new DictionaryValue()); |
| 267 | 279 |
| 268 // ...and pass them as the header filters to the request matcher. | 280 // ...and pass them as the header filters to the request matcher. |
| 269 condition_value.Set(keys::kRequestHeadersKey, | 281 condition_value.Set(keys::kRequestHeadersKey, |
| 270 request_header_filters.release()); | 282 request_header_filters.release()); |
| 271 condition_value.Set(keys::kResponseHeadersKey, | 283 condition_value.Set(keys::kResponseHeadersKey, |
| 272 response_header_filters.release()); | 284 response_header_filters.release()); |
| 273 | 285 |
| 274 // Test error on incompatible application stages for involved attributes. | 286 // Test error on incompatible application stages for involved attributes. |
| 275 error.clear(); | 287 error.clear(); |
| 276 result = WebRequestCondition::Create(matcher.condition_factory(), | 288 result = WebRequestCondition::Create(matcher.condition_factory(), |
| 289 first_party_matcher.condition_factory(), | |
| 277 condition_value, &error); | 290 condition_value, &error); |
| 278 EXPECT_FALSE(error.empty()); | 291 EXPECT_FALSE(error.empty()); |
| 279 EXPECT_FALSE(result.get()); | 292 EXPECT_FALSE(result.get()); |
| 280 } | 293 } |
| 281 | 294 |
| 282 } // namespace extensions | 295 } // namespace extensions |
| OLD | NEW |