summaryrefslogtreecommitdiff
path: root/chromium/content/browser/loader/mime_sniffing_resource_handler_unittest.cc
blob: 92a8a1fca76519caf57d34f99a5fb77efd5ef542 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
// Copyright 2014 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 "content/browser/loader/mime_sniffing_resource_handler.h"

#include <stdint.h>

#include <memory>

#include "base/files/file_path.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/browser/loader/intercepting_resource_handler.h"
#include "content/browser/loader/resource_dispatcher_host_impl.h"
#include "content/browser/loader/test_resource_handler.h"
#include "content/public/browser/resource_controller.h"
#include "content/public/browser/resource_dispatcher_host_delegate.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/common/resource_response.h"
#include "content/public/common/webplugininfo.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "content/test/fake_plugin_service.h"
#include "net/url_request/url_request_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace content {

namespace {

class TestResourceDispatcherHostDelegate
    : public ResourceDispatcherHostDelegate {
 public:
  explicit TestResourceDispatcherHostDelegate(bool must_download)
      : must_download_(must_download) {}

  bool ShouldForceDownloadResource(const GURL& url,
                                   const std::string& mime_type) override {
    return must_download_;
  }

 private:
  const bool must_download_;
};

class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
 public:
  explicit TestResourceDispatcherHost(bool stream_has_handler)
      : stream_has_handler_(stream_has_handler),
        intercepted_as_stream_(false),
        intercepted_as_stream_count_(0),
        new_resource_handler_(nullptr) {}

  bool intercepted_as_stream() const { return intercepted_as_stream_; }

  std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload(
      net::URLRequest* request,
      bool is_content_initiated,
      bool must_download,
      bool is_new_request) override {
    return CreateNewResourceHandler();
  }

  std::unique_ptr<ResourceHandler> MaybeInterceptAsStream(
      const base::FilePath& plugin_path,
      net::URLRequest* request,
      ResourceResponse* response,
      std::string* payload) override {
    intercepted_as_stream_count_++;
    if (stream_has_handler_)
      intercepted_as_stream_ = true;
    return CreateNewResourceHandler();
  }

  int intercepted_as_stream_count() const {
    return intercepted_as_stream_count_;
  }

  TestResourceHandler* new_resource_handler() const {
    return new_resource_handler_;
  }

 private:
  std::unique_ptr<ResourceHandler> CreateNewResourceHandler() {
    std::unique_ptr<TestResourceHandler> new_resource_handler(
        new TestResourceHandler());
    new_resource_handler->set_on_response_started_result(false);
    new_resource_handler_ = new_resource_handler.get();
    return std::move(new_resource_handler);
  }

  // Whether the URL request should be intercepted as a stream.
  bool stream_has_handler_;

  // Whether the URL request has been intercepted as a stream.
  bool intercepted_as_stream_;

  // Count of number of times MaybeInterceptAsStream function get called in a
  // test.
  int intercepted_as_stream_count_;

  // The last alternative TestResourceHandler created by this
  // TestResourceDispatcherHost.
  TestResourceHandler* new_resource_handler_;
};

class TestFakePluginService : public FakePluginService {
 public:
  // If |is_plugin_stale| is true, GetPluginInfo will indicate the plugins are
  // stale until GetPlugins is called.
  TestFakePluginService(bool plugin_available, bool is_plugin_stale)
      : plugin_available_(plugin_available),
        is_plugin_stale_(is_plugin_stale) {}

  bool GetPluginInfo(int render_process_id,
                     int render_frame_id,
                     ResourceContext* context,
                     const GURL& url,
                     const url::Origin& main_frame_origin,
                     const std::string& mime_type,
                     bool allow_wildcard,
                     bool* is_stale,
                     WebPluginInfo* info,
                     std::string* actual_mime_type) override {
    *is_stale = is_plugin_stale_;
    if (!is_plugin_stale_ || !plugin_available_)
      return false;
    info->type = WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
    info->path = base::FilePath::FromUTF8Unsafe(
        std::string("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/"));
    return true;
  }

  void GetPlugins(const GetPluginsCallback& callback) override {
    is_plugin_stale_ = false;
    std::vector<WebPluginInfo> plugins;
    base::ThreadTaskRunnerHandle::Get()->PostTask(
        FROM_HERE, base::Bind(callback, plugins));
  }

