summaryrefslogtreecommitdiffstats
path: root/toolkit/components/downloads/test/unit/head.js
blob: 44364c1cbfc439c2c16b7e8aac0ab8936a537d6a (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
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Provides infrastructure for automated download components tests.
 */

"use strict";

var { Integration } = ChromeUtils.importESModule(
  "resource://gre/modules/Integration.sys.mjs"
);
var { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

ChromeUtils.defineESModuleGetters(this, {
  DownloadPaths: "resource://gre/modules/DownloadPaths.sys.mjs",
  Downloads: "resource://gre/modules/Downloads.sys.mjs",
  E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs",
  FileTestUtils: "resource://testing-common/FileTestUtils.sys.mjs",
  FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
  MockRegistrar: "resource://testing-common/MockRegistrar.sys.mjs",
  PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
  PromiseUtils: "resource://gre/modules/PromiseUtils.sys.mjs",
  TelemetryTestUtils: "resource://testing-common/TelemetryTestUtils.sys.mjs",
  TestUtils: "resource://testing-common/TestUtils.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(this, {
  HttpServer: "resource://testing-common/httpd.js",
  NetUtil: "resource://gre/modules/NetUtil.jsm",
});

XPCOMUtils.defineLazyServiceGetter(
  this,
  "gExternalHelperAppService",
  "@mozilla.org/uriloader/external-helper-app-service;1",
  Ci.nsIExternalHelperAppService
);

/* global DownloadIntegration */
Integration.downloads.defineESModuleGetter(
  this,
  "DownloadIntegration",
  "resource://gre/modules/DownloadIntegration.sys.mjs"
);

const ServerSocket = Components.Constructor(
  "@mozilla.org/network/server-socket;1",
  "nsIServerSocket",
  "init"
);
const BinaryOutputStream = Components.Constructor(
  "@mozilla.org/binaryoutputstream;1",
  "nsIBinaryOutputStream",
  "setOutputStream"
);

XPCOMUtils.defineLazyServiceGetter(
  this,
  "gMIMEService",
  "@mozilla.org/mime;1",
  "nsIMIMEService"
);

const ReferrerInfo = Components.Constructor(
  "@mozilla.org/referrer-info;1",
  "nsIReferrerInfo",
  "init"
);

const TEST_TARGET_FILE_NAME = "test-download.txt";
const TEST_STORE_FILE_NAME = "test-downloads.json";

// We are testing an HTTPS referrer with HTTP downloads in order to verify that
// the default policy will not prevent the referrer from being passed around.
const TEST_REFERRER_URL = "https://www.example.com/referrer.html";

const TEST_DATA_SHORT = "This test string is downloaded.";
// Generate using gzipCompressString in TelemetryController.sys.mjs.
const TEST_DATA_SHORT_GZIP_ENCODED_FIRST = [
  31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 11, 201, 200, 44, 86, 40, 73, 45, 46, 81, 40,
  46, 41, 202, 204,
];
const TEST_DATA_SHORT_GZIP_ENCODED_SECOND = [
  75, 87, 0, 114, 83, 242, 203, 243, 114, 242, 19, 83, 82, 83, 244, 0, 151, 222,
  109, 43, 31, 0, 0, 0,
];
const TEST_DATA_SHORT_GZIP_ENCODED = TEST_DATA_SHORT_GZIP_ENCODED_FIRST.concat(
  TEST_DATA_SHORT_GZIP_ENCODED_SECOND
);

/**
 * All the tests are implemented with add_task, this starts them automatically.
 */
function run_test() {
  do_get_profile();
  run_next_test();
}

// Support functions

/**
 * HttpServer object initialized before tests start.
 */
var gHttpServer;

/**
 * Given a file name, returns a string containing an URI that points to the file
 * on the currently running instance of the test HTTP server.
 */
function httpUrl(aFileName) {
  return (
    "http://www.example.com:" +
    gHttpServer.identity.primaryPort +
    "/" +
    aFileName
  );
}

/**
 * Returns a reference to a temporary file that is guaranteed not to exist and
 * is cleaned up later. See FileTestUtils.getTempFile for details.
 */
function getTempFile(leafName) {
  return FileTestUtils.getTempFile(leafName);
}

/**
 * Check for file existence.
 * @param {string} path The file path.
 */
async function fileExists(path) {
  try {
    return (await IOUtils.stat(path)).type == "regular";
  } catch (ex) {
    return false;
  }
}

/**
 * Waits for pending events to be processed.
 *
 * @return {Promise}
 * @resolves When pending events have been processed.
 * @rejects Never.
 */
function promiseExecuteSoon() {
  return new Promise(resolve => {
    executeSoon(resolve);
  });
}

/**
 * Waits for a pending events to be processed after a timeout.
 *
 * @return {Promise}
 * @resolves When pending events have been processed.
 * @rejects Never.
 */
function promiseTimeout(aTime) {
  return new Promise(resolve => {
    do_timeout(aTime, resolve);
  });
}

/**
 * Waits for a new history visit to be notified for the specified URI.
 *
 * @param aUrl
 *        String containing the URI that will be visited.
 *
 * @return {Promise}
 * @resolves Array [aTime, aTransitionType] from page-visited places event.
 * @rejects Never.
 */
function promiseWaitForVisit(aUrl) {
  return new Promise(resolve => {
    function listener(aEvents) {
      Assert.equal(aEvents.length, 1);
      let event = aEvents[0];
      Assert.equal(event.type, "page-visited");
      if (event.url == aUrl) {
        PlacesObservers.removeListener(["page-visited"], listener);
        resolve([event.visitTime, event.transitionType, event.lastKnownTitle]);
      }
    }
    PlacesObservers.addListener(["page-visited"], listener);
  });
}

/**
 * Creates a new Download object, setting a temporary file as the target.
 *
 * @param aSourceUrl
 *        String containing the URI for the download source, or null to use
 *        httpUrl("source.txt").
 *
 * @return {Promise}
 * @resolves The newly created Download object.
 * @rejects JavaScript exception.
 */
function promiseNewDownload(aSourceUrl) {
  return Downloads.createDownload({
    source: aSourceUrl || httpUrl("source.txt"),
    target: getTempFile(TEST_TARGET_FILE_NAME),
  });
}

/**
 * Starts a new download using the nsIWebBrowserPersist interface, and controls
 * it using the legacy nsITransfer interface.
 *
 * @param aSourceUrl
 *        String containing the URI for the download source, or null to use
 *        httpUrl("source.txt").
 * @param aOptions
 *        An optional object used to control the behavior of this function.
 *        You may pass an object with a subset of the following fields:
 *        {
 *          isPrivate: Boolean indicating whether the download originated from a
 *                     private window.
 *          referrerInfo: referrerInfo for the download source.
 *          cookieJarSettings: cookieJarSettings for the download source.
 *          targetFile: nsIFile for the target, or null to use a temporary file.
 *          outPersist: Receives a reference to the created nsIWebBrowserPersist
 *                      instance.
 *          launchWhenSucceeded: Boolean indicating whether the target should
 *                               be launched when it has completed successfully.
 *          launcherPath: String containing the path of the custom executable to
 *                        use to launch the target of the download.
 *        }
 *
 * @return {Promise}
 * @resolves The Download object created as a consequence of controlling the
 *           download through the legacy nsITransfer interface.
 * @rejects Never.  The current test fails in case of exceptions.
 */
function promiseStartLegacyDownload(aSourceUrl, aOptions) {
  let sourceURI = NetUtil.newURI(aSourceUrl || httpUrl("source.txt"));
  let targetFile =
    (aOptions && aOptions.targetFile) || getTempFile(TEST_TARGET_FILE_NAME);

  let persist = Cc[
    "@mozilla.org/embedding/browser/nsWebBrowserPersist;1"
  ].createInstance(Ci.nsIWebBrowserPersist);
  if (aOptions) {
    aOptions.outPersist = persist;
  }

  let fileExtension = null,
    mimeInfo = null;
  let match = sourceURI.pathQueryRef.match(/\.([^.\/]+)$/);
  if (match) {
    fileExtension = match[1];
  }

  if (fileExtension) {
    try {
      mimeInfo = gMIMEService.getFromTypeAndExtension(null, fileExtension);
      mimeInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk;
    } catch (ex) {}
  }

  if (aOptions && aOptions.launcherPath) {
    Assert.ok(mimeInfo != null);

    let localHandlerApp = Cc[
      "@mozilla.org/uriloader/local-handler-app;1"
    ].createInstance(Ci.nsILocalHandlerApp);
    localHandlerApp.executable = new FileUtils.File(aOptions.launcherPath);

    mimeInfo.preferredApplicationHandler = localHandlerApp;
    mimeInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp;
  }

  if (aOptions && aOptions.launchWhenSucceeded) {
    Assert.ok(mimeInfo != null);

    mimeInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp;
  }

  // Apply decoding if required by the "Content-Encoding" header.
  persist.persistFlags &= ~Ci.nsIWebBrowserPersist.PERSIST_FLAGS_NO_CONVERSION;
  persist.persistFlags |=
    Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;

  let transfer = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer);

  return new Promise(resolve => {
    Downloads.getList(Downloads.ALL)
      .then(function (aList) {
        // Temporarily register a view that will get notified when the download we
        // are controlling becomes visible in the list of downloads.
        aList
          .addView({
            onDownloadAdded(aDownload) {
              aList.removeView(this).catch(do_report_unexpected_exception);

              // Remove the download to keep the list empty for the next test.  This
              // also allows the caller to register the "onchange" event directly.
              let promise = aList.remove(aDownload);

              // When the download object is ready, make it available to the caller.
              promise.then(
                () => resolve(aDownload),
                do_report_unexpected_exception
              );
            },
          })
          .catch(do_report_unexpected_exception);

        let isPrivate = aOptions && aOptions.isPrivate;
        let referrerInfo = aOptions ? aOptions.referrerInfo : null;
        let cookieJarSettings = aOptions ? aOptions.cookieJarSettings : null;
        let classification =
          aOptions?.downloadClassification ??
          Ci.nsITransfer.DOWNLOAD_ACCEPTABLE;
        // Initialize the components so they reference each other.  This will cause
        // the Download object to be created and added to the public downloads.
        transfer.init(
          sourceURI,
          null,
          NetUtil.newURI(targetFile),
          null,
          mimeInfo,
          null,
          null,
          persist,
          isPrivate,
          classification,
          null
        );
        persist.progressListener = transfer;

        // Start the actual download process.
        persist.saveURI(
          sourceURI,
          Services.scriptSecurityManager.getSystemPrincipal(),
          0,
          referrerInfo,
          cookieJarSettings,
          null,
          null,
          targetFile,
          Ci.nsIContentPolicy.TYPE_SAVEAS_DOWNLOAD,
          isPrivate
        );
      })
      .catch(do_report_unexpected_exception);
  });
}

/**
 * Starts a new download using the nsIHelperAppService interface, and controls
 * it using the legacy nsITransfer interface.  The source of the download will
 * be "interruptible_resumable.txt" and partially downloaded data will be kept.
 *
 * @param aSourceUrl
 *        String containing the URI for the download source, or null to use
 *        httpUrl("interruptible_resumable.txt").
 *
 * @return {Promise}
 * @resolves The Download object created as a consequence of controlling the
 *           download through the legacy nsITransfer interface.
 * @rejects Never.  The current test fails in case of exceptions.
 */
function promiseStartExternalHelperAppServiceDownload(aSourceUrl) {
  let sourceURI = NetUtil.newURI(
    aSourceUrl || httpUrl("interruptible_resumable.txt")
  );

  return new Promise(resolve => {
    Downloads.getList(Downloads.PUBLIC)
      .then(function (aList) {
        // Temporarily register a view that will get notified when the download we
        // are controlling becomes visible in the list of downloads.
        aList
          .addView({
            onDownloadAdded(aDownload) {
              aList.removeView(this).catch(do_report_unexpected_exception);

              // Remove the download to keep the list empty for the next test.  This
              // also allows the caller to register the "onchange" event directly.
              let promise = aList.remove(aDownload);

              // When the download object is ready, make it available to the caller.
              promise.then(
                () => resolve(aDownload),
                do_report_unexpected_exception
              );
            },
          })
          .catch(do_report_unexpected_exception);

        let channel = NetUtil.newChannel({
          uri: sourceURI,
          loadUsingSystemPrincipal: true,
        });

        // Start the actual download process.
        channel.asyncOpen({
          contentListener: null,

          onStartRequest(aRequest) {
            let requestChannel = aRequest.QueryInterface(Ci.nsIChannel);
            this.contentListener = gExternalHelperAppService.doContent(
              requestChannel.contentType,
              aRequest,
              null,
              true
            );
            this.contentListener.onStartRequest(aRequest);
          },

          onStopRequest(aRequest, aStatusCode) {
            this.contentListener.onStopRequest(aRequest, aStatusCode);
          },

          onDataAvailable(aRequest, aInputStream, aOffset, aCount) {
            this.contentListener.onDataAvailable(
              aRequest,
              aInputStream,
              aOffset,
              aCount
            );
          },
        });
      })
      .catch(do_report_unexpected_exception);
  });
}

/**
 * Waits for a download to reach half of its progress, in case it has not
 * reached the expected progress already.
 *
 * @param aDownload
 *        The Download object to wait upon.
 *
 * @return {Promise}
 * @resolves When the download has reached half of its progress.
 * @rejects Never.
 */
function promiseDownloadMidway(aDownload) {
  return new Promise(resolve => {
    // Wait for the download to reach half of its progress.
    let onchange = function () {
      if (
        !aDownload.stopped &&
        !aDownload.canceled &&
        aDownload.progress == 50
      ) {
        aDownload.onchange = null;
        resolve();
      }
    };

    // Register for the notification, but also call the function directly in
    // case the download already reached the expected progress.
    aDownload.onchange = onchange;
    onchange();
  });
}

/**
 * Waits for a download to make any amount of progress.
 *
 * @param aDownload
 *        The Download object to wait upon.
 *
 * @return {Promise}
 * @resolves When the download has transfered any number of bytes.
 * @rejects Never.
 */
function promiseDownloadStarted(aDownload) {
  return new Promise(resolve => {
    // Wait for the download to transfer some amount of bytes.
    let onchange = function () {
      if (
        !aDownload.stopped &&
        !aDownload.canceled &&
        aDownload.currentBytes > 0
      ) {
        aDownload.onchange = null;
        resolve();
      }
    };

    // Register for the notification, but also call the function directly in
    // case the download already reached the expected progress.
    aDownload.onchange = onchange;
    onchange();
  });
}

/**
 * Waits for a download to finish.
 *
 * @param aDownload
 *        The Download object to wait upon.
 *
 * @return {Promise}
 * @resolves When the download succeeded or errored.
 * @rejects Never.
 */
function promiseDownloadFinished(aDownload) {
  return new Promise(resolve => {
    // Wait for the download to finish.
    let onchange = function () {
      if (aDownload.succeeded || aDownload.error) {
        aDownload.onchange = null;
        resolve();
      }
    };

    // Register for the notification, but also call the function directly in
    // case the download already reached the expected progress.
    aDownload.onchange = onchange;
    onchange();
  });
}

/**
 * Waits for a download to finish, in case it has not finished already.
 *
 * @param aDownload
 *        The Download object to wait upon.
 *
 * @return {Promise}
 * @resolves When the download has finished successfully.
 * @rejects JavaScript exception if the download failed.
 */
function promiseDownloadStopped(aDownload) {
  if (!aDownload.stopped) {
    // The download is in progress, wait for the current attempt to finish and
    // report any errors that may occur.
    return aDownload.start();
  }

  if (aDownload.succeeded) {
    return Promise.resolve();
  }

  // The download failed or was canceled.
  return Promise.reject(aDownload.error || new Error("Download canceled."));
}

/**
 * Returns a new public or private DownloadList object.
 *
 * @param aIsPrivate
 *        True for the private list, false or undefined for the public list.
 *
 * @return {Promise}
 * @resolves The newly created DownloadList object.
 * @rejects JavaScript exception.
 */
function promiseNewList(aIsPrivate) {
  // We need to clear all the internal state for the list and summary objects,
  // since all the objects are interdependent internally.
  Downloads._promiseListsInitialized = null;
  Downloads._lists = {};
  Downloads._summaries = {};

  return Downloads.getList(aIsPrivate ? Downloads.PRIVATE : Downloads.PUBLIC);
}

/**
 * Ensures that the given file contents are equal to the given string.
 *
 * @param aPath
 *        String containing the path of the file whose contents should be
 *        verified.
 * @param aExpectedContents
 *        String containing the octets that are expected in the file.
 *
 * @return {Promise}
 * @resolves When the operation completes.
 * @rejects Never.
 */
async function promiseVerifyContents(aPath, aExpectedContents) {
  let file = new FileUtils.File(aPath);

  if (!(await IOUtils.exists(aPath))) {
    do_throw("File does not exist: " + aPath);
  }

  if ((await IOUtils.stat(aPath)).size == 0) {
    do_throw("File is empty: " + aPath);
  }

  await new Promise(resolve => {
    NetUtil.asyncFetch(
      { uri: NetUtil.newURI(file), loadUsingSystemPrincipal: true },
      function (aInputStream, aStatus) {
        Assert.ok(Components.isSuccessCode(aStatus));
        let contents = NetUtil.readInputStreamToString(
          aInputStream,
          aInputStream.available()
        );
        if (
          contents.length > TEST_DATA_SHORT.length * 2 ||
          /[^\x20-\x7E]/.test(contents)
        ) {
          // Do not print the entire content string to the test log.
          Assert.equal(contents.length, aExpectedContents.length);
          Assert.ok(contents == aExpectedContents);
        } else {
          // Print the string if it is short and made of printable characters.
          Assert.equal(contents, aExpectedContents);
        }
        resolve();
      }
    );
  });
}

/**
 * Creates and starts a new download, configured to keep partial data, and
 * returns only when the first part of "interruptible_resumable.txt" has been
 * saved to disk.  You must call "continueResponses" to allow the interruptible
 * request to continue.
 *
 * This function uses either DownloadCopySaver or DownloadLegacySaver based on
 * the current test run.
 *
 * @param aOptions
 *        An optional object used to control the behavior of this function.
 *        You may pass an object with a subset of the following fields:
 *        {
 *          useLegacySaver: Boolean indicating whether to launch a legacy download.
 *        }
 *
 * @return {Promise}
 * @resolves The newly created Download object, still in progress.
 * @rejects JavaScript exception.
 */
async function promiseStartDownload_tryToKeepPartialData({
  useLegacySaver = false,
} = {}) {
  mustInterruptResponses();

  // Start a new download and configure it to keep partially downloaded data.
  let download;
  if (!useLegacySaver) {
    let targetFilePath = getTempFile(TEST_TARGET_FILE_NAME).path;
    download = await Downloads.createDownload({
      source: httpUrl("interruptible_resumable.txt"),
      target: {
        path: targetFilePath,
        partFilePath: targetFilePath + ".part",
      },
    });
    download.tryToKeepPartialData = true;
    download.start().catch(() => {});
  } else {
    // Start a download using nsIExternalHelperAppService, that is configured
    // to keep partially downloaded data by default.
    download = await promiseStartExternalHelperAppServiceDownload();
  }

  await promiseDownloadMidway(download);
  await promisePartFileReady(download);

  return download;
}

/**
 * This function should be called after the progress notification for a download
 * is received, and waits for the worker thread of BackgroundFileSaver to
 * receive the data to be written to the ".part" file on disk.
 *
 * @return {Promise}
 * @resolves When the ".part" file has been written to disk.
 * @rejects JavaScript exception.
 */
async function promisePartFileReady(aDownload) {
  // We don't have control over the file output code in BackgroundFileSaver.
  // After we receive the download progress notification, we may only check
  // that the ".part" file has been created, while its size cannot be
  // determined because the file is currently open.
  try {
    do {
      await promiseTimeout(50);
    } while (!(await IOUtils.exists(aDownload.target.partFilePath)));
  } catch (ex) {
    if (!(ex instanceof IOUtils.Error)) {
      throw ex;
    }
    // This indicates that the file has been created and cannot be accessed.
    // The specific error might vary with the platform.
    info("Expected exception while checking existence: " + ex.toString());
    // Wait some more time to allow the write to complete.
    await promiseTimeout(100);
  }
}

/**
 * Create a download which will be reputation blocked.
 *
 * @param options
 *        {
 *           keepPartialData: bool,
 *           keepBlockedData: bool,
 *           useLegacySaver: bool,
 *           verdict: string indicating the detailed reason for the block,
 *        }
 * @return {Promise}
 * @resolves The reputation blocked download.
 * @rejects JavaScript exception.
 */
async function promiseBlockedDownload({
  keepPartialData,
  keepBlockedData,
  useLegacySaver,
  verdict = Downloads.Error.BLOCK_VERDICT_UNCOMMON,
} = {}) {
  let blockFn = base => ({
    shouldBlockForReputationCheck: () =>
      Promise.resolve({
        shouldBlock: true,
        verdict,
      }),
    shouldKeepBlockedData: () => Promise.resolve(keepBlockedData),
  });

  Integration.downloads.register(blockFn);
  function cleanup() {
    Integration.downloads.unregister(blockFn);
  }
  registerCleanupFunction(cleanup);

  let download;

  try {
    if (keepPartialData) {
      download = await promiseStartDownload_tryToKeepPartialData({
        useLegacySaver,
      });
      continueResponses();
    } else if (useLegacySaver) {
      download = await promiseStartLegacyDownload();
    } else {
      download = await promiseNewDownload();
      await download.start();
      do_throw("The download should have blocked.");
    }

    await promiseDownloadStopped(download);
    do_throw("The download should have blocked.");
  } catch (ex) {
    if (!(ex instanceof Downloads.Error) || !ex.becauseBlocked) {
      throw ex;
    }
    Assert.ok(ex.becauseBlockedByReputationCheck);
    Assert.equal(ex.reputationCheckVerdict, verdict);
    Assert.ok(download.error.becauseBlockedByReputationCheck);
    Assert.equal(download.error.reputationCheckVerdict, verdict);
  }

  Assert.ok(download.stopped);
  Assert.ok(!download.succeeded);

  cleanup();
  return download;
}

/**
 * Starts a socket listener that closes each incoming connection.
 *
 * @returns nsIServerSocket that listens for connections.  Call its "close"
 *          method to stop listening and free the server port.
 */
function startFakeServer() {
  let serverSocket = new ServerSocket(-1, true, -1);
  serverSocket.asyncListen({
    onSocketAccepted(aServ, aTransport) {
      aTransport.close(Cr.NS_BINDING_ABORTED);
    },
    onStopListening() {},
  });
  return serverSocket;
}

/**
 * This is an internal reference that should not be used directly by tests.
 */
var _gDeferResponses = PromiseUtils.defer();

/**
 * Ensures that all the interruptible requests started after this function is
 * called won't complete until the continueResponses function is called.
 *
 * Normally, the internal HTTP server returns all the available data as soon as
 * a request is received.  In order for some requests to be served one part at a
 * time, special interruptible handlers are registered on the HTTP server.  This
 * allows testing events or actions that need to happen in the middle of a
 * download.
 *
 * For example, the handler accessible at the httpUri("interruptible.txt")
 * address returns the TEST_DATA_SHORT text, then it may block until the
 * continueResponses method is called.  At this point, the handler sends the
 * TEST_DATA_SHORT text again to complete the response.
 *
 * If an interruptible request is started before the function is called, it may
 * or may not be blocked depending on the actual sequence of events.
 */
function mustInterruptResponses() {
  // If there are pending blocked requests, allow them to complete.  This is
  // done to prevent requests from being blocked forever, but should not affect
  // the test logic, since previously started requests should not be monitored
  // on the client side anymore.
  _gDeferResponses.resolve();

  info("Interruptible responses will be blocked midway.");
  _gDeferResponses = PromiseUtils.defer();
}

/**
 * Allows all the current and future interruptible requests to complete.
 */
function continueResponses() {
  info("Interruptible responses are now allowed to continue.");
  _gDeferResponses.resolve();
}

/**
 * Registers an interruptible response handler.
 *
 * @param aPath
 *        Path passed to nsIHttpServer.registerPathHandler.
 * @param aFirstPartFn
 *        This function is called when the response is received, with the
 *        aRequest and aResponse arguments of the server.
 * @param aSecondPartFn
 *        This function is called with the aRequest and aResponse arguments of
 *        the server, when the continueResponses function is called.
 */
function registerInterruptibleHandler(aPath, aFirstPartFn, aSecondPartFn) {
  gHttpServer.registerPathHandler(aPath, function (aRequest, aResponse) {
    info("Interruptible request started.");

    // Process the first part of the response.
    aResponse.processAsync();
    aFirstPartFn(aRequest, aResponse);

    // Wait on the current deferred object, then finish the request.
    _gDeferResponses.promise
      .then(function RIH_onSuccess() {
        aSecondPartFn(aRequest, aResponse);
        aResponse.finish();
        info("Interruptible request finished.");
      })
      .catch(console.error);
  });
}

/**
 * Ensure the given date object is valid.
 *
 * @param aDate
 *        The date object to be checked. This value can be null.
 */
function isValidDate(aDate) {
  return aDate && aDate.getTime && !isNaN(aDate.getTime());
}

/**
 * Check actual ReferrerInfo is the same as expected.
 * Because the actual download's referrer info's computedReferrer is computed
 * from referrerPolicy and originalReferrer and is non-null, and the expected
 * referrer info was constructed in isolation and therefore the computedReferrer
 * is null, it isn't possible to use equals here. */
function checkEqualReferrerInfos(aActualInfo, aExpectedInfo) {
  Assert.equal(
    !!aExpectedInfo.originalReferrer,
    !!aActualInfo.originalReferrer
  );
  if (aExpectedInfo.originalReferrer && aActualInfo.originalReferrer) {
    Assert.equal(
      aExpectedInfo.originalReferrer.spec,
      aActualInfo.originalReferrer.spec
    );
  }

  Assert.equal(aExpectedInfo.sendReferrer, aActualInfo.sendReferrer);
  Assert.equal(aExpectedInfo.referrerPolicy, aActualInfo.referrerPolicy);
}

/**
 * Waits for the download annotations to be set for the given page, required
 * because the addDownload method will add these to the database asynchronously.
 */
function waitForAnnotation(sourceUriSpec, annotationName, optionalValue) {
  return TestUtils.waitForCondition(async () => {
    let pageInfo = await PlacesUtils.history.fetch(sourceUriSpec, {
      includeAnnotations: true,
    });
    if (optionalValue) {
      return pageInfo?.annotations.get(annotationName) == optionalValue;
    }
    return pageInfo?.annotations.has(annotationName);
  }, `Should have found annotation ${annotationName} for ${sourceUriSpec}`);
}

/**
 * Position of the first byte served by the "interruptible_resumable.txt"
 * handler during the most recent response.
 */
var gMostRecentFirstBytePos;

// Initialization functions common to all tests

add_setup(function test_common_initialize() {
  // Start the HTTP server.
  gHttpServer = new HttpServer();
  gHttpServer.registerDirectory("/", do_get_file("../data"));
  gHttpServer.start(-1);
  registerCleanupFunction(() => {
    return new Promise(resolve => {
      // Ensure all the pending HTTP requests have a chance to finish.
      continueResponses();
      // Stop the HTTP server, calling resolve when it's done.
      gHttpServer.stop(resolve);
    });
  });

  // Serve the downloads from a domain located in the Internet zone on Windows.
  gHttpServer.identity.setPrimary(
    "http",
    "www.example.com",
    gHttpServer.identity.primaryPort
  );
  Services.prefs.setCharPref("network.dns.localDomains", "www.example.com");
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref("network.dns.localDomains");
  });

  // Cache locks might prevent concurrent requests to the same resource, and
  // this may block tests that use the interruptible handlers.
  Services.prefs.setBoolPref("browser.cache.disk.enable", false);
  Services.prefs.setBoolPref("browser.cache.memory.enable", false);
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref("browser.cache.disk.enable");
    Services.prefs.clearUserPref("browser.cache.memory.enable");
  });

  // Allow relaxing default referrer.
  Services.prefs.setBoolPref(
    "network.http.referer.disallowCrossSiteRelaxingDefault",
    false
  );
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref(
      "network.http.referer.disallowCrossSiteRelaxingDefault"
    );
  });

  registerInterruptibleHandler(
    "/interruptible.txt",
    function firstPart(aRequest, aResponse) {
      aResponse.setHeader("Content-Type", "text/plain", false);
      aResponse.setHeader(
        "Content-Length",
        "" + TEST_DATA_SHORT.length * 2,
        false
      );
      aResponse.write(TEST_DATA_SHORT);
    },
    function secondPart(aRequest, aResponse) {
      aResponse.write(TEST_DATA_SHORT);
    }
  );

  registerInterruptibleHandler(
    "/interruptible_nosize.txt",
    function firstPart(aRequest, aResponse) {
      aResponse.setHeader("Content-Type", "text/plain", false);
      aResponse.write(TEST_DATA_SHORT);
    },
    function secondPart(aRequest, aResponse) {
      aResponse.write(TEST_DATA_SHORT);
    }
  );

  registerInterruptibleHandler(
    "/interruptible_resumable.txt",
    function firstPart(aRequest, aResponse) {
      aResponse.setHeader("Content-Type", "text/plain", false);

      // Determine if only part of the data should be sent.
      let data = TEST_DATA_SHORT + TEST_DATA_SHORT;
      if (aRequest.hasHeader("Range")) {
        var matches = aRequest
          .getHeader("Range")
          .match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
        var firstBytePos = matches[1] === undefined ? 0 : matches[1];
        var lastBytePos =
          matches[2] === undefined ? data.length - 1 : matches[2];
        if (firstBytePos >= data.length) {
          aResponse.setStatusLine(
            aRequest.httpVersion,
            416,
            "Requested Range Not Satisfiable"
          );
          aResponse.setHeader("Content-Range", "*/" + data.length, false);
          aResponse.finish();
          return;
        }

        aResponse.setStatusLine(aRequest.httpVersion, 206, "Partial Content");
        aResponse.setHeader(
          "Content-Range",
          firstBytePos + "-" + lastBytePos + "/" + data.length,
          false
        );

        data = data.substring(firstBytePos, lastBytePos + 1);

        gMostRecentFirstBytePos = firstBytePos;
      } else {
        gMostRecentFirstBytePos = 0;
      }

      aResponse.setHeader("Content-Length", "" + data.length, false);

      aResponse.write(data.substring(0, data.length / 2));

      // Store the second part of the data on the response object, so that it
      // can be used by the secondPart function.
      aResponse.secondPartData = data.substring(data.length / 2);
    },
    function secondPart(aRequest, aResponse) {
      aResponse.write(aResponse.secondPartData);
    }
  );

  registerInterruptibleHandler(
    "/interruptible_gzip.txt",
    function firstPart(aRequest, aResponse) {
      aResponse.setHeader("Content-Type", "text/plain", false);
      aResponse.setHeader("Content-Encoding", "gzip", false);
      aResponse.setHeader(
        "Content-Length",
        "" + TEST_DATA_SHORT_GZIP_ENCODED.length
      );

      let bos = new BinaryOutputStream(aResponse.bodyOutputStream);
      bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED_FIRST);
    },
    function secondPart(aRequest, aResponse) {
      let bos = new BinaryOutputStream(aResponse.bodyOutputStream);
      bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED_SECOND);
    }
  );

  gHttpServer.registerPathHandler(
    "/shorter-than-content-length-http-1-1.txt",
    function (aRequest, aResponse) {
      aResponse.processAsync();
      aResponse.setStatusLine("1.1", 200, "OK");
      aResponse.setHeader("Content-Type", "text/plain", false);
      aResponse.setHeader(
        "Content-Length",
        "" + TEST_DATA_SHORT.length * 2,
        false
      );
      aResponse.write(TEST_DATA_SHORT);
      aResponse.finish();
    }
  );

  gHttpServer.registerPathHandler("/busy.txt", function (aRequest, aResponse) {
    aResponse.setStatusLine("1.1", 504, "Gateway Timeout");
    aResponse.setHeader("Content-Type", "text/plain", false);
    aResponse.setHeader("Content-Length", "" + TEST_DATA_SHORT.length, false);
    aResponse.write(TEST_DATA_SHORT);
  });

  gHttpServer.registerPathHandler("/redirect", function (aRequest, aResponse) {
    aResponse.setStatusLine("1.1", 301, "Moved Permanently");
    aResponse.setHeader("Location", httpUrl("busy.txt"), false);
    aResponse.setHeader("Content-Type", "text/javascript", false);
    aResponse.setHeader("Content-Length", "0", false);
  });

  // This URL will emulate being blocked by Windows Parental controls
  gHttpServer.registerPathHandler(
    "/parentalblocked.zip",
    function (aRequest, aResponse) {
      aResponse.setStatusLine(
        aRequest.httpVersion,
        450,
        "Blocked by Windows Parental Controls"
      );
    }
  );

  // This URL sends some data followed by an RST packet
  gHttpServer.registerPathHandler(
    "/netreset.txt",
    function (aRequest, aResponse) {
      info("Starting response that will be aborted.");
      aResponse.processAsync();
      aResponse.setHeader("Content-Type", "text/plain", false);
      aResponse.write(TEST_DATA_SHORT);
      promiseExecuteSoon()
        .then(() => {
          aResponse.abort(null, true);
          aResponse.finish();
          info("Aborting response with network reset.");
        })
        .then(null, console.error);
    }
  );

  // During unit tests, most of the functions that require profile access or
  // operating system features will be disabled. Individual tests may override
  // them again to check for specific behaviors.
  Integration.downloads.register(base => {
    let override = {
      loadPublicDownloadListFromStore: () => Promise.resolve(),
      shouldKeepBlockedData: () => Promise.resolve(false),
      shouldBlockForParentalControls: () => Promise.resolve(false),
      shouldBlockForReputationCheck: () =>
        Promise.resolve({
          shouldBlock: false,
          verdict: "",
        }),
      confirmLaunchExecutable: () => Promise.resolve(),
      launchFile: () => Promise.resolve(),
      showContainingDirectory: () => Promise.resolve(),
      // This flag allows re-enabling the default observers during their tests.
      allowObservers: false,
      addListObservers() {
        return this.allowObservers
          ? super.addListObservers(...arguments)
          : Promise.resolve();
      },
      // This flag allows re-enabling the download directory logic for its tests.
      _allowDirectories: false,
      set allowDirectories(value) {
        this._allowDirectories = value;
        // We have to invalidate the previously computed directory path.
        this._downloadsDirectory = null;
      },
      _getDirectory(name) {
        return super._getDirectory(this._allowDirectories ? name : "TmpD");
      },
    };
    Object.setPrototypeOf(override, base);
    return override;
  });

  // Make sure that downloads started using nsIExternalHelperAppService are
  // saved to disk without asking for a destination interactively.
  let mock = {
    QueryInterface: ChromeUtils.generateQI(["nsIHelperAppLauncherDialog"]),
    promptForSaveToFileAsync(
      aLauncher,
      aWindowContext,
      aDefaultFileName,
      aSuggestedFileExtension,
      aForcePrompt
    ) {
      // The dialog should create the empty placeholder file.
      let file = getTempFile(TEST_TARGET_FILE_NAME);
      file.create(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
      aLauncher.saveDestinationAvailable(file);
    },
  };

  let cid = MockRegistrar.register(
    "@mozilla.org/helperapplauncherdialog;1",
    mock
  );
  registerCleanupFunction(() => {
    MockRegistrar.unregister(cid);
  });
});