summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_standardurl.js
blob: b44289f2e665413836821ffed52d3532115a8d9b (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
"use strict";

const gPrefs = Services.prefs;

function symmetricEquality(expect, a, b) {
  /* Use if/else instead of |do_check_eq(expect, a.spec == b.spec)| so
     that we get the specs output on the console if the check fails.
   */
  if (expect) {
    /* Check all the sub-pieces too, since that can help with
       debugging cases when equals() returns something unexpected */
    /* We don't check port in the loop, because it can be defaulted in
       some cases. */
    [
      "spec",
      "prePath",
      "scheme",
      "userPass",
      "username",
      "password",
      "hostPort",
      "host",
      "pathQueryRef",
      "filePath",
      "query",
      "ref",
      "directory",
      "fileName",
      "fileBaseName",
      "fileExtension",
    ].map(function (prop) {
      dump("Testing '" + prop + "'\n");
      Assert.equal(a[prop], b[prop]);
    });
  } else {
    Assert.notEqual(a.spec, b.spec);
  }
  Assert.equal(expect, a.equals(b));
  Assert.equal(expect, b.equals(a));
}

function stringToURL(str) {
  return Cc["@mozilla.org/network/standard-url-mutator;1"]
    .createInstance(Ci.nsIStandardURLMutator)
    .init(Ci.nsIStandardURL.URLTYPE_AUTHORITY, 80, str, "UTF-8", null)
    .finalize()
    .QueryInterface(Ci.nsIURL);
}

function pairToURLs(pair) {
  Assert.equal(pair.length, 2);
  return pair.map(stringToURL);
}

add_test(function test_setEmptyPath() {
  var pairs = [
    ["http://example.com", "http://example.com/tests/dom/tests"],
    ["http://example.com:80", "http://example.com/tests/dom/tests"],
    ["http://example.com:80/", "http://example.com/tests/dom/test"],
    ["http://example.com/", "http://example.com/tests/dom/tests"],
    ["http://example.com/a", "http://example.com/tests/dom/tests"],
    ["http://example.com:80/a", "http://example.com/tests/dom/tests"],
  ].map(pairToURLs);

  for (var [provided, target] of pairs) {
    symmetricEquality(false, target, provided);

    provided = provided.mutate().setPathQueryRef("").finalize();
    target = target.mutate().setPathQueryRef("").finalize();

    Assert.equal(provided.spec, target.spec);
    symmetricEquality(true, target, provided);
  }
  run_next_test();
});

add_test(function test_setQuery() {
  var pairs = [
    ["http://example.com", "http://example.com/?foo"],
    ["http://example.com/bar", "http://example.com/bar?foo"],
    ["http://example.com#bar", "http://example.com/?foo#bar"],
    ["http://example.com/#bar", "http://example.com/?foo#bar"],
    ["http://example.com/?longerthanfoo#bar", "http://example.com/?foo#bar"],
    ["http://example.com/?longerthanfoo", "http://example.com/?foo"],
    /* And one that's nonempty but shorter than "foo" */
    ["http://example.com/?f#bar", "http://example.com/?foo#bar"],
    ["http://example.com/?f", "http://example.com/?foo"],
  ].map(pairToURLs);

  for (var [provided, target] of pairs) {
    symmetricEquality(false, provided, target);

    provided = provided
      .mutate()
      .setQuery("foo")
      .finalize()
      .QueryInterface(Ci.nsIURL);

    Assert.equal(provided.spec, target.spec);
    symmetricEquality(true, provided, target);
  }

  [provided, target] = [
    "http://example.com/#",
    "http://example.com/?foo#bar",
  ].map(stringToURL);
  symmetricEquality(false, provided, target);
  provided = provided
    .mutate()
    .setQuery("foo")
    .finalize()
    .QueryInterface(Ci.nsIURL);
  symmetricEquality(false, provided, target);

  var newProvided = Services.io
    .newURI("#bar", null, provided)
    .QueryInterface(Ci.nsIURL);

  Assert.equal(newProvided.spec, target.spec);
  symmetricEquality(true, newProvided, target);
  run_next_test();
});

add_test(function test_setRef() {
  var tests = [
    ["http://example.com", "", "http://example.com/"],
    ["http://example.com:80", "", "http://example.com:80/"],
    ["http://example.com:80/", "", "http://example.com:80/"],
    ["http://example.com/", "", "http://example.com/"],
    ["http://example.com/a", "", "http://example.com/a"],
    ["http://example.com:80/a", "", "http://example.com:80/a"],

    ["http://example.com", "x", "http://example.com/#x"],
    ["http://example.com:80", "x", "http://example.com:80/#x"],
    ["http://example.com:80/", "x", "http://example.com:80/#x"],
    ["http://example.com/", "x", "http://example.com/#x"],
    ["http://example.com/a", "x", "http://example.com/a#x"],
    ["http://example.com:80/a", "x", "http://example.com:80/a#x"],

    ["http://example.com", "xx", "http://example.com/#xx"],
    ["http://example.com:80", "xx", "http://example.com:80/#xx"],
    ["http://example.com:80/", "xx", "http://example.com:80/#xx"],
    ["http://example.com/", "xx", "http://example.com/#xx"],
    ["http://example.com/a", "xx", "http://example.com/a#xx"],
    ["http://example.com:80/a", "xx", "http://example.com:80/a#xx"],

    [
      "http://example.com",
      "xxxxxxxxxxxxxx",
      "http://example.com/#xxxxxxxxxxxxxx",
    ],
    [
      "http://example.com:80",
      "xxxxxxxxxxxxxx",
      "http://example.com:80/#xxxxxxxxxxxxxx",
    ],
    [
      "http://example.com:80/",
      "xxxxxxxxxxxxxx",
      "http://example.com:80/#xxxxxxxxxxxxxx",
    ],
    [
      "http://example.com/",
      "xxxxxxxxxxxxxx",
      "http://example.com/#xxxxxxxxxxxxxx",
    ],
    [
      "http://example.com/a",
      "xxxxxxxxxxxxxx",
      "http://example.com/a#xxxxxxxxxxxxxx",
    ],
    [
      "http://example.com:80/a",
      "xxxxxxxxxxxxxx",
      "http://example.com:80/a#xxxxxxxxxxxxxx",
    ],
  ];

  for (var [before, ref, result] of tests) {
    /* Test1: starting with empty ref */
    var a = stringToURL(before);
    a = a.mutate().setRef(ref).finalize().QueryInterface(Ci.nsIURL);
    var b = stringToURL(result);

    Assert.equal(a.spec, b.spec);
    Assert.equal(ref, b.ref);
    symmetricEquality(true, a, b);

    /* Test2: starting with non-empty */
    a = a.mutate().setRef("yyyy").finalize().QueryInterface(Ci.nsIURL);
    var c = stringToURL(before);
    c = c.mutate().setRef("yyyy").finalize().QueryInterface(Ci.nsIURL);
    symmetricEquality(true, a, c);

    /* Test3: reset the ref */
    a = a.mutate().setRef("").finalize().QueryInterface(Ci.nsIURL);
    symmetricEquality(true, a, stringToURL(before));

    /* Test4: verify again after reset */
    a = a.mutate().setRef(ref).finalize().QueryInterface(Ci.nsIURL);
    symmetricEquality(true, a, b);
  }
  run_next_test();
});

// Bug 960014 - Make nsStandardURL::SetHost less magical around IPv6
add_test(function test_ipv6() {
  var url = stringToURL("http://example.com");
  url = url.mutate().setHost("[2001::1]").finalize();
  Assert.equal(url.host, "2001::1");

  url = stringToURL("http://example.com");
  url = url.mutate().setHostPort("[2001::1]:30").finalize();
  Assert.equal(url.host, "2001::1");
  Assert.equal(url.port, 30);
  Assert.equal(url.hostPort, "[2001::1]:30");

  url = stringToURL("http://example.com");
  url = url.mutate().setHostPort("2001:1").finalize();
  Assert.equal(url.host, "0.0.7.209");
  Assert.equal(url.port, 1);
  Assert.equal(url.hostPort, "0.0.7.209:1");
  run_next_test();
});

add_test(function test_ipv6_fail() {
  var url = stringToURL("http://example.com");

  Assert.throws(
    () => {
      url = url.mutate().setHost("2001::1").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "missing brackets"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHost("[2001::1]:20").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "url.host with port"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHost("[2001::1").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "missing last bracket"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHost("2001::1]").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "missing first bracket"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHost("2001[::1]").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad bracket position"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHost("[]").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "empty IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHost("[hello]").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHost("[192.168.1.1]").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("2001::1").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "missing brackets"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("[2001::1]30").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "missing : after IP"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("[2001:1]").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("[2001:1]10").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("[2001:1]10:20").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("[2001:1]:10:20").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("[2001:1").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("2001]:1").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("2001:1]").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "bad IPv6 address"
  );
  Assert.throws(
    () => {
      url = url.mutate().setHostPort("").finalize();
    },
    /NS_ERROR_UNEXPECTED/,
    "Empty hostPort should fail"
  );

  // These checks used to fail, but now don't (see bug 1433958 comment 57)
  url = url.mutate().setHostPort("[2001::1]:").finalize();
  Assert.equal(url.spec, "http://[2001::1]/");
  url = url.mutate().setHostPort("[2002::1]:bad").finalize();
  Assert.equal(url.spec, "http://[2002::1]/");

  run_next_test();
});

add_test(function test_clearedSpec() {
  var url = stringToURL("http://example.com/path");
  Assert.throws(
    () => {
      url = url.mutate().setSpec("http: example").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "set bad spec"
  );
  Assert.throws(
    () => {
      url = url.mutate().setSpec("").finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "set empty spec"
  );
  Assert.equal(url.spec, "http://example.com/path");
  url = url
    .mutate()
    .setHost("allizom.org")
    .finalize()
    .QueryInterface(Ci.nsIURL);

  var ref = stringToURL("http://allizom.org/path");
  symmetricEquality(true, url, ref);
  run_next_test();
});

add_test(function test_escapeBrackets() {
  // Query
  var url = stringToURL("http://example.com/?a[x]=1");
  Assert.equal(url.spec, "http://example.com/?a[x]=1");

  url = stringToURL("http://example.com/?a%5Bx%5D=1");
  Assert.equal(url.spec, "http://example.com/?a%5Bx%5D=1");

  url = stringToURL("http://[2001::1]/?a[x]=1");
  Assert.equal(url.spec, "http://[2001::1]/?a[x]=1");

  url = stringToURL("http://[2001::1]/?a%5Bx%5D=1");
  Assert.equal(url.spec, "http://[2001::1]/?a%5Bx%5D=1");

  // Path
  url = stringToURL("http://example.com/brackets[x]/test");
  Assert.equal(url.spec, "http://example.com/brackets[x]/test");

  url = stringToURL("http://example.com/a%5Bx%5D/test");
  Assert.equal(url.spec, "http://example.com/a%5Bx%5D/test");
  run_next_test();
});

add_test(function test_escapeQuote() {
  var url = stringToURL("http://example.com/#'");
  Assert.equal(url.spec, "http://example.com/#'");
  Assert.equal(url.ref, "'");
  url = url.mutate().setRef("test'test").finalize();
  Assert.equal(url.spec, "http://example.com/#test'test");
  Assert.equal(url.ref, "test'test");
  run_next_test();
});

add_test(function test_apostropheEncoding() {
  // For now, single quote is escaped everywhere _except_ the path.
  // This policy is controlled by the bitmask in nsEscape.cpp::EscapeChars[]
  var url = stringToURL("http://example.com/dir'/file'.ext'");
  Assert.equal(url.spec, "http://example.com/dir'/file'.ext'");
  run_next_test();
});

add_test(function test_accentEncoding() {
  var url = stringToURL("http://example.com/?hello=`");
  Assert.equal(url.spec, "http://example.com/?hello=`");
  Assert.equal(url.query, "hello=`");

  url = stringToURL("http://example.com/?hello=%2C");
  Assert.equal(url.spec, "http://example.com/?hello=%2C");
  Assert.equal(url.query, "hello=%2C");
  run_next_test();
});

add_test(
  { skip_if: () => AppConstants.MOZ_APP_NAME == "thunderbird" },
  function test_percentDecoding() {
    var url = stringToURL("http://%70%61%73%74%65%62%69%6E.com");
    Assert.equal(url.spec, "http://pastebin.com/");

    // Disallowed hostname characters are rejected even when percent encoded
    Assert.throws(
      () => {
        url = stringToURL("http://example.com%0a%23.google.com/");
      },
      /NS_ERROR_MALFORMED_URI/,
      "invalid characters are not allowed"
    );
    run_next_test();
  }
);

add_test(function test_hugeStringThrows() {
  let prefs = Services.prefs;
  let maxLen = prefs.getIntPref("network.standard-url.max-length");
  let url = stringToURL("http://test:test@example.com");

  let hugeString = new Array(maxLen + 1).fill("a").join("");
  let setters = [
    { method: "setSpec", qi: Ci.nsIURIMutator },
    { method: "setUsername", qi: Ci.nsIURIMutator },
    { method: "setPassword", qi: Ci.nsIURIMutator },
    { method: "setFilePath", qi: Ci.nsIURIMutator },
    { method: "setHostPort", qi: Ci.nsIURIMutator },
    { method: "setHost", qi: Ci.nsIURIMutator },
    { method: "setUserPass", qi: Ci.nsIURIMutator },
    { method: "setPathQueryRef", qi: Ci.nsIURIMutator },
    { method: "setQuery", qi: Ci.nsIURIMutator },
    { method: "setRef", qi: Ci.nsIURIMutator },
    { method: "setScheme", qi: Ci.nsIURIMutator },
    { method: "setFileName", qi: Ci.nsIURLMutator },
    { method: "setFileExtension", qi: Ci.nsIURLMutator },
    { method: "setFileBaseName", qi: Ci.nsIURLMutator },
  ];

  for (let prop of setters) {
    Assert.throws(
      () =>
        (url = url
          .mutate()
          .QueryInterface(prop.qi)
          [prop.method](hugeString)
          .finalize()),
      /NS_ERROR_MALFORMED_URI/,
      `Passing a huge string to "${prop.method}" should throw`
    );
  }

  run_next_test();
});

add_test(function test_filterWhitespace() {
  let url = stringToURL(
    " \r\n\th\nt\rt\tp://ex\r\n\tample.com/path\r\n\t/\r\n\tto the/fil\r\n\te.e\r\n\txt?que\r\n\try#ha\r\n\tsh \r\n\t "
  );
  Assert.equal(
    url.spec,
    "http://example.com/path/to%20the/file.ext?query#hash"
  );

  // These setters should filter \r\n\t.
  url = stringToURL("http://test.com/path?query#hash");
  url = url.mutate().setFilePath("pa\r\n\tth").finalize();
  Assert.equal(url.spec, "http://test.com/path?query#hash");
  url = url.mutate().setQuery("que\r\n\try").finalize();
  Assert.equal(url.spec, "http://test.com/path?query#hash");
  url = url.mutate().setRef("ha\r\n\tsh").finalize();
  Assert.equal(url.spec, "http://test.com/path?query#hash");
  url = url
    .mutate()
    .QueryInterface(Ci.nsIURLMutator)
    .setFileName("fi\r\n\tle.name")
    .finalize();
  Assert.equal(url.spec, "http://test.com/fi%0D%0A%09le.name?query#hash");

  run_next_test();
});

add_test(function test_backslashReplacement() {
  var url = stringToURL(
    "http:\\\\test.com\\path/to\\file?query\\backslash#hash\\"
  );
  Assert.equal(
    url.spec,
    "http://test.com/path/to/file?query\\backslash#hash\\"
  );

  url = stringToURL("http:\\\\test.com\\example.org/path\\to/file");
  Assert.equal(url.spec, "http://test.com/example.org/path/to/file");
  Assert.equal(url.host, "test.com");
  Assert.equal(url.pathQueryRef, "/example.org/path/to/file");

  run_next_test();
});

add_test(function test_authority_host() {
  Assert.throws(
    () => {
      stringToURL("http:");
    },
    /NS_ERROR_MALFORMED_URI/,
    "TYPE_AUTHORITY should have host"
  );
  Assert.throws(
    () => {
      stringToURL("http:///");
    },
    /NS_ERROR_MALFORMED_URI/,
    "TYPE_AUTHORITY should have host"
  );

  run_next_test();
});

add_test(function test_trim_C0_and_space() {
  var url = stringToURL(
    "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f http://example.com/ \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f "
  );
  Assert.equal(url.spec, "http://example.com/");
  url = url
    .mutate()
    .setSpec(
      "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f http://test.com/ \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f "
    )
    .finalize();
  Assert.equal(url.spec, "http://test.com/");
  Assert.throws(
    () => {
      url = url
        .mutate()
        .setSpec(
          "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19 "
        )
        .finalize();
    },
    /NS_ERROR_MALFORMED_URI/,
    "set empty spec"
  );
  run_next_test();
});

// This tests that C0-and-space characters in the path, query and ref are
// percent encoded.
add_test(function test_encode_C0_and_space() {
  function toHex(d) {
    var hex = d.toString(16);
    if (hex.length == 1) {
      hex = "0" + hex;
    }
    return hex.toUpperCase();
  }

  for (var i = 0x0; i <= 0x20; i++) {
    // These characters get filtered - they are not encoded.
    if (
      String.fromCharCode(i) == "\r" ||
      String.fromCharCode(i) == "\n" ||
      String.fromCharCode(i) == "\t"
    ) {
      continue;
    }
    let url = stringToURL(
      "http://example.com/pa" +
        String.fromCharCode(i) +
        "th?qu" +
        String.fromCharCode(i) +
        "ery#ha" +
        String.fromCharCode(i) +
        "sh"
    );
    Assert.equal(
      url.spec,
      "http://example.com/pa%" +
        toHex(i) +
        "th?qu%" +
        toHex(i) +
        "ery#ha%" +
        toHex(i) +
        "sh"
    );
  }

  // Additionally, we need to check the setters.
  let url = stringToURL("http://example.com/path?query#hash");
  url = url.mutate().setFilePath("pa\0th").finalize();
  Assert.equal(url.spec, "http://example.com/pa%00th?query#hash");
  url = url.mutate().setQuery("qu\0ery").finalize();
  Assert.equal(url.spec, "http://example.com/pa%00th?qu%00ery#hash");
  url = url.mutate().setRef("ha\0sh").finalize();
  Assert.equal(url.spec, "http://example.com/pa%00th?qu%00ery#ha%00sh");
  url = url
    .mutate()
    .QueryInterface(Ci.nsIURLMutator)
    .setFileName("fi\0le.name")
    .finalize();
  Assert.equal(url.spec, "http://example.com/fi%00le.name?qu%00ery#ha%00sh");

  run_next_test();
});

add_test(function test_ipv4Normalize() {
  var localIPv4s = [
    "http://127.0.0.1",
    "http://127.0.1",
    "http://127.1",
    "http://2130706433",
    "http://0177.00.00.01",
    "http://0177.00.01",
    "http://0177.01",
    "http://00000000000000000000000000177.0000000.0000000.0001",
    "http://000000177.0000001",
    "http://017700000001",
    "http://0x7f.0x00.0x00.0x01",
    "http://0x7f.0x01",
    "http://0x7f000001",
    "http://0x007f.0x0000.0x0000.0x0001",
    "http://000177.0.00000.0x0001",
    "http://127.0.0.1.",
  ].map(stringToURL);

  let url;
  for (url of localIPv4s) {
    Assert.equal(url.spec, "http://127.0.0.1/");
  }

  // These should treated as a domain instead of an IPv4.
  var nonIPv4s = [
    "http://2+3/",
    "http://0.0.0.-1/",
    "http://1.2.3.4../",
    "resource://123/",
    "resource://4294967296/",
  ];
  var spec;
  for (spec of nonIPv4s) {
    url = stringToURL(spec);
    Assert.equal(url.spec, spec);
  }

  url = stringToURL("resource://path/to/resource/");
  url = url.mutate().setHost("123").finalize();
  Assert.equal(url.host, "123");

  run_next_test();
});

add_test(function test_invalidHostChars() {
  var url = stringToURL("http://example.org/");
  for (let i = 0; i <= 0x20; i++) {
    // These characters get filtered.
    if (
      String.fromCharCode(i) == "\r" ||
      String.fromCharCode(i) == "\n" ||
      String.fromCharCode(i) == "\t"
    ) {
      continue;
    }
    Assert.throws(
      () => {
        url = url
          .mutate()
          .setHost("a" + String.fromCharCode(i) + "b")
          .finalize();
      },
      /NS_ERROR_MALFORMED_URI/,
      "Trying to set hostname containing char code: " + i
    );
  }
  for (let c of '@[]*<>|:"') {
    Assert.throws(
      () => {
        url = url
          .mutate()
          .setHost("a" + c)
          .finalize();
      },
      /NS_ERROR_MALFORMED_URI/,
      "Trying to set hostname containing char: " + c
    );
  }

  // It also can't contain /, \, #, ?, but we treat these characters as
  // hostname separators, so there is no way to set them and fail.
  run_next_test();
});

add_test(function test_normalize_ipv6() {
  var url = stringToURL("http://example.com");
  url = url.mutate().setHost("[::192.9.5.5]").finalize();
  Assert.equal(url.spec, "http://[::c009:505]/");

  run_next_test();
});

add_test(function test_emptyPassword() {
  var url = stringToURL("http://a:@example.com");
  Assert.equal(url.spec, "http://a@example.com/");
  url = url.mutate().setPassword("pp").finalize();
  Assert.equal(url.spec, "http://a:pp@example.com/");
  url = url.mutate().setPassword("").finalize();
  Assert.equal(url.spec, "http://a@example.com/");
  url = url.mutate().setUserPass("xxx:").finalize();
  Assert.equal(url.spec, "http://xxx@example.com/");
  url = url.mutate().setPassword("zzzz").finalize();
  Assert.equal(url.spec, "http://xxx:zzzz@example.com/");
  url = url.mutate().setUserPass("xxxxx:yyyyyy").finalize();
  Assert.equal(url.spec, "http://xxxxx:yyyyyy@example.com/");
  url = url.mutate().setUserPass("z:").finalize();
  Assert.equal(url.spec, "http://z@example.com/");
  url = url.mutate().setPassword("ppppppppppp").finalize();
  Assert.equal(url.spec, "http://z:ppppppppppp@example.com/");

  url = stringToURL("http://example.com");
  url = url.mutate().setPassword("").finalize(); // Still empty. Should work.
  Assert.equal(url.spec, "http://example.com/");

  run_next_test();
});

add_test(function test_emptyUser() {
  let url = stringToURL("http://:a@example.com/path/to/something?query#hash");
  Assert.equal(url.spec, "http://:a@example.com/path/to/something?query#hash");
  url = stringToURL("http://:@example.com/path/to/something?query#hash");
  Assert.equal(url.spec, "http://example.com/path/to/something?query#hash");

  const kurl = stringToURL(
    "http://user:pass@example.com:8888/path/to/something?query#hash"
  );
  url = kurl.mutate().setUsername("").finalize();
  Assert.equal(
    url.spec,
    "http://:pass@example.com:8888/path/to/something?query#hash"
  );
  Assert.equal(url.host, "example.com");
  Assert.equal(url.hostPort, "example.com:8888");
  Assert.equal(url.filePath, "/path/to/something");
  Assert.equal(url.query, "query");
  Assert.equal(url.ref, "hash");
  url = kurl.mutate().setUserPass(":pass1").finalize();
  Assert.equal(
    url.spec,
    "http://:pass1@example.com:8888/path/to/something?query#hash"
  );
  Assert.equal(url.host, "example.com");
  Assert.equal(url.hostPort, "example.com:8888");
  Assert.equal(url.filePath, "/path/to/something");
  Assert.equal(url.query, "query");
  Assert.equal(url.ref, "hash");
  url = url.mutate().setUsername("user2").finalize();
  Assert.equal(
    url.spec,
    "http://user2:pass1@example.com:8888/path/to/something?query#hash"
  );
  Assert.equal(url.host, "example.com");
  url = url.mutate().setUserPass(":pass234").finalize();
  Assert.equal(
    url.spec,
    "http://:pass234@example.com:8888/path/to/something?query#hash"
  );
  Assert.equal(url.host, "example.com");
  url = url.mutate().setUserPass("").finalize();
  Assert.equal(
    url.spec,
    "http://example.com:8888/path/to/something?query#hash"
  );
  Assert.equal(url.host, "example.com");
  url = url.mutate().setPassword("pa").finalize();
  Assert.equal(
    url.spec,
    "http://:pa@example.com:8888/path/to/something?query#hash"
  );
  Assert.equal(url.host, "example.com");
  url = url.mutate().setUserPass("user:pass").finalize();
  symmetricEquality(true, url.QueryInterface(Ci.nsIURL), kurl);

  url = stringToURL("http://example.com:8888/path/to/something?query#hash");
  url = url.mutate().setPassword("pass").finalize();
  Assert.equal(
    url.spec,
    "http://:pass@example.com:8888/path/to/something?query#hash"
  );
  url = url.mutate().setUsername("").finalize();
  Assert.equal(
    url.spec,
    "http://:pass@example.com:8888/path/to/something?query#hash"
  );

  url = stringToURL("http://example.com:8888");
  url = url.mutate().setUsername("user").finalize();
  url = url.mutate().setUsername("").finalize();
  Assert.equal(url.spec, "http://example.com:8888/");

  url = stringToURL("http://:pass@example.com");
  Assert.equal(url.spec, "http://:pass@example.com/");
  url = url.mutate().setPassword("").finalize();
  Assert.equal(url.spec, "http://example.com/");
  url = url.mutate().setUserPass("user:pass").finalize();
  Assert.equal(url.spec, "http://user:pass@example.com/");
  Assert.equal(url.host, "example.com");
  url = url.mutate().setUserPass("u:p").finalize();
  Assert.equal(url.spec, "http://u:p@example.com/");
  Assert.equal(url.host, "example.com");
  url = url.mutate().setUserPass("u1:p23").finalize();
  Assert.equal(url.spec, "http://u1:p23@example.com/");
  Assert.equal(url.host, "example.com");
  url = url.mutate().setUsername("u").finalize();
  Assert.equal(url.spec, "http://u:p23@example.com/");
  Assert.equal(url.host, "example.com");
  url = url.mutate().setPassword("p").finalize();
  Assert.equal(url.spec, "http://u:p@example.com/");
  Assert.equal(url.host, "example.com");

  url = url.mutate().setUserPass("u2:p2").finalize();
  Assert.equal(url.spec, "http://u2:p2@example.com/");
  Assert.equal(url.host, "example.com");
  url = url.mutate().setUserPass("u23:p23").finalize();
  Assert.equal(url.spec, "http://u23:p23@example.com/");
  Assert.equal(url.host, "example.com");

  run_next_test();
});

registerCleanupFunction(function () {
  gPrefs.clearUserPref("network.standard-url.punycode-host");
});

add_test(function test_idna_host() {
  // See bug 945240 - this test makes sure that URLs return a punycode hostname
  let url = stringToURL(
    "http://user:password@ält.example.org:8080/path?query#etc"
  );
  equal(url.host, "xn--lt-uia.example.org");
  equal(url.hostPort, "xn--lt-uia.example.org:8080");
  equal(url.prePath, "http://user:password@xn--lt-uia.example.org:8080");
  equal(
    url.spec,
    "http://user:password@xn--lt-uia.example.org:8080/path?query#etc"
  );
  equal(
    url.specIgnoringRef,
    "http://user:password@xn--lt-uia.example.org:8080/path?query"
  );
  equal(
    url
      .QueryInterface(Ci.nsISensitiveInfoHiddenURI)
      .getSensitiveInfoHiddenSpec(),
    "http://user:****@xn--lt-uia.example.org:8080/path?query#etc"
  );

  equal(url.displayHost, "ält.example.org");
  equal(url.displayHostPort, "ält.example.org:8080");
  equal(
    url.displaySpec,
    "http://user:password@ält.example.org:8080/path?query#etc"
  );

  equal(url.asciiHost, "xn--lt-uia.example.org");
  equal(url.asciiHostPort, "xn--lt-uia.example.org:8080");
  equal(
    url.asciiSpec,
    "http://user:password@xn--lt-uia.example.org:8080/path?query#etc"
  );

  url = url.mutate().setRef("").finalize(); // SetRef calls InvalidateCache()
  equal(
    url.spec,
    "http://user:password@xn--lt-uia.example.org:8080/path?query"
  );
  equal(
    url.displaySpec,
    "http://user:password@ält.example.org:8080/path?query"
  );
  equal(
    url.asciiSpec,
    "http://user:password@xn--lt-uia.example.org:8080/path?query"
  );

  url = stringToURL("http://user:password@www.ält.com:8080/path?query#etc");
  url = url.mutate().setRef("").finalize();
  equal(url.spec, "http://user:password@www.xn--lt-uia.com:8080/path?query");

  run_next_test();
});

add_test(
  { skip_if: () => AppConstants.MOZ_APP_NAME == "thunderbird" },
  function test_bug1517025() {
    Assert.throws(
      () => {
        stringToURL("https://b%9a/");
      },
      /NS_ERROR_MALFORMED_URI/,
      "bad URI"
    );

    Assert.throws(
      () => {
        stringToURL("https://b%9ª/");
      },
      /NS_ERROR_MALFORMED_URI/,
      "bad URI"
    );

    let base = stringToURL(
      "https://bug1517025.bmoattachments.org/attachment.cgi?id=9033787"
    );
    Assert.throws(
      () => {
        Services.io.newURI("/\\b%9ª", "windows-1252", base);
      },
      /NS_ERROR_MALFORMED_URI/,
      "bad URI"
    );

    run_next_test();
  }
);

add_task(async function test_emptyHostWithURLType() {
  let makeURL = (str, type) => {
    return Cc["@mozilla.org/network/standard-url-mutator;1"]
      .createInstance(Ci.nsIStandardURLMutator)
      .init(type, 80, str, "UTF-8", null)
      .finalize()
      .QueryInterface(Ci.nsIURL);
  };

  let url = makeURL("http://foo.com/bar/", Ci.nsIStandardURL.URLTYPE_AUTHORITY);
  Assert.throws(
    () => url.mutate().setHost("").finalize().spec,
    /NS_ERROR_UNEXPECTED/,
    "Empty host is not allowed for URLTYPE_AUTHORITY"
  );

  url = makeURL("http://foo.com/bar/", Ci.nsIStandardURL.URLTYPE_STANDARD);
  Assert.throws(
    () => url.mutate().setHost("").finalize().spec,
    /NS_ERROR_UNEXPECTED/,
    "Empty host is not allowed for URLTYPE_STANDARD"
  );

  url = makeURL("http://foo.com/bar/", Ci.nsIStandardURL.URLTYPE_NO_AUTHORITY);
  equal(
    url.spec,
    "http:///bar/",
    "Host is removed when parsing URLTYPE_NO_AUTHORITY"
  );
  equal(
    url.mutate().setHost("").finalize().spec,
    "http:///bar/",
    "Setting an empty host does nothing for URLTYPE_NO_AUTHORITY"
  );
  Assert.throws(
    () => url.mutate().setHost("something").finalize().spec,
    /NS_ERROR_UNEXPECTED/,
    "Setting a non-empty host is not allowed for URLTYPE_NO_AUTHORITY"
  );
  equal(
    url.mutate().setHost("#j").finalize().spec,
    "http:///bar/",
    "Setting a pseudo-empty host does nothing for URLTYPE_NO_AUTHORITY"
  );

  url = makeURL(
    "http://example.org:123/foo?bar#baz",
    Ci.nsIStandardURL.URLTYPE_AUTHORITY
  );
  Assert.throws(
    () => url.mutate().setHost("#j").finalize().spec,
    /NS_ERROR_UNEXPECTED/,
    "A pseudo-empty host is not allowed for URLTYPE_AUTHORITY"
  );
});

add_task(async function test_fuzz() {
  let makeURL = str => {
    return (
      Cc["@mozilla.org/network/standard-url-mutator;1"]
        .createInstance(Ci.nsIStandardURLMutator)
        .QueryInterface(Ci.nsIURIMutator)
        // .init(type, 80, str, "UTF-8", null)
        .setSpec(str)
        .finalize()
        .QueryInterface(Ci.nsIURL)
    );
  };

  Assert.throws(() => {
    let url = makeURL("/");
    url.mutate().setHost("(").finalize();
  }, /NS_ERROR_MALFORMED_URI/);
});

add_task(async function test_bug1648493() {
  let url = stringToURL("https://example.com/");
  url = url.mutate().setScheme("file").finalize();
  url = url.mutate().setScheme("resource").finalize();
  url = url.mutate().setPassword("ê").finalize();
  url = url.mutate().setUsername("ç").finalize();
  url = url.mutate().setScheme("t").finalize();
  equal(url.spec, "t://%C3%83%C2%A7:%C3%83%C2%AA@example.com/");
  equal(url.username, "%C3%83%C2%A7");
});

add_task(async function test_bug1873976() {
  let url = Services.io.newURI("file:.");
  equal(url.spec, "file:///");
});

add_task(async function test_bug1890346() {
  let url = Services.io.newURI("file:..?/..");
  equal(url.spec, "file:///?/..");
});