 private:
  const bool plugin_available_;
  bool is_plugin_stale_;

  DISALLOW_COPY_AND_ASSIGN(TestFakePluginService);
};

class TestResourceController : public ResourceController {
 public:
  TestResourceController() : cancel_call_count_(0), resume_call_count_(0) {}

  void Cancel() override { cancel_call_count_++; }

  void CancelAndIgnore() override { NOTREACHED(); }

  void CancelWithError(int error_code) override { NOTREACHED(); }

  void Resume() override { resume_call_count_++; }

  int cancel_call_count() const { return cancel_call_count_; }
  int resume_call_count() const { return resume_call_count_; }

 private:
  int cancel_call_count_;
  int resume_call_count_;
};

}  // namespace

class MimeSniffingResourceHandlerTest : public testing::Test {
 public:
  MimeSniffingResourceHandlerTest()
      : stream_has_handler_(false),
        plugin_available_(false),
        plugin_stale_(false) {}

  // Tests that the MimeSniffingHandler properly sets the accept field in the
  // header. Returns the accept header value.
  std::string TestAcceptHeaderSetting(ResourceType request_resource_type);
  std::string TestAcceptHeaderSettingWithURLRequest(
      ResourceType request_resource_type,
      net::URLRequest* request);

  void set_stream_has_handler(bool stream_has_handler) {
    stream_has_handler_ = stream_has_handler;
  }

  void set_plugin_available(bool plugin_available) {
    plugin_available_ = plugin_available;
  }

  void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; }

  bool TestStreamIsIntercepted(bool allow_download,
                               bool must_download,
                               ResourceType request_resource_type);

  // Tests the operation of the MimeSniffingHandler when it needs to buffer
  // data (example case: the response is text/plain).
  void TestHandlerSniffing(bool response_started,
                           bool defer_response_started,
                           bool will_read,
                           bool read_completed,
                           bool defer_read_completed);

  // Tests the operation of the MimeSniffingHandler when it doesn't buffer
  // data (example case: the response is text/html).
  void TestHandlerNoSniffing(bool response_started,
                             bool defer_response_started,
                             bool will_read,
                             bool read_completed,
                             bool defer_read_completed);

 private:
  // Whether the URL request should be intercepted as a stream.
  bool stream_has_handler_;
  bool plugin_available_;
  bool plugin_stale_;

  TestBrowserThreadBundle thread_bundle_;
};

