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/test/values_test_util.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" | 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" |
13 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 14 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
14 #include "content/public/browser/resource_request_info.h" | 15 #include "content/public/browser/resource_request_info.h" |
15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace extensions { | 19 namespace extensions { |
19 | 20 |
20 namespace keys = declarative_webrequest_constants; | 21 namespace keys = declarative_webrequest_constants; |
21 namespace keys2 = url_matcher_constants; | 22 namespace keys2 = url_matcher_constants; |
22 | 23 |
23 TEST(WebRequestConditionTest, CreateCondition) { | 24 TEST(WebRequestConditionTest, CreateCondition) { |
24 // Necessary for TestURLRequest. | 25 // Necessary for TestURLRequest. |
25 MessageLoop message_loop(MessageLoop::TYPE_IO); | 26 MessageLoop message_loop(MessageLoop::TYPE_IO); |
26 URLMatcher matcher; | 27 URLMatcher matcher; |
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 invalid_condition.SetString("invalid", "foobar"); | |
33 invalid_condition.SetString(keys::kInstanceTypeKey, | |
34 keys::kRequestMatcherType); | |
35 | |
36 DictionaryValue invalid_condition2; | |
37 invalid_condition2.Set(keys::kUrlKey, new ListValue); | |
38 invalid_condition2.SetString(keys::kInstanceTypeKey, | |
39 keys::kRequestMatcherType); | |
40 | |
41 ListValue* resource_type_list = new ListValue(); | |
42 resource_type_list->Append(Value::CreateStringValue("main_frame")); | |
43 DictionaryValue* url_filter = new DictionaryValue(); | |
44 url_filter->SetString(keys2::kHostSuffixKey, "example.com"); | |
45 DictionaryValue valid_condition; | |
46 valid_condition.Set(keys::kResourceTypeKey, resource_type_list); | |
47 valid_condition.Set(keys::kUrlKey, url_filter); | |
48 valid_condition.SetString(keys::kInstanceTypeKey, | |
49 keys::kRequestMatcherType); | |
50 | 32 |
51 // Test wrong condition name passed. | 33 // Test wrong condition name passed. |
52 error.clear(); | 34 error.clear(); |
53 result = WebRequestCondition::Create(matcher.condition_factory(), | 35 result = WebRequestCondition::Create( |
54 invalid_condition, &error); | 36 matcher.condition_factory(), |
| 37 *base::test::ParseJson( |
| 38 "{ \"invalid\": \"foobar\", \n" |
| 39 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 40 "}"), |
| 41 &error); |
55 EXPECT_FALSE(error.empty()); | 42 EXPECT_FALSE(error.empty()); |
56 EXPECT_FALSE(result.get()); | 43 EXPECT_FALSE(result.get()); |
57 | 44 |
58 // Test wrong datatype in host_suffix. | 45 // Test wrong datatype in host_suffix. |
59 error.clear(); | 46 error.clear(); |
60 result = WebRequestCondition::Create(matcher.condition_factory(), | 47 result = WebRequestCondition::Create( |
61 invalid_condition2, &error); | 48 matcher.condition_factory(), |
| 49 *base::test::ParseJson( |
| 50 "{ \n" |
| 51 " \"url\": [], \n" |
| 52 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 53 "}"), |
| 54 &error); |
62 EXPECT_FALSE(error.empty()); | 55 EXPECT_FALSE(error.empty()); |
63 EXPECT_FALSE(result.get()); | 56 EXPECT_FALSE(result.get()); |
64 | 57 |
65 // Test success (can we support multiple criteria?) | 58 // Test success (can we support multiple criteria?) |
66 error.clear(); | 59 error.clear(); |
67 result = WebRequestCondition::Create(matcher.condition_factory(), | 60 result = WebRequestCondition::Create( |
68 valid_condition, &error); | 61 matcher.condition_factory(), |
| 62 *base::test::ParseJson( |
| 63 "{ \n" |
| 64 " \"resourceType\": [\"main_frame\"], \n" |
| 65 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" |
| 66 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 67 "}"), |
| 68 &error); |
69 EXPECT_EQ("", error); | 69 EXPECT_EQ("", error); |
70 ASSERT_TRUE(result.get()); | 70 ASSERT_TRUE(result.get()); |
71 | 71 |
72 net::TestURLRequestContext context; | 72 net::TestURLRequestContext context; |
73 net::TestURLRequest match_request( | 73 net::TestURLRequest match_request( |
74 GURL("http://www.example.com"), NULL, &context); | 74 GURL("http://www.example.com"), NULL, &context); |
75 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 75 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
76 ResourceType::MAIN_FRAME, NULL, -1, -1); | 76 ResourceType::MAIN_FRAME, NULL, -1, -1); |
77 EXPECT_TRUE(result->IsFulfilled( | 77 EXPECT_TRUE(result->IsFulfilled( |
78 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); | 78 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); |
79 | 79 |
80 net::TestURLRequest wrong_resource_type( | 80 net::TestURLRequest wrong_resource_type( |
81 GURL("https://www.example.com"), NULL, &context); | 81 GURL("https://www.example.com"), NULL, &context); |
82 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 82 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
83 ResourceType::SUB_FRAME, NULL, -1, -1); | 83 ResourceType::SUB_FRAME, NULL, -1, -1); |
84 EXPECT_FALSE(result->IsFulfilled( | 84 EXPECT_FALSE(result->IsFulfilled( |
85 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | 85 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); |
86 } | 86 } |
87 | 87 |
88 TEST(WebRequestConditionTest, CreateConditionSet) { | 88 TEST(WebRequestConditionTest, CreateConditionSet) { |
89 // Necessary for TestURLRequest. | 89 // Necessary for TestURLRequest. |
90 MessageLoop message_loop(MessageLoop::TYPE_IO); | 90 MessageLoop message_loop(MessageLoop::TYPE_IO); |
91 URLMatcher matcher; | 91 URLMatcher matcher; |
92 | 92 |
93 ListValue* http_scheme_list = new ListValue(); | |
94 http_scheme_list->Append(Value::CreateStringValue("http")); | |
95 DictionaryValue* http_url_filter = new DictionaryValue(); | |
96 http_url_filter->SetString(keys2::kHostSuffixKey, "example.com"); | |
97 http_url_filter->Set(keys2::kSchemesKey, http_scheme_list); | |
98 DictionaryValue http_condition; | |
99 http_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | |
100 http_condition.Set(keys::kUrlKey, http_url_filter); | |
101 | |
102 ListValue* https_scheme_list = new ListValue(); | |
103 https_scheme_list->Append(Value::CreateStringValue("https")); | |
104 DictionaryValue* https_url_filter = new DictionaryValue(); | |
105 https_url_filter->SetString(keys2::kHostSuffixKey, "example.com"); | |
106 https_url_filter->SetString(keys2::kHostPrefixKey, "www"); | |
107 https_url_filter->Set(keys2::kSchemesKey, https_scheme_list); | |
108 DictionaryValue https_condition; | |
109 https_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | |
110 https_condition.Set(keys::kUrlKey, https_url_filter); | |
111 | |
112 WebRequestConditionSet::AnyVector conditions; | 93 WebRequestConditionSet::AnyVector conditions; |
113 | 94 |
114 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 95 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
115 new json_schema_compiler::any::Any); | 96 new json_schema_compiler::any::Any); |
116 condition1->Init(http_condition); | 97 condition1->Init(*base::test::ParseJson( |
| 98 "{ \n" |
| 99 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 100 " \"url\": { \n" |
| 101 " \"hostSuffix\": \"example.com\", \n" |
| 102 " \"schemes\": [\"http\"], \n" |
| 103 " }, \n" |
| 104 "}")); |
117 conditions.push_back(condition1); | 105 conditions.push_back(condition1); |
118 | 106 |
119 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( | 107 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( |
120 new json_schema_compiler::any::Any); | 108 new json_schema_compiler::any::Any); |
121 condition2->Init(https_condition); | 109 condition2->Init(*base::test::ParseJson( |
| 110 "{ \n" |
| 111 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 112 " \"url\": { \n" |
| 113 " \"hostSuffix\": \"example.com\", \n" |
| 114 " \"hostPrefix\": \"www\", \n" |
| 115 " \"schemes\": [\"https\"], \n" |
| 116 " }, \n" |
| 117 "}")); |
122 conditions.push_back(condition2); | 118 conditions.push_back(condition2); |
123 | 119 |
124 // Test insertion | 120 // Test insertion |
125 std::string error; | 121 std::string error; |
126 scoped_ptr<WebRequestConditionSet> result = | 122 scoped_ptr<WebRequestConditionSet> result = |
127 WebRequestConditionSet::Create(matcher.condition_factory(), | 123 WebRequestConditionSet::Create(matcher.condition_factory(), |
128 conditions, &error); | 124 conditions, &error); |
129 EXPECT_EQ("", error); | 125 EXPECT_EQ("", error); |
130 ASSERT_TRUE(result.get()); | 126 ASSERT_TRUE(result.get()); |
131 EXPECT_EQ(2u, result->conditions().size()); | 127 EXPECT_EQ(2u, result->conditions().size()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 ++number_matches; | 173 ++number_matches; |
178 } | 174 } |
179 EXPECT_EQ(0, number_matches); | 175 EXPECT_EQ(0, number_matches); |
180 } | 176 } |
181 | 177 |
182 TEST(WebRequestConditionTest, TestPortFilter) { | 178 TEST(WebRequestConditionTest, TestPortFilter) { |
183 // Necessary for TestURLRequest. | 179 // Necessary for TestURLRequest. |
184 MessageLoop message_loop(MessageLoop::TYPE_IO); | 180 MessageLoop message_loop(MessageLoop::TYPE_IO); |
185 URLMatcher matcher; | 181 URLMatcher matcher; |
186 | 182 |
187 // Allow 80;1000-1010. | |
188 ListValue* port_range = new ListValue(); | |
189 port_range->Append(Value::CreateIntegerValue(1000)); | |
190 port_range->Append(Value::CreateIntegerValue(1010)); | |
191 ListValue* port_ranges = new ListValue(); | |
192 port_ranges->Append(Value::CreateIntegerValue(80)); | |
193 port_ranges->Append(port_range); | |
194 | |
195 DictionaryValue* url_filter = new DictionaryValue(); | |
196 url_filter->Set(keys2::kPortsKey, port_ranges); | |
197 url_filter->SetString(keys2::kHostSuffixKey, "example.com"); | |
198 | |
199 DictionaryValue condition; | |
200 condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | |
201 condition.Set(keys::kUrlKey, url_filter); | |
202 | |
203 linked_ptr<json_schema_compiler::any::Any> any_condition = | 183 linked_ptr<json_schema_compiler::any::Any> any_condition = |
204 make_linked_ptr(new json_schema_compiler::any::Any); | 184 make_linked_ptr(new json_schema_compiler::any::Any); |
205 any_condition->Init(condition); | 185 any_condition->Init(*base::test::ParseJson( |
| 186 "{ \n" |
| 187 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 188 " \"url\": { \n" |
| 189 " \"ports\": [80, [1000, 1010]], \n" // Allow 80;1000-1010. |
| 190 " \"hostSuffix\": \"example.com\", \n" |
| 191 " }, \n" |
| 192 "}")); |
206 WebRequestConditionSet::AnyVector conditions; | 193 WebRequestConditionSet::AnyVector conditions; |
207 conditions.push_back(any_condition); | 194 conditions.push_back(any_condition); |
208 | 195 |
209 // Test insertion | 196 // Test insertion |
210 std::string error; | 197 std::string error; |
211 scoped_ptr<WebRequestConditionSet> result = | 198 scoped_ptr<WebRequestConditionSet> result = |
212 WebRequestConditionSet::Create(matcher.condition_factory(), | 199 WebRequestConditionSet::Create(matcher.condition_factory(), |
213 conditions, &error); | 200 conditions, &error); |
214 EXPECT_EQ("", error); | 201 EXPECT_EQ("", error); |
215 ASSERT_TRUE(result.get()); | 202 ASSERT_TRUE(result.get()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 // Necessary for TestURLRequest. | 239 // Necessary for TestURLRequest. |
253 MessageLoop message_loop(MessageLoop::TYPE_IO); | 240 MessageLoop message_loop(MessageLoop::TYPE_IO); |
254 URLMatcher matcher; | 241 URLMatcher matcher; |
255 | 242 |
256 std::string error; | 243 std::string error; |
257 scoped_ptr<WebRequestCondition> result; | 244 scoped_ptr<WebRequestCondition> result; |
258 | 245 |
259 DictionaryValue condition_value; | 246 DictionaryValue condition_value; |
260 condition_value.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 247 condition_value.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
261 | 248 |
262 // Create two JS arrays, each with one empty object... | |
263 scoped_ptr<ListValue> request_header_filters(new ListValue()); | |
264 request_header_filters->Append(new DictionaryValue()); | |
265 scoped_ptr<ListValue> response_header_filters(new ListValue()); | |
266 response_header_filters->Append(new DictionaryValue()); | |
267 | |
268 // ...and pass them as the header filters to the request matcher. | |
269 condition_value.Set(keys::kRequestHeadersKey, | |
270 request_header_filters.release()); | |
271 condition_value.Set(keys::kResponseHeadersKey, | |
272 response_header_filters.release()); | |
273 | 249 |
274 // Test error on incompatible application stages for involved attributes. | 250 // Test error on incompatible application stages for involved attributes. |
275 error.clear(); | 251 error.clear(); |
276 result = WebRequestCondition::Create(matcher.condition_factory(), | 252 result = WebRequestCondition::Create( |
277 condition_value, &error); | 253 matcher.condition_factory(), |
| 254 *base::test::ParseJson( |
| 255 "{ \n" |
| 256 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 257 // Pass a JS array with one empty object to each of the header |
| 258 // filters. |
| 259 " \"requestHeaders\": [{}], \n" |
| 260 " \"responseHeaders\": [{}], \n" |
| 261 "}"), |
| 262 &error); |
278 EXPECT_FALSE(error.empty()); | 263 EXPECT_FALSE(error.empty()); |
279 EXPECT_FALSE(result.get()); | 264 EXPECT_FALSE(result.get()); |
280 } | 265 } |
281 | 266 |
282 } // namespace extensions | 267 } // namespace extensions |
OLD | NEW |