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
|
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.crypt</title>
<script src="../base.js"></script>
<script>
goog.require('goog.crypt');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
var UTF8_RANGES_BYTE_ARRAY = [
0x00,
0x7F,
0xC2, 0x80,
0xDF, 0xBF,
0xE0, 0xA0, 0x80,
0xEF, 0xBF, 0xBF];
var UTF8_RANGES_STRING = '\u0000\u007F\u0080\u07FF\u0800\uFFFF';
function testStringToUtf8ByteArray() {
// Known encodings taken from Java's String.getBytes("UTF8")
assertArrayEquals('ASCII',
[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100],
goog.crypt.stringToUtf8ByteArray('Hello, world'));
assertArrayEquals('Latin',
[83, 99, 104, 195, 182, 110],
goog.crypt.stringToUtf8ByteArray('Sch\u00f6n'));
assertArrayEquals('limits of the first 3 UTF-8 character ranges',
UTF8_RANGES_BYTE_ARRAY,
goog.crypt.stringToUtf8ByteArray(UTF8_RANGES_STRING));
}
function testUtf8ByteArrayToString() {
// Known encodings taken from Java's String.getBytes("UTF8")
assertEquals('ASCII', 'Hello, world', goog.crypt.utf8ByteArrayToString(
[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]));
assertEquals('Latin', 'Sch\u00f6n', goog.crypt.utf8ByteArrayToString(
[83, 99, 104, 195, 182, 110]));
assertEquals('limits of the first 3 UTF-8 character ranges',
UTF8_RANGES_STRING,
goog.crypt.utf8ByteArrayToString(UTF8_RANGES_BYTE_ARRAY));
}
function testByteArrayToString() {
assertEquals('', goog.crypt.byteArrayToString([]));
assertEquals('abc', goog.crypt.byteArrayToString([97, 98, 99]));
}
function testHexToByteArray() {
assertElementsEquals(
[202, 254, 222, 173],
// Java magic number
goog.crypt.hexToByteArray('cafedead'));
assertElementsEquals(
[222, 173, 190, 239],
// IBM magic number
goog.crypt.hexToByteArray('DEADBEEF'));
}
function testByteArrayToHex() {
assertEquals(
// Java magic number
'cafedead',
goog.crypt.byteArrayToHex([202, 254, 222, 173]));
assertEquals(
// IBM magic number
'deadbeef',
goog.crypt.byteArrayToHex([222, 173, 190, 239]));
}
</script>
</body>
</html>
|