std::string MimeSniffingResourceHandlerTest::TestAcceptHeaderSetting(
    ResourceType request_resource_type) {
  net::URLRequestContext context;
  std::unique_ptr<net::URLRequest> request(context.CreateRequest(
      GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
  return TestAcceptHeaderSettingWithURLRequest(request_resource_type,
                                               request.get());
}

std::string
MimeSniffingResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest(
    ResourceType request_resource_type,
    net::URLRequest* request) {
  bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME;
  ResourceRequestInfo::AllocateForTesting(request, request_resource_type,
                                          nullptr,        // context
                                          0,              // render_process_id
                                          0,              // render_view_id
                                          0,              // render_frame_id
                                          is_main_frame,  // is_main_frame
                                          false,   // parent_is_main_frame
                                          false,   // allow_download
                                          true,    // is_async
                                          false);  // is_using_lofi

  std::unique_ptr<TestResourceHandler> scoped_test_handler(
      new TestResourceHandler());
  scoped_test_handler->set_on_response_started_result(false);

  std::unique_ptr<ResourceHandler> mime_sniffing_handler(
      new MimeSniffingResourceHandler(std::move(scoped_test_handler), nullptr,
                                      nullptr, nullptr, request,
                                      REQUEST_CONTEXT_TYPE_UNSPECIFIED));

  bool defer = false;
  mime_sniffing_handler->OnWillStart(request->url(), &defer);
  content::RunAllPendingInMessageLoop();

  std::string accept_header;
  request->extra_request_headers().GetHeader("Accept", &accept_header);
  return accept_header;
}

bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted(
    bool allow_download,
    bool must_download,
    ResourceType request_resource_type) {
  net::URLRequestContext context;
  std::unique_ptr<net::URLRequest> request(context.CreateRequest(
      GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
  bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME;
  ResourceRequestInfo::AllocateForTesting(request.get(), request_resource_type,
                                          nullptr,        // context
                                          0,              // render_process_id
                                          0,              // render_view_id
                                          0,              // render_frame_id
                                          is_main_frame,  // is_main_frame
                                          false,  // parent_is_main_frame
                                          allow_download,  // allow_download
                                          true,            // is_async
                                          false);          // is_using_lofi

  TestResourceDispatcherHost host(stream_has_handler_);
  TestResourceDispatcherHostDelegate host_delegate(must_download);
  host.SetDelegate(&host_delegate);

  TestFakePluginService plugin_service(plugin_available_, plugin_stale_);

  std::unique_ptr<InterceptingResourceHandler> intercepting_handler(
      new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(),
                                      nullptr));
  std::unique_ptr<TestResourceHandler> scoped_test_handler(
      new TestResourceHandler());
  scoped_test_handler->set_on_response_started_result(false);
  std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler(
      std::unique_ptr<ResourceHandler>(std::move(scoped_test_handler)), &host,
      &plugin_service, intercepting_handler.get(), request.get(),
      REQUEST_CONTEXT_TYPE_UNSPECIFIED));

  TestResourceController resource_controller;
  mime_handler->SetController(&resource_controller);

  scoped_refptr<ResourceResponse> response(new ResourceResponse);
  // The MIME type isn't important but it shouldn't be empty.
  response->head.mime_type = "application/pdf";

  bool defer = false;
  mime_handler->OnWillStart(request->url(), &defer);
  EXPECT_FALSE(defer);

  mime_handler->OnResponseStarted(response.get(), &defer);

  content::RunAllPendingInMessageLoop();
  EXPECT_LT(host.intercepted_as_stream_count(), 2);
  if (allow_download)
    EXPECT_TRUE(intercepting_handler->new_handler_for_testing());
  return host.intercepted_as_stream();
}

void MimeSniffingResourceHandlerTest::TestHandlerSniffing(
    bool response_started,
    bool defer_response_started,
    bool will_read,
    bool read_completed,
    bool defer_read_completed) {
  net::URLRequestContext context;
  std::unique_ptr<net::URLRequest> request(context.CreateRequest(
      GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
  ResourceRequestInfo::AllocateForTesting(request.get(),
                                          RESOURCE_TYPE_MAIN_FRAME,
                                          nullptr,  // context
                                          0,        // render_process_id
                                          0,        // render_view_id
                                          0,        // render_frame_id
                                          true,     // is_main_frame
                                          false,    // parent_is_main_frame
                                          false,    // allow_download
                                          true,     // is_async
                                          false);   // is_using_lofi

  TestResourceDispatcherHost host(false);
  TestResourceDispatcherHostDelegate host_delegate(false);
  host.SetDelegate(&host_delegate);

  TestFakePluginService plugin_service(plugin_available_, plugin_stale_);
  std::unique_ptr<InterceptingResourceHandler> intercepting_handler(
      new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(),
                                      nullptr));

  std::unique_ptr<TestResourceHandler> scoped_test_handler(
      new TestResourceHandler());
  scoped_test_handler->set_on_response_started_result(response_started);
  scoped_test_handler->set_defer_on_response_started(defer_response_started);
  scoped_test_handler->set_on_will_read_result(will_read);
  scoped_test_handler->set_on_read_completed_result(read_completed);
  scoped_test_handler->set_defer_on_read_completed(defer_read_completed);
  TestResourceHandler* test_handler = scoped_test_handler.get();
  std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler(
      new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
                                      &plugin_service,
                                      intercepting_handler.get(), request.get(),
                                      REQUEST_CONTEXT_TYPE_UNSPECIFIED));

  TestResourceController resource_controller;
  mime_sniffing_handler->SetController(&resource_controller);

  bool defer = false;
  mime_sniffing_handler->OnWillStart(GURL(), &defer);

  // The response should be sniffed.
  scoped_refptr<ResourceResponse> response(new ResourceResponse);
  response->head.mime_type.assign("text/plain");

  // Simulate the response starting. The MimeSniffingHandler should start
  // buffering, so the return value should always be true.
  EXPECT_TRUE(mime_sniffing_handler->OnResponseStarted(response.get(), &defer));
  EXPECT_EQ(0, resource_controller.cancel_call_count());
  EXPECT_EQ(0, resource_controller.resume_call_count());
  EXPECT_FALSE(defer);

  // Read some data to sniff the mime type. This will ask the next
  // ResourceHandler for a buffer.
  scoped_refptr<net::IOBuffer> read_buffer;
  int buf_size = 0;
  EXPECT_EQ(will_read,
            mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1));
  EXPECT_EQ(0, resource_controller.cancel_call_count());

  if (!will_read) {
    EXPECT_EQ(1, test_handler->on_will_start_called());
    EXPECT_EQ(0, test_handler->on_request_redirected_called());
    EXPECT_EQ(0, test_handler->on_response_started_called());
    EXPECT_EQ(1, test_handler->on_will_read_called());
    EXPECT_EQ(0, test_handler->on_read_completed_called());

    // Process all messages to ensure proper test teardown.
    content::RunAllPendingInMessageLoop();
    return;
  }

  // Simulate an HTML page. The mime sniffer will identify the MimeType and
  // proceed with replay.
  char data[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>";
  memcpy(read_buffer->data(), data, sizeof(data));

  defer = false;
  bool return_value =
      mime_sniffing_handler->OnReadCompleted(sizeof(data), &defer);

  // If the next handler cancels the response start, the caller of
  // MimeSniffingHandler::OnReadCompleted should be notified immediately.
  if (!response_started) {
    EXPECT_FALSE(defer);
    EXPECT_EQ(response_started, return_value);
    EXPECT_EQ(0, resource_controller.cancel_call_count());

    EXPECT_EQ(1, test_handler->on_will_start_called());
    EXPECT_EQ(0, test_handler->on_request_redirected_called());
    EXPECT_EQ(1, test_handler->on_response_started_called());
    EXPECT_EQ(1, test_handler->on_will_read_called());
    EXPECT_EQ(0, test_handler->on_read_completed_called());

    // Process all messages to ensure proper test teardown.
    content::RunAllPendingInMessageLoop();
    return;
  }

  // The replay can be deferred both at response started and read replay
  // stages.
  EXPECT_EQ(defer, defer_response_started || defer_read_completed);
  if (defer_response_started) {
    EXPECT_TRUE(defer);
    EXPECT_TRUE(return_value);
    EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED,
              mime_sniffing_handler->state_);
    mime_sniffing_handler->Resume();
  }

  // The body that was sniffed should be transmitted to the next handler. This
  // may cancel the request.
  if (!read_completed) {
    if (defer_response_started) {
      EXPECT_EQ(1, resource_controller.cancel_call_count());
    } else {
      EXPECT_EQ(0, resource_controller.cancel_call_count());
      EXPECT_FALSE(return_value);
    }
    // Process all messages to ensure proper test teardown.
    content::RunAllPendingInMessageLoop();
    return;
  }

  EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING,
            mime_sniffing_handler->state_);

  // The request may be deferred by the next handler once the read is done.
  if (defer_read_completed) {
    EXPECT_TRUE(defer);
    mime_sniffing_handler->Resume();
  }

  EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING,
            mime_sniffing_handler->state_);
  EXPECT_EQ(0, resource_controller.cancel_call_count());

  // Even if the next handler defers the request twice, the
  // MimeSniffingResourceHandler should only call Resume on its controller
  // once.
  if (defer_response_started || defer_read_completed) {
    EXPECT_EQ(1, resource_controller.resume_call_count());
  } else {
    EXPECT_EQ(0, resource_controller.resume_call_count());
  }

  EXPECT_EQ(1, test_handler->on_will_start_called());
  EXPECT_EQ(0, test_handler->on_request_redirected_called());
  EXPECT_EQ(1, test_handler->on_response_started_called());
  EXPECT_EQ(1, test_handler->on_will_read_called());
  EXPECT_EQ(1, test_handler->on_read_completed_called());

  // Process all messages to ensure proper test teardown.
  content::RunAllPendingInMessageLoop();
}

