1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/search/iframe_source.h"
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/search/instant_io_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/resource_request_info.h"
14 #include "content/public/test/mock_resource_context.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "grit/browser_resources.h"
17 #include "ipc/ipc_message.h"
18 #include "net/base/request_priority.h"
19 #include "net/url_request/url_request.h"
20 #include "net/url_request/url_request_context.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
25 const int kNonInstantRendererPID
= 0;
26 const char kNonInstantOrigin
[] = "http://evil";
27 const int kInstantRendererPID
= 1;
28 const char kInstantOrigin
[] = "chrome-search://instant";
29 const int kInvalidRendererPID
= 42;
31 class TestIframeSource
: public IframeSource
{
33 using IframeSource::GetMimeType
;
34 using IframeSource::ShouldServiceRequest
;
35 using IframeSource::SendResource
;
36 using IframeSource::SendJSWithOrigin
;
39 virtual std::string
GetSource() const OVERRIDE
{
43 virtual bool ServesPath(const std::string
& path
) const OVERRIDE
{
44 return path
== "/valid.html" || path
== "/valid.js";
47 virtual void StartDataRequest(
48 const std::string
& path
,
49 int render_process_id
,
51 const content::URLDataSource::GotDataCallback
& callback
) OVERRIDE
{
54 // RenderFrameHost is hard to mock in concert with everything else, so stub
55 // this method out for testing.
56 virtual bool GetOrigin(
59 std::string
* origin
) const OVERRIDE
{
60 if (process_id
== kInstantRendererPID
) {
61 *origin
= kInstantOrigin
;
64 if (process_id
== kNonInstantRendererPID
) {
65 *origin
= kNonInstantOrigin
;
72 class IframeSourceTest
: public testing::Test
{
74 // net::URLRequest wants to be executed with a message loop that has TYPE_IO.
75 // InstantIOContext needs to be created on the UI thread and have everything
76 // else happen on the IO thread. This setup is a hacky way to satisfy all
79 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
80 resource_context_(&test_url_request_context_
),
81 instant_io_context_(NULL
),
85 TestIframeSource
* source() { return source_
.get(); }
87 std::string
response_string() {
88 if (response_
.get()) {
89 return std::string(reinterpret_cast<const char*>(response_
->front()),
95 net::URLRequest
* MockRequest(
96 const std::string
& url
,
98 int render_process_id
,
99 int render_frame_id
) {
100 net::URLRequest
* request
=
101 new net::URLRequest(GURL(url
),
102 net::DEFAULT_PRIORITY
,
104 resource_context_
.GetRequestContext());
106 content::ResourceRequestInfo::AllocateForTesting(request
,
107 ResourceType::SUB_FRAME
,
117 void SendResource(int resource_id
) {
118 source()->SendResource(resource_id
, callback_
);
121 void SendJSWithOrigin(
123 int render_process_id
,
124 int render_frame_id
) {
125 source()->SendJSWithOrigin(resource_id
, render_process_id
, render_frame_id
,
130 virtual void SetUp() OVERRIDE
{
131 source_
.reset(new TestIframeSource());
132 callback_
= base::Bind(&IframeSourceTest::SaveResponse
,
133 base::Unretained(this));
134 instant_io_context_
= new InstantIOContext
;
135 InstantIOContext::SetUserDataOnIO(&resource_context_
, instant_io_context_
);
136 InstantIOContext::AddInstantProcessOnIO(instant_io_context_
,
137 kInstantRendererPID
);
141 virtual void TearDown() {
145 void SaveResponse(base::RefCountedMemory
* data
) {
149 content::TestBrowserThreadBundle thread_bundle_
;
151 net::TestURLRequestContext test_url_request_context_
;
152 content::MockResourceContext resource_context_
;
153 scoped_ptr
<TestIframeSource
> source_
;
154 content::URLDataSource::GotDataCallback callback_
;
155 scoped_refptr
<InstantIOContext
> instant_io_context_
;
156 scoped_refptr
<base::RefCountedMemory
> response_
;
159 TEST_F(IframeSourceTest
, ShouldServiceRequest
) {
160 scoped_ptr
<net::URLRequest
> request
;
161 request
.reset(MockRequest("http://test/loader.js", true,
162 kNonInstantRendererPID
, 0));
163 EXPECT_FALSE(source()->ShouldServiceRequest(request
.get()));
164 request
.reset(MockRequest("chrome-search://bogus/valid.js", true,
165 kInstantRendererPID
, 0));
166 EXPECT_FALSE(source()->ShouldServiceRequest(request
.get()));
167 request
.reset(MockRequest("chrome-search://test/bogus.js", true,
168 kInstantRendererPID
, 0));
169 EXPECT_FALSE(source()->ShouldServiceRequest(request
.get()));
170 request
.reset(MockRequest("chrome-search://test/valid.js", true,
171 kInstantRendererPID
, 0));
172 EXPECT_TRUE(source()->ShouldServiceRequest(request
.get()));
173 request
.reset(MockRequest("chrome-search://test/valid.js", true,
174 kNonInstantRendererPID
, 0));
175 EXPECT_FALSE(source()->ShouldServiceRequest(request
.get()));
176 request
.reset(MockRequest("chrome-search://test/valid.js", true,
177 kInvalidRendererPID
, 0));
178 EXPECT_FALSE(source()->ShouldServiceRequest(request
.get()));
181 TEST_F(IframeSourceTest
, GetMimeType
) {
182 // URLDataManagerBackend does not include / in path_and_query.
183 EXPECT_EQ("text/html", source()->GetMimeType("foo.html"));
184 EXPECT_EQ("application/javascript", source()->GetMimeType("foo.js"));
185 EXPECT_EQ("text/css", source()->GetMimeType("foo.css"));
186 EXPECT_EQ("image/png", source()->GetMimeType("foo.png"));
187 EXPECT_EQ("", source()->GetMimeType("bogus"));
190 TEST_F(IframeSourceTest
, SendResource
) {
191 SendResource(IDR_MOST_VISITED_TITLE_HTML
);
192 EXPECT_FALSE(response_string().empty());
195 TEST_F(IframeSourceTest
, SendJSWithOrigin
) {
196 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS
, kInstantRendererPID
, 0);
197 EXPECT_FALSE(response_string().empty());
198 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS
, kNonInstantRendererPID
, 0);
199 EXPECT_FALSE(response_string().empty());
200 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS
, kInvalidRendererPID
, 0);
201 EXPECT_TRUE(response_string().empty());