blob: 228633b0abce8768755b50e319aa2dc559e4f045 [file] [log] [blame]
[email protected]f90bf0d92011-01-13 02:12:441// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include <string>
6#include <vector>
7
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/scoped_ptr.h"
[email protected]2602087e2009-08-24 23:12:169#include "base/message_loop.h"
[email protected]4003d7142009-01-12 12:56:2010#include "base/process.h"
[email protected]a68114f72009-11-30 23:32:4911#include "base/process_util.h"
[email protected]940895b2011-08-20 00:50:0512#include "content/common/request_extra_data.h"
[email protected]620161e2011-03-07 18:05:2613#include "content/common/resource_dispatcher.h"
[email protected]94dc971d2011-03-05 19:08:3214#include "content/common/resource_messages.h"
[email protected]620161e2011-03-07 18:05:2615#include "content/common/resource_response.h"
[email protected]939856a2010-08-24 20:29:0216#include "net/base/upload_data.h"
[email protected]7a4de7a62010-08-17 18:38:2417#include "net/http/http_response_headers.h"
initial.commit09911bf2008-07-26 23:55:2918#include "testing/gtest/include/gtest/gtest.h"
[email protected]f430b5712009-08-21 21:46:3119#include "webkit/appcache/appcache_interfaces.h"
initial.commit09911bf2008-07-26 23:55:2920
21using webkit_glue::ResourceLoaderBridge;
[email protected]b4b3a912010-10-08 19:05:3722using webkit_glue::ResourceResponseInfo;
initial.commit09911bf2008-07-26 23:55:2923
24static const char test_page_url[] = "http://www.google.com/";
25static const char test_page_headers[] =
26 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
27static const char test_page_mime_type[] = "text/html";
28static const char test_page_charset[] = "";
29static const char test_page_contents[] =
30 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
[email protected]b5ab3982010-02-16 23:58:2731static const uint32 test_page_contents_len = arraysize(test_page_contents) - 1;
initial.commit09911bf2008-07-26 23:55:2932
[email protected]b8901082010-11-12 01:14:2833static const char kShmemSegmentName[] = "DeferredResourceLoaderTest";
34
initial.commit09911bf2008-07-26 23:55:2935// Listens for request response data and stores it so that it can be compared
36// to the reference data.
37class TestRequestCallback : public ResourceLoaderBridge::Peer {
38 public:
39 TestRequestCallback() : complete_(false) {
40 }
41
[email protected]bb551622010-07-22 20:52:4942 virtual void OnUploadProgress(uint64 position, uint64 size) {
43 }
44
[email protected]6568a9e32009-07-30 18:01:3945 virtual bool OnReceivedRedirect(
46 const GURL& new_url,
[email protected]b4b3a912010-10-08 19:05:3747 const ResourceResponseInfo& info,
[email protected]041b0bbb2009-11-18 02:27:3448 bool* has_new_first_party_for_cookies,
[email protected]2581e572009-11-13 21:54:5549 GURL* new_first_party_for_cookies) {
[email protected]041b0bbb2009-11-18 02:27:3450 *has_new_first_party_for_cookies = false;
[email protected]6568a9e32009-07-30 18:01:3951 return true;
initial.commit09911bf2008-07-26 23:55:2952 }
53
[email protected]b9e8ea62011-03-04 06:29:0954 virtual void OnReceivedResponse(const ResourceResponseInfo& info) {
initial.commit09911bf2008-07-26 23:55:2955 }
56
[email protected]bb551622010-07-22 20:52:4957 virtual void OnDownloadedData(int len) {
58 }
59
[email protected]8bd0de72011-04-08 18:52:1060 virtual void OnReceivedData(const char* data,
61 int data_length,
[email protected]dfd682172011-04-13 19:57:2562 int encoded_data_length) {
initial.commit09911bf2008-07-26 23:55:2963 EXPECT_FALSE(complete_);
[email protected]8bd0de72011-04-08 18:52:1064 data_.append(data, data_length);
[email protected]dfd682172011-04-13 19:57:2565 total_encoded_data_length_ += encoded_data_length;
initial.commit09911bf2008-07-26 23:55:2966 }
67
[email protected]f90bf0d92011-01-13 02:12:4468 virtual void OnCompletedRequest(const net::URLRequestStatus& status,
[email protected]e0f458c2010-09-16 04:50:4169 const std::string& security_info,
70 const base::Time& completion_time) {
initial.commit09911bf2008-07-26 23:55:2971 EXPECT_FALSE(complete_);
72 complete_ = true;
73 }
74
[email protected]8bd0de72011-04-08 18:52:1075 bool complete() const {
76 return complete_;
77 }
initial.commit09911bf2008-07-26 23:55:2978 const std::string& data() const {
79 return data_;
80 }
[email protected]dfd682172011-04-13 19:57:2581 int total_encoded_data_length() const {
82 return total_encoded_data_length_;
initial.commit09911bf2008-07-26 23:55:2983 }
84
85 private:
86 bool complete_;
87 std::string data_;
[email protected]dfd682172011-04-13 19:57:2588 int total_encoded_data_length_;
initial.commit09911bf2008-07-26 23:55:2989};
90
91
92// Sets up the message sender override for the unit test
93class ResourceDispatcherTest : public testing::Test,
94 public IPC::Message::Sender {
95 public:
96 // Emulates IPC send operations (IPC::Message::Sender) by adding
97 // pending messages to the queue.
98 virtual bool Send(IPC::Message* msg) {
99 message_queue_.push_back(IPC::Message(*msg));
100 delete msg;
101 return true;
102 }
103
104 // Emulates the browser process and processes the pending IPC messages,
105 // returning the hardcoded file contents.
106 void ProcessMessages() {
107 while (!message_queue_.empty()) {
initial.commit09911bf2008-07-26 23:55:29108 int request_id;
[email protected]94dc971d2011-03-05 19:08:32109 ResourceHostMsg_Request request;
110 ASSERT_TRUE(ResourceHostMsg_RequestResource::Read(
[email protected]eb9989092009-03-12 21:42:52111 &message_queue_[0], &request_id, &request));
initial.commit09911bf2008-07-26 23:55:29112
113 // check values
114 EXPECT_EQ(test_page_url, request.url.spec());
115
116 // received response message
[email protected]e09ba552009-02-05 03:26:29117 ResourceResponseHead response;
initial.commit09911bf2008-07-26 23:55:29118 std::string raw_headers(test_page_headers);
119 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
120 response.headers = new net::HttpResponseHeaders(raw_headers);
121 response.mime_type = test_page_mime_type;
122 response.charset = test_page_charset;
initial.commit09911bf2008-07-26 23:55:29123 dispatcher_->OnReceivedResponse(request_id, response);
124
125 // received data message with the test contents
[email protected]176aa482008-11-14 03:25:15126 base::SharedMemory shared_mem;
[email protected]54e3dfa22010-10-27 18:16:06127 EXPECT_TRUE(shared_mem.CreateAndMapAnonymous(test_page_contents_len));
initial.commit09911bf2008-07-26 23:55:29128 char* put_data_here = static_cast<char*>(shared_mem.memory());
129 memcpy(put_data_here, test_page_contents, test_page_contents_len);
[email protected]176aa482008-11-14 03:25:15130 base::SharedMemoryHandle dup_handle;
[email protected]4003d7142009-01-12 12:56:20131 EXPECT_TRUE(shared_mem.GiveToProcess(
132 base::Process::Current().handle(), &dup_handle));
[email protected]eb9989092009-03-12 21:42:52133 dispatcher_->OnReceivedData(
[email protected]8bd0de72011-04-08 18:52:10134 message_queue_[0],
135 request_id,
136 dup_handle,
137 test_page_contents_len,
138 test_page_contents_len);
initial.commit09911bf2008-07-26 23:55:29139
140 message_queue_.erase(message_queue_.begin());
141
142 // read the ack message.
[email protected]c2fe31542009-05-20 18:24:14143 Tuple1<int> request_ack;
[email protected]94dc971d2011-03-05 19:08:32144 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
[email protected]eb9989092009-03-12 21:42:52145 &message_queue_[0], &request_ack));
initial.commit09911bf2008-07-26 23:55:29146
[email protected]c2fe31542009-05-20 18:24:14147 ASSERT_EQ(request_ack.a, request_id);
initial.commit09911bf2008-07-26 23:55:29148
149 message_queue_.erase(message_queue_.begin());
150 }
151 }
152
153 protected:
initial.commit09911bf2008-07-26 23:55:29154 // testing::Test
155 virtual void SetUp() {
[email protected]eb9989092009-03-12 21:42:52156 dispatcher_.reset(new ResourceDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29157 }
158 virtual void TearDown() {
[email protected]eb9989092009-03-12 21:42:52159 dispatcher_.reset();
initial.commit09911bf2008-07-26 23:55:29160 }
161
[email protected]2602087e2009-08-24 23:12:16162 ResourceLoaderBridge* CreateBridge() {
[email protected]46b0d4a2009-12-19 00:46:33163 webkit_glue::ResourceLoaderBridge::RequestInfo request_info;
164 request_info.method = "GET";
165 request_info.url = GURL(test_page_url);
166 request_info.first_party_for_cookies = GURL(test_page_url);
167 request_info.referrer = GURL();
[email protected]46b0d4a2009-12-19 00:46:33168 request_info.headers = std::string();
169 request_info.load_flags = 0;
170 request_info.requestor_pid = 0;
171 request_info.request_type = ResourceType::SUB_RESOURCE;
[email protected]52bbcd932010-01-06 18:56:12172 request_info.appcache_host_id = appcache::kNoHostId;
173 request_info.routing_id = 0;
[email protected]940895b2011-08-20 00:50:05174 RequestExtraData extra_data(true, 0);
175 request_info.extra_data = &extra_data;
[email protected]46b0d4a2009-12-19 00:46:33176
[email protected]50285ff2011-03-11 23:10:56177 return dispatcher_->CreateBridge(request_info);
[email protected]2602087e2009-08-24 23:12:16178 }
179
initial.commit09911bf2008-07-26 23:55:29180 std::vector<IPC::Message> message_queue_;
[email protected]eb9989092009-03-12 21:42:52181 static scoped_ptr<ResourceDispatcher> dispatcher_;
initial.commit09911bf2008-07-26 23:55:29182};
183
184/*static*/
[email protected]eb9989092009-03-12 21:42:52185scoped_ptr<ResourceDispatcher> ResourceDispatcherTest::dispatcher_;
initial.commit09911bf2008-07-26 23:55:29186
187// Does a simple request and tests that the correct data is received.
188TEST_F(ResourceDispatcherTest, RoundTrip) {
189 TestRequestCallback callback;
[email protected]2602087e2009-08-24 23:12:16190 ResourceLoaderBridge* bridge = CreateBridge();
initial.commit09911bf2008-07-26 23:55:29191
192 bridge->Start(&callback);
193
194 ProcessMessages();
195
196 // FIXME(brettw) when the request complete messages are actually handledo
197 // and dispatched, uncomment this.
198 //EXPECT_TRUE(callback.complete());
199 //EXPECT_STREQ(test_page_contents, callback.data().c_str());
[email protected]dfd682172011-04-13 19:57:25200 //EXPECT_EQ(test_page_contents_len, callback.total_encoded_data_length());
initial.commit09911bf2008-07-26 23:55:29201
202 delete bridge;
203}
204
205// Tests that the request IDs are straight when there are multiple requests.
206TEST_F(ResourceDispatcherTest, MultipleRequests) {
207 // FIXME
208}
209
210// Tests that the cancel method prevents other messages from being received
211TEST_F(ResourceDispatcherTest, Cancel) {
212 // FIXME
213}
214
215TEST_F(ResourceDispatcherTest, Cookies) {
216 // FIXME
217}
218
219TEST_F(ResourceDispatcherTest, SerializedPostData) {
220 // FIXME
221}
[email protected]2602087e2009-08-24 23:12:16222
223// This class provides functionality to validate whether the ResourceDispatcher
224// object honors the deferred loading contract correctly, i.e. if deferred
225// loading is enabled it should queue up any responses received. If deferred
226// loading is enabled/disabled in the context of a dispatched message, other
227// queued messages should not be dispatched until deferred load is turned off.
228class DeferredResourceLoadingTest : public ResourceDispatcherTest,
229 public ResourceLoaderBridge::Peer {
230 public:
231 DeferredResourceLoadingTest()
232 : defer_loading_(false) {
233 }
234
235 virtual bool Send(IPC::Message* msg) {
236 delete msg;
237 return true;
238 }
239
240 void InitMessages() {
241 set_defer_loading(true);
242
243 ResourceResponseHead response_head;
[email protected]f90bf0d92011-01-13 02:12:44244 response_head.status.set_status(net::URLRequestStatus::SUCCESS);
[email protected]2602087e2009-08-24 23:12:16245
246 IPC::Message* response_message =
[email protected]94dc971d2011-03-05 19:08:32247 new ResourceMsg_ReceivedResponse(0, 0, response_head);
[email protected]2602087e2009-08-24 23:12:16248
249 dispatcher_->OnMessageReceived(*response_message);
250
251 delete response_message;
252
[email protected]a68114f72009-11-30 23:32:49253 // Duplicate the shared memory handle so both the test and the callee can
254 // close their copy.
255 base::SharedMemoryHandle duplicated_handle;
256 EXPECT_TRUE(shared_handle_.ShareToProcess(base::GetCurrentProcessHandle(),
257 &duplicated_handle));
258
[email protected]2602087e2009-08-24 23:12:16259 response_message =
[email protected]8bd0de72011-04-08 18:52:10260 new ResourceMsg_DataReceived(0, 0, duplicated_handle, 100, 100);
[email protected]2602087e2009-08-24 23:12:16261
262 dispatcher_->OnMessageReceived(*response_message);
263
264 delete response_message;
265
266 set_defer_loading(false);
267 }
268
269 // ResourceLoaderBridge::Peer methods.
[email protected]bb551622010-07-22 20:52:49270 virtual void OnUploadProgress(uint64 position, uint64 size) {
[email protected]2602087e2009-08-24 23:12:16271 }
272
273 virtual bool OnReceivedRedirect(
274 const GURL& new_url,
[email protected]b4b3a912010-10-08 19:05:37275 const ResourceResponseInfo& info,
[email protected]041b0bbb2009-11-18 02:27:34276 bool* has_new_first_party_for_cookies,
[email protected]2581e572009-11-13 21:54:55277 GURL* new_first_party_for_cookies) {
[email protected]041b0bbb2009-11-18 02:27:34278 *has_new_first_party_for_cookies = false;
[email protected]2602087e2009-08-24 23:12:16279 return true;
280 }
281
[email protected]b9e8ea62011-03-04 06:29:09282 virtual void OnReceivedResponse(const ResourceResponseInfo& info) {
[email protected]bb551622010-07-22 20:52:49283 EXPECT_EQ(defer_loading_, false);
284 set_defer_loading(true);
285 }
286
287 virtual void OnDownloadedData(int len) {
288 }
289
[email protected]8bd0de72011-04-08 18:52:10290 virtual void OnReceivedData(const char* data,
291 int data_length,
[email protected]dfd682172011-04-13 19:57:25292 int encoded_data_length) {
[email protected]2602087e2009-08-24 23:12:16293 EXPECT_EQ(defer_loading_, false);
294 set_defer_loading(false);
295 }
296
[email protected]f90bf0d92011-01-13 02:12:44297 virtual void OnCompletedRequest(const net::URLRequestStatus& status,
[email protected]e0f458c2010-09-16 04:50:41298 const std::string& security_info,
299 const base::Time& completion_time) {
[email protected]2602087e2009-08-24 23:12:16300 }
301
[email protected]2602087e2009-08-24 23:12:16302 protected:
303 virtual void SetUp() {
[email protected]2602087e2009-08-24 23:12:16304 ResourceDispatcherTest::SetUp();
[email protected]b8901082010-11-12 01:14:28305 shared_handle_.Delete(kShmemSegmentName);
[email protected]515f2492011-01-14 10:36:28306 EXPECT_TRUE(shared_handle_.CreateNamed(kShmemSegmentName, false, 100));
[email protected]2602087e2009-08-24 23:12:16307 }
308
309 virtual void TearDown() {
310 shared_handle_.Close();
[email protected]b8901082010-11-12 01:14:28311 EXPECT_TRUE(shared_handle_.Delete(kShmemSegmentName));
[email protected]2602087e2009-08-24 23:12:16312 ResourceDispatcherTest::TearDown();
313 }
314
315 private:
316 void set_defer_loading(bool defer) {
317 defer_loading_ = defer;
318 dispatcher_->SetDefersLoading(0, defer);
319 }
320
321 bool defer_loading() const {
322 return defer_loading_;
323 }
324
325 bool defer_loading_;
326 base::SharedMemory shared_handle_;
327};
328
329TEST_F(DeferredResourceLoadingTest, DeferredLoadTest) {
330 MessageLoop message_loop(MessageLoop::TYPE_IO);
331
332 ResourceLoaderBridge* bridge = CreateBridge();
333
334 bridge->Start(this);
335 InitMessages();
336
337 // Dispatch deferred messages.
338 message_loop.RunAllPending();
339 delete bridge;
340}