void MimeSniffingResourceHandlerTest::TestHandlerNoSniffing(
    bool response_started,
    bool defer_response_started,
    bool will_read,
    bool read_completed,
    bool defer_read_completed) {
  net::URLRequestContext context;
  std::unique_ptr<net::URLRequest> request(context.CreateRequest(
      GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
  ResourceRequestInfo::AllocateForTesting(request.get(),
                                          RESOURCE_TYPE_MAIN_FRAME,
                                          nullptr,  // context
                                          0,        // render_process_id
                                          0,        // render_view_id
                                          0,        // render_frame_id
                                          true,     // is_main_frame
                                          false,    // parent_is_main_frame
                                          false,    // allow_download
                                          true,     // is_async
                                          false);   // is_using_lofi

  TestResourceDispatcherHost host(false);
  TestResourceDispatcherHostDelegate host_delegate(false);
  host.SetDelegate(&host_delegate);

  TestFakePluginService plugin_service(plugin_available_, plugin_stale_);
  std::unique_ptr<InterceptingResourceHandler> intercepting_handler(
      new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(),
                                      nullptr));

  std::unique_ptr<TestResourceHandler> scoped_test_handler(
      new TestResourceHandler());
  scoped_test_handler->set_on_response_started_result(response_started);
  scoped_test_handler->set_defer_on_response_started(defer_response_started);
  scoped_test_handler->set_on_will_read_result(will_read);
  scoped_test_handler->set_on_read_completed_result(read_completed);
  scoped_test_handler->set_defer_on_read_completed(defer_read_completed);
  TestResourceHandler* test_handler = scoped_test_handler.get();
  std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler(
      new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
                                      &plugin_service,
                                      intercepting_handler.get(), request.get(),
                                      REQUEST_CONTEXT_TYPE_UNSPECIFIED));

  TestResourceController resource_controller;
  mime_sniffing_handler->SetController(&resource_controller);

  int expected_resume_calls = 0;

  bool defer = false;
  mime_sniffing_handler->OnWillStart(GURL(), &defer);

  // The response should not be sniffed.
  scoped_refptr<ResourceResponse> response(new ResourceResponse);
  response->head.mime_type.assign("text/html");

  // Simulate the response starting. There should be no need for buffering, so
  // the return value should be that of the next handler.
  EXPECT_EQ(response_started,
            mime_sniffing_handler->OnResponseStarted(response.get(), &defer));
  EXPECT_EQ(0, resource_controller.cancel_call_count());

  if (!response_started) {
    EXPECT_FALSE(defer);

    EXPECT_EQ(1, test_handler->on_will_start_called());
    EXPECT_EQ(0, test_handler->on_request_redirected_called());
    EXPECT_EQ(1, test_handler->on_response_started_called());
    EXPECT_EQ(0, test_handler->on_will_read_called());
    EXPECT_EQ(0, test_handler->on_read_completed_called());

    // Process all messages to ensure proper test teardown.
    content::RunAllPendingInMessageLoop();
    return;
  }

  EXPECT_EQ(defer_response_started, defer);
  if (defer) {
    EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED,
              mime_sniffing_handler->state_);
    expected_resume_calls++;
    mime_sniffing_handler->Resume();
  }

  EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count());

  // The MimeSniffingResourceHandler should be acting as a pass-through
  // ResourceHandler.
  scoped_refptr<net::IOBuffer> read_buffer;
  int buf_size = 0;
  EXPECT_EQ(will_read,
            mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1));
  EXPECT_EQ(0, resource_controller.cancel_call_count());

  if (!will_read) {
    EXPECT_EQ(1, test_handler->on_will_start_called());
    EXPECT_EQ(0, test_handler->on_request_redirected_called());
    EXPECT_EQ(1, test_handler->on_response_started_called());
    EXPECT_EQ(1, test_handler->on_will_read_called());
    EXPECT_EQ(0, test_handler->on_read_completed_called());

    // Process all messages to ensure proper test teardown.
    content::RunAllPendingInMessageLoop();
    return;
  }

  defer = false;
  EXPECT_EQ(read_completed,
            mime_sniffing_handler->OnReadCompleted(2000, &defer));
  EXPECT_EQ(0, resource_controller.cancel_call_count());

  EXPECT_EQ(1, test_handler->on_will_start_called());
  EXPECT_EQ(0, test_handler->on_request_redirected_called());
  EXPECT_EQ(1, test_handler->on_response_started_called());
  EXPECT_EQ(1, test_handler->on_will_read_called());
  EXPECT_EQ(1, test_handler->on_read_completed_called());

  if (!read_completed) {
    EXPECT_FALSE(defer);

    // Process all messages to ensure proper test teardown.
    content::RunAllPendingInMessageLoop();
    return;
  }

  EXPECT_EQ(defer_read_completed, defer);
  if (defer) {
    expected_resume_calls++;
    mime_sniffing_handler->Resume();
  }
  EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count());

  // Process all messages to ensure proper test teardown.
  content::RunAllPendingInMessageLoop();
}

