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

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: Resolving Build Errors. 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/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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 net::URLRequest* request, 94 net::URLRequest* request,
93 bool is_content_initiated, 95 bool is_content_initiated,
94 bool must_download, 96 bool must_download,
95 uint32 id, 97 uint32 id,
96 scoped_ptr<DownloadSaveInfo> save_info, 98 scoped_ptr<DownloadSaveInfo> save_info,
97 const DownloadUrlParameters::OnStartedCallback& started_cb) override { 99 const DownloadUrlParameters::OnStartedCallback& started_cb) override {
98 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); 100 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
99 } 101 }
100 102
101 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( 103 scoped_ptr<ResourceHandler> MaybeInterceptAsStream(
104 const base::FilePath& plugin_path,
102 net::URLRequest* request, 105 net::URLRequest* request,
103 ResourceResponse* response, 106 ResourceResponse* response,
104 std::string* payload) override { 107 std::string* payload) override {
105 if (stream_has_handler_) { 108 if (stream_has_handler_) {
106 intercepted_as_stream_ = true; 109 intercepted_as_stream_ = true;
107 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); 110 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass();
108 } else { 111 } else {
109 return scoped_ptr<ResourceHandler>(); 112 return scoped_ptr<ResourceHandler>();
110 } 113 }
111 } 114 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 147
145 void CancelWithError(int error_code) override { 148 void CancelWithError(int error_code) override {
146 NOTREACHED(); 149 NOTREACHED();
147 } 150 }
148 151
149 void Resume() override { 152 void Resume() override {
150 NOTREACHED(); 153 NOTREACHED();
151 } 154 }
152 }; 155 };
153 156
157 class TestFakePluginService : public FakePluginService {
158 public:
159 explicit TestFakePluginService(bool plugin_available, bool is_plugin_stale)
mmenke 2015/03/25 14:11:44 nit: Explicit no longer needed.
Deepak 2015/03/26 04:55:12 Done.
160 : plugin_available_(plugin_available),
161 is_plugin_stale_(is_plugin_stale) {}
162
163 bool GetPluginInfo(int render_process_id,
164 int render_frame_id,
165 ResourceContext* context,
166 const GURL& url,
167 const GURL& page_url,
168 const std::string& mime_type,
169 bool allow_wildcard,
170 bool* is_stale,
171 WebPluginInfo* info,
172 std::string* actual_mime_type) override {
173 *is_stale = is_plugin_stale_;
174 if (!plugin_available_)
175 return false;
176 info->type = WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
177 info->path = base::FilePath::FromUTF8Unsafe(
178 std::string("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/"));
179 return true;
180 }
181
182 private:
183 const bool plugin_available_;
184 const bool is_plugin_stale_;
185 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService);
mmenke 2015/03/25 14:11:44 On 2015/03/24 14:44:32, mmenke wrote: > nit: Line
Deepak 2015/03/26 04:55:12 Done.
186 };
187
154 class BufferedResourceHandlerTest : public testing::Test { 188 class BufferedResourceHandlerTest : public testing::Test {
155 public: 189 public:
156 BufferedResourceHandlerTest() : stream_has_handler_(false) {} 190 BufferedResourceHandlerTest()
191 : stream_has_handler_(false),
192 plugin_available_(false),
193 plugin_stale_(false) {}
157 194
158 void set_stream_has_handler(bool stream_has_handler) { 195 void set_stream_has_handler(bool stream_has_handler) {
159 stream_has_handler_ = stream_has_handler; 196 stream_has_handler_ = stream_has_handler;
160 } 197 }
161 198
199 void set_plugin_available(bool plugin_available) {
200 plugin_available_ = plugin_available;
201 }
202
203 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; }
204
162 bool TestStreamIsIntercepted(bool allow_download, 205 bool TestStreamIsIntercepted(bool allow_download,
163 bool must_download, 206 bool must_download,
164 ResourceType request_resource_type); 207 ResourceType request_resource_type);
165 208
166 private: 209 private:
167 // Whether the URL request should be intercepted as a stream. 210 // Whether the URL request should be intercepted as a stream.
168 bool stream_has_handler_; 211 bool stream_has_handler_;
212 bool plugin_available_;
213 bool plugin_stale_;
169 214
170 TestBrowserThreadBundle thread_bundle_; 215 TestBrowserThreadBundle thread_bundle_;
171 }; 216 };
172 217
173 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( 218 bool BufferedResourceHandlerTest::TestStreamIsIntercepted(
174 bool allow_download, 219 bool allow_download,
175 bool must_download, 220 bool must_download,
176 ResourceType request_resource_type) { 221 ResourceType request_resource_type) {
177 net::URLRequestContext context; 222 net::URLRequestContext context;
178 scoped_ptr<net::URLRequest> request(context.CreateRequest( 223 scoped_ptr<net::URLRequest> request(context.CreateRequest(
179 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); 224 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
180 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; 225 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME;
181 ResourceRequestInfo::AllocateForTesting( 226 ResourceRequestInfo::AllocateForTesting(
182 request.get(), 227 request.get(),
183 request_resource_type, 228 request_resource_type,
184 nullptr, // context 229 nullptr, // context
185 0, // render_process_id 230 0, // render_process_id
186 0, // render_view_id 231 0, // render_view_id
187 0, // render_frame_id 232 0, // render_frame_id
188 is_main_frame, // is_main_frame 233 is_main_frame, // is_main_frame
189 false, // parent_is_main_frame 234 false, // parent_is_main_frame
190 allow_download, // allow_download 235 allow_download, // allow_download
191 true); // is_async 236 true); // is_async
192 237
193 TestResourceDispatcherHost host(stream_has_handler_); 238 TestResourceDispatcherHost host(stream_has_handler_);
194 TestResourceDispatcherHostDelegate host_delegate(must_download); 239 TestResourceDispatcherHostDelegate host_delegate(must_download);
195 host.SetDelegate(&host_delegate); 240 host.SetDelegate(&host_delegate);
196 241
197 FakePluginService plugin_service; 242 TestFakePluginService plugin_service(plugin_available_, plugin_stale_);
198 scoped_ptr<ResourceHandler> buffered_handler( 243 scoped_ptr<ResourceHandler> buffered_handler(
199 new BufferedResourceHandler( 244 new BufferedResourceHandler(
200 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), 245 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(),
201 &host, 246 &host,
202 &plugin_service, 247 &plugin_service,
203 request.get())); 248 request.get()));
204 TestResourceController resource_controller; 249 TestResourceController resource_controller;
205 buffered_handler->SetController(&resource_controller); 250 buffered_handler->SetController(&resource_controller);
206 251
207 scoped_refptr<ResourceResponse> response(new ResourceResponse); 252 scoped_refptr<ResourceResponse> response(new ResourceResponse);
(...skipping 12 matching lines...) Expand all
220 // circumstances. 265 // circumstances.
221 TEST_F(BufferedResourceHandlerTest, StreamHandling) { 266 TEST_F(BufferedResourceHandlerTest, StreamHandling) {
222 bool allow_download; 267 bool allow_download;
223 bool must_download; 268 bool must_download;
224 ResourceType resource_type; 269 ResourceType resource_type;
225 270
226 // Ensure the stream is handled by MaybeInterceptAsStream in the 271 // Ensure the stream is handled by MaybeInterceptAsStream in the
227 // ResourceDispatcherHost. 272 // ResourceDispatcherHost.
228 set_stream_has_handler(true); 273 set_stream_has_handler(true);
229 274
275 set_plugin_available(true);
276 set_plugin_stale(false);
mmenke 2015/03/25 14:11:44 optional nit: This is the default, so no need to
Deepak 2015/03/26 04:55:11 Done.
230 // Main frame request with no download allowed. Stream shouldn't be 277 // Main frame request with no download allowed. Stream shouldn't be
231 // intercepted. 278 // intercepted.
232 allow_download = false; 279 allow_download = false;
233 must_download = false; 280 must_download = false;
234 resource_type = RESOURCE_TYPE_MAIN_FRAME; 281 resource_type = RESOURCE_TYPE_MAIN_FRAME;
235 EXPECT_FALSE( 282 EXPECT_FALSE(
236 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 283 TestStreamIsIntercepted(allow_download, must_download, resource_type));
237 284
238 // Main frame request with download allowed. Stream should be intercepted. 285 // Main frame request with download allowed. Stream should be intercepted.
239 allow_download = true; 286 allow_download = true;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 must_download = false; 319 must_download = false;
273 resource_type = RESOURCE_TYPE_OBJECT; 320 resource_type = RESOURCE_TYPE_OBJECT;
274 EXPECT_FALSE( 321 EXPECT_FALSE(
275 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 322 TestStreamIsIntercepted(allow_download, must_download, resource_type));
276 323
277 allow_download = true; 324 allow_download = true;
278 must_download = false; 325 must_download = false;
279 resource_type = RESOURCE_TYPE_MAIN_FRAME; 326 resource_type = RESOURCE_TYPE_MAIN_FRAME;
280 EXPECT_FALSE( 327 EXPECT_FALSE(
281 TestStreamIsIntercepted(allow_download, must_download, resource_type)); 328 TestStreamIsIntercepted(allow_download, must_download, resource_type));
329
330 // Test the cases where the stream handled by MaybeInterceptAsStream
331 // with plugin not available. This is the case when intercepting streams for
332 // the streamsPrivate extensions API.
333 set_stream_has_handler(true);
334 allow_download = false;
335 must_download = false;
336 resource_type = RESOURCE_TYPE_OBJECT;
337 set_plugin_available(false);
338 set_plugin_stale(false);
339 EXPECT_TRUE(
340 TestStreamIsIntercepted(allow_download, must_download, resource_type));
341
342 // Test the cases where the stream handled by MaybeInterceptAsStream
343 // with plugin not available. This is the case when intercepting streams for
344 // the streamsPrivate extensions API with stale plugin.
345 set_stream_has_handler(true);
346 allow_download = false;
347 must_download = false;
348 set_plugin_available(false);
349 set_plugin_stale(true);
350 resource_type = RESOURCE_TYPE_OBJECT;
351 EXPECT_FALSE(
352 TestStreamIsIntercepted(allow_download, must_download, resource_type));
mmenke 2015/03/25 14:11:44 I'm not familiar with this test fixture, but is th
raymes 2015/03/25 23:32:08 If we understood how the resume request process wo
Deepak 2015/03/26 04:55:11 It appears,in actual scenerio GetPlugins() in plug
mmenke 2015/03/26 14:59:27 So we can just override FakePluginService::GetPlug
282 } 353 }
283 354
284 } // namespace 355 } // namespace
285 356
286 } // namespace content 357 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698