summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/file_browser_resource_throttle_unittest.cc
blob: 7456c46fb4e306a6b1e09688a97ef4c294c03a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/message_loop.h"
#include "chrome/browser/chromeos/extensions/file_browser_resource_throttle.h"
#include "chrome/browser/extensions/extension_info_map.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_builder.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/file_browser_handler.h"
#include "chrome/common/extensions/value_builder.h"
#include "content/public/browser/resource_controller.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using content::BrowserThread;
using extensions::DictionaryBuilder;
using extensions::Extension;
using extensions::ExtensionBuilder;
using extensions::ListBuilder;
using testing::_;

namespace {

// Mock file browser handler event router to be used in the test.
class MockFileBrowserHandlerEventRouter
    : public FileBrowserResourceThrottle::FileBrowserHandlerEventRouter {
 public:
  virtual ~MockFileBrowserHandlerEventRouter() {}

  MOCK_METHOD5(DispatchMimeTypeHandlerEvent,
               void(int render_process_id,
                    int render_process_view,
                    const std::string& mime_type,
                    const GURL& request_url,
                    const std::string& extension_id));
};

// Resource controller to be used in the tests.
class MockResourceController : public content::ResourceController {
 public:
  virtual ~MockResourceController() {}
  MOCK_METHOD0(Cancel, void());
  MOCK_METHOD0(CancelAndIgnore, void());
  MOCK_METHOD1(CancelWithError, void(int error_code));
  MOCK_METHOD0(Resume, void());
};

class FileBrowserResourceThrottleTest : public testing::Test {
 public:
  typedef FileBrowserResourceThrottle::FileBrowserHandlerEventRouter
          HandlerEventRouter;

  FileBrowserResourceThrottleTest()
      : test_extension_id_("test_extension_id"),
        test_render_process_id_(2),
        test_render_view_id_(12),
        test_request_url_("http://some_url/file.txt"),
        ui_thread_(content::BrowserThread::UI, &message_loop_),
        io_thread_(content::BrowserThread::IO, &message_loop_) {
  }

  virtual ~FileBrowserResourceThrottleTest() {}

  virtual void SetUp() {
    // Extension info map must be created before |CreateAndIstallTestExtension|
    // is called (the method will add created extension to the info map).
    extension_info_map_ = new ExtensionInfoMap();
    CreateAndInstallTestExtension();
    InitResourceController();
  }

  virtual void TearDown() {
    FileBrowserHandler::set_extension_whitelisted_for_test(NULL);
  }

 protected:
  // Creates the test extension, and adds it to the |extension_info_map_|.
  // The extension has separate file browser handlers that can handle
  // 'plain/html' and 'plain/text' MIME types.
  void CreateAndInstallTestExtension() {
    // The extension must be white-listed in order to be successfully created.
    FileBrowserHandler::set_extension_whitelisted_for_test(
        &test_extension_id_);

    extension_ =
        ExtensionBuilder()
        .SetManifest(DictionaryBuilder()
                     .Set("name", "file browser handler test")
                     .Set("version", "1.0.0")
                     .Set("manifest_version", 2)
                     .Set("file_browser_handlers", ListBuilder()
                         .Append(DictionaryBuilder()
                             // Handler that handles 'plain/html', among others.
                             .Set("id", "ID_handle_html")
                             .Set("default_title", "Default title")
                             .Set("default_icon", "icon.png")
                             // file_filters_field is mandatory, even though
                             // it's not used in the tests.
                             .Set("file_filters", ListBuilder()
                                 .Append("filesystem:*.html"))
                             .Set("mime_types", ListBuilder()
                                 .Append("random/mime1")
                                 .Append("random/mime2")
                                 .Append("plain/html")))
                         .Append(DictionaryBuilder()
                             // Handler that handles only 'plain/text'.
                             .Set("id", "ID_handle_text")
                             .Set("default_title", "Default title")
                             .Set("default_icon", "icon.png")
                             // file_filters_field is mandatory, even though
                             // it's not used in the tests.
                             .Set("file_filters", ListBuilder()
                                 .Append("filesystem:*.txt"))
                             .Set("mime_types", ListBuilder()
                                 .Append("plain/text")))))
        .SetID(test_extension_id_)
        .Build();

    // 'Install' the extension.
    extension_info_map_->AddExtension(extension_,
                                      base::Time(),  // install time
                                      true);  // enable_incognito
  }