// Test that the proper Accept: header is set based on the ResourceType
TEST_F(MimeSniffingResourceHandlerTest, AcceptHeaders) {
  EXPECT_EQ(
      "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,"
      "*/*;q=0.8",
      TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME));
  EXPECT_EQ(
      "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,"
      "*/*;q=0.8",
      TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME));
  EXPECT_EQ("text/css,*/*;q=0.1",
            TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT));
  EXPECT_EQ("image/webp,image/*,*/*;q=0.8",
            TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE));
  EXPECT_EQ("image/webp,image/*,*/*;q=0.8",
            TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT));
  EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE));

  // Ensure that if an Accept header is already set, it is not overwritten.
  net::URLRequestContext context;
  std::unique_ptr<net::URLRequest> request(context.CreateRequest(
      GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
  request->SetExtraRequestHeaderByName("Accept", "*", true);
  EXPECT_EQ("*", TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR,
                                                       request.get()));
}

// Test that stream requests are correctly intercepted under the right
// circumstances. Test is not relevent when plugins are disabled.
#if defined(ENABLE_PLUGINS)
TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) {
  bool allow_download;
  bool must_download;
  ResourceType resource_type;

  // Ensure the stream is handled by MaybeInterceptAsStream in the
  // ResourceDispatcherHost.
  set_stream_has_handler(true);
  set_plugin_available(true);

  // Main frame request with no download allowed. Stream shouldn't be
  // intercepted.
  allow_download = false;
  must_download = false;
  resource_type = RESOURCE_TYPE_MAIN_FRAME;
  EXPECT_FALSE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));

  // Main frame request with download allowed. Stream should be intercepted.
  allow_download = true;
  must_download = false;
  resource_type = RESOURCE_TYPE_MAIN_FRAME;
  EXPECT_TRUE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));

  // Main frame request with download forced. Stream shouldn't be intercepted.
  allow_download = true;
  must_download = true;
  resource_type = RESOURCE_TYPE_MAIN_FRAME;
  EXPECT_FALSE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));

  // Sub-resource request with download not allowed. Stream shouldn't be
  // intercepted.
  allow_download = false;
  must_download = false;
  resource_type = RESOURCE_TYPE_SUB_RESOURCE;
  EXPECT_FALSE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));

  // Plugin resource request with download not allowed. Stream shouldn't be
  // intercepted.
  allow_download = false;
  must_download = false;
  resource_type = RESOURCE_TYPE_PLUGIN_RESOURCE;
  EXPECT_FALSE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));

  // Object request with download not allowed. Stream should be intercepted.
  allow_download = false;
  must_download = false;
  resource_type = RESOURCE_TYPE_OBJECT;
  EXPECT_TRUE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));

  // Test the cases where the stream isn't handled by MaybeInterceptAsStream
  // in the ResourceDispatcherHost.
  set_stream_has_handler(false);
  allow_download = false;
  must_download = false;
  resource_type = RESOURCE_TYPE_OBJECT;
  EXPECT_FALSE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));

  // Test the cases where the stream handled by MaybeInterceptAsStream
  // with plugin not available. This is the case when intercepting streams for
  // the streamsPrivate extensions API.
  set_stream_has_handler(true);
  set_plugin_available(false);
  allow_download = false;
  must_download = false;
  resource_type = RESOURCE_TYPE_OBJECT;
  EXPECT_TRUE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));

  // Test the cases where the stream handled by MaybeInterceptAsStream
  // with plugin not available. This is the case when intercepting streams for
  // the streamsPrivate extensions API with stale plugin.
  set_plugin_stale(true);
  allow_download = false;
  must_download = false;
  resource_type = RESOURCE_TYPE_OBJECT;
  EXPECT_TRUE(
      TestStreamIsIntercepted(allow_download, must_download, resource_type));
}
#endif

