1// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28/**
29 * @fileoverview Check that the global escape and unescape functions work
30 * right.
31 */
32
33// Section B.2.1 of ECMAScript 3
34var unescaped = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
35
36// Check the unescape chars are not escaped
37assertEquals(unescaped, escape(unescaped));
38// Check spaces are escaped
39assertEquals("%20/%20", escape(" / "));
40// Check that null chars are escaped and do not terminate the string
41assertEquals("%000", escape("\0" + "0"));
42// Check a unicode escape
43assertEquals("A%20B%u1234%00%20C", escape(String.fromCharCode(0x41, 0x20, 0x42, 0x1234, 0, 0x20, 0x43)));
44// Check unicode escapes have a leading zero to pad to 4 digits
45assertEquals("%u0123", escape(String.fromCharCode(0x123)));
46// Check escapes are upper case
47assertEquals("%uABCD", escape(String.fromCharCode(0xabcd)));
48assertEquals("%AB", escape(String.fromCharCode(0xab)));
49assertEquals("%0A", escape("\n"));
50
51// Check first 1000 chars individually for escaped/not escaped
52for (var i = 0; i < 1000; i++) {
53  var s = String.fromCharCode(i);
54  if (unescaped.indexOf(s, 0) == -1) {
55    assertFalse(s == escape(s));
56  } else {
57    assertTrue(s == escape(s));
58  }
59}
60
61// Check all chars up to 1000 in groups of 10 using unescape as a check
62for (var i = 0; i < 1000; i += 10) {
63  var s = String.fromCharCode(i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9);
64  assertEquals(s, unescape(escape(s)));
65}
66
67// Benchmark
68var example = "Now is the time for all good men to come to the aid of the party.";
69example = example + String.fromCharCode(267, 0x1234, 0x6667, 0xabcd);
70example = example + " The quick brown fox jumps over the lazy dog."
71example = example + String.fromCharCode(171, 172, 173, 174, 175, 176, 178, 179);
72
73for (var i = 0; i < 3000; i++) {
74  assertEquals(example, unescape(escape(example)));
75}
76
77// Check unescape can cope with upper and lower case
78assertEquals(unescape("%41%4A%4a"), "AJJ");
79
80// Check upper case U
81assertEquals("%U1234", unescape("%U1234"));
82
83// Check malformed unescapes
84assertEquals("%", unescape("%"));
85assertEquals("%4", unescape("%4"));
86assertEquals("%u", unescape("%u"));
87assertEquals("%u4", unescape("%u4"));
88assertEquals("%u44", unescape("%u44"));
89assertEquals("%u444", unescape("%u444"));
90assertEquals("%4z", unescape("%4z"));
91assertEquals("%uzzzz", unescape("%uzzzz"));
92assertEquals("%u4zzz", unescape("%u4zzz"));
93assertEquals("%u44zz", unescape("%u44zz"));
94assertEquals("%u444z", unescape("%u444z"));
95assertEquals("%4<", unescape("%4<"));
96assertEquals("%u<<<<", unescape("%u<<<<"));
97assertEquals("%u4<<<", unescape("%u4<<<"));
98assertEquals("%u44<<", unescape("%u44<<"));
99assertEquals("%u444<", unescape("%u444<"));
100assertEquals("foo%4<", unescape("foo%4<"));
101assertEquals("foo%u<<<<", unescape("foo%u<<<<"));
102assertEquals("foo%u4<<<", unescape("foo%u4<<<"));
103assertEquals("foo%u44<<", unescape("foo%u44<<"));
104assertEquals("foo%u444<", unescape("foo%u444<"));
105assertEquals("foo%4<bar", unescape("foo%4<bar"));
106assertEquals("foo%u<<<<bar", unescape("foo%u<<<<bar"));
107assertEquals("foo%u4<<<bar", unescape("foo%u4<<<bar"));
108assertEquals("foo%u44<<bar", unescape("foo%u44<<bar"));
109assertEquals("foo%u444<bar", unescape("foo%u444<bar"));
110assertEquals("% ", unescape("%%20"));
111assertEquals("%% ", unescape("%%%20"));
112
113// Unescape stress
114var eexample = escape(example);
115
116for (var i = 1; i < 3000; i++) {
117  assertEquals(example, unescape(eexample));
118}
119