Debian Codegraph @

closure-library/closure/goog/crypt/crypt_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.cryptTest');
16 goog.setTestOnly('goog.cryptTest');
17
18 goog.require('goog.crypt');
19 goog.require('goog.string');
20 goog.require('goog.testing.jsunit');
21
22 var UTF8_RANGES_BYTE_ARRAY =
23 [0x00, 0x7F, 0xC2, 0x80, 0xDF, 0xBF, 0xE0, 0xA0, 0x80, 0xEF, 0xBF, 0xBF];
24
25 var UTF8_SURROGATE_PAIR_RANGES_BYTE_ARRAY = [
26 0xF0, 0x90, 0x80, 0x80, // \uD800\uDC00
27 0xF0, 0x90, 0x8F, 0xBF, // \uD800\uDFFF
28 0xF4, 0x8F, 0xB0, 0x80, // \uDBFF\uDC00
29 0xF4, 0x8F, 0xBF, 0xBF // \uDBFF\uDFFF
30 ];
31
32 var UTF8_RANGES_STRING = '\u0000\u007F\u0080\u07FF\u0800\uFFFF';
33
34 var UTF8_SURROGATE_PAIR_RANGES_STRING =
35 '\uD800\uDC00\uD800\uDFFF\uDBFF\uDC00\uDBFF\uDFFF';
36
37 function testStringToUtf8ByteArray() {
38 // Known encodings taken from Java's String.getBytes("UTF8")
39
40 assertArrayEquals(
41 'ASCII', [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100],
42 goog.crypt.stringToUtf8ByteArray('Hello, world'));
43
44 assertArrayEquals(
45 'Latin', [83, 99, 104, 195, 182, 110],
46 goog.crypt.stringToUtf8ByteArray('Sch\u00f6n'));
47
48 assertArrayEquals(
49 'limits of the first 3 UTF-8 character ranges', UTF8_RANGES_BYTE_ARRAY,
50 goog.crypt.stringToUtf8ByteArray(UTF8_RANGES_STRING));
51
52 assertArrayEquals(
53 'Surrogate Pair', UTF8_SURROGATE_PAIR_RANGES_BYTE_ARRAY,
54 goog.crypt.stringToUtf8ByteArray(UTF8_SURROGATE_PAIR_RANGES_STRING));
55 }
56
57 function testUtf8ByteArrayToString() {
58 // Known encodings taken from Java's String.getBytes("UTF8")
59
60 assertEquals('ASCII', 'Hello, world', goog.crypt.utf8ByteArrayToString([
61 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100
62 ]));
63
64 assertEquals(
65 'Latin', 'Sch\u00f6n',
66 goog.crypt.utf8ByteArrayToString([83, 99, 104, 195, 182, 110]));
67
68 assertEquals(
69 'limits of the first 3 UTF-8 character ranges', UTF8_RANGES_STRING,
70 goog.crypt.utf8ByteArrayToString(UTF8_RANGES_BYTE_ARRAY));
71
72 assertEquals(
73 'Surrogate Pair', UTF8_SURROGATE_PAIR_RANGES_STRING,
74 goog.crypt.utf8ByteArrayToString(UTF8_SURROGATE_PAIR_RANGES_BYTE_ARRAY));
75 }
76
77
78 /**
79 * Same as testUtf8ByteArrayToString but with Uint8Array instead of
80 * Array<number>.
81 */
82 function testUint8ArrayToString() {
83 if (!goog.global.Uint8Array) {
84 // Uint8Array not supported.
85 return;
86 }
87
88 var arr =
89 new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]);
90 assertEquals('ASCII', 'Hello, world', goog.crypt.utf8ByteArrayToString(arr));
91
92 arr = new Uint8Array([83, 99, 104, 195, 182, 110]);
93 assertEquals('Latin', 'Sch\u00f6n', goog.crypt.utf8ByteArrayToString(arr));
94
95 arr = new Uint8Array(UTF8_RANGES_BYTE_ARRAY);
96 assertEquals(
97 'limits of the first 3 UTF-8 character ranges', UTF8_RANGES_STRING,
98 goog.crypt.utf8ByteArrayToString(arr));
99 }
100
101 function testByteArrayToString() {
102 assertEquals('', goog.crypt.byteArrayToString([]));
103 assertEquals('abc', goog.crypt.byteArrayToString([97, 98, 99]));
104 }
105
106 function testHexToByteArray() {
107 assertElementsEquals(
108 [202, 254, 222, 173],
109 // Java magic number
110 goog.crypt.hexToByteArray('cafedead'));
111
112 assertElementsEquals(
113 [222, 173, 190, 239],
114 // IBM magic number
115 goog.crypt.hexToByteArray('DEADBEEF'));
116 }
117
118 function testByteArrayToHex() {
119 assertEquals(
120 // Java magic number
121 'cafedead', goog.crypt.byteArrayToHex([202, 254, 222, 173]));
122
123 assertEquals(
124 // IBM magic number
125 'deadbeef', goog.crypt.byteArrayToHex([222, 173, 190, 239]));
126 }
127
128
129 /** Same as testByteArrayToHex but with Uint8Array instead of Array<number>. */
130 function testUint8ArrayToHex() {
131 if (!goog.isDef(goog.global.Uint8Array)) {
132 // Uint8Array not supported.
133 return;
134 }
135
136 assertEquals(
137 // Java magic number
138 'cafedead',
139 goog.crypt.byteArrayToHex(new Uint8Array([202, 254, 222, 173])));
140
141 assertEquals(
142 // IBM magic number
143 'deadbeef',
144 goog.crypt.byteArrayToHex(new Uint8Array([222, 173, 190, 239])));
145 }
146
147 function testXorByteArray() {
148 assertElementsEquals(
149 [20, 83, 96, 66],
150 goog.crypt.xorByteArray([202, 254, 222, 173], [222, 173, 190, 239]));
151 }
152
153
154 /** Same as testXorByteArray but with Uint8Array instead of Array<number>. */
155 function testXorUint8Array() {
156 if (!goog.isDef(goog.global.Uint8Array)) {
157 // Uint8Array not supported.
158 return;
159 }
160
161 assertElementsEquals(
162 [20, 83, 96, 66], goog.crypt.xorByteArray(
163 new Uint8Array([202, 254, 222, 173]),
164 new Uint8Array([222, 173, 190, 239])));
165 }
166
167
168 // Tests a one-megabyte byte array conversion to string.
169 // This would break on many JS implementations unless byteArrayToString
170 // split the input up.
171 // See discussion and bug report: http://goo.gl/LrWmZ9
172 function testByteArrayToStringCallStack() {
173 // One megabyte is 2 to the 20th.
174 var count = Math.pow(2, 20);
175 var bytes = [];
176 for (var i = 0; i < count; i++) {
177 bytes.push('A'.charCodeAt(0));
178 }
179 var str = goog.crypt.byteArrayToString(bytes);
180 assertEquals(goog.string.repeat('A', count), str);
181 }