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

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: Changes as per review comments. Created 5 years, 8 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/files/file_path.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/macros.h" 9 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
10 #include "content/browser/loader/resource_dispatcher_host_impl.h" 11 #include "content/browser/loader/resource_dispatcher_host_impl.h"
11 #include "content/public/browser/resource_controller.h" 12 #include "content/public/browser/resource_controller.h"
12 #include "content/public/browser/resource_dispatcher_host_delegate.h" 13 #include "content/public/browser/resource_dispatcher_host_delegate.h"
13 #include "content/public/browser/resource_request_info.h" 14 #include "content/public/browser/resource_request_info.h"
14 #include "content/public/common/resource_response.h" 15 #include "content/public/common/resource_response.h"
16 #include "content/public/common/webplugininfo.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "content/public/test/test_utils.h" 18 #include "content/public/test/test_utils.h"
17 #include "content/test/fake_plugin_service.h" 19 #include "content/test/fake_plugin_service.h"
18 #include "net/url_request/url_request_context.h" 20 #include "net/url_request/url_request_context.h"
19 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h" 22 #include "url/gurl.h"
21 23
22 namespace content { 24 namespace content {
23 25
24 namespace { 26 namespace {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 } 79 }
78 80
79 private: 81 private:
80 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); 82 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler);
81 }; 83 };
82 84
83 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { 85 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
84 public: 86 public:
85 explicit TestResourceDispatcherHost(bool stream_has_handler) 87 explicit TestResourceDispatcherHost(bool stream_has_handler)
86 : stream_has_handler_(stream_has_handler), 88 : stream_has_handler_(stream_has_handler),
87 intercepted_as_stream_(false) {} 89 intercepted_as_stream_(false),
90 intercepted_as_stream_count(0) {}
88 91
89 bool intercepted_as_stream() const { return intercepted_as_stream_; } 92 bool intercepted_as_stream() const { return intercepted_as_stream_; }
90 93
91 scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload( 94 scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload(
92 net::URLRequest* request, 95 net::URLRequest* request,
93 bool is_content_initiated, 96 bool is_content_initiated,
94 bool must_download, 97 bool must_download,
95 uint32 id, 98 uint32 id,
96 scoped_ptr<DownloadSaveInfo> save_info, 99 scoped_ptr<DownloadSaveInfo> save_info,
97 const DownloadUrlParameters::OnStartedCallback& started_cb) override { 100 const DownloadUrlParameters::OnStartedCallback& started_cb) override {
98 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); 101 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
99 } 102 }
100 103
101 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( 104 scoped_ptr<ResourceHandler> MaybeInterceptAsStream(
105 const base::FilePath& plugin_path,
102 net::URLRequest* request, 106 net::URLRequest* request,
103 ResourceResponse* response, 107 ResourceResponse* response,
104 std::string* payload) override { 108 std::string* payload) override {
109 intercepted_as_stream_count++;
105 if (stream_has_handler_) { 110 if (stream_has_handler_) {
106 intercepted_as_stream_ = true; 111 intercepted_as_stream_ = true;
107 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); 112 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
108 } else { 113 } else {
109 return scoped_ptr<ResourceHandler>(); 114 return scoped_ptr<ResourceHandler>();
110 } 115 }
111 } 116 }
112 117
118 int GetInterceptAsStream() { return intercepted_as_stream_count; }
mmenke 2015/04/07 14:52:51 GetInterceptAsStream -> intercepted_as_stream_coun
mmenke 2015/04/07 14:52:51 nit: const
119
113 private: 120 private:
114 // Whether the URL request should be intercepted as a stream. 121 // Whether the URL request should be intercepted as a stream.
115 bool stream_has_handler_; 122 bool stream_has_handler_;
116 123
117 // Whether the URL request has been intercepted as a stream. 124 // Whether the URL request has been intercepted as a stream.
118 bool intercepted_as_stream_; 125 bool intercepted_as_stream_;
126
127 // Count of number of times MaybeInterceptAsStream function get called in a
128 // test.
129 int intercepted_as_stream_count;
mmenke 2015/04/07 14:52:51 nit: intercepted_as_stream_count_
Deepak 2015/04/07 15:27:44 On 2015/04/07 14:52:51, mmenke wrote: Done.
119 }; 130 };
120 131
121 class TestResourceDispatcherHostDelegate 132 class TestResourceDispatcherHostDelegate
122 : public ResourceDispatcherHostDelegate { 133 : public ResourceDispatcherHostDelegate {
123 public: 134 public:
124 TestResourceDispatcherHostDelegate(bool must_download) 135 TestResourceDispatcherHostDelegate(bool must_download)
125 : must_download_(must_download) { 136 : must_download_(must_download) {
126 } 137 }
127 138
128 bool ShouldForceDownloadResource(const GURL& url, 139 bool ShouldForceDownloadResource(const GURL& url,
(...skipping 15 matching lines...) Expand all
144 155
145 void CancelWithError(int error_code) override { 156 void CancelWithError(int error_code) override {
146 NOTREACHED(); 157 NOTREACHED();
147 } 158 }
148 159
149 void Resume() override { 160 void Resume() override {
150 NOTREACHED(); 161 NOTREACHED();
151 } 162 }
152 }; 163 };
153 164
165 class TestFakePluginService : public FakePluginService {
166 public:
167 TestFakePluginService(bool plugin_available, bool is_plugin_stale)
mmenke 2015/04/07 14:52:51 Should comment about is_plugin_stale behavior.
Deepak 2015/04/07 15:27:44 On 2015/04/07 14:52:51, mmenke wrote: Done.
168 : plugin_available_(plugin_available),
169 is_plugin_stale_(is_plugin_stale) {}
170
171 bool GetPluginInfo(int render_process_id,
172 int render_frame_id,
173 ResourceContext* context,
174 const GURL& url,
175 const GURL& page_url,
176 const std::string& mime_type,
177 bool allow_wildcard,
178 bool* is_stale,
179 WebPluginInfo* info,
180 std::string* actual_mime_type) override {
181 *is_stale = is_plugin_stale_;
182 if (!is_plugin_stale_ || !plugin_available_)
183 return false;
184 info->type = WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
185 info->path = base::FilePath::FromUTF8Unsafe(
186 std::string("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/"));
187 return true;
188 }
189
190 void GetPlugins(const GetPluginsCallback& callback) override {
191 is_plugin_stale_ = false;
192 scoped_refptr<base::MessageLoopProxy> target_loop(
193 base::MessageLoop::current()->message_loop_proxy());
mmenke 2015/04/07 14:52:51 No longer needed.
Deepak 2015/04/07 15:27:44 On 2015/04/07 14:52:51, mmenke wrote: Done.
194 std::vector<WebPluginInfo> plugins;
195 base::MessageLoop::current()->PostTask(FROM_HERE,
196 base::Bind(callback, plugins));
197 }
198
199 private:
200 const bool plugin_available_;
201 bool is_plugin_stale_;
202
203 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService);
204 };
205
154 class BufferedResourceHandlerTest : public testing::Test { 206 class BufferedResourceHandlerTest : public testing::Test {
155 public: 207 public:
156 BufferedResourceHandlerTest() : stream_has_handler_(false) {} 208 BufferedResourceHandlerTest()
209 : stream_has_handler_(false),
210 plugin_available_(false),
211 plugin_stale_(false) {}
157 212
158 void set_stream_has_handler(bool stream_has_handler) { 213 void set_stream_has_handler(bool stream_has_handler) {
159 stream_has_handler_ = stream_has_handler; 214 stream_has_handler_ = stream_has_handler;
160 } 215 }
161 216
217 void set_plugin_available(bool plugin_available) {
218 plugin_available_ = plugin_available;
219 }
220
221 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; }
222
162 bool TestStreamIsIntercepted(bool allow_download, 223 bool TestStreamIsIntercepted(bool allow_download,
163 bool must_download, 224 bool must_download,
164 ResourceType request_resource_type); 225 ResourceType request_resource_type);
165 226
166 private: 227 private:
167 // Whether the URL request should be intercepted as a stream. 228 // Whether the URL request should be intercepted as a stream.
168 bool stream_has_handler_; 229 bool stream_has_handler_;
230 bool plugin_available_;
231 bool plugin_stale_;
169 232
170 TestBrowserThreadBundle thread_bundle_; 233 TestBrowserThreadBundle thread_bundle_;
171 }; 234 };
172 235
173 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( 236 bool BufferedResourceHandlerTest::TestStreamIsIntercepted(
174 bool allow_download, 237 bool allow_download,
175 bool must_download, 238 bool must_download,
176 ResourceType request_resource_type) { 239 ResourceType request_resource_type) {
177 net::URLRequestContext context; 240 net::URLRequestContext context;
178 scoped_ptr<net::URLRequest> request(context.CreateRequest( 241 scoped_ptr<net::URLRequest> request(context.CreateRequest(
179 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); 242 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
180 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; 243 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME;
181 ResourceRequestInfo::AllocateForTesting( 244 ResourceRequestInfo::AllocateForTesting(
182 request.get(), 245 request.get(),
183 request_resource_type, 246 request_resource_type,
184 nullptr, // context 247 nullptr, // context
185 0, // render_process_id 248 0, // render_process_id
186 0, // render_view_id 249 0, // render_view_id
187 0, // render_frame_id 250 0, // render_frame_id
188 is_main_frame, // is_main_frame 251 is_main_frame, // is_main_frame
189 false, // parent_is_main_frame 252 false, // parent_is_main_frame
190 allow_download, // allow_download 253 allow_download, // allow_download
191 true); // is_async 254 true); // is_async
192 255
193 TestResourceDispatcherHost host(stream_has_handler_); 256 TestResourceDispatcherHost host(stream_has_handler_);
194 TestResourceDispatcherHostDelegate host_delegate(must_download); 257 TestResourceDispatcherHostDelegate host_delegate(must_download);
195 host.SetDelegate(&host_delegate); 258 host.SetDelegate(&host_delegate);
196 259
197 FakePluginService plugin_service; 260 TestFakePluginService plugin_service(plugin_available_, plugin_stale_);
198 scoped_ptr<ResourceHandler> buffered_handler( 261 scoped_ptr<ResourceHandler> buffered_handler(
199 new BufferedResourceHandler( 262 new BufferedResourceHandler(
200 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), 263 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(),
201 &host, 264 &host,
202 &plugin_service, 265 &plugin_service,
203 request.get())); 266 request.get()));
204 TestResourceController resource_controller; 267 TestResourceController resource_controller;
205 buffered_handler->SetController(&resource_controller); 268 buffered_handler->SetController(&resource_controller);
206 269
207 scoped_refptr<ResourceResponse> response(new ResourceResponse); 270 scoped_refptr<ResourceResponse> response(new ResourceResponse);
208 // The MIME type isn't important but it shouldn't be empty. 271 // The MIME type isn't important but it shouldn't be empty.
209 response->head.mime_type = "application/pdf"; 272 response->head.mime_type = "application/pdf";
210 273
211 bool defer = false; 274 bool defer = false;
212 buffered_handler->OnResponseStarted(response.get(), &defer); 275 buffered_handler->OnResponseStarted(response.get(), &defer);
213 276
214 content::RunAllPendingInMessageLoop(); 277 content::RunAllPendingInMessageLoop();
215 278 EXPECT_TRUE(host.GetInterceptAsStream() < 2);
mmenke 2015/04/07 14:52:51 Prefer EXPECT_LT, since it prints out more useful
Deepak 2015/04/07 15:27:44 On 2015/04/07 14:52:51, mmenke wrote: Done.
Deepak 2015/04/07 15:27:44 On 2015/04/07 14:52:51, mmenke wrote: Done.
216 return host.intercepted_as_stream(); 279 return host.intercepted_as_stream();
217 } 280 }
218 281
219 // Test that stream requests are correctly intercepted under the right 282 // Test that stream requests are correctly intercepted under the right
220 // circumstances. 283 // circumstances. Test is not relevent for Android OS as plugin is disabled and
284 // always returns false.
mmenke 2015/04/07 14:52:51 nit: "as plugins are disabled"
Deepak 2015/04/07 15:27:44 On 2015/04/07 14:52:51, mmenke wrote: Done.
285 #if !defined(OS_ANDROID)
mmenke 2015/04/07 14:52:51 Can we use "#if defined(ENABLE_PLUGINS)" instead (
Deepak 2015/04/07 15:27:44 On 2015/04/07 14:52:51, mmenke wrote: Done.
221 TEST_F(BufferedResourceHandlerTest, StreamHandling) { 286 TEST_F(BufferedResourceHandlerTest, StreamHandling) {
222 bool allow_download; 287 bool allow_download;
223 bool must_download; 288 bool must_download;
224 ResourceType resource_type; 289 ResourceType resource_type;
225 290
226 // Ensure the stream is handled by MaybeInterceptAsStream in the 291 // Ensure the stream is handled by MaybeInterceptAsStream in the
227 // ResourceDispatcherHost. 292 // ResourceDispatcherHost.
228 set_stream_has_handler(true); 293 set_stream_has_handler(true);
294 set_plugin_available(true);
229 295
230 // Main frame request with no download allowed. Stream shouldn't be 296 // Main frame request with no download allowed. Stream shouldn't be
231 // intercepted. 297 // intercepted.
232 allow_download = false; 298 allow_download = false;
233 must_download = false; 299 must_download = false;
234 resource_type = RESOURCE_TYPE_MAIN_FRAME; 300 resource_type = RESOURCE_TYPE_MAIN_FRAME;
235 EXPECT_FALSE( 301 EXPECT_FALSE(
236 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 302 TestStreamIsIntercepted(allow_download, must_download, resource_type));
237 303
238 // Main frame request with download allowed. Stream should be intercepted. 304 // Main frame request with download allowed. Stream should be intercepted.
(...skipping 21 matching lines...) Expand all
260 // Object request with download not allowed. Stream should be intercepted. 326 // Object request with download not allowed. Stream should be intercepted.
261 allow_download = false; 327 allow_download = false;
262 must_download = false; 328 must_download = false;
263 resource_type = RESOURCE_TYPE_OBJECT; 329 resource_type = RESOURCE_TYPE_OBJECT;
264 EXPECT_TRUE( 330 EXPECT_TRUE(
265 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 331 TestStreamIsIntercepted(allow_download, must_download, resource_type));
266 332
267 // Test the cases where the stream isn't handled by MaybeInterceptAsStream 333 // Test the cases where the stream isn't handled by MaybeInterceptAsStream
268 // in the ResourceDispatcherHost. 334 // in the ResourceDispatcherHost.
269 set_stream_has_handler(false); 335 set_stream_has_handler(false);
270
271 allow_download = false; 336 allow_download = false;
272 must_download = false; 337 must_download = false;
273 resource_type = RESOURCE_TYPE_OBJECT; 338 resource_type = RESOURCE_TYPE_OBJECT;
274 EXPECT_FALSE( 339 EXPECT_FALSE(
275 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 340 TestStreamIsIntercepted(allow_download, must_download, resource_type));
276 341
277 allow_download = true; 342 allow_download = true;
278 must_download = false; 343 must_download = false;
279 resource_type = RESOURCE_TYPE_MAIN_FRAME; 344 resource_type = RESOURCE_TYPE_MAIN_FRAME;
280 EXPECT_FALSE( 345 EXPECT_FALSE(
281 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 346 TestStreamIsIntercepted(allow_download, must_download, resource_type));
347
348 // Test the cases where the stream handled by MaybeInterceptAsStream
349 // with plugin not available. This is the case when intercepting streams for
350 // the streamsPrivate extensions API.
351 set_stream_has_handler(true);
352 set_plugin_available(false);
353 allow_download = false;
354 must_download = false;
355 resource_type = RESOURCE_TYPE_OBJECT;
356 EXPECT_TRUE(
357 TestStreamIsIntercepted(allow_download, must_download, resource_type));
358
359 // Test the cases where the stream handled by MaybeInterceptAsStream
360 // with plugin not available. This is the case when intercepting streams for
361 // the streamsPrivate extensions API with stale plugin.
362 set_plugin_stale(true);
363 allow_download = false;
364 must_download = false;
365 resource_type = RESOURCE_TYPE_OBJECT;
366 EXPECT_TRUE(
367 TestStreamIsIntercepted(allow_download, must_download, resource_type));
282 } 368 }
369 #endif
283 370
284 } // namespace 371 } // namespace
285 372
286 } // namespace content 373 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/buffered_resource_handler.cc ('k') | content/browser/loader/resource_dispatcher_host_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698