| 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/test/values_test_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" | |
| 14 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 13 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
| 15 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 namespace extensions { | 18 namespace extensions { |
| 20 | 19 |
| 21 namespace keys = declarative_webrequest_constants; | 20 namespace keys = declarative_webrequest_constants; |
| 22 namespace keys2 = url_matcher_constants; | 21 namespace keys2 = url_matcher_constants; |
| 23 | 22 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 "{ \n" | 62 "{ \n" |
| 64 " \"resourceType\": [\"main_frame\"], \n" | 63 " \"resourceType\": [\"main_frame\"], \n" |
| 65 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" | 64 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" |
| 66 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 65 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 67 "}"), | 66 "}"), |
| 68 &error); | 67 &error); |
| 69 EXPECT_EQ("", error); | 68 EXPECT_EQ("", error); |
| 70 ASSERT_TRUE(result.get()); | 69 ASSERT_TRUE(result.get()); |
| 71 | 70 |
| 72 URLMatcherConditionSet::Vector url_matcher_condition_set; | 71 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 73 url_matcher_condition_set.push_back(result->url_matcher_condition_set()); | 72 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 74 matcher.AddConditionSets(url_matcher_condition_set); | 73 matcher.AddConditionSets(url_matcher_condition_set); |
| 75 std::set<URLMatcherConditionSet::ID> url_match_ids; | 74 std::set<URLMatcherConditionSet::ID> url_match_ids; |
| 76 | 75 |
| 77 net::TestURLRequestContext context; | 76 net::TestURLRequestContext context; |
| 78 GURL http_url("http://www.example.com"); | 77 GURL http_url("http://www.example.com"); |
| 79 net::TestURLRequest match_request(http_url, NULL, &context); | 78 net::TestURLRequest match_request(http_url, NULL, &context); |
| 80 url_match_ids = matcher.MatchURL(http_url); | 79 url_match_ids = matcher.MatchURL(http_url); |
| 81 EXPECT_EQ(1u, url_match_ids.size()); | 80 EXPECT_EQ(1u, url_match_ids.size()); |
| 82 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 81 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
| 83 ResourceType::MAIN_FRAME, NULL, -1, -1); | 82 ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 84 EXPECT_TRUE(result->IsFulfilled( | 83 EXPECT_TRUE(result->IsFulfilled( |
| 85 url_match_ids, | 84 url_match_ids, |
| 86 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); | 85 DeclarativeWebRequestData(&match_request, ON_BEFORE_REQUEST))); |
| 87 | 86 |
| 88 GURL https_url("https://www.example.com"); | 87 GURL https_url("https://www.example.com"); |
| 89 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); | 88 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); |
| 90 url_match_ids = matcher.MatchURL(https_url); | 89 url_match_ids = matcher.MatchURL(https_url); |
| 91 // Make sure IsFulfilled does not fail because of URL matching. | 90 // Make sure IsFulfilled does not fail because of URL matching. |
| 92 EXPECT_EQ(1u, url_match_ids.size()); | 91 EXPECT_EQ(1u, url_match_ids.size()); |
| 93 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 92 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
| 94 ResourceType::SUB_FRAME, NULL, -1, -1); | 93 ResourceType::SUB_FRAME, NULL, -1, -1); |
| 95 EXPECT_FALSE(result->IsFulfilled( | 94 EXPECT_FALSE(result->IsFulfilled( |
| 96 url_match_ids, | 95 url_match_ids, |
| 97 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | 96 DeclarativeWebRequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); |
| 98 } | 97 } |
| 99 | 98 |
| 100 // Conditions without UrlFilter attributes need to be independent of URL | 99 // Conditions without UrlFilter attributes need to be independent of URL |
| 101 // matching results. We test here that: | 100 // matching results. We test here that: |
| 102 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 101 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 103 // attributes are fulfilled. | 102 // attributes are fulfilled. |
| 104 // 2. An empty condition (in particular, without UrlFilter attributes) is | 103 // 2. An empty condition (in particular, without UrlFilter attributes) is |
| 105 // always fulfilled. | 104 // always fulfilled. |
| 106 TEST(WebRequestConditionTest, NoUrlAttributes) { | 105 TEST(WebRequestConditionTest, NoUrlAttributes) { |
| 107 // Necessary for TestURLRequest. | 106 // Necessary for TestURLRequest. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 152 |
| 154 net::TestURLRequestContext context; | 153 net::TestURLRequestContext context; |
| 155 net::TestURLRequest https_request( | 154 net::TestURLRequest https_request( |
| 156 GURL("https://www.example.com"), NULL, &context); | 155 GURL("https://www.example.com"), NULL, &context); |
| 157 std::set<URLMatcherConditionSet::ID> dummy_match_set; | 156 std::set<URLMatcherConditionSet::ID> dummy_match_set; |
| 158 | 157 |
| 159 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 158 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 160 // attributes are fulfilled. | 159 // attributes are fulfilled. |
| 161 EXPECT_FALSE(condition_no_url_false->IsFulfilled( | 160 EXPECT_FALSE(condition_no_url_false->IsFulfilled( |
| 162 dummy_match_set, | 161 dummy_match_set, |
| 163 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | 162 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); |
| 164 | 163 |
| 165 EXPECT_TRUE(condition_no_url_true->IsFulfilled( | 164 EXPECT_TRUE(condition_no_url_true->IsFulfilled( |
| 166 dummy_match_set, | 165 dummy_match_set, |
| 167 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | 166 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); |
| 168 | 167 |
| 169 // 2. An empty condition (in particular, without UrlFilter attributes) is | 168 // 2. An empty condition (in particular, without UrlFilter attributes) is |
| 170 // always fulfilled. | 169 // always fulfilled. |
| 171 EXPECT_TRUE(condition_empty->IsFulfilled( | 170 EXPECT_TRUE(condition_empty->IsFulfilled( |
| 172 dummy_match_set, | 171 dummy_match_set, |
| 173 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | 172 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); |
| 174 } | 173 } |
| 175 | 174 |
| 176 TEST(WebRequestConditionTest, CreateConditionSet) { | 175 TEST(WebRequestConditionTest, CreateConditionSet) { |
| 177 // Necessary for TestURLRequest. | 176 // Necessary for TestURLRequest. |
| 178 MessageLoop message_loop(MessageLoop::TYPE_IO); | 177 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 179 URLMatcher matcher; | 178 URLMatcher matcher; |
| 180 | 179 |
| 181 WebRequestConditionSet::AnyVector conditions; | 180 WebRequestConditionSet::AnyVector conditions; |
| 182 | 181 |
| 183 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 182 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 // Test that the result is correct and matches http://www.example.com and | 223 // Test that the result is correct and matches http://www.example.com and |
| 225 // https://www.example.com | 224 // https://www.example.com |
| 226 GURL http_url("http://www.example.com"); | 225 GURL http_url("http://www.example.com"); |
| 227 net::TestURLRequestContext context; | 226 net::TestURLRequestContext context; |
| 228 net::TestURLRequest http_request(http_url, NULL, &context); | 227 net::TestURLRequest http_request(http_url, NULL, &context); |
| 229 url_match_ids = matcher.MatchURL(http_url); | 228 url_match_ids = matcher.MatchURL(http_url); |
| 230 EXPECT_EQ(1u, url_match_ids.size()); | 229 EXPECT_EQ(1u, url_match_ids.size()); |
| 231 EXPECT_TRUE(result->IsFulfilled( | 230 EXPECT_TRUE(result->IsFulfilled( |
| 232 *(url_match_ids.begin()), | 231 *(url_match_ids.begin()), |
| 233 url_match_ids, | 232 url_match_ids, |
| 234 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST))); | 233 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST))); |
| 235 | 234 |
| 236 GURL https_url("https://www.example.com"); | 235 GURL https_url("https://www.example.com"); |
| 237 url_match_ids = matcher.MatchURL(https_url); | 236 url_match_ids = matcher.MatchURL(https_url); |
| 238 EXPECT_EQ(1u, url_match_ids.size()); | 237 EXPECT_EQ(1u, url_match_ids.size()); |
| 239 net::TestURLRequest https_request(https_url, NULL, &context); | 238 net::TestURLRequest https_request(https_url, NULL, &context); |
| 240 EXPECT_TRUE(result->IsFulfilled( | 239 EXPECT_TRUE(result->IsFulfilled( |
| 241 *(url_match_ids.begin()), | 240 *(url_match_ids.begin()), |
| 242 url_match_ids, | 241 url_match_ids, |
| 243 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | 242 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); |
| 244 | 243 |
| 245 // Check that both, hostPrefix and hostSuffix are evaluated. | 244 // Check that both, hostPrefix and hostSuffix are evaluated. |
| 246 GURL https_foo_url("https://foo.example.com"); | 245 GURL https_foo_url("https://foo.example.com"); |
| 247 url_match_ids = matcher.MatchURL(https_foo_url); | 246 url_match_ids = matcher.MatchURL(https_foo_url); |
| 248 EXPECT_EQ(0u, url_match_ids.size()); | 247 EXPECT_EQ(0u, url_match_ids.size()); |
| 249 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 248 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
| 250 EXPECT_FALSE(result->IsFulfilled( | 249 EXPECT_FALSE(result->IsFulfilled( |
| 251 -1, | 250 -1, |
| 252 url_match_ids, | 251 url_match_ids, |
| 253 WebRequestRule::RequestData(&https_foo_request, ON_BEFORE_REQUEST))); | 252 DeclarativeWebRequestData(&https_foo_request, ON_BEFORE_REQUEST))); |
| 254 } | 253 } |
| 255 | 254 |
| 256 TEST(WebRequestConditionTest, TestPortFilter) { | 255 TEST(WebRequestConditionTest, TestPortFilter) { |
| 257 // Necessary for TestURLRequest. | 256 // Necessary for TestURLRequest. |
| 258 MessageLoop message_loop(MessageLoop::TYPE_IO); | 257 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 259 URLMatcher matcher; | 258 URLMatcher matcher; |
| 260 | 259 |
| 261 linked_ptr<json_schema_compiler::any::Any> any_condition = | 260 linked_ptr<json_schema_compiler::any::Any> any_condition = |
| 262 make_linked_ptr(new json_schema_compiler::any::Any); | 261 make_linked_ptr(new json_schema_compiler::any::Any); |
| 263 any_condition->Init(*base::test::ParseJson( | 262 any_condition->Init(*base::test::ParseJson( |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 // filters. | 335 // filters. |
| 337 " \"requestHeaders\": [{}], \n" | 336 " \"requestHeaders\": [{}], \n" |
| 338 " \"responseHeaders\": [{}], \n" | 337 " \"responseHeaders\": [{}], \n" |
| 339 "}"), | 338 "}"), |
| 340 &error); | 339 &error); |
| 341 EXPECT_FALSE(error.empty()); | 340 EXPECT_FALSE(error.empty()); |
| 342 EXPECT_FALSE(result.get()); | 341 EXPECT_FALSE(result.get()); |
| 343 } | 342 } |
| 344 | 343 |
| 345 } // namespace extensions | 344 } // namespace extensions |
| OLD | NEW |