  // Initiates the default mock_resource_controller_ expectations.
  // By the default, resource controller should not be called at all.
  void InitResourceController() {
    EXPECT_CALL(mock_resource_controller_, Cancel()).Times(0);
    EXPECT_CALL(mock_resource_controller_, CancelAndIgnore()).Times(0);
    EXPECT_CALL(mock_resource_controller_, CancelWithError(_)).Times(0);
    EXPECT_CALL(mock_resource_controller_, Resume()).Times(0);
  }

  // Removes the test extension to |extension_info_map_|'s disabled extensions.
  void DisableTestExtension() {
    extension_info_map_->RemoveExtension(test_extension_id_,
                                         extension_misc::UNLOAD_REASON_DISABLE);
  }

  void ReloadTestExtensionIncognitoDisabled() {
    extension_info_map_->RemoveExtension(
        test_extension_id_, extension_misc::UNLOAD_REASON_UNINSTALL);
    extension_info_map_->AddExtension(extension_,
                                      base::Time(),  // install_time
                                      false);  // enable incognito
  }

  // Creates the resource throttle that should be tested.
  // It's setup with |mock_event_router| passed to the method and
  // |mock_resource_throttle_|.
  // |mock_event_router|'s expectations must be set before calling this method.
  scoped_ptr<FileBrowserResourceThrottle> CreateThrottleToTest(
      bool is_incognito,
      scoped_ptr<MockFileBrowserHandlerEventRouter> mock_event_router,
      const std::string& mime_type) {
    scoped_ptr<HandlerEventRouter> event_router(mock_event_router.release());
    scoped_ptr<FileBrowserResourceThrottle> test_throttle(
        FileBrowserResourceThrottle::CreateForTest(test_render_process_id_,
                                                   test_render_view_id_,
                                                   mime_type,
                                                   test_request_url_,
                                                   is_incognito,
                                                   extension_info_map_.get(),
                                                   event_router.Pass()));

    test_throttle->set_controller_for_testing(&mock_resource_controller_);

    return test_throttle.Pass();
  }

  // The test extension's id.
  std::string test_extension_id_;
  // The test extension.
  scoped_refptr<const Extension> extension_;

  // The extension info map used in the test. The extensions are considered
  // installed/disabled depending on the extension_info_map_ state.
  scoped_refptr<ExtensionInfoMap> extension_info_map_;

  MockResourceController mock_resource_controller_;

  // Parameters used to create the test resource throttle. Unlike, mime_type
  // these can be selected at random.
  int test_render_process_id_;
  int test_render_view_id_;
  GURL test_request_url_;