// Test that the MimeSniffingHandler operates properly when it doesn't sniff
// resources.
TEST_F(MimeSniffingResourceHandlerTest, NoSniffing) {
  // Test simple case.
  TestHandlerNoSniffing(
      true  /* response_started_succeeds */,
      false /* defer_response_started */,
      true  /* will_read_succeeds */,
      true  /* read_completed_succeeds */,
      false /* defer_read_completed */);

  // Test deferral in OnResponseStarted and/or in OnReadCompleted.
  TestHandlerNoSniffing(
      true  /* response_started_succeeds */,
      true  /* defer_response_started */,
      true  /* will_read_succeeds */,
      true  /* read_completed_succeeds */,
      false /* defer_read_completed */);
  TestHandlerNoSniffing(
      true  /* response_started_succeeds */,
      false /* defer_response_started */,
      true  /* will_read_succeeds */,
      true  /* read_completed_succeeds */,
      true  /* defer_read_completed */);
  TestHandlerNoSniffing(
      true  /* response_started_succeeds */,
      true  /* defer_response_started */,
      true  /* will_read_succeeds */,
      true  /* read_completed_succeeds */,
      true  /* defer_read_completed */);

  // Test cancel in OnResponseStarted, OnWillRead, OnReadCompleted.
  TestHandlerNoSniffing(
      false /* response_started_succeeds */,
      false /* defer_response_started */,
      false /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);
  TestHandlerNoSniffing(
      true  /* response_started_succeeds */,
      false /* defer_response_started */,
      false /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);
  TestHandlerNoSniffing(
      true  /* response_started_succeeds */,
      false /* defer_response_started */,
      true  /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);

  // Test cancel after OnResponseStarted deferral.
  TestHandlerNoSniffing(
      true  /* response_started_succeeds */,
      true  /* defer_response_started */,
      false /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);
  TestHandlerNoSniffing(
      true  /* response_started_succeeds */,
      true  /* defer_response_started */,
      true  /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);

  content::RunAllPendingInMessageLoop();
}

