| 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_attribute.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 namespace { | 24 namespace { |
| 25 const char kUnknownConditionName[] = "unknownType"; | 25 const char kUnknownConditionName[] = "unknownType"; |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 namespace extensions { | 28 namespace extensions { |
| 29 | 29 |
| 30 namespace keys = declarative_webrequest_constants; | 30 namespace keys = declarative_webrequest_constants; |
| 31 | 31 |
| 32 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { | 32 TEST(WebRequestConditionAttributeTest, CreateConditionAttribute) { |
| 33 // Necessary for TestURLRequest. | 33 // Necessary for TestURLRequest. |
| 34 MessageLoop message_loop(MessageLoop::TYPE_IO); | 34 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 35 | 35 |
| 36 std::string error; | 36 std::string error; |
| 37 scoped_refptr<const WebRequestConditionAttribute> result; | 37 scoped_refptr<const WebRequestConditionAttribute> result; |
| 38 StringValue string_value("main_frame"); | 38 StringValue string_value("main_frame"); |
| 39 ListValue resource_types; | 39 ListValue resource_types; |
| 40 resource_types.Append(Value::CreateStringValue("main_frame")); | 40 resource_types.Append(Value::CreateStringValue("main_frame")); |
| 41 | 41 |
| 42 // Test wrong condition name passed. | 42 // Test wrong condition name passed. |
| 43 error.clear(); | 43 error.clear(); |
| 44 result = WebRequestConditionAttribute::Create( | 44 result = WebRequestConditionAttribute::Create( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 65 keys::kResourceTypeKey, &resource_types, &error); | 65 keys::kResourceTypeKey, &resource_types, &error); |
| 66 EXPECT_EQ("", error); | 66 EXPECT_EQ("", error); |
| 67 ASSERT_TRUE(result.get()); | 67 ASSERT_TRUE(result.get()); |
| 68 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, | 68 EXPECT_EQ(WebRequestConditionAttribute::CONDITION_RESOURCE_TYPE, |
| 69 result->GetType()); | 69 result->GetType()); |
| 70 EXPECT_EQ(std::string(keys::kResourceTypeKey), result->GetName()); | 70 EXPECT_EQ(std::string(keys::kResourceTypeKey), result->GetName()); |
| 71 } | 71 } |
| 72 | 72 |
| 73 TEST(WebRequestConditionAttributeTest, ResourceType) { | 73 TEST(WebRequestConditionAttributeTest, ResourceType) { |
| 74 // Necessary for TestURLRequest. | 74 // Necessary for TestURLRequest. |
| 75 MessageLoop message_loop(MessageLoop::TYPE_IO); | 75 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 76 | 76 |
| 77 std::string error; | 77 std::string error; |
| 78 ListValue resource_types; | 78 ListValue resource_types; |
| 79 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding | 79 // The 'sub_frame' value is chosen arbitrarily, so as the corresponding |
| 80 // ResourceType::Type is not 0, the default value. | 80 // ResourceType::Type is not 0, the default value. |
| 81 resource_types.Append(Value::CreateStringValue("sub_frame")); | 81 resource_types.Append(Value::CreateStringValue("sub_frame")); |
| 82 | 82 |
| 83 scoped_refptr<const WebRequestConditionAttribute> attribute = | 83 scoped_refptr<const WebRequestConditionAttribute> attribute = |
| 84 WebRequestConditionAttribute::Create( | 84 WebRequestConditionAttribute::Create( |
| 85 keys::kResourceTypeKey, &resource_types, &error); | 85 keys::kResourceTypeKey, &resource_types, &error); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 98 net::TestURLRequest url_request_fail( | 98 net::TestURLRequest url_request_fail( |
| 99 GURL("http://www.example.com"), NULL, &context, NULL); | 99 GURL("http://www.example.com"), NULL, &context, NULL); |
| 100 content::ResourceRequestInfo::AllocateForTesting(&url_request_fail, | 100 content::ResourceRequestInfo::AllocateForTesting(&url_request_fail, |
| 101 ResourceType::MAIN_FRAME, NULL, -1, -1); | 101 ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 102 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, | 102 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(&url_request_fail, |
| 103 ON_BEFORE_REQUEST))); | 103 ON_BEFORE_REQUEST))); |
| 104 } | 104 } |
| 105 | 105 |
| 106 TEST(WebRequestConditionAttributeTest, ContentType) { | 106 TEST(WebRequestConditionAttributeTest, ContentType) { |
| 107 // Necessary for TestURLRequest. | 107 // Necessary for TestURLRequest. |
| 108 MessageLoop message_loop(MessageLoop::TYPE_IO); | 108 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 109 | 109 |
| 110 std::string error; | 110 std::string error; |
| 111 scoped_refptr<const WebRequestConditionAttribute> result; | 111 scoped_refptr<const WebRequestConditionAttribute> result; |
| 112 | 112 |
| 113 net::SpawnedTestServer test_server( | 113 net::SpawnedTestServer test_server( |
| 114 net::SpawnedTestServer::TYPE_HTTP, | 114 net::SpawnedTestServer::TYPE_HTTP, |
| 115 net::SpawnedTestServer::kLocalhost, | 115 net::SpawnedTestServer::kLocalhost, |
| 116 base::FilePath(FILE_PATH_LITERAL( | 116 base::FilePath(FILE_PATH_LITERAL( |
| 117 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 117 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
| 118 ASSERT_TRUE(test_server.Start()); | 118 ASSERT_TRUE(test_server.Start()); |
| 119 | 119 |
| 120 net::TestURLRequestContext context; | 120 net::TestURLRequestContext context; |
| 121 net::TestDelegate delegate; | 121 net::TestDelegate delegate; |
| 122 net::TestURLRequest url_request( | 122 net::TestURLRequest url_request( |
| 123 test_server.GetURL("files/headers.html"), &delegate, &context, NULL); | 123 test_server.GetURL("files/headers.html"), &delegate, &context, NULL); |
| 124 url_request.Start(); | 124 url_request.Start(); |
| 125 MessageLoop::current()->Run(); | 125 base::MessageLoop::current()->Run(); |
| 126 | 126 |
| 127 ListValue content_types; | 127 ListValue content_types; |
| 128 content_types.Append(Value::CreateStringValue("text/plain")); | 128 content_types.Append(Value::CreateStringValue("text/plain")); |
| 129 scoped_refptr<const WebRequestConditionAttribute> attribute_include = | 129 scoped_refptr<const WebRequestConditionAttribute> attribute_include = |
| 130 WebRequestConditionAttribute::Create( | 130 WebRequestConditionAttribute::Create( |
| 131 keys::kContentTypeKey, &content_types, &error); | 131 keys::kContentTypeKey, &content_types, &error); |
| 132 EXPECT_EQ("", error); | 132 EXPECT_EQ("", error); |
| 133 ASSERT_TRUE(attribute_include.get()); | 133 ASSERT_TRUE(attribute_include.get()); |
| 134 EXPECT_FALSE(attribute_include->IsFulfilled( | 134 EXPECT_FALSE(attribute_include->IsFulfilled( |
| 135 WebRequestData(&url_request, ON_BEFORE_REQUEST, | 135 WebRequestData(&url_request, ON_BEFORE_REQUEST, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( | 167 EXPECT_TRUE(attribute_unexcluded->IsFulfilled( |
| 168 WebRequestData(&url_request, ON_HEADERS_RECEIVED, | 168 WebRequestData(&url_request, ON_HEADERS_RECEIVED, |
| 169 url_request.response_headers()))); | 169 url_request.response_headers()))); |
| 170 EXPECT_EQ(std::string(keys::kExcludeContentTypeKey), | 170 EXPECT_EQ(std::string(keys::kExcludeContentTypeKey), |
| 171 attribute_unexcluded->GetName()); | 171 attribute_unexcluded->GetName()); |
| 172 } | 172 } |
| 173 | 173 |
| 174 // Testing WebRequestConditionAttributeThirdParty. | 174 // Testing WebRequestConditionAttributeThirdParty. |
| 175 TEST(WebRequestConditionAttributeTest, ThirdParty) { | 175 TEST(WebRequestConditionAttributeTest, ThirdParty) { |
| 176 // Necessary for TestURLRequest. | 176 // Necessary for TestURLRequest. |
| 177 MessageLoop message_loop(MessageLoop::TYPE_IO); | 177 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 178 | 178 |
| 179 std::string error; | 179 std::string error; |
| 180 const FundamentalValue value_true(true); | 180 const FundamentalValue value_true(true); |
| 181 // This attribute matches only third party requests. | 181 // This attribute matches only third party requests. |
| 182 scoped_refptr<const WebRequestConditionAttribute> third_party_attribute = | 182 scoped_refptr<const WebRequestConditionAttribute> third_party_attribute = |
| 183 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, | 183 WebRequestConditionAttribute::Create(keys::kThirdPartyKey, |
| 184 &value_true, | 184 &value_true, |
| 185 &error); | 185 &error); |
| 186 ASSERT_EQ("", error); | 186 ASSERT_EQ("", error); |
| 187 ASSERT_TRUE(third_party_attribute.get()); | 187 ASSERT_TRUE(third_party_attribute.get()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 stage))); | 228 stage))); |
| 229 } | 229 } |
| 230 } | 230 } |
| 231 | 231 |
| 232 // Testing WebRequestConditionAttributeStages. This iterates over all stages, | 232 // Testing WebRequestConditionAttributeStages. This iterates over all stages, |
| 233 // and tests a couple of "stage" attributes -- one created with an empty set of | 233 // and tests a couple of "stage" attributes -- one created with an empty set of |
| 234 // applicable stages, one for each stage applicable for that stage, and one | 234 // applicable stages, one for each stage applicable for that stage, and one |
| 235 // applicable in all stages. | 235 // applicable in all stages. |
| 236 TEST(WebRequestConditionAttributeTest, Stages) { | 236 TEST(WebRequestConditionAttributeTest, Stages) { |
| 237 // Necessary for TestURLRequest. | 237 // Necessary for TestURLRequest. |
| 238 MessageLoop message_loop(MessageLoop::TYPE_IO); | 238 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 239 | 239 |
| 240 typedef std::pair<RequestStage, const char*> StageNamePair; | 240 typedef std::pair<RequestStage, const char*> StageNamePair; |
| 241 static const StageNamePair active_stages[] = { | 241 static const StageNamePair active_stages[] = { |
| 242 StageNamePair(ON_BEFORE_REQUEST, keys::kOnBeforeRequestEnum), | 242 StageNamePair(ON_BEFORE_REQUEST, keys::kOnBeforeRequestEnum), |
| 243 StageNamePair(ON_BEFORE_SEND_HEADERS, keys::kOnBeforeSendHeadersEnum), | 243 StageNamePair(ON_BEFORE_SEND_HEADERS, keys::kOnBeforeSendHeadersEnum), |
| 244 StageNamePair(ON_HEADERS_RECEIVED, keys::kOnHeadersReceivedEnum), | 244 StageNamePair(ON_HEADERS_RECEIVED, keys::kOnHeadersReceivedEnum), |
| 245 StageNamePair(ON_AUTH_REQUIRED, keys::kOnAuthRequiredEnum) | 245 StageNamePair(ON_AUTH_REQUIRED, keys::kOnAuthRequiredEnum) |
| 246 }; | 246 }; |
| 247 | 247 |
| 248 // Check that exactly all active stages are considered in this test. | 248 // Check that exactly all active stages are considered in this test. |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 } | 404 } |
| 405 | 405 |
| 406 } // namespace | 406 } // namespace |
| 407 | 407 |
| 408 // Here we test WebRequestConditionAttributeRequestHeaders for matching | 408 // Here we test WebRequestConditionAttributeRequestHeaders for matching |
| 409 // correctly against request headers. This test is not as extensive as | 409 // correctly against request headers. This test is not as extensive as |
| 410 // "ResponseHeaders" (below), because the header-matching code is shared | 410 // "ResponseHeaders" (below), because the header-matching code is shared |
| 411 // by both types of condition attributes, so it is enough to test it once. | 411 // by both types of condition attributes, so it is enough to test it once. |
| 412 TEST(WebRequestConditionAttributeTest, RequestHeaders) { | 412 TEST(WebRequestConditionAttributeTest, RequestHeaders) { |
| 413 // Necessary for TestURLRequest. | 413 // Necessary for TestURLRequest. |
| 414 MessageLoop message_loop(MessageLoop::TYPE_IO); | 414 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 415 | 415 |
| 416 net::TestURLRequestContext context; | 416 net::TestURLRequestContext context; |
| 417 net::TestDelegate delegate; | 417 net::TestDelegate delegate; |
| 418 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. | 418 net::TestURLRequest url_request(GURL("http://example.com"), // Dummy URL. |
| 419 &delegate, &context, NULL); | 419 &delegate, &context, NULL); |
| 420 url_request.SetExtraRequestHeaderByName( | 420 url_request.SetExtraRequestHeaderByName( |
| 421 "Custom-header", "custom/value", true /* overwrite */); | 421 "Custom-header", "custom/value", true /* overwrite */); |
| 422 url_request.Start(); | 422 url_request.Start(); |
| 423 MessageLoop::current()->Run(); | 423 base::MessageLoop::current()->Run(); |
| 424 | 424 |
| 425 std::vector<std::vector<const std::string*> > tests; | 425 std::vector<std::vector<const std::string*> > tests; |
| 426 bool result = false; | 426 bool result = false; |
| 427 | 427 |
| 428 const RequestStage stage = ON_BEFORE_SEND_HEADERS; | 428 const RequestStage stage = ON_BEFORE_SEND_HEADERS; |
| 429 | 429 |
| 430 // First set of test data -- passing conjunction. | 430 // First set of test data -- passing conjunction. |
| 431 const std::string kPassingCondition[] = { | 431 const std::string kPassingCondition[] = { |
| 432 keys::kNameContainsKey, "CuStOm", // Header names are case insensitive. | 432 keys::kNameContainsKey, "CuStOm", // Header names are case insensitive. |
| 433 keys::kNameEqualsKey, "custom-header", | 433 keys::kNameEqualsKey, "custom-header", |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result); | 482 tests, keys::kExcludeRequestHeadersKey, stage, &url_request, &result); |
| 483 EXPECT_FALSE(result); | 483 EXPECT_FALSE(result); |
| 484 } | 484 } |
| 485 | 485 |
| 486 // Here we test WebRequestConditionAttributeResponseHeaders for: | 486 // Here we test WebRequestConditionAttributeResponseHeaders for: |
| 487 // 1. Correct implementation of prefix/suffix/contains/equals matching. | 487 // 1. Correct implementation of prefix/suffix/contains/equals matching. |
| 488 // 2. Performing logical disjunction (||) between multiple specifications. | 488 // 2. Performing logical disjunction (||) between multiple specifications. |
| 489 // 3. Negating the match in case of 'doesNotContainHeaders'. | 489 // 3. Negating the match in case of 'doesNotContainHeaders'. |
| 490 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { | 490 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { |
| 491 // Necessary for TestURLRequest. | 491 // Necessary for TestURLRequest. |
| 492 MessageLoop message_loop(MessageLoop::TYPE_IO); | 492 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 493 | 493 |
| 494 net::SpawnedTestServer test_server( | 494 net::SpawnedTestServer test_server( |
| 495 net::SpawnedTestServer::TYPE_HTTP, | 495 net::SpawnedTestServer::TYPE_HTTP, |
| 496 net::SpawnedTestServer::kLocalhost, | 496 net::SpawnedTestServer::kLocalhost, |
| 497 base::FilePath(FILE_PATH_LITERAL( | 497 base::FilePath(FILE_PATH_LITERAL( |
| 498 "chrome/test/data/extensions/api_test/webrequest/declarative"))); | 498 "chrome/test/data/extensions/api_test/webrequest/declarative"))); |
| 499 ASSERT_TRUE(test_server.Start()); | 499 ASSERT_TRUE(test_server.Start()); |
| 500 | 500 |
| 501 net::TestURLRequestContext context; | 501 net::TestURLRequestContext context; |
| 502 net::TestDelegate delegate; | 502 net::TestDelegate delegate; |
| 503 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), | 503 net::TestURLRequest url_request(test_server.GetURL("files/headers.html"), |
| 504 &delegate, &context, NULL); | 504 &delegate, &context, NULL); |
| 505 url_request.Start(); | 505 url_request.Start(); |
| 506 MessageLoop::current()->Run(); | 506 base::MessageLoop::current()->Run(); |
| 507 | 507 |
| 508 // In all the tests below we assume that the server includes the headers | 508 // In all the tests below we assume that the server includes the headers |
| 509 // Custom-Header: custom/value | 509 // Custom-Header: custom/value |
| 510 // Custom-Header-B: valueA | 510 // Custom-Header-B: valueA |
| 511 // Custom-Header-B: valueB | 511 // Custom-Header-B: valueB |
| 512 // Custom-Header-C: valueC, valueD | 512 // Custom-Header-C: valueC, valueD |
| 513 // Custom-Header-D: | 513 // Custom-Header-D: |
| 514 // in the response, but does not include "Non-existing: void". | 514 // in the response, but does not include "Non-existing: void". |
| 515 | 515 |
| 516 std::vector< std::vector<const std::string*> > tests; | 516 std::vector< std::vector<const std::string*> > tests; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 keys::kValueEqualsKey, "valueB" | 663 keys::kValueEqualsKey, "valueB" |
| 664 }; | 664 }; |
| 665 const size_t kExistingSize[] = { arraysize(kExisting) }; | 665 const size_t kExistingSize[] = { arraysize(kExisting) }; |
| 666 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 666 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
| 667 MatchAndCheck( | 667 MatchAndCheck( |
| 668 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); | 668 tests, keys::kExcludeResponseHeadersKey, stage, &url_request, &result); |
| 669 EXPECT_FALSE(result); | 669 EXPECT_FALSE(result); |
| 670 } | 670 } |
| 671 | 671 |
| 672 } // namespace extensions | 672 } // namespace extensions |
| OLD | NEW |