OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "content/browser/renderer_host/resource_dispatcher_host.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/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
13 #include "content/browser/browser_thread_impl.h" | 13 #include "content/browser/browser_thread_impl.h" |
14 #include "content/browser/child_process_security_policy.h" | 14 #include "content/browser/child_process_security_policy.h" |
15 #include "content/browser/download/download_id.h" | 15 #include "content/browser/download/download_id.h" |
16 #include "content/browser/download/download_id_factory.h" | 16 #include "content/browser/download/download_id_factory.h" |
17 #include "content/browser/mock_resource_context.h" | 17 #include "content/browser/mock_resource_context.h" |
18 #include "content/browser/renderer_host/dummy_resource_handler.h" | 18 #include "content/browser/renderer_host/dummy_resource_handler.h" |
19 #include "content/browser/renderer_host/global_request_id.h" | 19 #include "content/browser/renderer_host/global_request_id.h" |
20 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 20 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
21 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" | 21 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" |
22 #include "content/browser/renderer_host/resource_handler.h" | 22 #include "content/browser/renderer_host/resource_handler.h" |
23 #include "content/browser/renderer_host/resource_message_filter.h" | 23 #include "content/browser/renderer_host/resource_message_filter.h" |
24 #include "content/common/resource_messages.h" | 24 #include "content/common/resource_messages.h" |
25 #include "content/common/resource_response.h" | |
26 #include "content/common/view_messages.h" | 25 #include "content/common/view_messages.h" |
| 26 #include "content/public/common/resource_response.h" |
27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
28 #include "net/base/upload_data.h" | 28 #include "net/base/upload_data.h" |
29 #include "net/http/http_util.h" | 29 #include "net/http/http_util.h" |
30 #include "net/url_request/url_request.h" | 30 #include "net/url_request/url_request.h" |
31 #include "net/url_request/url_request_job.h" | 31 #include "net/url_request/url_request_job.h" |
32 #include "net/url_request/url_request_test_job.h" | 32 #include "net/url_request/url_request_test_job.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
34 #include "webkit/appcache/appcache_interfaces.h" | 34 #include "webkit/appcache/appcache_interfaces.h" |
35 | 35 |
36 using content::BrowserThread; | 36 using content::BrowserThread; |
37 using content::BrowserThreadImpl; | 37 using content::BrowserThreadImpl; |
38 | 38 |
39 // TODO(eroman): Write unit tests for SafeBrowsing that exercise | 39 // TODO(eroman): Write unit tests for SafeBrowsing that exercise |
40 // SafeBrowsingResourceHandler. | 40 // SafeBrowsingResourceHandler. |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
44 // Returns the resource response header structure for this request. | 44 // Returns the resource response header structure for this request. |
45 void GetResponseHead(const std::vector<IPC::Message>& messages, | 45 void GetResponseHead(const std::vector<IPC::Message>& messages, |
46 ResourceResponseHead* response_head) { | 46 content::ResourceResponseHead* response_head) { |
47 ASSERT_GE(messages.size(), 2U); | 47 ASSERT_GE(messages.size(), 2U); |
48 | 48 |
49 // The first messages should be received response. | 49 // The first messages should be received response. |
50 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, messages[0].type()); | 50 ASSERT_EQ(ResourceMsg_ReceivedResponse::ID, messages[0].type()); |
51 | 51 |
52 void* iter = NULL; | 52 void* iter = NULL; |
53 int request_id; | 53 int request_id; |
54 ASSERT_TRUE(IPC::ReadParam(&messages[0], &iter, &request_id)); | 54 ASSERT_TRUE(IPC::ReadParam(&messages[0], &iter, &request_id)); |
55 ASSERT_TRUE(IPC::ReadParam(&messages[0], &iter, response_head)); | 55 ASSERT_TRUE(IPC::ReadParam(&messages[0], &iter, response_head)); |
56 } | 56 } |
(...skipping 943 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1000 // Flush all pending requests. | 1000 // Flush all pending requests. |
1001 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1001 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
1002 | 1002 |
1003 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1003 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
1004 | 1004 |
1005 // Sorts out all the messages we saw by request. | 1005 // Sorts out all the messages we saw by request. |
1006 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1006 ResourceIPCAccumulator::ClassifiedMessages msgs; |
1007 accum_.GetClassifiedMessages(&msgs); | 1007 accum_.GetClassifiedMessages(&msgs); |
1008 ASSERT_EQ(1U, msgs.size()); | 1008 ASSERT_EQ(1U, msgs.size()); |
1009 | 1009 |
1010 ResourceResponseHead response_head; | 1010 content::ResourceResponseHead response_head; |
1011 GetResponseHead(msgs[0], &response_head); | 1011 GetResponseHead(msgs[0], &response_head); |
1012 ASSERT_EQ("text/html", response_head.mime_type); | 1012 ASSERT_EQ("text/html", response_head.mime_type); |
1013 } | 1013 } |
1014 | 1014 |
1015 // Tests that we don't sniff the mime type when the server provides one. | 1015 // Tests that we don't sniff the mime type when the server provides one. |
1016 TEST_F(ResourceDispatcherHostTest, MimeNotSniffed) { | 1016 TEST_F(ResourceDispatcherHostTest, MimeNotSniffed) { |
1017 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1017 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
1018 | 1018 |
1019 std::string response("HTTP/1.1 200 OK\n" | 1019 std::string response("HTTP/1.1 200 OK\n" |
1020 "Content-type: image/jpeg\n\n"); | 1020 "Content-type: image/jpeg\n\n"); |
1021 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | 1021 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), |
1022 response.size())); | 1022 response.size())); |
1023 std::string response_data("<html><title>Test One</title></html>"); | 1023 std::string response_data("<html><title>Test One</title></html>"); |
1024 SetResponse(raw_headers, response_data); | 1024 SetResponse(raw_headers, response_data); |
1025 | 1025 |
1026 HandleScheme("http"); | 1026 HandleScheme("http"); |
1027 MakeTestRequest(0, 1, GURL("http:bla")); | 1027 MakeTestRequest(0, 1, GURL("http:bla")); |
1028 | 1028 |
1029 // Flush all pending requests. | 1029 // Flush all pending requests. |
1030 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1030 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
1031 | 1031 |
1032 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1032 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
1033 | 1033 |
1034 // Sorts out all the messages we saw by request. | 1034 // Sorts out all the messages we saw by request. |
1035 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1035 ResourceIPCAccumulator::ClassifiedMessages msgs; |
1036 accum_.GetClassifiedMessages(&msgs); | 1036 accum_.GetClassifiedMessages(&msgs); |
1037 ASSERT_EQ(1U, msgs.size()); | 1037 ASSERT_EQ(1U, msgs.size()); |
1038 | 1038 |
1039 ResourceResponseHead response_head; | 1039 content::ResourceResponseHead response_head; |
1040 GetResponseHead(msgs[0], &response_head); | 1040 GetResponseHead(msgs[0], &response_head); |
1041 ASSERT_EQ("image/jpeg", response_head.mime_type); | 1041 ASSERT_EQ("image/jpeg", response_head.mime_type); |
1042 } | 1042 } |
1043 | 1043 |
1044 // Tests that we don't sniff the mime type when there is no message body. | 1044 // Tests that we don't sniff the mime type when there is no message body. |
1045 TEST_F(ResourceDispatcherHostTest, MimeNotSniffed2) { | 1045 TEST_F(ResourceDispatcherHostTest, MimeNotSniffed2) { |
1046 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1046 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
1047 | 1047 |
1048 std::string response("HTTP/1.1 304 Not Modified\n\n"); | 1048 std::string response("HTTP/1.1 304 Not Modified\n\n"); |
1049 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | 1049 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), |
1050 response.size())); | 1050 response.size())); |
1051 std::string response_data; | 1051 std::string response_data; |
1052 SetResponse(raw_headers, response_data); | 1052 SetResponse(raw_headers, response_data); |
1053 | 1053 |
1054 HandleScheme("http"); | 1054 HandleScheme("http"); |
1055 MakeTestRequest(0, 1, GURL("http:bla")); | 1055 MakeTestRequest(0, 1, GURL("http:bla")); |
1056 | 1056 |
1057 // Flush all pending requests. | 1057 // Flush all pending requests. |
1058 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1058 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
1059 | 1059 |
1060 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1060 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
1061 | 1061 |
1062 // Sorts out all the messages we saw by request. | 1062 // Sorts out all the messages we saw by request. |
1063 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1063 ResourceIPCAccumulator::ClassifiedMessages msgs; |
1064 accum_.GetClassifiedMessages(&msgs); | 1064 accum_.GetClassifiedMessages(&msgs); |
1065 ASSERT_EQ(1U, msgs.size()); | 1065 ASSERT_EQ(1U, msgs.size()); |
1066 | 1066 |
1067 ResourceResponseHead response_head; | 1067 content::ResourceResponseHead response_head; |
1068 GetResponseHead(msgs[0], &response_head); | 1068 GetResponseHead(msgs[0], &response_head); |
1069 ASSERT_EQ("", response_head.mime_type); | 1069 ASSERT_EQ("", response_head.mime_type); |
1070 } | 1070 } |
1071 | 1071 |
1072 TEST_F(ResourceDispatcherHostTest, MimeSniff204) { | 1072 TEST_F(ResourceDispatcherHostTest, MimeSniff204) { |
1073 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1073 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
1074 | 1074 |
1075 std::string response("HTTP/1.1 204 No Content\n\n"); | 1075 std::string response("HTTP/1.1 204 No Content\n\n"); |
1076 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | 1076 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), |
1077 response.size())); | 1077 response.size())); |
1078 std::string response_data; | 1078 std::string response_data; |
1079 SetResponse(raw_headers, response_data); | 1079 SetResponse(raw_headers, response_data); |
1080 | 1080 |
1081 HandleScheme("http"); | 1081 HandleScheme("http"); |
1082 MakeTestRequest(0, 1, GURL("http:bla")); | 1082 MakeTestRequest(0, 1, GURL("http:bla")); |
1083 | 1083 |
1084 // Flush all pending requests. | 1084 // Flush all pending requests. |
1085 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1085 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
1086 | 1086 |
1087 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1087 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
1088 | 1088 |
1089 // Sorts out all the messages we saw by request. | 1089 // Sorts out all the messages we saw by request. |
1090 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1090 ResourceIPCAccumulator::ClassifiedMessages msgs; |
1091 accum_.GetClassifiedMessages(&msgs); | 1091 accum_.GetClassifiedMessages(&msgs); |
1092 ASSERT_EQ(1U, msgs.size()); | 1092 ASSERT_EQ(1U, msgs.size()); |
1093 | 1093 |
1094 ResourceResponseHead response_head; | 1094 content::ResourceResponseHead response_head; |
1095 GetResponseHead(msgs[0], &response_head); | 1095 GetResponseHead(msgs[0], &response_head); |
1096 ASSERT_EQ("text/plain", response_head.mime_type); | 1096 ASSERT_EQ("text/plain", response_head.mime_type); |
1097 } | 1097 } |
1098 | 1098 |
1099 // Tests for crbug.com/31266 (Non-2xx + application/octet-stream). | 1099 // Tests for crbug.com/31266 (Non-2xx + application/octet-stream). |
1100 TEST_F(ResourceDispatcherHostTest, ForbiddenDownload) { | 1100 TEST_F(ResourceDispatcherHostTest, ForbiddenDownload) { |
1101 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1101 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
1102 | 1102 |
1103 std::string response("HTTP/1.1 403 Forbidden\n" | 1103 std::string response("HTTP/1.1 403 Forbidden\n" |
1104 "Content-disposition: attachment; filename=blah\n" | 1104 "Content-disposition: attachment; filename=blah\n" |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1258 net::URLRequestStatus status; | 1258 net::URLRequestStatus status; |
1259 | 1259 |
1260 void* iter = NULL; | 1260 void* iter = NULL; |
1261 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &request_id)); | 1261 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &request_id)); |
1262 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &status)); | 1262 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &status)); |
1263 | 1263 |
1264 EXPECT_EQ(1, request_id); | 1264 EXPECT_EQ(1, request_id); |
1265 EXPECT_EQ(net::URLRequestStatus::FAILED, status.status()); | 1265 EXPECT_EQ(net::URLRequestStatus::FAILED, status.status()); |
1266 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, status.error()); | 1266 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, status.error()); |
1267 } | 1267 } |
OLD | NEW |