// Test that the MimeSniffingHandler operates properly when it sniffs
// resources.
TEST_F(MimeSniffingResourceHandlerTest, Sniffing) {
  // Test simple case.
  TestHandlerSniffing(
      true  /* response_started_succeeds */,
      false /* defer_response_started */,
      true  /* will_read_succeeds */,
      true  /* read_completed_succeeds */,
      false /* defer_read_completed */);

  // Test deferral in OnResponseStarted and/or in OnReadCompleted.
  TestHandlerSniffing(
      true  /* response_started_succeeds */,
      true  /* defer_response_started */,
      true  /* will_read_succeeds */,
      true  /* read_completed_succeeds */,
      false /* defer_read_completed */);
  TestHandlerSniffing(
      true  /* response_started_succeeds */,
      false /* defer_response_started */,
      true  /* will_read_succeeds */,
      true  /* read_completed_succeeds */,
      true  /* defer_read_completed */);
  TestHandlerSniffing(
      true  /* response_started_succeeds */,
      true  /* defer_response_started */,
      true  /* will_read_succeeds */,
      true  /* read_completed_succeeds */,
      true  /* defer_read_completed */);

  // Test cancel in OnResponseStarted, OnWillRead, OnReadCompleted.
  TestHandlerSniffing(
      false /* response_started_succeeds */,
      false /* defer_response_started */,
      false /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);
  TestHandlerSniffing(
      true  /* response_started_succeeds */,
      false /* defer_response_started */,
      false /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);
  TestHandlerSniffing(
      true  /* response_started_succeeds */,
      false /* defer_response_started */,
      true  /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);

  // Test cancel after OnResponseStarted deferral.
  TestHandlerSniffing(
      true  /* response_started_succeeds */,
      true  /* defer_response_started */,
      false /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);
  TestHandlerSniffing(
      true  /* response_started_succeeds */,
      true  /* defer_response_started */,
      true  /* will_read_succeeds */,
      false /* read_completed_succeeds */,
      false /* defer_read_completed */);

  content::RunAllPendingInMessageLoop();
}

