summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_authentication.js
blob: 1e9617178cd2a5d21d1a57d8661d071df8e75365 (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
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
// This file tests authentication prompt callbacks
// TODO NIT use do_check_eq(expected, actual) consistently, not sometimes eq(actual, expected)

"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

// Turn off the authentication dialog blocking for this test.
Services.prefs.setIntPref("network.auth.subresource-http-auth-allow", 2);

XPCOMUtils.defineLazyGetter(this, "URL", function () {
  return "http://localhost:" + httpserv.identity.primaryPort;
});

XPCOMUtils.defineLazyGetter(this, "PORT", function () {
  return httpserv.identity.primaryPort;
});

const FLAG_RETURN_FALSE = 1 << 0;
const FLAG_WRONG_PASSWORD = 1 << 1;
const FLAG_BOGUS_USER = 1 << 2;
const FLAG_PREVIOUS_FAILED = 1 << 3;
const CROSS_ORIGIN = 1 << 4;
const FLAG_NO_REALM = 1 << 5;
const FLAG_NON_ASCII_USER_PASSWORD = 1 << 6;

const nsIAuthPrompt2 = Ci.nsIAuthPrompt2;
const nsIAuthInformation = Ci.nsIAuthInformation;

function AuthPrompt1(flags) {
  this.flags = flags;
}

AuthPrompt1.prototype = {
  user: "guest",
  pass: "guest",

  expectedRealm: "secret",

  QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt"]),

  prompt: function ap1_prompt(title, text, realm, save, defaultText, result) {
    do_throw("unexpected prompt call");
  },

  promptUsernameAndPassword: function ap1_promptUP(
    title,
    text,
    realm,
    savePW,
    user,
    pw
  ) {
    if (this.flags & FLAG_NO_REALM) {
      // Note that the realm here isn't actually the realm. it's a pw mgr key.
      Assert.equal(URL + " (" + this.expectedRealm + ")", realm);
    }
    if (!(this.flags & CROSS_ORIGIN)) {
      if (!text.includes(this.expectedRealm)) {
        do_throw("Text must indicate the realm");
      }
    } else if (text.includes(this.expectedRealm)) {
      do_throw("There should not be realm for cross origin");
    }
    if (!text.includes("localhost")) {
      do_throw("Text must indicate the hostname");
    }
    if (!text.includes(String(PORT))) {
      do_throw("Text must indicate the port");
    }
    if (text.includes("-1")) {
      do_throw("Text must contain negative numbers");
    }

    if (this.flags & FLAG_RETURN_FALSE) {
      return false;
    }

    if (this.flags & FLAG_BOGUS_USER) {
      this.user = "foo\nbar";
    } else if (this.flags & FLAG_NON_ASCII_USER_PASSWORD) {
      this.user = "é";
    }

    user.value = this.user;
    if (this.flags & FLAG_WRONG_PASSWORD) {
      pw.value = this.pass + ".wrong";
      // Now clear the flag to avoid an infinite loop
      this.flags &= ~FLAG_WRONG_PASSWORD;
    } else if (this.flags & FLAG_NON_ASCII_USER_PASSWORD) {
      pw.value = "é";
    } else {
      pw.value = this.pass;
    }
    return true;
  },

  promptPassword: function ap1_promptPW(title, text, realm, save, pwd) {
    do_throw("unexpected promptPassword call");
  },
};

function AuthPrompt2(flags) {
  this.flags = flags;
}

AuthPrompt2.prototype = {
  user: "guest",
  pass: "guest",

  expectedRealm: "secret",

  QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt2"]),

  promptAuth: function ap2_promptAuth(channel, level, authInfo) {
    var isNTLM = channel.URI.pathQueryRef.includes("ntlm");
    var isDigest = channel.URI.pathQueryRef.includes("digest");

    if (isNTLM || this.flags & FLAG_NO_REALM) {
      this.expectedRealm = ""; // NTLM knows no realms
    }

    Assert.equal(this.expectedRealm, authInfo.realm);

    var expectedLevel =
      isNTLM || isDigest
        ? nsIAuthPrompt2.LEVEL_PW_ENCRYPTED
        : nsIAuthPrompt2.LEVEL_NONE;
    Assert.equal(expectedLevel, level);

    var expectedFlags = nsIAuthInformation.AUTH_HOST;

    if (this.flags & FLAG_PREVIOUS_FAILED) {
      expectedFlags |= nsIAuthInformation.PREVIOUS_FAILED;
    }

    if (this.flags & CROSS_ORIGIN) {
      expectedFlags |= nsIAuthInformation.CROSS_ORIGIN_SUB_RESOURCE;
    }

    if (isNTLM) {
      expectedFlags |= nsIAuthInformation.NEED_DOMAIN;
    }

    const kAllKnownFlags = 127; // Don't fail test for newly added flags
    Assert.equal(expectedFlags, authInfo.flags & kAllKnownFlags);

    // eslint-disable-next-line no-nested-ternary
    var expectedScheme = isNTLM ? "ntlm" : isDigest ? "digest" : "basic";
    Assert.equal(expectedScheme, authInfo.authenticationScheme);

    // No passwords in the URL -> nothing should be prefilled
    Assert.equal(authInfo.username, "");
    Assert.equal(authInfo.password, "");
    Assert.equal(authInfo.domain, "");

    if (this.flags & FLAG_RETURN_FALSE) {
      this.flags |= FLAG_PREVIOUS_FAILED;
      return false;
    }

    if (this.flags & FLAG_BOGUS_USER) {
      this.user = "foo\nbar";
    } else if (this.flags & FLAG_NON_ASCII_USER_PASSWORD) {
      this.user = "é";
    }

    authInfo.username = this.user;
    if (this.flags & FLAG_WRONG_PASSWORD) {
      authInfo.password = this.pass + ".wrong";
      this.flags |= FLAG_PREVIOUS_FAILED;
      // Now clear the flag to avoid an infinite loop
      this.flags &= ~FLAG_WRONG_PASSWORD;
    } else if (this.flags & FLAG_NON_ASCII_USER_PASSWORD) {
      authInfo.password = "é";
    } else {
      authInfo.password = this.pass;
      this.flags &= ~FLAG_PREVIOUS_FAILED;
    }
    return true;
  },

  asyncPromptAuth: function ap2_async(chan, cb, ctx, lvl, info) {
    let self = this;
    executeSoon(function () {
      let ret = self.promptAuth(chan, lvl, info);
      if (ret) {
        cb.onAuthAvailable(ctx, info);
      } else {
        cb.onAuthCancelled(ctx, true);
      }
    });
  },
};

function Requestor(flags, versions) {
  this.flags = flags;
  this.versions = versions;
}

Requestor.prototype = {
  QueryInterface: ChromeUtils.generateQI(["nsIInterfaceRequestor"]),

  getInterface: function requestor_gi(iid) {
    if (this.versions & 1 && iid.equals(Ci.nsIAuthPrompt)) {
      // Allow the prompt to store state by caching it here
      if (!this.prompt1) {
        this.prompt1 = new AuthPrompt1(this.flags);
      }
      return this.prompt1;
    }
    if (this.versions & 2 && iid.equals(Ci.nsIAuthPrompt2)) {
      // Allow the prompt to store state by caching it here
      if (!this.prompt2) {
        this.prompt2 = new AuthPrompt2(this.flags);
      }
      return this.prompt2;
    }

    throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
  },

  prompt1: null,
  prompt2: null,
};

function RealmTestRequestor() {
  this.promptRealm = "";
}

RealmTestRequestor.prototype = {
  QueryInterface: ChromeUtils.generateQI([
    "nsIInterfaceRequestor",
    "nsIAuthPrompt2",
  ]),

  getInterface: function realmtest_interface(iid) {
    if (iid.equals(Ci.nsIAuthPrompt2)) {
      return this;
    }

    throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
  },

  promptAuth: function realmtest_checkAuth(channel, level, authInfo) {
    this.promptRealm = authInfo.realm;

    return false;
  },

  asyncPromptAuth: function realmtest_async(chan, cb, ctx, lvl, info) {
    throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
  },
};

var listener = {
  expectedCode: -1, // Uninitialized
  nextTest: undefined,

  onStartRequest: function test_onStartR(request) {
    try {
      if (!Components.isSuccessCode(request.status)) {
        do_throw("Channel should have a success code!");
      }

      if (!(request instanceof Ci.nsIHttpChannel)) {
        do_throw("Expecting an HTTP channel");
      }

      Assert.equal(request.responseStatus, this.expectedCode);
      // The request should be succeeded if we expect 200
      Assert.equal(request.requestSucceeded, this.expectedCode == 200);
    } catch (e) {
      do_throw("Unexpected exception: " + e);
    }

    throw Components.Exception("", Cr.NS_ERROR_ABORT);
  },

  onDataAvailable: function test_ODA() {
    do_throw("Should not get any data!");
  },

  onStopRequest: function test_onStopR(request, status) {
    Assert.equal(status, Cr.NS_ERROR_ABORT);

    this.nextTest();
  },
};

function makeChan(
  url,
  loadingUrl,
  securityFlags = Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
  contentPolicyType = Ci.nsIContentPolicy.TYPE_OTHER
) {
  var principal = Services.scriptSecurityManager.createContentPrincipal(
    Services.io.newURI(loadingUrl),
    {}
  );
  return NetUtil.newChannel({
    uri: url,
    loadingPrincipal: principal,
    securityFlags,
    contentPolicyType,
  });
}

var httpserv = null;

function setup() {
  httpserv = new HttpServer();

  httpserv.registerPathHandler("/auth", authHandler);
  httpserv.registerPathHandler("/auth/ntlm/simple", authNtlmSimple);
  httpserv.registerPathHandler("/auth/realm", authRealm);
  httpserv.registerPathHandler("/auth/non_ascii", authNonascii);
  httpserv.registerPathHandler("/auth/digest_md5", authDigestMD5);
  httpserv.registerPathHandler("/auth/digest_md5sess", authDigestMD5sess);
  httpserv.registerPathHandler("/auth/digest_sha256", authDigestSHA256);
  httpserv.registerPathHandler("/auth/digest_sha256sess", authDigestSHA256sess);
  httpserv.registerPathHandler("/auth/digest_sha256_md5", authDigestSHA256_MD5);
  httpserv.registerPathHandler("/auth/digest_md5_sha256", authDigestMD5_SHA256);
  httpserv.registerPathHandler(
    "/auth/digest_md5_sha256_oneline",
    authDigestMD5_SHA256_oneline
  );
  httpserv.registerPathHandler("/auth/short_digest", authShortDigest);
  httpserv.registerPathHandler("/largeRealm", largeRealm);
  httpserv.registerPathHandler("/largeDomain", largeDomain);

  httpserv.registerPathHandler("/corp-coep", corpAndCoep);

  httpserv.start(-1);

  registerCleanupFunction(async () => {
    await httpserv.stop();
  });
}
setup();

async function openAndListen(chan) {
  await new Promise(resolve => {
    listener.nextTest = resolve;
    chan.asyncOpen(listener);
  });
  Cc["@mozilla.org/network/http-auth-manager;1"]
    .getService(Ci.nsIHttpAuthManager)
    .clearAll();
}

add_task(async function test_noauth() {
  var chan = makeChan(URL + "/auth", URL);

  listener.expectedCode = 401; // Unauthorized
  await openAndListen(chan);
});

add_task(async function test_returnfalse1() {
  var chan = makeChan(URL + "/auth", URL);

  chan.notificationCallbacks = new Requestor(FLAG_RETURN_FALSE, 1);
  listener.expectedCode = 401; // Unauthorized
  await openAndListen(chan);
});

add_task(async function test_wrongpw1() {
  var chan = makeChan(URL + "/auth", URL);

  chan.notificationCallbacks = new Requestor(FLAG_WRONG_PASSWORD, 1);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_prompt1() {
  var chan = makeChan(URL + "/auth", URL);

  chan.notificationCallbacks = new Requestor(0, 1);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_prompt1CrossOrigin() {
  var chan = makeChan(URL + "/auth", "http://example.org");

  chan.notificationCallbacks = new Requestor(16, 1);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_prompt2CrossOrigin() {
  var chan = makeChan(URL + "/auth", "http://example.org");

  chan.notificationCallbacks = new Requestor(16, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_returnfalse2() {
  var chan = makeChan(URL + "/auth", URL);

  chan.notificationCallbacks = new Requestor(FLAG_RETURN_FALSE, 2);
  listener.expectedCode = 401; // Unauthorized
  await openAndListen(chan);
});

add_task(async function test_wrongpw2() {
  var chan = makeChan(URL + "/auth", URL);

  chan.notificationCallbacks = new Requestor(FLAG_WRONG_PASSWORD, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_prompt2() {
  var chan = makeChan(URL + "/auth", URL);

  chan.notificationCallbacks = new Requestor(0, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_ntlm() {
  var chan = makeChan(URL + "/auth/ntlm/simple", URL);

  chan.notificationCallbacks = new Requestor(FLAG_RETURN_FALSE, 2);
  listener.expectedCode = 401; // Unauthorized
  await openAndListen(chan);
});

add_task(async function test_basicrealm() {
  var chan = makeChan(URL + "/auth/realm", URL);

  let requestor = new RealmTestRequestor();
  chan.notificationCallbacks = requestor;
  listener.expectedCode = 401; // Unauthorized
  await openAndListen(chan);
  Assert.equal(requestor.promptRealm, '"foo_bar');
});

add_task(async function test_nonascii() {
  var chan = makeChan(URL + "/auth/non_ascii", URL);

  chan.notificationCallbacks = new Requestor(FLAG_NON_ASCII_USER_PASSWORD, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_digest_noauth() {
  var chan = makeChan(URL + "/auth/digest_md5", URL);

  // chan.notificationCallbacks = new Requestor(FLAG_RETURN_FALSE, 2);
  listener.expectedCode = 401; // Unauthorized
  await openAndListen(chan);
});

add_task(async function test_digest_md5() {
  var chan = makeChan(URL + "/auth/digest_md5", URL);

  chan.notificationCallbacks = new Requestor(0, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_digest_md5sess() {
  var chan = makeChan(URL + "/auth/digest_md5sess", URL);

  chan.notificationCallbacks = new Requestor(0, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_digest_sha256() {
  var chan = makeChan(URL + "/auth/digest_sha256", URL);

  chan.notificationCallbacks = new Requestor(0, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_digest_sha256sess() {
  var chan = makeChan(URL + "/auth/digest_sha256sess", URL);

  chan.notificationCallbacks = new Requestor(0, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_digest_sha256_md5() {
  var chan = makeChan(URL + "/auth/digest_sha256_md5", URL);

  chan.notificationCallbacks = new Requestor(0, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_digest_md5_sha256() {
  var chan = makeChan(URL + "/auth/digest_md5_sha256", URL);

  chan.notificationCallbacks = new Requestor(0, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_digest_md5_sha256_oneline() {
  var chan = makeChan(URL + "/auth/digest_md5_sha256_oneline", URL);

  chan.notificationCallbacks = new Requestor(0, 2);
  listener.expectedCode = 200; // OK
  await openAndListen(chan);
});

add_task(async function test_digest_bogus_user() {
  var chan = makeChan(URL + "/auth/digest_md5", URL);
  chan.notificationCallbacks = new Requestor(FLAG_BOGUS_USER, 2);
  listener.expectedCode = 401; // unauthorized
  await openAndListen(chan);
});

// Test header "WWW-Authenticate: Digest" - bug 1338876.
add_task(async function test_short_digest() {
  var chan = makeChan(URL + "/auth/short_digest", URL);
  chan.notificationCallbacks = new Requestor(FLAG_NO_REALM, 2);
  listener.expectedCode = 401; // OK
  await openAndListen(chan);
});

// Test that COOP/COEP are processed even though asyncPromptAuth is cancelled.
add_task(async function test_corp_coep() {
  var chan = makeChan(
    URL + "/corp-coep",
    URL,
    Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT,
    Ci.nsIContentPolicy.TYPE_DOCUMENT
  );

  chan.notificationCallbacks = new Requestor(FLAG_RETURN_FALSE, 2);
  listener.expectedCode = 401; // OK
  await openAndListen(chan);

  Assert.equal(
    chan.getResponseHeader("cross-origin-embedder-policy"),
    "require-corp"
  );
  Assert.equal(
    chan.getResponseHeader("cross-origin-opener-policy"),
    "same-origin"
  );
});

// XXX(valentin): this makes tests fail if it's not run last. Why?
add_task(async function test_nonascii_xhr() {
  await new Promise(resolve => {
    let xhr = new XMLHttpRequest();
    xhr.open("GET", URL + "/auth/non_ascii", true, "é", "é");
    xhr.onreadystatechange = function (event) {
      if (xhr.readyState == 4) {
        Assert.equal(xhr.status, 200);
        resolve();
        xhr.onreadystatechange = null;
      }
    };
    xhr.send(null);
  });
});

// PATH HANDLERS

// /auth
function authHandler(metadata, response) {
  // btoa("guest:guest"), but that function is not available here
  var expectedHeader = "Basic Z3Vlc3Q6Z3Vlc3Q=";

  var body;
  if (
    metadata.hasHeader("Authorization") &&
    metadata.getHeader("Authorization") == expectedHeader
  ) {
    response.setStatusLine(metadata.httpVersion, 200, "OK, authorized");
    response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);

    body = "success";
  } else {
    // didn't know guest:guest, failure
    response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
    response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);

    body = "failed";
  }

  response.bodyOutputStream.write(body, body.length);
}

// /auth/ntlm/simple
function authNtlmSimple(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
  response.setHeader(
    "WWW-Authenticate",
    "NTLM" /* + ' realm="secret"' */,
    false
  );

  var body =
    "NOTE: This just sends an NTLM challenge, it never\n" +
    "accepts the authentication. It also closes\n" +
    "the connection after sending the challenge\n";

  response.bodyOutputStream.write(body, body.length);
}

// /auth/realm
function authRealm(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
  response.setHeader("WWW-Authenticate", 'Basic realm="\\"f\\oo_bar"', false);
  var body = "success";

  response.bodyOutputStream.write(body, body.length);
}

// /auth/nonAscii
function authNonascii(metadata, response) {
  // btoa("é:é"), but that function is not available here
  var expectedHeader = "Basic w6k6w6k=";

  var body;
  if (
    metadata.hasHeader("Authorization") &&
    metadata.getHeader("Authorization") == expectedHeader
  ) {
    response.setStatusLine(metadata.httpVersion, 200, "OK, authorized");
    response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);

    // Use correct XML syntax since this function is also used for testing XHR.
    body = "<?xml version='1.0' ?><root>success</root>";
  } else {
    // didn't know é:é, failure
    response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
    response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);

    body = "<?xml version='1.0' ?><root>failed</root>";
  }

  response.bodyOutputStream.write(body, body.length);
}

function corpAndCoep(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
  response.setHeader("cross-origin-embedder-policy", "require-corp");
  response.setHeader("cross-origin-opener-policy", "same-origin");
  response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
}

//
// Digest functions
//
function bytesFromString(str) {
  var converter = Cc[
    "@mozilla.org/intl/scriptableunicodeconverter"
  ].createInstance(Ci.nsIScriptableUnicodeConverter);
  converter.charset = "UTF-8";
  var data = converter.convertToByteArray(str);
  return data;
}

// return the two-digit hexadecimal code for a byte
function toHexString(charCode) {
  return ("0" + charCode.toString(16)).slice(-2);
}

function HMD5(str) {
  var data = bytesFromString(str);
  var ch = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
  ch.init(Ci.nsICryptoHash.MD5);
  ch.update(data, data.length);
  var hash = ch.finish(false);
  return Array.from(hash, (c, i) => toHexString(hash.charCodeAt(i))).join("");
}

function HSHA256(str) {
  var data = bytesFromString(str);
  var ch = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
  ch.init(Ci.nsICryptoHash.SHA256);
  ch.update(data, data.length);
  var hash = ch.finish(false);
  return Array.from(hash, (c, i) => toHexString(hash.charCodeAt(i))).join("");
}

//
// Digest handler
//
// /auth/digest
function authDigestMD5_helper(metadata, response, test_name) {
  var nonce = "6f93719059cf8d568005727f3250e798";
  var opaque = "1234opaque1234";
  var body;
  var send_401 = 0;
  // check creds if we have them
  if (metadata.hasHeader("Authorization")) {
    var cnonceRE = /cnonce="(\w+)"/;
    var responseRE = /response="(\w+)"/;
    var usernameRE = /username="(\w+)"/;
    var algorithmRE = /algorithm=([\w-]+)/;
    var auth = metadata.getHeader("Authorization");
    var cnonce = auth.match(cnonceRE)[1];
    var clientDigest = auth.match(responseRE)[1];
    var username = auth.match(usernameRE)[1];
    var algorithm = auth.match(algorithmRE)[1];
    var nc = "00000001";

    if (username != "guest") {
      response.setStatusLine(metadata.httpVersion, 400, "bad request");
      body = "should never get here";
    } else if (
      algorithm != null &&
      algorithm != "MD5" &&
      algorithm != "MD5-sess"
    ) {
      response.setStatusLine(metadata.httpVersion, 400, "bad request");
      body = "Algorithm must be same as provided in WWW-Authenticate header";
    } else {
      // see RFC2617 for the description of this calculation
      var A1 = "guest:secret:guest";
      if (algorithm == "MD5-sess") {
        A1 = [HMD5(A1), nonce, cnonce].join(":");
      }
      var A2 = "GET:/auth/" + test_name;
      var noncebits = [nonce, nc, cnonce, "auth", HMD5(A2)].join(":");
      var digest = HMD5([HMD5(A1), noncebits].join(":"));

      if (clientDigest == digest) {
        response.setStatusLine(metadata.httpVersion, 200, "OK, authorized");
        body = "success";
      } else {
        send_401 = 1;
        body = "auth failed";
      }
    }
  } else {
    // no header, send one
    send_401 = 1;
    body = "failed, no header";
  }

  if (send_401) {
    var authenticate_md5 =
      'Digest realm="secret", domain="/",  qop=auth,' +
      'algorithm=MD5, nonce="' +
      nonce +
      '" opaque="' +
      opaque +
      '"';
    var authenticate_md5sess =
      'Digest realm="secret", domain="/",  qop=auth,' +
      'algorithm=MD5, nonce="' +
      nonce +
      '" opaque="' +
      opaque +
      '"';
    if (test_name == "digest_md5") {
      response.setHeader("WWW-Authenticate", authenticate_md5, false);
    } else if (test_name == "digest_md5sess") {
      response.setHeader("WWW-Authenticate", authenticate_md5sess, false);
    }
    response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
  }

  response.bodyOutputStream.write(body, body.length);
}

function authDigestMD5(metadata, response) {
  authDigestMD5_helper(metadata, response, "digest_md5");
}

function authDigestMD5sess(metadata, response) {
  authDigestMD5_helper(metadata, response, "digest_md5sess");
}

function authDigestSHA256_helper(metadata, response, test_name) {
  var nonce = "6f93719059cf8d568005727f3250e798";
  var opaque = "1234opaque1234";
  var body;
  var send_401 = 0;
  // check creds if we have them
  if (metadata.hasHeader("Authorization")) {
    var cnonceRE = /cnonce="(\w+)"/;
    var responseRE = /response="(\w+)"/;
    var usernameRE = /username="(\w+)"/;
    var algorithmRE = /algorithm=([\w-]+)/;
    var auth = metadata.getHeader("Authorization");
    var cnonce = auth.match(cnonceRE)[1];
    var clientDigest = auth.match(responseRE)[1];
    var username = auth.match(usernameRE)[1];
    var algorithm = auth.match(algorithmRE)[1];
    var nc = "00000001";

    if (username != "guest") {
      response.setStatusLine(metadata.httpVersion, 400, "bad request");
      body = "should never get here";
    } else if (algorithm != "SHA-256" && algorithm != "SHA-256-sess") {
      response.setStatusLine(metadata.httpVersion, 400, "bad request");
      body = "Algorithm must be same as provided in WWW-Authenticate header";
    } else {
      // see RFC7616 for the description of this calculation
      var A1 = "guest:secret:guest";
      if (algorithm == "SHA-256-sess") {
        A1 = [HSHA256(A1), nonce, cnonce].join(":");
      }
      var A2 = "GET:/auth/" + test_name;
      var noncebits = [nonce, nc, cnonce, "auth", HSHA256(A2)].join(":");
      var digest = HSHA256([HSHA256(A1), noncebits].join(":"));

      if (clientDigest == digest) {
        response.setStatusLine(metadata.httpVersion, 200, "OK, authorized");
        body = "success";
      } else {
        send_401 = 1;
        body = "auth failed";
      }
    }
  } else {
    // no header, send one
    send_401 = 1;
    body = "failed, no header";
  }

  if (send_401) {
    var authenticate_sha256 =
      'Digest realm="secret", domain="/", qop=auth, ' +
      'algorithm=SHA-256, nonce="' +
      nonce +
      '", opaque="' +
      opaque +
      '"';
    var authenticate_sha256sess =
      'Digest realm="secret", domain="/", qop=auth, ' +
      'algorithm=SHA-256-sess, nonce="' +
      nonce +
      '", opaque="' +
      opaque +
      '"';
    var authenticate_md5 =
      'Digest realm="secret", domain="/", qop=auth, ' +
      'algorithm=MD5, nonce="' +
      nonce +
      '", opaque="' +
      opaque +
      '"';
    if (test_name == "digest_sha256") {
      response.setHeader("WWW-Authenticate", authenticate_sha256, false);
    } else if (test_name == "digest_sha256sess") {
      response.setHeader("WWW-Authenticate", authenticate_sha256sess, false);
    } else if (test_name == "digest_md5_sha256") {
      response.setHeader("WWW-Authenticate", authenticate_md5, false);
      response.setHeader("WWW-Authenticate", authenticate_sha256, true);
    } else if (test_name == "digest_md5_sha256_oneline") {
      response.setHeader(
        "WWW-Authenticate",
        authenticate_md5 + " " + authenticate_sha256,
        false
      );
    } else if (test_name == "digest_sha256_md5") {
      response.setHeader("WWW-Authenticate", authenticate_sha256, false);
      response.setHeader("WWW-Authenticate", authenticate_md5, true);
    }
    response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
  }

  response.bodyOutputStream.write(body, body.length);
}

function authDigestSHA256(metadata, response) {
  authDigestSHA256_helper(metadata, response, "digest_sha256");
}

function authDigestSHA256sess(metadata, response) {
  authDigestSHA256_helper(metadata, response, "digest_sha256sess");
}

function authDigestSHA256_MD5(metadata, response) {
  authDigestSHA256_helper(metadata, response, "digest_sha256_md5");
}

function authDigestMD5_SHA256(metadata, response) {
  authDigestSHA256_helper(metadata, response, "digest_md5_sha256");
}

function authDigestMD5_SHA256_oneline(metadata, response) {
  authDigestSHA256_helper(metadata, response, "digest_md5_sha256_oneline");
}

function authShortDigest(metadata, response) {
  // no header, send one
  response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
  response.setHeader("WWW-Authenticate", "Digest", false);
}

let buildLargePayload = (function () {
  let size = 33 * 1024;
  let ret = "";
  return function () {
    // Return cached value.
    if (ret.length) {
      return ret;
    }
    for (let i = 0; i < size; i++) {
      ret += "a";
    }
    return ret;
  };
})();

function largeRealm(metadata, response) {
  // test > 32KB realm tokens
  var body;

  response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
  response.setHeader(
    "WWW-Authenticate",
    'Digest realm="' + buildLargePayload() + '", domain="foo"'
  );

  body = "need to authenticate";
  response.bodyOutputStream.write(body, body.length);
}

function largeDomain(metadata, response) {
  // test > 32KB domain tokens
  var body;

  response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
  response.setHeader(
    "WWW-Authenticate",
    'Digest realm="foo", domain="' + buildLargePayload() + '"'
  );

  body = "need to authenticate";
  response.bodyOutputStream.write(body, body.length);
}

add_task(async function test_large_realm() {
  var chan = makeChan(URL + "/largeRealm", URL);

  listener.expectedCode = 401; // Unauthorized
  await openAndListen(chan);
});

add_task(async function test_large_domain() {
  var chan = makeChan(URL + "/largeDomain", URL);

  listener.expectedCode = 401; // Unauthorized
  await openAndListen(chan);
});

async function add_parse_realm_testcase(testcase) {
  httpserv.registerPathHandler("/parse_realm", (metadata, response) => {
    response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
    response.setHeader("WWW-Authenticate", testcase.input, false);

    let body = "failed";
    response.bodyOutputStream.write(body, body.length);
  });

  let chan = makeChan(URL + "/parse_realm", URL);
  let requestor = new RealmTestRequestor();
  chan.notificationCallbacks = requestor;

  listener.expectedCode = 401;
  await openAndListen(chan);
  Assert.equal(requestor.promptRealm, testcase.realm);
}

add_task(async function simplebasic() {
  await add_parse_realm_testcase({
    input: `Basic realm="foo"`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasiclf() {
  await add_parse_realm_testcase({
    input: `Basic\r\n realm="foo"`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasicucase() {
  await add_parse_realm_testcase({
    input: `BASIC REALM="foo"`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasictok() {
  await add_parse_realm_testcase({
    input: `Basic realm=foo`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasictokbs() {
  await add_parse_realm_testcase({
    input: `Basic realm=\\f\\o\\o`,
    scheme: `Basic`,
    realm: `\\foo`,
  });
});

add_task(async function simplebasicsq() {
  await add_parse_realm_testcase({
    input: `Basic realm='foo'`,
    scheme: `Basic`,
    realm: `'foo'`,
  });
});

add_task(async function simplebasicpct() {
  await add_parse_realm_testcase({
    input: `Basic realm="foo%20bar"`,
    scheme: `Basic`,
    realm: `foo%20bar`,
  });
});

add_task(async function simplebasiccomma() {
  await add_parse_realm_testcase({
    input: `Basic , realm="foo"`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasiccomma2() {
  await add_parse_realm_testcase({
    input: `Basic, realm="foo"`,
    scheme: `Basic`,
    realm: ``,
  });
});

add_task(async function simplebasicnorealm() {
  await add_parse_realm_testcase({
    input: `Basic`,
    scheme: `Basic`,
    realm: ``,
  });
});

add_task(async function simplebasic2realms() {
  await add_parse_realm_testcase({
    input: `Basic realm="foo", realm="bar"`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasicwsrealm() {
  await add_parse_realm_testcase({
    input: `Basic realm = "foo"`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasicrealmsqc() {
  await add_parse_realm_testcase({
    input: `Basic realm="\\f\\o\\o"`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasicrealmsqc2() {
  await add_parse_realm_testcase({
    input: `Basic realm="\\"foo\\""`,
    scheme: `Basic`,
    realm: `"foo"`,
  });
});

add_task(async function simplebasicnewparam1() {
  await add_parse_realm_testcase({
    input: `Basic realm="foo", bar="xyz",, a=b,,,c=d`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasicnewparam2() {
  await add_parse_realm_testcase({
    input: `Basic bar="xyz", realm="foo"`,
    scheme: `Basic`,
    realm: `foo`,
  });
});

add_task(async function simplebasicrealmiso88591() {
  await add_parse_realm_testcase({
    input: `Basic realm="foo-ä"`,
    scheme: `Basic`,
    realm: `foo-ä`,
  });
});

add_task(async function simplebasicrealmutf8() {
  await add_parse_realm_testcase({
    input: `Basic realm="foo-ä"`,
    scheme: `Basic`,
    realm: `foo-ä`,
  });
});

add_task(async function simplebasicrealmrfc2047() {
  await add_parse_realm_testcase({
    input: `Basic realm="=?ISO-8859-1?Q?foo-=E4?="`,
    scheme: `Basic`,
    realm: `=?ISO-8859-1?Q?foo-=E4?=`,
  });
});

add_task(async function multibasicunknown() {
  await add_parse_realm_testcase({
    input: `Basic realm="basic", Newauth realm="newauth"`,
    scheme: `Basic`,
    realm: `basic`,
  });
});

add_task(async function multibasicunknownnoparam() {
  await add_parse_realm_testcase({
    input: `Basic realm="basic", Newauth`,
    scheme: `Basic`,
    realm: `basic`,
  });
});

add_task(async function multibasicunknown2() {
  await add_parse_realm_testcase({
    input: `Newauth realm="newauth", Basic realm="basic"`,
    scheme: `Basic`,
    realm: `basic`,
  });
});

add_task(async function multibasicunknown2np() {
  await add_parse_realm_testcase({
    input: `Newauth, Basic realm="basic"`,
    scheme: `Basic`,
    realm: `basic`,
  });
});

add_task(async function multibasicunknown2mf() {
  httpserv.registerPathHandler("/parse_realm", (metadata, response) => {
    response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
    response.setHeader("WWW-Authenticate", `Newauth realm="newauth"`, false);
    response.setHeader("WWW-Authenticate", `Basic realm="basic"`, false);

    let body = "failed";
    response.bodyOutputStream.write(body, body.length);
  });

  let chan = makeChan(URL + "/parse_realm", URL);
  let requestor = new RealmTestRequestor();
  chan.notificationCallbacks = requestor;

  listener.expectedCode = 401;
  await openAndListen(chan);
  Assert.equal(requestor.promptRealm, "basic");
});

add_task(async function multibasicempty() {
  await add_parse_realm_testcase({
    input: `,Basic realm="basic"`,
    scheme: `Basic`,
    realm: `basic`,
  });
});

add_task(async function multibasicqs() {
  await add_parse_realm_testcase({
    input: `Newauth realm="apps", type=1, title="Login to \"apps\"", Basic realm="simple"`,
    scheme: `Basic`,
    realm: `simple`,
  });
});

add_task(async function multidisgscheme() {
  await add_parse_realm_testcase({
    input: `Newauth realm="Newauth Realm", basic=foo, Basic realm="Basic Realm"`,
    scheme: `Basic`,
    realm: `Basic Realm`,
  });
});

add_task(async function unknown() {
  await add_parse_realm_testcase({
    input: `Newauth param="value"`,
    scheme: `Basic`,
    realm: ``,
  });
});

add_task(async function parametersnotrequired() {
  await add_parse_realm_testcase({ input: `A, B`, scheme: `Basic`, realm: `` });
});

add_task(async function disguisedrealm() {
  await add_parse_realm_testcase({
    input: `Basic foo="realm=nottherealm", realm="basic"`,
    scheme: `Basic`,
    realm: `basic`,
  });
});

add_task(async function disguisedrealm2() {
  await add_parse_realm_testcase({
    input: `Basic nottherealm="nottherealm", realm="basic"`,
    scheme: `Basic`,
    realm: `basic`,
  });
});

add_task(async function missingquote() {
  await add_parse_realm_testcase({
    input: `Basic realm="basic`,
    scheme: `Basic`,
    realm: `basic`,
  });
});