 private:
  // ExtensionInfoMap needs IO thread.
  MessageLoop message_loop_;
  content::TestBrowserThread ui_thread_;
  content::TestBrowserThread io_thread_;
};

// Tests that the request gets canceled (mock_resource_controller_.Cancel() is
// called) and the event_router is invoked when a white-listed extension has a
// file browser handler that can handle the request's MIME type.
TEST_F(FileBrowserResourceThrottleTest, HandlerWhiteListed) {
  EXPECT_CALL(mock_resource_controller_, CancelAndIgnore()).Times(1);

  scoped_ptr<MockFileBrowserHandlerEventRouter> mock_event_router(
      new MockFileBrowserHandlerEventRouter());
  EXPECT_CALL(*mock_event_router,
      DispatchMimeTypeHandlerEvent(test_render_process_id_,
                                   test_render_view_id_,
                                   "plain/html",
                                   test_request_url_,
                                   test_extension_id_))
      .Times(1);

  scoped_ptr<FileBrowserResourceThrottle> throttle(
      CreateThrottleToTest(false, mock_event_router.Pass(), "plain/html"));

  bool defer = false;
  throttle->WillProcessResponse(&defer);
  EXPECT_FALSE(defer);
}

// Tests that the request gets canceled (mock_resource_controller_.Cancel() is
// called) and the event_router is invoked when a white-listed extension has a
// file browser handler that can handle the request's MIME type, even when the
// file browser handler is not first in the extension's file browser handler
// list.
TEST_F(FileBrowserResourceThrottleTest, SecondHandlerWhiteListed) {
  EXPECT_CALL(mock_resource_controller_, CancelAndIgnore()).Times(1);

  scoped_ptr<MockFileBrowserHandlerEventRouter> mock_event_router(
      new MockFileBrowserHandlerEventRouter());
  EXPECT_CALL(*mock_event_router,
      DispatchMimeTypeHandlerEvent(test_render_process_id_,
                                   test_render_view_id_,
                                   "plain/text",
                                   test_request_url_,
                                   test_extension_id_))
      .Times(1);

  scoped_ptr<FileBrowserResourceThrottle> throttle(
      CreateThrottleToTest(false, mock_event_router.Pass(), "plain/text"));

  bool defer = false;
  throttle->WillProcessResponse(&defer);
  EXPECT_FALSE(defer);
}

// Tests that the request is not canceled and the event router is not invoked
// if there is no file browser handlers registered for the request's MIME type.
TEST_F(FileBrowserResourceThrottleTest, NoWhiteListedHandler) {
  scoped_ptr<MockFileBrowserHandlerEventRouter> mock_event_router(
      new MockFileBrowserHandlerEventRouter());
  EXPECT_CALL(*mock_event_router, DispatchMimeTypeHandlerEvent(_, _, _, _, _))
      .Times(0);

  scoped_ptr<FileBrowserResourceThrottle> throttle(
      CreateThrottleToTest(false, mock_event_router.Pass(),
                           "random_mime_type"));

  bool defer = false;
  throttle->WillProcessResponse(&defer);
  EXPECT_FALSE(defer);
}

// Tests that the request is not canceled and the event router is not invoked
// if there is an extension with the file browser handler that can handle the
// request's MIME type, but the extension is disabled.
TEST_F(FileBrowserResourceThrottleTest, HandlerWhiteListedAndDisabled) {
  DisableTestExtension();

  scoped_ptr<MockFileBrowserHandlerEventRouter> mock_event_router(
      new MockFileBrowserHandlerEventRouter());
  EXPECT_CALL(*mock_event_router, DispatchMimeTypeHandlerEvent(_, _, _, _, _))
      .Times(0);

  scoped_ptr<FileBrowserResourceThrottle> throttle(
      CreateThrottleToTest(false, mock_event_router.Pass(), "plain/text"));

  bool defer = false;
  throttle->WillProcessResponse(&defer);
  EXPECT_FALSE(defer);
}

// Tests that the request is not canceled and the event router is not invoked
// in incognito mode if the extension that handles the request's MIME type is
// not incognito enabled.
TEST_F(FileBrowserResourceThrottleTest, IncognitoExtensionNotEnabled) {
  ReloadTestExtensionIncognitoDisabled();

  scoped_ptr<MockFileBrowserHandlerEventRouter> mock_event_router(
      new MockFileBrowserHandlerEventRouter());
  EXPECT_CALL(*mock_event_router, DispatchMimeTypeHandlerEvent(_, _, _, _, _))
      .Times(0);

  scoped_ptr<FileBrowserResourceThrottle> throttle(
      CreateThrottleToTest(true, mock_event_router.Pass(), "plain/text"));

  bool defer = false;
  throttle->WillProcessResponse(&defer);
  EXPECT_FALSE(defer);
}

// Tests that the request gets canceled (mock_resource_controller_.Cancel() is
// called) and the event_router is invoked in incognito when a white-listed
// extension that handles request's MIME type is incognito enabled.
TEST_F(FileBrowserResourceThrottleTest, IncognitoExtensionEnabled) {
  EXPECT_CALL(mock_resource_controller_, CancelAndIgnore()).Times(1);

  scoped_ptr<MockFileBrowserHandlerEventRouter> mock_event_router(
      new MockFileBrowserHandlerEventRouter());
  EXPECT_CALL(*mock_event_router,
      DispatchMimeTypeHandlerEvent(test_render_process_id_,
                                   test_render_view_id_,
                                   "plain/html",
                                   test_request_url_,
                                   test_extension_id_))
      .Times(1);

  scoped_ptr<FileBrowserResourceThrottle> throttle(
      CreateThrottleToTest(true, mock_event_router.Pass(), "plain/html"));

  bool defer = false;
  throttle->WillProcessResponse(&defer);
  EXPECT_FALSE(defer);
}

}  // namespace