crypt_test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. goog.provide('goog.cryptTest');
  15. goog.setTestOnly('goog.cryptTest');
  16. goog.require('goog.crypt');
  17. goog.require('goog.string');
  18. goog.require('goog.testing.jsunit');
  19. var UTF8_RANGES_BYTE_ARRAY =
  20. [0x00, 0x7F, 0xC2, 0x80, 0xDF, 0xBF, 0xE0, 0xA0, 0x80, 0xEF, 0xBF, 0xBF];
  21. var UTF8_SURROGATE_PAIR_RANGES_BYTE_ARRAY = [
  22. 0xF0, 0x90, 0x80, 0x80, // \uD800\uDC00
  23. 0xF0, 0x90, 0x8F, 0xBF, // \uD800\uDFFF
  24. 0xF4, 0x8F, 0xB0, 0x80, // \uDBFF\uDC00
  25. 0xF4, 0x8F, 0xBF, 0xBF // \uDBFF\uDFFF
  26. ];
  27. var UTF8_RANGES_STRING = '\u0000\u007F\u0080\u07FF\u0800\uFFFF';
  28. var UTF8_SURROGATE_PAIR_RANGES_STRING =
  29. '\uD800\uDC00\uD800\uDFFF\uDBFF\uDC00\uDBFF\uDFFF';
  30. function testStringToUtf8ByteArray() {
  31. // Known encodings taken from Java's String.getBytes("UTF8")
  32. assertArrayEquals(
  33. 'ASCII', [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100],
  34. goog.crypt.stringToUtf8ByteArray('Hello, world'));
  35. assertArrayEquals(
  36. 'Latin', [83, 99, 104, 195, 182, 110],
  37. goog.crypt.stringToUtf8ByteArray('Sch\u00f6n'));
  38. assertArrayEquals(
  39. 'limits of the first 3 UTF-8 character ranges', UTF8_RANGES_BYTE_ARRAY,
  40. goog.crypt.stringToUtf8ByteArray(UTF8_RANGES_STRING));
  41. assertArrayEquals(
  42. 'Surrogate Pair', UTF8_SURROGATE_PAIR_RANGES_BYTE_ARRAY,
  43. goog.crypt.stringToUtf8ByteArray(UTF8_SURROGATE_PAIR_RANGES_STRING));
  44. }
  45. function testUtf8ByteArrayToString() {
  46. // Known encodings taken from Java's String.getBytes("UTF8")
  47. assertEquals('ASCII', 'Hello, world', goog.crypt.utf8ByteArrayToString([
  48. 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100
  49. ]));
  50. assertEquals(
  51. 'Latin', 'Sch\u00f6n',
  52. goog.crypt.utf8ByteArrayToString([83, 99, 104, 195, 182, 110]));
  53. assertEquals(
  54. 'limits of the first 3 UTF-8 character ranges', UTF8_RANGES_STRING,
  55. goog.crypt.utf8ByteArrayToString(UTF8_RANGES_BYTE_ARRAY));
  56. assertEquals(
  57. 'Surrogate Pair', UTF8_SURROGATE_PAIR_RANGES_STRING,
  58. goog.crypt.utf8ByteArrayToString(UTF8_SURROGATE_PAIR_RANGES_BYTE_ARRAY));
  59. }
  60. /**
  61. * Same as testUtf8ByteArrayToString but with Uint8Array instead of
  62. * Array<number>.
  63. */
  64. function testUint8ArrayToString() {
  65. if (!goog.global.Uint8Array) {
  66. // Uint8Array not supported.
  67. return;
  68. }
  69. var arr =
  70. new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]);
  71. assertEquals('ASCII', 'Hello, world', goog.crypt.utf8ByteArrayToString(arr));
  72. arr = new Uint8Array([83, 99, 104, 195, 182, 110]);
  73. assertEquals('Latin', 'Sch\u00f6n', goog.crypt.utf8ByteArrayToString(arr));
  74. arr = new Uint8Array(UTF8_RANGES_BYTE_ARRAY);
  75. assertEquals(
  76. 'limits of the first 3 UTF-8 character ranges', UTF8_RANGES_STRING,
  77. goog.crypt.utf8ByteArrayToString(arr));
  78. }
  79. function testByteArrayToString() {
  80. assertEquals('', goog.crypt.byteArrayToString([]));
  81. assertEquals('abc', goog.crypt.byteArrayToString([97, 98, 99]));
  82. }
  83. function testHexToByteArray() {
  84. assertElementsEquals(
  85. [202, 254, 222, 173],
  86. // Java magic number
  87. goog.crypt.hexToByteArray('cafedead'));
  88. assertElementsEquals(
  89. [222, 173, 190, 239],
  90. // IBM magic number
  91. goog.crypt.hexToByteArray('DEADBEEF'));
  92. }
  93. function testByteArrayToHex() {
  94. assertEquals(
  95. // Java magic number
  96. 'cafedead', goog.crypt.byteArrayToHex([202, 254, 222, 173]));
  97. assertEquals(
  98. // IBM magic number
  99. 'deadbeef', goog.crypt.byteArrayToHex([222, 173, 190, 239]));
  100. }
  101. /** Same as testByteArrayToHex but with Uint8Array instead of Array<number>. */
  102. function testUint8ArrayToHex() {
  103. if (!goog.isDef(goog.global.Uint8Array)) {
  104. // Uint8Array not supported.
  105. return;
  106. }
  107. assertEquals(
  108. // Java magic number
  109. 'cafedead',
  110. goog.crypt.byteArrayToHex(new Uint8Array([202, 254, 222, 173])));
  111. assertEquals(
  112. // IBM magic number
  113. 'deadbeef',
  114. goog.crypt.byteArrayToHex(new Uint8Array([222, 173, 190, 239])));
  115. }
  116. function testXorByteArray() {
  117. assertElementsEquals(
  118. [20, 83, 96, 66],
  119. goog.crypt.xorByteArray([202, 254, 222, 173], [222, 173, 190, 239]));
  120. }
  121. /** Same as testXorByteArray but with Uint8Array instead of Array<number>. */
  122. function testXorUint8Array() {
  123. if (!goog.isDef(goog.global.Uint8Array)) {
  124. // Uint8Array not supported.
  125. return;
  126. }
  127. assertElementsEquals(
  128. [20, 83, 96, 66], goog.crypt.xorByteArray(
  129. new Uint8Array([202, 254, 222, 173]),
  130. new Uint8Array([222, 173, 190, 239])));
  131. }
  132. // Tests a one-megabyte byte array conversion to string.
  133. // This would break on many JS implementations unless byteArrayToString
  134. // split the input up.
  135. // See discussion and bug report: http://goo.gl/LrWmZ9
  136. function testByteArrayToStringCallStack() {
  137. // One megabyte is 2 to the 20th.
  138. var count = Math.pow(2, 20);
  139. var bytes = [];
  140. for (var i = 0; i < count; i++) {
  141. bytes.push('A'.charCodeAt(0));
  142. }
  143. var str = goog.crypt.byteArrayToString(bytes);
  144. assertEquals(goog.string.repeat('A', count), str);
  145. }