summaryrefslogtreecommitdiffstats
path: root/LayoutTests/fast/url/script-tests/ipv6.js
blob: 38d9e2269a3d9549d49211c51c21e411cebaf485 (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
description("Canonicalization of IPv6 addresses.");

cases = [ 
    ["[", ""],
    ["[:", ""],
    ["]", ""],
    [":]", ""],
    ["[]", ""],
    ["[:]", ""],

    // Regular IP address is invalid without bounding '[' and ']'.
    ["2001:db8::1", ""],
    ["[2001:db8::1", ""],
    ["2001:db8::1]", ""],

    // Regular IP addresses.
    ["[::]", "[::]"],
    ["[::1]", "[::1]"],
    ["[1::]", "[1::]"],
    ["[::192.168.0.1]", "[::c0a8:1]"],
    ["[::ffff:192.168.0.1]", "[::ffff:c0a8:1]"],

    // Leading zeros should be stripped.
    ["[000:01:02:003:004:5:6:007]", "[0:1:2:3:4:5:6:7]"],

    // Upper case letters should be lowercased.
    ["[A:b:c:DE:fF:0:1:aC]", "[a:b:c:de:ff:0:1:ac]"],

    // The same address can be written with different contractions, but should
    // get canonicalized to the same thing.
    ["[1:0:0:2::3:0]", "[1::2:0:0:3:0]"],
    ["[1::2:0:0:3:0]", "[1::2:0:0:3:0]"],

    // IPv4 addresses
    // Only mapped and compat addresses can have IPv4 syntax embedded.
    ["[::eeee:192.168.0.1]", ""],
    ["[2001::192.168.0.1]", ""],
    ["[1:2:192.168.0.1:5:6]", ""],

    // IPv4 with last component missing.
    ["[::ffff:192.1.2]", "[::ffff:c001:2]"],

    // IPv4 using hex.
    // FIXME: Should this format be disallowed?
    ["[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]"],

    // There may be zeros surrounding the "::" contraction.
    ["[0:0::0:0:8]", "[::8]"],

    ["[2001:db8::1]", "[2001:db8::1]"],

    // Can only have one "::" contraction in an IPv6 string literal.
    ["[2001::db8::1]", ""],

    // No more than 2 consecutive ':'s.
    ["[2001:db8:::1]", ""],
    ["[:::]", ""],

    // Non-IP addresses due to invalid characters.
    ["[2001::.com]", ""],

    // Too many components means not an IP address.  Similarly with too few if using IPv4 compat or mapped addresses.
    ["[::192.168.0.0.1]", ""],
    ["[::ffff:192.168.0.0.1]", ""],
    ["[1:2:3:4:5:6:7:8:9]", ""],

    // Too many bits (even though 8 comonents, the last one holds 32 bits).
    ["[0:0:0:0:0:0:0:192.168.0.1]", ""],

    // Too many bits specified -- the contraction would have to be zero-length
    // to not exceed 128 bits.
    ["[1:2:3:4:5:6::192.168.0.1]", ""],

    // The contraction is for 16 bits of zero.
    ["[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]"],

    // Cannot have a trailing colon.
    ["[1:2:3:4:5:6:7:8:]", ""],
    ["[1:2:3:4:5:6:192.168.0.1:]", ""],

    // Cannot have negative numbers.
    ["[-1:2:3:4:5:6:7:8]", ""],

    // Scope ID -- the URL may contain an optional ["%" <scope_id>] section.
    // The scope_id should be included in the canonicalized URL, and is an
    // unsigned decimal number.

    // Don't allow scope-id
    ["[1::%1]", ""],
    ["[1::%eth0]", ""],
    ["[1::%]", ""],
    ["[%]", ""],
    ["[::%:]", ""],

    // Don't allow leading or trailing colons.
    ["[:0:0::0:0:8]", ""],
    ["[0:0::0:0:8:]", ""],
    ["[:0:0::0:0:8:]", ""],

    // Two dots in a row means not an IP address.
    ["[::192.168..1]", ""],

    // Spaces should be rejected.
    ["[::1 hello]", ""]
];

// We test the empty string individually.
shouldBe("canonicalize('http:///')", "'http:'");

for (var i = 0; i < cases.length; ++i) {
  test_vector = cases[i][0];
  expected_result = cases[i][1];
  if (expected_result === "") {
    // We use "" to represent that the test vector ought not to parse.
    // It appears that we're supposed to apply a default canonicalization,
    // and the escape function escapes too much!
    expected_result = test_vector.toLowerCase();
    ['%', ' '].forEach(function(c){
      expected_result = expected_result.replace(c, escape(c));
    })
  }
  shouldBe("canonicalize('http://" + test_vector + "/')",
           "'http://" + expected_result + "/'");
}

var successfullyParsed = true;