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 "extensions/browser/api/declarative_webrequest/webrequest_condition_att
ribute.h" | 5 #include "extensions/browser/api/declarative_webrequest/webrequest_condition_att
ribute.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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
92 scoped_refptr<const WebRequestConditionAttribute> attribute = | 92 scoped_refptr<const WebRequestConditionAttribute> attribute = |
93 WebRequestConditionAttribute::Create( | 93 WebRequestConditionAttribute::Create( |
94 keys::kResourceTypeKey, &resource_types, &error); | 94 keys::kResourceTypeKey, &resource_types, &error); |
95 EXPECT_EQ("", error); | 95 EXPECT_EQ("", error); |
96 ASSERT_TRUE(attribute.get()); | 96 ASSERT_TRUE(attribute.get()); |
97 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); | 97 EXPECT_EQ(std::string(keys::kResourceTypeKey), attribute->GetName()); |
98 | 98 |
99 net::TestURLRequestContext context; | 99 net::TestURLRequestContext context; |
100 scoped_ptr<net::URLRequest> url_request_ok(context.CreateRequest( | 100 scoped_ptr<net::URLRequest> url_request_ok(context.CreateRequest( |
101 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL, NULL)); | 101 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); |
102 content::ResourceRequestInfo::AllocateForTesting( | 102 content::ResourceRequestInfo::AllocateForTesting( |
103 url_request_ok.get(), | 103 url_request_ok.get(), |
104 content::RESOURCE_TYPE_SUB_FRAME, | 104 content::RESOURCE_TYPE_SUB_FRAME, |
105 NULL, | 105 nullptr, |
106 -1, | 106 -1, |
107 -1, | 107 -1, |
108 -1, | 108 -1, |
109 false); | 109 false); |
110 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(url_request_ok.get(), | 110 EXPECT_TRUE(attribute->IsFulfilled(WebRequestData(url_request_ok.get(), |
111 ON_BEFORE_REQUEST))); | 111 ON_BEFORE_REQUEST))); |
112 | 112 |
113 scoped_ptr<net::URLRequest> url_request_fail(context.CreateRequest( | 113 scoped_ptr<net::URLRequest> url_request_fail(context.CreateRequest( |
114 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, NULL, NULL)); | 114 GURL("http://www.example.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); |
115 content::ResourceRequestInfo::AllocateForTesting( | 115 content::ResourceRequestInfo::AllocateForTesting( |
116 url_request_fail.get(), | 116 url_request_fail.get(), |
117 content::RESOURCE_TYPE_MAIN_FRAME, | 117 content::RESOURCE_TYPE_MAIN_FRAME, |
118 NULL, | 118 nullptr, |
119 -1, | 119 -1, |
120 -1, | 120 -1, |
121 -1, | 121 -1, |
122 false); | 122 false); |
123 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(url_request_fail.get(), | 123 EXPECT_FALSE(attribute->IsFulfilled(WebRequestData(url_request_fail.get(), |
124 ON_BEFORE_REQUEST))); | 124 ON_BEFORE_REQUEST))); |
125 } | 125 } |
126 | 126 |
127 TEST(WebRequestConditionAttributeTest, ContentType) { | 127 TEST(WebRequestConditionAttributeTest, ContentType) { |
128 // Necessary for TestURLRequest. | 128 // Necessary for TestURLRequest. |
129 base::MessageLoopForIO message_loop; | 129 base::MessageLoopForIO message_loop; |
130 | 130 |
131 std::string error; | 131 std::string error; |
132 scoped_refptr<const WebRequestConditionAttribute> result; | 132 scoped_refptr<const WebRequestConditionAttribute> result; |
133 | 133 |
134 net::test_server::EmbeddedTestServer test_server; | 134 net::test_server::EmbeddedTestServer test_server; |
135 test_server.ServeFilesFromDirectory(TestDataPath( | 135 test_server.ServeFilesFromDirectory(TestDataPath( |
136 "chrome/test/data/extensions/api_test/webrequest/declarative")); | 136 "chrome/test/data/extensions/api_test/webrequest/declarative")); |
137 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); | 137 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); |
138 | 138 |
139 net::TestURLRequestContext context; | 139 net::TestURLRequestContext context; |
140 net::TestDelegate delegate; | 140 net::TestDelegate delegate; |
141 scoped_ptr<net::URLRequest> url_request( | 141 scoped_ptr<net::URLRequest> url_request( |
142 context.CreateRequest(test_server.GetURL("/headers.html"), | 142 context.CreateRequest(test_server.GetURL("/headers.html"), |
143 net::DEFAULT_PRIORITY, | 143 net::DEFAULT_PRIORITY, |
144 &delegate, | 144 &delegate, |
145 NULL)); | 145 nullptr)); |
146 url_request->Start(); | 146 url_request->Start(); |
147 base::MessageLoop::current()->Run(); | 147 base::MessageLoop::current()->Run(); |
148 | 148 |
149 base::ListValue content_types; | 149 base::ListValue content_types; |
150 content_types.Append(new base::StringValue("text/plain")); | 150 content_types.Append(new base::StringValue("text/plain")); |
151 scoped_refptr<const WebRequestConditionAttribute> attribute_include = | 151 scoped_refptr<const WebRequestConditionAttribute> attribute_include = |
152 WebRequestConditionAttribute::Create( | 152 WebRequestConditionAttribute::Create( |
153 keys::kContentTypeKey, &content_types, &error); | 153 keys::kContentTypeKey, &content_types, &error); |
154 EXPECT_EQ("", error); | 154 EXPECT_EQ("", error); |
155 ASSERT_TRUE(attribute_include.get()); | 155 ASSERT_TRUE(attribute_include.get()); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 ASSERT_TRUE(first_party_attribute.get()); | 219 ASSERT_TRUE(first_party_attribute.get()); |
220 EXPECT_EQ(std::string(keys::kThirdPartyKey), | 220 EXPECT_EQ(std::string(keys::kThirdPartyKey), |
221 first_party_attribute->GetName()); | 221 first_party_attribute->GetName()); |
222 | 222 |
223 const GURL url_empty; | 223 const GURL url_empty; |
224 const GURL url_a("http://a.com"); | 224 const GURL url_a("http://a.com"); |
225 const GURL url_b("http://b.com"); | 225 const GURL url_b("http://b.com"); |
226 net::TestURLRequestContext context; | 226 net::TestURLRequestContext context; |
227 net::TestDelegate delegate; | 227 net::TestDelegate delegate; |
228 scoped_ptr<net::URLRequest> url_request( | 228 scoped_ptr<net::URLRequest> url_request( |
229 context.CreateRequest(url_a, net::DEFAULT_PRIORITY, &delegate, NULL)); | 229 context.CreateRequest(url_a, net::DEFAULT_PRIORITY, &delegate, nullptr)); |
230 | 230 |
231 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { | 231 for (unsigned int i = 1; i <= kLastActiveStage; i <<= 1) { |
232 if (!(kActiveStages & i)) | 232 if (!(kActiveStages & i)) |
233 continue; | 233 continue; |
234 const RequestStage stage = static_cast<RequestStage>(i); | 234 const RequestStage stage = static_cast<RequestStage>(i); |
235 url_request->set_first_party_for_cookies(url_empty); | 235 url_request->set_first_party_for_cookies(url_empty); |
236 EXPECT_FALSE(third_party_attribute->IsFulfilled( | 236 EXPECT_FALSE(third_party_attribute->IsFulfilled( |
237 WebRequestData(url_request.get(), stage))); | 237 WebRequestData(url_request.get(), stage))); |
238 EXPECT_TRUE(first_party_attribute->IsFulfilled( | 238 EXPECT_TRUE(first_party_attribute->IsFulfilled( |
239 WebRequestData(url_request.get(), stage))); | 239 WebRequestData(url_request.get(), stage))); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 one_stage_attributes; | 303 one_stage_attributes; |
304 | 304 |
305 for (size_t i = 0; i < arraysize(active_stages); ++i) { | 305 for (size_t i = 0; i < arraysize(active_stages); ++i) { |
306 base::ListValue single_stage_list; | 306 base::ListValue single_stage_list; |
307 single_stage_list.AppendString(active_stages[i].second); | 307 single_stage_list.AppendString(active_stages[i].second); |
308 one_stage_attributes.push_back( | 308 one_stage_attributes.push_back( |
309 WebRequestConditionAttribute::Create(keys::kStagesKey, | 309 WebRequestConditionAttribute::Create(keys::kStagesKey, |
310 &single_stage_list, | 310 &single_stage_list, |
311 &error)); | 311 &error)); |
312 EXPECT_EQ("", error); | 312 EXPECT_EQ("", error); |
313 ASSERT_TRUE(one_stage_attributes.back().get() != NULL); | 313 ASSERT_TRUE(one_stage_attributes.back().get() != nullptr); |
314 } | 314 } |
315 | 315 |
316 const GURL url_empty; | 316 const GURL url_empty; |
317 net::TestURLRequestContext context; | 317 net::TestURLRequestContext context; |
318 net::TestDelegate delegate; | 318 net::TestDelegate delegate; |
319 scoped_ptr<net::URLRequest> url_request( | 319 scoped_ptr<net::URLRequest> url_request(context.CreateRequest( |
320 context.CreateRequest(url_empty, net::DEFAULT_PRIORITY, &delegate, NULL)); | 320 url_empty, net::DEFAULT_PRIORITY, &delegate, nullptr)); |
321 | 321 |
322 for (size_t i = 0; i < arraysize(active_stages); ++i) { | 322 for (size_t i = 0; i < arraysize(active_stages); ++i) { |
323 EXPECT_FALSE(empty_attribute->IsFulfilled( | 323 EXPECT_FALSE(empty_attribute->IsFulfilled( |
324 WebRequestData(url_request.get(), active_stages[i].first))); | 324 WebRequestData(url_request.get(), active_stages[i].first))); |
325 | 325 |
326 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { | 326 for (size_t j = 0; j < one_stage_attributes.size(); ++j) { |
327 EXPECT_EQ(i == j, | 327 EXPECT_EQ(i == j, |
328 one_stage_attributes[j]->IsFulfilled( | 328 one_stage_attributes[j]->IsFulfilled( |
329 WebRequestData(url_request.get(), active_stages[i].first))); | 329 WebRequestData(url_request.get(), active_stages[i].first))); |
330 } | 330 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 scoped_ptr<base::DictionaryValue> GetDictionaryFromArray( | 364 scoped_ptr<base::DictionaryValue> GetDictionaryFromArray( |
365 const std::vector<const std::string*>& array) { | 365 const std::vector<const std::string*>& array) { |
366 const size_t length = array.size(); | 366 const size_t length = array.size(); |
367 CHECK(length % 2 == 0); | 367 CHECK(length % 2 == 0); |
368 | 368 |
369 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); | 369 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); |
370 for (size_t i = 0; i < length; i += 2) { | 370 for (size_t i = 0; i < length; i += 2) { |
371 const std::string* name = array[i]; | 371 const std::string* name = array[i]; |
372 const std::string* value = array[i+1]; | 372 const std::string* value = array[i+1]; |
373 if (dictionary->HasKey(*name)) { | 373 if (dictionary->HasKey(*name)) { |
374 base::Value* entry = NULL; | 374 base::Value* entry = nullptr; |
375 scoped_ptr<base::Value> entry_owned; | 375 scoped_ptr<base::Value> entry_owned; |
376 base::ListValue* list = NULL; | 376 base::ListValue* list = nullptr; |
377 if (!dictionary->GetWithoutPathExpansion(*name, &entry)) | 377 if (!dictionary->GetWithoutPathExpansion(*name, &entry)) |
378 return scoped_ptr<base::DictionaryValue>(); | 378 return scoped_ptr<base::DictionaryValue>(); |
379 switch (entry->GetType()) { | 379 switch (entry->GetType()) { |
380 case base::Value::TYPE_STRING: | 380 case base::Value::TYPE_STRING: |
381 // Replace the present string with a list. | 381 // Replace the present string with a list. |
382 list = new base::ListValue; | 382 list = new base::ListValue; |
383 // Ignoring return value, we already verified the entry is there. | 383 // Ignoring return value, we already verified the entry is there. |
384 dictionary->RemoveWithoutPathExpansion(*name, &entry_owned); | 384 dictionary->RemoveWithoutPathExpansion(*name, &entry_owned); |
385 list->Append(entry_owned.release()); | 385 list->Append(entry_owned.release()); |
386 list->Append(new base::StringValue(*value)); | 386 list->Append(new base::StringValue(*value)); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 TEST(WebRequestConditionAttributeTest, RequestHeaders) { | 438 TEST(WebRequestConditionAttributeTest, RequestHeaders) { |
439 // Necessary for TestURLRequest. | 439 // Necessary for TestURLRequest. |
440 base::MessageLoopForIO message_loop; | 440 base::MessageLoopForIO message_loop; |
441 | 441 |
442 net::TestURLRequestContext context; | 442 net::TestURLRequestContext context; |
443 net::TestDelegate delegate; | 443 net::TestDelegate delegate; |
444 scoped_ptr<net::URLRequest> url_request( | 444 scoped_ptr<net::URLRequest> url_request( |
445 context.CreateRequest(GURL("http://example.com"), // Dummy URL. | 445 context.CreateRequest(GURL("http://example.com"), // Dummy URL. |
446 net::DEFAULT_PRIORITY, | 446 net::DEFAULT_PRIORITY, |
447 &delegate, | 447 &delegate, |
448 NULL)); | 448 nullptr)); |
449 url_request->SetExtraRequestHeaderByName( | 449 url_request->SetExtraRequestHeaderByName( |
450 "Custom-header", "custom/value", true /* overwrite */); | 450 "Custom-header", "custom/value", true /* overwrite */); |
451 url_request->Start(); | 451 url_request->Start(); |
452 base::MessageLoop::current()->Run(); | 452 base::MessageLoop::current()->Run(); |
453 | 453 |
454 std::vector<std::vector<const std::string*> > tests; | 454 std::vector<std::vector<const std::string*> > tests; |
455 bool result = false; | 455 bool result = false; |
456 | 456 |
457 const RequestStage stage = ON_BEFORE_SEND_HEADERS; | 457 const RequestStage stage = ON_BEFORE_SEND_HEADERS; |
458 | 458 |
(...skipping 27 matching lines...) Expand all Loading... |
486 // Positive filter, failing (disjunction of tests). | 486 // Positive filter, failing (disjunction of tests). |
487 MatchAndCheck(tests, keys::kRequestHeadersKey, stage, url_request.get(), | 487 MatchAndCheck(tests, keys::kRequestHeadersKey, stage, url_request.get(), |
488 &result); | 488 &result); |
489 EXPECT_FALSE(result); | 489 EXPECT_FALSE(result); |
490 // Negative filter, passing (disjunction of tests). | 490 // Negative filter, passing (disjunction of tests). |
491 MatchAndCheck(tests, keys::kExcludeRequestHeadersKey, stage, | 491 MatchAndCheck(tests, keys::kExcludeRequestHeadersKey, stage, |
492 url_request.get(), &result); | 492 url_request.get(), &result); |
493 EXPECT_TRUE(result); | 493 EXPECT_TRUE(result); |
494 | 494 |
495 // Third set of test data, corner case -- empty disjunction. | 495 // Third set of test data, corner case -- empty disjunction. |
496 GetArrayAsVector(NULL, NULL, 0u, &tests); | 496 GetArrayAsVector(nullptr, nullptr, 0u, &tests); |
497 // Positive filter, failing (no test to pass). | 497 // Positive filter, failing (no test to pass). |
498 MatchAndCheck(tests, keys::kRequestHeadersKey, stage, url_request.get(), | 498 MatchAndCheck(tests, keys::kRequestHeadersKey, stage, url_request.get(), |
499 &result); | 499 &result); |
500 EXPECT_FALSE(result); | 500 EXPECT_FALSE(result); |
501 // Negative filter, passing (no test to fail). | 501 // Negative filter, passing (no test to fail). |
502 MatchAndCheck(tests, keys::kExcludeRequestHeadersKey, stage, | 502 MatchAndCheck(tests, keys::kExcludeRequestHeadersKey, stage, |
503 url_request.get(), &result); | 503 url_request.get(), &result); |
504 EXPECT_TRUE(result); | 504 EXPECT_TRUE(result); |
505 | 505 |
506 // Fourth set of test data, corner case -- empty conjunction. | 506 // Fourth set of test data, corner case -- empty conjunction. |
507 const size_t kEmptyConjunctionSizes[] = { 0u }; | 507 const size_t kEmptyConjunctionSizes[] = { 0u }; |
508 GetArrayAsVector(NULL, kEmptyConjunctionSizes, 1u, &tests); | 508 GetArrayAsVector(nullptr, kEmptyConjunctionSizes, 1u, &tests); |
509 // Positive filter, passing (trivial test). | 509 // Positive filter, passing (trivial test). |
510 MatchAndCheck(tests, keys::kRequestHeadersKey, stage, url_request.get(), | 510 MatchAndCheck(tests, keys::kRequestHeadersKey, stage, url_request.get(), |
511 &result); | 511 &result); |
512 EXPECT_TRUE(result); | 512 EXPECT_TRUE(result); |
513 // Negative filter, failing. | 513 // Negative filter, failing. |
514 MatchAndCheck(tests, keys::kExcludeRequestHeadersKey, stage, | 514 MatchAndCheck(tests, keys::kExcludeRequestHeadersKey, stage, |
515 url_request.get(), &result); | 515 url_request.get(), &result); |
516 EXPECT_FALSE(result); | 516 EXPECT_FALSE(result); |
517 } | 517 } |
518 | 518 |
519 // Here we test WebRequestConditionAttributeResponseHeaders for: | 519 // Here we test WebRequestConditionAttributeResponseHeaders for: |
520 // 1. Correct implementation of prefix/suffix/contains/equals matching. | 520 // 1. Correct implementation of prefix/suffix/contains/equals matching. |
521 // 2. Performing logical disjunction (||) between multiple specifications. | 521 // 2. Performing logical disjunction (||) between multiple specifications. |
522 // 3. Negating the match in case of 'doesNotContainHeaders'. | 522 // 3. Negating the match in case of 'doesNotContainHeaders'. |
523 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { | 523 TEST(WebRequestConditionAttributeTest, ResponseHeaders) { |
524 // Necessary for TestURLRequest. | 524 // Necessary for TestURLRequest. |
525 base::MessageLoopForIO message_loop; | 525 base::MessageLoopForIO message_loop; |
526 | 526 |
527 net::test_server::EmbeddedTestServer test_server; | 527 net::test_server::EmbeddedTestServer test_server; |
528 test_server.ServeFilesFromDirectory(TestDataPath( | 528 test_server.ServeFilesFromDirectory(TestDataPath( |
529 "chrome/test/data/extensions/api_test/webrequest/declarative")); | 529 "chrome/test/data/extensions/api_test/webrequest/declarative")); |
530 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); | 530 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); |
531 | 531 |
532 net::TestURLRequestContext context; | 532 net::TestURLRequestContext context; |
533 net::TestDelegate delegate; | 533 net::TestDelegate delegate; |
534 scoped_ptr<net::URLRequest> url_request( | 534 scoped_ptr<net::URLRequest> url_request( |
535 context.CreateRequest(test_server.GetURL("/headers.html"), | 535 context.CreateRequest(test_server.GetURL("/headers.html"), |
536 net::DEFAULT_PRIORITY, | 536 net::DEFAULT_PRIORITY, |
537 &delegate, | 537 &delegate, |
538 NULL)); | 538 nullptr)); |
539 url_request->Start(); | 539 url_request->Start(); |
540 base::MessageLoop::current()->Run(); | 540 base::MessageLoop::current()->Run(); |
541 | 541 |
542 // In all the tests below we assume that the server includes the headers | 542 // In all the tests below we assume that the server includes the headers |
543 // Custom-Header: custom/value | 543 // Custom-Header: custom/value |
544 // Custom-Header-B: valueA | 544 // Custom-Header-B: valueA |
545 // Custom-Header-B: valueB | 545 // Custom-Header-B: valueB |
546 // Custom-Header-C: valueC, valueD | 546 // Custom-Header-C: valueC, valueD |
547 // Custom-Header-D: | 547 // Custom-Header-D: |
548 // in the response, but does not include "Non-existing: void". | 548 // in the response, but does not include "Non-existing: void". |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 }; | 710 }; |
711 const size_t kExistingSize[] = { arraysize(kExisting) }; | 711 const size_t kExistingSize[] = { arraysize(kExisting) }; |
712 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); | 712 GetArrayAsVector(kExisting, kExistingSize, 1u, &tests); |
713 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, stage, | 713 MatchAndCheck(tests, keys::kExcludeResponseHeadersKey, stage, |
714 url_request.get(), &result); | 714 url_request.get(), &result); |
715 EXPECT_FALSE(result); | 715 EXPECT_FALSE(result); |
716 } | 716 } |
717 | 717 |
718 } // namespace | 718 } // namespace |
719 } // namespace extensions | 719 } // namespace extensions |
OLD | NEW |