Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: content/browser/renderer_host/resource_dispatcher_host_unittest.cc

Issue 10612002: Adds a test for http://codereview.chromium.org/10599002/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "content/browser/renderer_host/resource_dispatcher_host_impl.h" 5 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/file_path.h" 10 #include "base/file_path.h"
11 #include "base/memory/scoped_vector.h" 11 #include "base/memory/scoped_vector.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/process_util.h" 13 #include "base/process_util.h"
14 #include "base/string_number_conversions.h"
15 #include "base/string_split.h"
14 #include "content/browser/browser_thread_impl.h" 16 #include "content/browser/browser_thread_impl.h"
15 #include "content/browser/child_process_security_policy_impl.h" 17 #include "content/browser/child_process_security_policy_impl.h"
16 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" 18 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
17 #include "content/browser/renderer_host/resource_message_filter.h" 19 #include "content/browser/renderer_host/resource_message_filter.h"
18 #include "content/common/child_process_host_impl.h" 20 #include "content/common/child_process_host_impl.h"
19 #include "content/common/resource_messages.h" 21 #include "content/common/resource_messages.h"
20 #include "content/common/view_messages.h" 22 #include "content/common/view_messages.h"
21 #include "content/public/browser/global_request_id.h" 23 #include "content/public/browser/global_request_id.h"
22 #include "content/public/browser/resource_context.h" 24 #include "content/public/browser/resource_context.h"
23 #include "content/public/browser/resource_dispatcher_host_delegate.h" 25 #include "content/public/browser/resource_dispatcher_host_delegate.h"
24 #include "content/public/browser/resource_throttle.h" 26 #include "content/public/browser/resource_throttle.h"
25 #include "content/public/common/resource_response.h" 27 #include "content/public/common/resource_response.h"
26 #include "content/public/test/test_browser_context.h" 28 #include "content/public/test/test_browser_context.h"
27 #include "net/base/net_errors.h" 29 #include "net/base/net_errors.h"
28 #include "net/base/upload_data.h" 30 #include "net/base/upload_data.h"
29 #include "net/http/http_util.h" 31 #include "net/http/http_util.h"
30 #include "net/url_request/url_request.h" 32 #include "net/url_request/url_request.h"
31 #include "net/url_request/url_request_job.h" 33 #include "net/url_request/url_request_job.h"
34 #include "net/url_request/url_request_simple_job.h"
32 #include "net/url_request/url_request_test_job.h" 35 #include "net/url_request/url_request_test_job.h"
33 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
34 #include "webkit/appcache/appcache_interfaces.h" 37 #include "webkit/appcache/appcache_interfaces.h"
35 38
36 // TODO(eroman): Write unit tests for SafeBrowsing that exercise 39 // TODO(eroman): Write unit tests for SafeBrowsing that exercise
37 // SafeBrowsingResourceHandler. 40 // SafeBrowsingResourceHandler.
38 41
39 namespace content { 42 namespace content {
40 43
41 namespace { 44 namespace {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 messages_.push_back(msg); 109 messages_.push_back(msg);
107 } 110 }
108 111
109 // This groups the messages by their request ID. The groups will be in order 112 // This groups the messages by their request ID. The groups will be in order
110 // that the first message for each request ID was received, and the messages 113 // that the first message for each request ID was received, and the messages
111 // within the groups will be in the order that they appeared. 114 // within the groups will be in the order that they appeared.
112 // Note that this clears messages_. 115 // Note that this clears messages_.
113 typedef std::vector< std::vector<IPC::Message> > ClassifiedMessages; 116 typedef std::vector< std::vector<IPC::Message> > ClassifiedMessages;
114 void GetClassifiedMessages(ClassifiedMessages* msgs); 117 void GetClassifiedMessages(ClassifiedMessages* msgs);
115 118
119 private:
116 std::vector<IPC::Message> messages_; 120 std::vector<IPC::Message> messages_;
117 }; 121 };
118 122
119 // This is very inefficient as a result of repeatedly extracting the ID, use 123 // This is very inefficient as a result of repeatedly extracting the ID, use
120 // only for tests! 124 // only for tests!
121 void ResourceIPCAccumulator::GetClassifiedMessages(ClassifiedMessages* msgs) { 125 void ResourceIPCAccumulator::GetClassifiedMessages(ClassifiedMessages* msgs) {
122 while (!messages_.empty()) { 126 while (!messages_.empty()) {
123 std::vector<IPC::Message> cur_requests; 127 std::vector<IPC::Message> cur_requests;
124 cur_requests.push_back(messages_[0]); 128 cur_requests.push_back(messages_[0]);
125 int cur_id = RequestIDForMessage(messages_[0]); 129 int cur_id = RequestIDForMessage(messages_[0]);
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 : net::URLRequestTestJob(request, response_headers, 285 : net::URLRequestTestJob(request, response_headers,
282 response_data, auto_advance) {} 286 response_data, auto_advance) {}
283 287
284 protected: 288 protected:
285 ~URLRequestTestDelayedCompletionJob() {} 289 ~URLRequestTestDelayedCompletionJob() {}
286 290
287 private: 291 private:
288 virtual bool NextReadAsync() OVERRIDE { return true; } 292 virtual bool NextReadAsync() OVERRIDE { return true; }
289 }; 293 };
290 294
295 class URLRequestBigJob : public net::URLRequestSimpleJob {
296 public:
297 URLRequestBigJob(net::URLRequest* request)
298 : net::URLRequestSimpleJob(request) {
299 }
291 300
301 virtual bool GetData(std::string* mime_type,
302 std::string* charset,
303 std::string* data) const {
304 *mime_type = "text/plain";
305 *charset = "UTF-8";
306
307 std::string text;
308 int count;
309 if (!ParseURL(request_->url(), &text, &count))
310 return false;
311
312 data->reserve(text.size() * count);
313 for (int i = 0; i < count; ++i)
314 data->append(text);
315
316 return true;
317 }
318
319 private:
320 virtual ~URLRequestBigJob() {}
321
322 // big-job:substring,N
323 static bool ParseURL(const GURL& url, std::string* text, int* count) {
324 std::vector<std::string> parts;
325 base::SplitString(url.path(), ',', &parts);
326
327 if (parts.size() != 2)
328 return false;
329
330 *text = parts[0];
331 return base::StringToInt(parts[1], count);
332 }
333 };
292 334
293 // Associated with an URLRequest to determine if the URLRequest gets deleted. 335 // Associated with an URLRequest to determine if the URLRequest gets deleted.
294 class TestUserData : public base::SupportsUserData::Data { 336 class TestUserData : public base::SupportsUserData::Data {
295 public: 337 public:
296 explicit TestUserData(bool* was_deleted) 338 explicit TestUserData(bool* was_deleted)
297 : was_deleted_(was_deleted) { 339 : was_deleted_(was_deleted) {
298 } 340 }
299 341
300 ~TestUserData() { 342 ~TestUserData() {
301 *was_deleted_ = true; 343 *was_deleted_ = true;
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 // ForwardingFilter. 481 // ForwardingFilter.
440 void MakeTestRequest(ResourceMessageFilter* filter, 482 void MakeTestRequest(ResourceMessageFilter* filter,
441 int render_view_id, 483 int render_view_id,
442 int request_id, 484 int request_id,
443 const GURL& url); 485 const GURL& url);
444 486
445 void CancelRequest(int request_id); 487 void CancelRequest(int request_id);
446 488
447 void CompleteStartRequest(int request_id); 489 void CompleteStartRequest(int request_id);
448 490
449 void EnsureTestSchemeIsAllowed() { 491 void EnsureSchemeIsAllowed(const std::string& scheme) {
450 ChildProcessSecurityPolicyImpl* policy = 492 ChildProcessSecurityPolicyImpl* policy =
451 ChildProcessSecurityPolicyImpl::GetInstance(); 493 ChildProcessSecurityPolicyImpl::GetInstance();
452 if (!policy->IsWebSafeScheme("test")) 494 if (!policy->IsWebSafeScheme(scheme))
453 policy->RegisterWebSafeScheme("test"); 495 policy->RegisterWebSafeScheme(scheme);
496 }
497
498 void EnsureTestSchemeIsAllowed() {
499 EnsureSchemeIsAllowed("test");
454 } 500 }
455 501
456 // Sets a particular response for any request from now on. To switch back to 502 // Sets a particular response for any request from now on. To switch back to
457 // the default bahavior, pass an empty |headers|. |headers| should be raw- 503 // the default bahavior, pass an empty |headers|. |headers| should be raw-
458 // formatted (NULLs instead of EOLs). 504 // formatted (NULLs instead of EOLs).
459 void SetResponse(const std::string& headers, const std::string& data) { 505 void SetResponse(const std::string& headers, const std::string& data) {
460 response_headers_ = headers; 506 response_headers_ = headers;
461 response_data_ = data; 507 response_data_ = data;
462 } 508 }
463 509
464 // Sets a particular resource type for any request from now on. 510 // Sets a particular resource type for any request from now on.
465 void SetResourceType(ResourceType::Type type) { 511 void SetResourceType(ResourceType::Type type) {
466 resource_type_ = type; 512 resource_type_ = type;
467 } 513 }
468 514
469 // Intercepts requests for the given protocol. 515 // Intercepts requests for the given protocol.
470 void HandleScheme(const std::string& scheme) { 516 void HandleScheme(const std::string& scheme) {
471 DCHECK(scheme_.empty()); 517 DCHECK(scheme_.empty());
472 DCHECK(!old_factory_); 518 DCHECK(!old_factory_);
473 scheme_ = scheme; 519 scheme_ = scheme;
474 old_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( 520 old_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory(
475 scheme_, &ResourceDispatcherHostTest::Factory); 521 scheme_, &ResourceDispatcherHostTest::Factory);
522 EnsureSchemeIsAllowed(scheme);
476 } 523 }
477 524
478 // Our own net::URLRequestJob factory. 525 // Our own net::URLRequestJob factory.
479 static net::URLRequestJob* Factory(net::URLRequest* request, 526 static net::URLRequestJob* Factory(net::URLRequest* request,
480 const std::string& scheme) { 527 const std::string& scheme) {
481 if (test_fixture_->response_headers_.empty()) { 528 if (test_fixture_->response_headers_.empty()) {
482 if (delay_start_) { 529 if (delay_start_) {
483 return new URLRequestTestDelayedStartJob(request); 530 return new URLRequestTestDelayedStartJob(request);
484 } else if (delay_complete_) { 531 } else if (delay_complete_) {
485 return new URLRequestTestDelayedCompletionJob(request); 532 return new URLRequestTestDelayedCompletionJob(request);
533 } else if (scheme == "big-job") {
534 return new URLRequestBigJob(request);
486 } else { 535 } else {
487 return new net::URLRequestTestJob(request); 536 return new net::URLRequestTestJob(request);
488 } 537 }
489 } else { 538 } else {
490 if (delay_start_) { 539 if (delay_start_) {
491 return new URLRequestTestDelayedStartJob( 540 return new URLRequestTestDelayedStartJob(
492 request, test_fixture_->response_headers_, 541 request, test_fixture_->response_headers_,
493 test_fixture_->response_data_, false); 542 test_fixture_->response_data_, false);
494 } else if (delay_complete_) { 543 } else if (delay_complete_) {
495 return new URLRequestTestDelayedCompletionJob( 544 return new URLRequestTestDelayedCompletionJob(
(...skipping 929 matching lines...) Expand 10 before | Expand all | Expand 10 after
1425 1474
1426 PickleIterator iter(msgs[0][0]); 1475 PickleIterator iter(msgs[0][0]);
1427 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &request_id)); 1476 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &request_id));
1428 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &status)); 1477 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &status));
1429 1478
1430 EXPECT_EQ(1, request_id); 1479 EXPECT_EQ(1, request_id);
1431 EXPECT_EQ(net::URLRequestStatus::FAILED, status.status()); 1480 EXPECT_EQ(net::URLRequestStatus::FAILED, status.status());
1432 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, status.error()); 1481 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, status.error());
1433 } 1482 }
1434 1483
1484 TEST_F(ResourceDispatcherHostTest, DelayedDataReceivedACK) {
1485 EXPECT_EQ(0, host_.pending_requests());
1486
1487 HandleScheme("big-job");
1488 MakeTestRequest(0, 1, GURL("big-job:0123456789,1000000"));
1489
1490 // Sort all the messages we saw by request.
1491 ResourceIPCAccumulator::ClassifiedMessages msgs;
1492 accum_.GetClassifiedMessages(&msgs);
1493
1494 // We expect 1x ReceivedResponse + Nx ReceivedData messages.
1495 EXPECT_EQ(ResourceMsg_ReceivedResponse::ID, msgs[0][0].type());
1496 for (size_t i = 1; i < msgs[0].size(); ++i)
1497 EXPECT_EQ(ResourceMsg_DataReceived::ID, msgs[0][i].type());
1498
1499 // NOTE: If we fail the above checks then it means that we probably didn't
1500 // load a big enough response to trigger the delay mechanism we are trying to
1501 // test!
1502
1503 msgs[0].erase(msgs[0].begin());
1504
1505 // ACK all DataReceived messages until we find a RequestComplete message.
1506 bool complete = false;
1507 while (!complete) {
1508 for (size_t i = 0; i < msgs[0].size(); ++i) {
1509 if (msgs[0][i].type() == ResourceMsg_RequestComplete::ID) {
1510 complete = true;
1511 break;
1512 }
1513
1514 EXPECT_EQ(ResourceMsg_DataReceived::ID, msgs[0][i].type());
1515
1516 ResourceHostMsg_DataReceived_ACK msg(0, 1);
1517 bool msg_was_ok;
1518 host_.OnMessageReceived(msg, filter_.get(), &msg_was_ok);
1519 }
1520
1521 MessageLoop::current()->RunAllPending();
1522
1523 msgs.clear();
1524 accum_.GetClassifiedMessages(&msgs);
1525 }
1526 }
1527
1435 } // namespace content 1528 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698