// Tests that 304s do not trigger a change in handlers.
TEST_F(MimeSniffingResourceHandlerTest, 304Handling) {
  net::URLRequestContext context;
  std::unique_ptr<net::URLRequest> request(context.CreateRequest(
      GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
  ResourceRequestInfo::AllocateForTesting(request.get(),
                                          RESOURCE_TYPE_MAIN_FRAME,
                                          nullptr,  // context
                                          0,        // render_process_id
                                          0,        // render_view_id
                                          0,        // render_frame_id
                                          true,     // is_main_frame
                                          false,    // parent_is_main_frame
                                          true,     // allow_download
                                          true,     // is_async
                                          false);   // is_using_lofi

  TestResourceDispatcherHost host(false);
  TestResourceDispatcherHostDelegate host_delegate(false);
  host.SetDelegate(&host_delegate);

  TestFakePluginService plugin_service(false, false);
  std::unique_ptr<ResourceHandler> intercepting_handler(
      new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(),
                                      nullptr));
  std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler(
      std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host,
      &plugin_service,
      static_cast<InterceptingResourceHandler*>(intercepting_handler.get()),
      request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED));

  TestResourceController resource_controller;
  mime_handler->SetController(&resource_controller);

  // Request starts.
  bool defer = false;
  mime_handler->OnWillStart(request->url(), &defer);
  EXPECT_FALSE(defer);

  // Simulate a 304 response.
  scoped_refptr<ResourceResponse> response(new ResourceResponse);
  // The MIME type isn't important but it shouldn't be empty.
  response->head.mime_type = "application/pdf";
  response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK");

  // The response is received. No new ResourceHandler should be created to
  // handle the download.
  mime_handler->OnResponseStarted(response.get(), &defer);
  EXPECT_FALSE(defer);
  EXPECT_FALSE(host.new_resource_handler());

  content::RunAllPendingInMessageLoop();
}

TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) {
  net::URLRequestContext context;
  std::unique_ptr<net::URLRequest> request(context.CreateRequest(
      GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
  ResourceRequestInfo::AllocateForTesting(request.get(),
                                          RESOURCE_TYPE_MAIN_FRAME,
                                          nullptr,  // context
                                          0,        // render_process_id
                                          0,        // render_view_id
                                          0,        // render_frame_id
                                          true,     // is_main_frame
                                          false,    // parent_is_main_frame
                                          false,    // allow_download
                                          true,     // is_async
                                          false);   // is_using_lofi

  TestResourceDispatcherHost host(false);

  TestFakePluginService plugin_service(false, false);
  std::unique_ptr<InterceptingResourceHandler> intercepting_handler(
      new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(),
                                      nullptr));

  std::unique_ptr<TestResourceHandler> scoped_test_handler(
      new TestResourceHandler());
  scoped_test_handler->set_on_response_started_result(false);
  std::unique_ptr<ResourceHandler> mime_sniffing_handler(
      new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
                                      &plugin_service,
                                      intercepting_handler.get(), request.get(),
                                      REQUEST_CONTEXT_TYPE_FETCH));

  TestResourceController resource_controller;
  mime_sniffing_handler->SetController(&resource_controller);

  bool defer = false;
  mime_sniffing_handler->OnWillStart(GURL(), &defer);
  ASSERT_FALSE(defer);

  scoped_refptr<ResourceResponse> response(new ResourceResponse);
  response->head.mime_type = "text/plain";

  // |mime_sniffing_handler->OnResponseStarted| should return false because
  // mime sniffing is disabled and the wrapped resource handler returns false
  // on OnResponseStarted.
  EXPECT_FALSE(
      mime_sniffing_handler->OnResponseStarted(response.get(), &defer));

  // Process all messages to ensure proper test teardown.
  content::RunAllPendingInMessageLoop();
}

}  // namespace content