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

Side by Side Diff: content/browser/loader/buffered_resource_handler_unittest.cc

Issue 953793003: Ensuring interception of stream get determined by plugin path before checking mime type. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/loader/buffered_resource_handler.h" 5 #include "content/browser/loader/buffered_resource_handler.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "content/browser/loader/resource_dispatcher_host_impl.h" 10 #include "content/browser/loader/resource_dispatcher_host_impl.h"
11 #include "content/public/browser/resource_controller.h" 11 #include "content/public/browser/resource_controller.h"
12 #include "content/public/browser/resource_dispatcher_host_delegate.h" 12 #include "content/public/browser/resource_dispatcher_host_delegate.h"
13 #include "content/public/browser/resource_request_info.h" 13 #include "content/public/browser/resource_request_info.h"
14 #include "content/public/common/resource_response.h" 14 #include "content/public/common/resource_response.h"
15 #include "content/public/common/webplugininfo.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 16 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "content/public/test/test_utils.h" 17 #include "content/public/test/test_utils.h"
17 #include "content/test/fake_plugin_service.h" 18 #include "content/test/fake_plugin_service.h"
18 #include "net/url_request/url_request_context.h" 19 #include "net/url_request/url_request_context.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h" 21 #include "url/gurl.h"
21 22
22 namespace content { 23 namespace content {
23 24
24 namespace { 25 namespace {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 net::URLRequest* request, 93 net::URLRequest* request,
93 bool is_content_initiated, 94 bool is_content_initiated,
94 bool must_download, 95 bool must_download,
95 uint32 id, 96 uint32 id,
96 scoped_ptr<DownloadSaveInfo> save_info, 97 scoped_ptr<DownloadSaveInfo> save_info,
97 const DownloadUrlParameters::OnStartedCallback& started_cb) override { 98 const DownloadUrlParameters::OnStartedCallback& started_cb) override {
98 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); 99 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
99 } 100 }
100 101
101 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( 102 scoped_ptr<ResourceHandler> MaybeInterceptAsStream(
103 const base::FilePath& plugin_path,
102 net::URLRequest* request, 104 net::URLRequest* request,
103 ResourceResponse* response, 105 ResourceResponse* response,
104 std::string* payload) override { 106 std::string* payload) override {
105 if (stream_has_handler_) { 107 if (stream_has_handler_) {
106 intercepted_as_stream_ = true; 108 intercepted_as_stream_ = true;
107 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); 109 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
108 } else { 110 } else {
109 return scoped_ptr<ResourceHandler>(); 111 return scoped_ptr<ResourceHandler>();
110 } 112 }
111 } 113 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 146
145 void CancelWithError(int error_code) override { 147 void CancelWithError(int error_code) override {
146 NOTREACHED(); 148 NOTREACHED();
147 } 149 }
148 150
149 void Resume() override { 151 void Resume() override {
150 NOTREACHED(); 152 NOTREACHED();
151 } 153 }
152 }; 154 };
153 155
156 class TestFakePluginService : public FakePluginService {
157 public:
158 explicit TestFakePluginService(bool plugin_available)
159 : plugin_available_(plugin_available) {}
mmenke 2015/03/24 14:44:32 nit: Blank line after constructor.
Deepak 2015/03/25 05:43:34 Done.
160 bool GetPluginInfo(int render_process_id,
161 int render_frame_id,
162 ResourceContext* context,
163 const GURL& url,
164 const GURL& page_url,
165 const std::string& mime_type,
166 bool allow_wildcard,
167 bool* is_stale,
168 WebPluginInfo* info,
169 std::string* actual_mime_type) override;
170
171 private:
172 bool plugin_available_;
mmenke 2015/03/24 14:44:32 nit: const?
Deepak 2015/03/25 05:43:35 Done.
173 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService);
mmenke 2015/03/24 14:44:32 nit: Line break before DISALLOW_COPY_AND_ASSIGN
Deepak 2015/03/25 05:43:35 Done.
174 };
175
176 bool TestFakePluginService::GetPluginInfo(int render_process_id,
177 int render_frame_id,
178 ResourceContext* context,
179 const GURL& url,
180 const GURL& page_url,
181 const std::string& mime_type,
182 bool allow_wildcard,
183 bool* is_stale,
184 WebPluginInfo* info,
185 std::string* actual_mime_type) {
mmenke 2015/03/24 14:44:32 optional: May want to just inline this method, gi
Deepak 2015/03/25 05:43:35 Done.
186 *is_stale = false;
187 if (!plugin_available_)
188 return false;
189 info->type = WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
190 info->path = base::FilePath::FromUTF8Unsafe(std::string(
mmenke 2015/03/24 14:44:32 include base::FilePath
Deepak 2015/03/25 05:43:34 Done.
191 "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/"));
192 return true;
193 }
194
154 class BufferedResourceHandlerTest : public testing::Test { 195 class BufferedResourceHandlerTest : public testing::Test {
155 public: 196 public:
156 BufferedResourceHandlerTest() : stream_has_handler_(false) {} 197 BufferedResourceHandlerTest()
198 : stream_has_handler_(false), plugin_available_(false) {}
157 199
158 void set_stream_has_handler(bool stream_has_handler) { 200 void set_stream_has_handler(bool stream_has_handler) {
159 stream_has_handler_ = stream_has_handler; 201 stream_has_handler_ = stream_has_handler;
160 } 202 }
161 203
204 void set_plugin_available(bool plugin_available) {
205 plugin_available_ = plugin_available;
206 }
207
162 bool TestStreamIsIntercepted(bool allow_download, 208 bool TestStreamIsIntercepted(bool allow_download,
163 bool must_download, 209 bool must_download,
164 ResourceType request_resource_type); 210 ResourceType request_resource_type);
165 211
166 private: 212 private:
167 // Whether the URL request should be intercepted as a stream. 213 // Whether the URL request should be intercepted as a stream.
168 bool stream_has_handler_; 214 bool stream_has_handler_;
215 bool plugin_available_;
169 216
170 TestBrowserThreadBundle thread_bundle_; 217 TestBrowserThreadBundle thread_bundle_;
171 }; 218 };
172 219
173 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( 220 bool BufferedResourceHandlerTest::TestStreamIsIntercepted(
174 bool allow_download, 221 bool allow_download,
175 bool must_download, 222 bool must_download,
176 ResourceType request_resource_type) { 223 ResourceType request_resource_type) {
177 net::URLRequestContext context; 224 net::URLRequestContext context;
178 scoped_ptr<net::URLRequest> request(context.CreateRequest( 225 scoped_ptr<net::URLRequest> request(context.CreateRequest(
179 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); 226 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
180 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; 227 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME;
181 ResourceRequestInfo::AllocateForTesting( 228 ResourceRequestInfo::AllocateForTesting(
182 request.get(), 229 request.get(),
183 request_resource_type, 230 request_resource_type,
184 nullptr, // context 231 nullptr, // context
185 0, // render_process_id 232 0, // render_process_id
186 0, // render_view_id 233 0, // render_view_id
187 0, // render_frame_id 234 0, // render_frame_id
188 is_main_frame, // is_main_frame 235 is_main_frame, // is_main_frame
189 false, // parent_is_main_frame 236 false, // parent_is_main_frame
190 allow_download, // allow_download 237 allow_download, // allow_download
191 true); // is_async 238 true); // is_async
192 239
193 TestResourceDispatcherHost host(stream_has_handler_); 240 TestResourceDispatcherHost host(stream_has_handler_);
194 TestResourceDispatcherHostDelegate host_delegate(must_download); 241 TestResourceDispatcherHostDelegate host_delegate(must_download);
195 host.SetDelegate(&host_delegate); 242 host.SetDelegate(&host_delegate);
196 243
197 FakePluginService plugin_service; 244 TestFakePluginService plugin_service(plugin_available_);
198 scoped_ptr<ResourceHandler> buffered_handler( 245 scoped_ptr<ResourceHandler> buffered_handler(
199 new BufferedResourceHandler( 246 new BufferedResourceHandler(
200 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), 247 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(),
201 &host, 248 &host,
202 &plugin_service, 249 &plugin_service,
203 request.get())); 250 request.get()));
204 TestResourceController resource_controller; 251 TestResourceController resource_controller;
205 buffered_handler->SetController(&resource_controller); 252 buffered_handler->SetController(&resource_controller);
206 253
207 scoped_refptr<ResourceResponse> response(new ResourceResponse); 254 scoped_refptr<ResourceResponse> response(new ResourceResponse);
(...skipping 12 matching lines...) Expand all
220 // circumstances. 267 // circumstances.
221 TEST_F(BufferedResourceHandlerTest, StreamHandling) { 268 TEST_F(BufferedResourceHandlerTest, StreamHandling) {
222 bool allow_download; 269 bool allow_download;
223 bool must_download; 270 bool must_download;
224 ResourceType resource_type; 271 ResourceType resource_type;
225 272
226 // Ensure the stream is handled by MaybeInterceptAsStream in the 273 // Ensure the stream is handled by MaybeInterceptAsStream in the
227 // ResourceDispatcherHost. 274 // ResourceDispatcherHost.
228 set_stream_has_handler(true); 275 set_stream_has_handler(true);
229 276
277 set_plugin_available(true);
230 // Main frame request with no download allowed. Stream shouldn't be 278 // Main frame request with no download allowed. Stream shouldn't be
231 // intercepted. 279 // intercepted.
232 allow_download = false; 280 allow_download = false;
233 must_download = false; 281 must_download = false;
234 resource_type = RESOURCE_TYPE_MAIN_FRAME; 282 resource_type = RESOURCE_TYPE_MAIN_FRAME;
235 EXPECT_FALSE( 283 EXPECT_FALSE(
236 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 284 TestStreamIsIntercepted(allow_download, must_download, resource_type));
237 285
238 // Main frame request with download allowed. Stream should be intercepted. 286 // Main frame request with download allowed. Stream should be intercepted.
239 allow_download = true; 287 allow_download = true;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 must_download = false; 320 must_download = false;
273 resource_type = RESOURCE_TYPE_OBJECT; 321 resource_type = RESOURCE_TYPE_OBJECT;
274 EXPECT_FALSE( 322 EXPECT_FALSE(
275 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 323 TestStreamIsIntercepted(allow_download, must_download, resource_type));
276 324
277 allow_download = true; 325 allow_download = true;
278 must_download = false; 326 must_download = false;
279 resource_type = RESOURCE_TYPE_MAIN_FRAME; 327 resource_type = RESOURCE_TYPE_MAIN_FRAME;
280 EXPECT_FALSE( 328 EXPECT_FALSE(
281 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 329 TestStreamIsIntercepted(allow_download, must_download, resource_type));
330
331 // Test the cases where the stream handled by MaybeInterceptAsStream
332 // with plugin not available. This is the case when intercepting streams for
333 // the streamsPrivate extensions API.
334 set_stream_has_handler(true);
335 allow_download = false;
336 must_download = false;
337 resource_type = RESOURCE_TYPE_OBJECT;
338 set_plugin_available(false);
339 EXPECT_TRUE(
340 TestStreamIsIntercepted(allow_download, must_download, resource_type));
mmenke 2015/03/24 14:44:32 Should we also have a similar test where is_stale
Deepak 2015/03/25 05:43:34 Done.
282 } 341 }
283 342
284 } // namespace 343 } // namespace
285 344
286 } // namespace content 345 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698