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 <queue> | 5 #include <queue> |
6 #include <map> | 6 #include <map> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 } | 66 } |
67 | 67 |
68 // Searches |key| in |collection| by iterating over its elements and returns | 68 // Searches |key| in |collection| by iterating over its elements and returns |
69 // true if found. | 69 // true if found. |
70 template <typename Collection, typename Key> | 70 template <typename Collection, typename Key> |
71 bool Contains(const Collection& collection, const Key& key) { | 71 bool Contains(const Collection& collection, const Key& key) { |
72 return std::find(collection.begin(), collection.end(), key) != | 72 return std::find(collection.begin(), collection.end(), key) != |
73 collection.end(); | 73 collection.end(); |
74 } | 74 } |
75 | 75 |
76 // Inspects the data attached to the |message| and tries to extract its | |
77 // "postData" section into |post_data|. | |
78 void GetPostData(IPC::Message* message, std::string* post_data, bool* success) { | |
battre
2012/07/16 17:39:45
nit: return boolean instead of passing bool* succe
vabr (Chromium)
2012/07/17 11:11:11
That would not work, ASSERT_* cannot be used in no
battre
2012/07/17 12:46:07
You can replace all ASSERT_* statements with CHECK
vabr (Chromium)
2012/07/17 13:04:52
Done.
| |
79 ASSERT_TRUE(success != NULL); | |
80 *success = false; | |
81 ASSERT_EQ(message->type(), ExtensionMsg_MessageInvoke::ID); | |
battre
2012/07/16 17:39:45
as message is provided and not generated by the te
vabr (Chromium)
2012/07/17 11:11:11
Done.
QUESTION:
Do you want me to change this also
| |
82 ExtensionMsg_MessageInvoke::Param param; | |
83 Value* temp_value = NULL; | |
84 ASSERT_TRUE(ExtensionMsg_MessageInvoke::Read(message, ¶m)); | |
85 ASSERT_GE(param.c.GetSize(), static_cast<size_t>(2)); | |
86 ASSERT_TRUE(param.c.Get(1, &temp_value)); | |
87 std::string args; | |
88 ASSERT_TRUE(temp_value->GetAsString(&args)); | |
battre
2012/07/16 17:39:45
nit: new line after 88?
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
89 const char kPostDataHead[] = "\"postData\":{"; | |
90 size_t post_data_start = args.find(kPostDataHead); | |
91 if (post_data_start == std::string::npos) | |
92 return; | |
93 post_data_start += strlen(kPostDataHead); | |
battre
2012/07/16 17:39:45
nit: new line after 93?
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
94 const size_t post_data_end = args.find("}", post_data_start); | |
95 ASSERT_NE(post_data_end, std::string::npos); | |
96 const size_t post_data_length = post_data_end - post_data_start; | |
97 *post_data = std::string(args, post_data_start, post_data_length); | |
98 *success = true; | |
99 return; | |
100 } | |
101 | |
76 } // namespace | 102 } // namespace |
77 | 103 |
78 // A mock event router that responds to events with a pre-arranged queue of | 104 // A mock event router that responds to events with a pre-arranged queue of |
79 // Tasks. | 105 // Tasks. |
80 class TestIPCSender : public IPC::Sender { | 106 class TestIPCSender : public IPC::Sender { |
81 public: | 107 public: |
82 typedef std::list<linked_ptr<IPC::Message> > SentMessages; | 108 typedef std::list<linked_ptr<IPC::Message> > SentMessages; |
83 | 109 |
84 // Adds a Task to the queue. We will fire these in order as events are | 110 // Adds a Task to the queue. We will fire these in order as events are |
85 // dispatched. | 111 // dispatched. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 enable_referrers_.Init( | 152 enable_referrers_.Init( |
127 prefs::kEnableReferrers, profile_.GetTestingPrefService(), NULL); | 153 prefs::kEnableReferrers, profile_.GetTestingPrefService(), NULL); |
128 network_delegate_.reset(new ChromeNetworkDelegate( | 154 network_delegate_.reset(new ChromeNetworkDelegate( |
129 event_router_.get(), NULL, NULL, &profile_, | 155 event_router_.get(), NULL, NULL, &profile_, |
130 CookieSettings::Factory::GetForProfile(&profile_), &enable_referrers_)); | 156 CookieSettings::Factory::GetForProfile(&profile_), &enable_referrers_)); |
131 context_.reset(new TestURLRequestContext(true)); | 157 context_.reset(new TestURLRequestContext(true)); |
132 context_->set_network_delegate(network_delegate_.get()); | 158 context_->set_network_delegate(network_delegate_.get()); |
133 context_->Init(); | 159 context_->Init(); |
134 } | 160 } |
135 | 161 |
162 void FireURLRequestWithPostData(const char* content_type, const char* bytes); | |
163 | |
136 MessageLoopForIO message_loop_; | 164 MessageLoopForIO message_loop_; |
137 content::TestBrowserThread ui_thread_; | 165 content::TestBrowserThread ui_thread_; |
138 content::TestBrowserThread io_thread_; | 166 content::TestBrowserThread io_thread_; |
139 TestingProfile profile_; | 167 TestingProfile profile_; |
140 TestDelegate delegate_; | 168 TestDelegate delegate_; |
141 BooleanPrefMember enable_referrers_; | 169 BooleanPrefMember enable_referrers_; |
142 TestIPCSender ipc_sender_; | 170 TestIPCSender ipc_sender_; |
143 scoped_refptr<ExtensionEventRouterForwarder> event_router_; | 171 scoped_refptr<ExtensionEventRouterForwarder> event_router_; |
144 scoped_refptr<ExtensionInfoMap> extension_info_map_; | 172 scoped_refptr<ExtensionInfoMap> extension_info_map_; |
145 scoped_ptr<ChromeNetworkDelegate> network_delegate_; | 173 scoped_ptr<ChromeNetworkDelegate> network_delegate_; |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
391 EXPECT_TRUE(!request.is_pending()); | 419 EXPECT_TRUE(!request.is_pending()); |
392 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); | 420 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); |
393 EXPECT_EQ(net::ERR_ABORTED, request.status().error()); | 421 EXPECT_EQ(net::ERR_ABORTED, request.status().error()); |
394 EXPECT_EQ(request_url, request.url()); | 422 EXPECT_EQ(request_url, request.url()); |
395 EXPECT_EQ(1U, request.url_chain().size()); | 423 EXPECT_EQ(1U, request.url_chain().size()); |
396 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 424 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
397 | 425 |
398 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 426 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
399 &profile_, extension_id, kEventName + "/1"); | 427 &profile_, extension_id, kEventName + "/1"); |
400 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 428 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( |
401 &profile_, extension_id, kEventName2 + "/1"); | 429 &profile_, extension_id, kEventName2 + "/1"); |
430 } | |
431 | |
432 // Fires a URLRequest with the specified |content_type|. Method will be "POST" | |
433 // and the data will be |bytes|. | |
battre
2012/07/16 17:39:45
the comment goes to the declaration above.
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
434 void ExtensionWebRequestTest::FireURLRequestWithPostData( | |
435 const char* content_type, | |
436 const char* bytes) { | |
437 // The request URL can be arbitrary but must have a HTTP or HTTPS scheme. | |
battre
2012/07/16 17:39:45
nit: s/a HTTP/an HTTP/
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
438 GURL request_url("http://www.example.com"); | |
439 net::URLRequest request(request_url, &delegate_, context_.get()); | |
440 request.set_method("POST"); | |
441 request.SetExtraRequestHeaderByName("Content-Type", content_type, true); | |
442 request.AppendBytesToUpload(bytes, strlen(bytes)); | |
443 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); | |
444 request.Start(); | |
445 } | |
battre
2012/07/16 17:39:45
nit: empty line
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
446 TEST_F(ExtensionWebRequestTest, AccessPostData) { | |
447 // We verify that POST data are accessible to OnBeforeRequest listeners. | |
448 // Construct the test data. | |
449 #define kBoundary "THIS_IS_A_BOUNDARY" | |
450 #define kBlock "--" kBoundary "\r\n" \ | |
451 "Content-Disposition: form-data; name=\"text\"\r\n" \ | |
452 "\r\n" \ | |
453 "test text\r\n" \ | |
454 "--" kBoundary "--" | |
455 // POST data input. | |
456 const char kMultipartBytes[] = kBlock; | |
457 // POST data output. | |
458 const char kResultString[] = "\"text\":[\"test text\"]"; | |
459 // Header. | |
460 const char kMultipart[] = "multipart/form-data; boundary=" kBoundary; | |
461 #undef kBlock | |
462 #undef kBoundary | |
463 bool kSuccessExpected[] = {true, false}; | |
464 | |
465 // Set up a dummy extension name. | |
466 ExtensionWebRequestEventRouter::RequestFilter filter; | |
467 std::string extension_id("1"); | |
468 int extra_info_spec_post = | |
469 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING | | |
470 ExtensionWebRequestEventRouter::ExtraInfoSpec::REQUEST_POST_DATA; | |
471 int extra_info_spec_no_post = | |
472 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING; | |
473 | |
474 // Subscribe to OnBeforeRequest with POST data requirement. | |
475 const std::string kEventName(keys::kOnBeforeRequest); | |
476 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); | |
477 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | |
478 &profile_, extension_id, extension_id, kEventName, kEventName + "/1", | |
battre
2012/07/16 17:39:45
nit: +2 spaces
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
479 filter, extra_info_spec_post, ipc_sender_factory.GetWeakPtr()); | |
480 | |
481 FireURLRequestWithPostData(kMultipart, kMultipartBytes); | |
482 | |
483 MessageLoop::current()->RunAllPending(); | |
484 | |
485 // Now remove the requirement of POST data. | |
486 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | |
487 &profile_, extension_id, kEventName + "/1"); | |
488 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | |
489 &profile_, extension_id, extension_id, kEventName, kEventName + "/1", | |
battre
2012/07/16 17:39:45
+2 spaces
vabr (Chromium)
2012/07/17 11:11:11
Done.
| |
490 filter, extra_info_spec_no_post, ipc_sender_factory.GetWeakPtr()); | |
491 | |
492 FireURLRequestWithPostData(kMultipart, kMultipartBytes); | |
493 | |
494 MessageLoop::current()->RunAllPending(); | |
495 | |
496 IPC::Message* message = NULL; | |
497 std::string post_data; | |
498 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); | |
499 for (size_t test = 0; test < arraysize(kSuccessExpected); ++test) { | |
500 EXPECT_NE(i, ipc_sender_.sent_end()); | |
501 message = (i++)->get(); | |
502 bool post_data_found; | |
503 GetPostData(message, &post_data, &post_data_found); | |
504 if (kSuccessExpected[test]) { | |
505 EXPECT_TRUE(post_data_found); | |
battre
2012/07/16 17:39:45
if you write
EXPECT_TRUE(post_data_found) << test;
vabr (Chromium)
2012/07/17 11:11:11
Done. Thanks for teaching me this. :)
| |
506 EXPECT_EQ(kResultString, post_data); | |
507 } else { | |
508 EXPECT_FALSE(post_data_found); | |
509 } | |
510 } | |
511 | |
512 EXPECT_TRUE(i == ipc_sender_.sent_end()); | |
battre
2012/07/16 17:39:45
Does EXPECT_EQ work?
vabr (Chromium)
2012/07/17 11:11:11
Done. It seems to work, just as the EXPECT_NE a co
| |
513 | |
514 // Clean-up. | |
515 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | |
516 &profile_, extension_id, kEventName + "/1"); | |
402 } | 517 } |
403 | 518 |
404 struct HeaderModificationTest_Header { | 519 struct HeaderModificationTest_Header { |
405 const char* name; | 520 const char* name; |
406 const char* value; | 521 const char* value; |
407 }; | 522 }; |
408 | 523 |
409 struct HeaderModificationTest_Modification { | 524 struct HeaderModificationTest_Modification { |
410 enum Type { | 525 enum Type { |
411 SET, | 526 SET, |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
656 true, | 771 true, |
657 ExtensionWebRequestEventRouter::ExtraInfoSpec::RESPONSE_HEADERS); | 772 ExtensionWebRequestEventRouter::ExtraInfoSpec::RESPONSE_HEADERS); |
658 TestInitFromValue( | 773 TestInitFromValue( |
659 "blocking", | 774 "blocking", |
660 true, | 775 true, |
661 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING); | 776 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING); |
662 TestInitFromValue( | 777 TestInitFromValue( |
663 "asyncBlocking", | 778 "asyncBlocking", |
664 true, | 779 true, |
665 ExtensionWebRequestEventRouter::ExtraInfoSpec::ASYNC_BLOCKING); | 780 ExtensionWebRequestEventRouter::ExtraInfoSpec::ASYNC_BLOCKING); |
781 TestInitFromValue( | |
782 "requestPostData", | |
783 true, | |
784 ExtensionWebRequestEventRouter::ExtraInfoSpec::REQUEST_POST_DATA); | |
666 | 785 |
667 // Multiple valid values are bitwise-or'ed. | 786 // Multiple valid values are bitwise-or'ed. |
668 TestInitFromValue( | 787 TestInitFromValue( |
669 "requestHeaders,blocking", | 788 "requestHeaders,blocking", |
670 true, | 789 true, |
671 ExtensionWebRequestEventRouter::ExtraInfoSpec::REQUEST_HEADERS | | 790 ExtensionWebRequestEventRouter::ExtraInfoSpec::REQUEST_HEADERS | |
672 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING); | 791 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING); |
673 | 792 |
674 // Any invalid values lead to a bad parse. | 793 // Any invalid values lead to a bad parse. |
675 TestInitFromValue("invalidValue", false, 0); | 794 TestInitFromValue("invalidValue", false, 0); |
(...skipping 849 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1525 // Check that requests are rejected if their first party url is sensitive. | 1644 // Check that requests are rejected if their first party url is sensitive. |
1526 ASSERT_GE(arraysize(non_sensitive_urls), 1u); | 1645 ASSERT_GE(arraysize(non_sensitive_urls), 1u); |
1527 GURL non_sensitive_url(non_sensitive_urls[0]); | 1646 GURL non_sensitive_url(non_sensitive_urls[0]); |
1528 for (size_t i = 0; i < arraysize(sensitive_urls); ++i) { | 1647 for (size_t i = 0; i < arraysize(sensitive_urls); ++i) { |
1529 TestURLRequest request(non_sensitive_url, NULL, &context); | 1648 TestURLRequest request(non_sensitive_url, NULL, &context); |
1530 GURL sensitive_url(sensitive_urls[i]); | 1649 GURL sensitive_url(sensitive_urls[i]); |
1531 request.set_first_party_for_cookies(sensitive_url); | 1650 request.set_first_party_for_cookies(sensitive_url); |
1532 EXPECT_TRUE(helpers::HideRequest(&request)) << sensitive_urls[i]; | 1651 EXPECT_TRUE(helpers::HideRequest(&request)) << sensitive_urls[i]; |
1533 } | 1652 } |
1534 } | 1653 } |
OLD | NEW |