| 1 |
// Copyright 2007 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.crypt.base64Test'); |
| 16 |
goog.setTestOnly('goog.crypt.base64Test'); |
| 17 |
|
| 18 |
goog.require('goog.crypt'); |
| 19 |
goog.require('goog.crypt.base64'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
// Static test data |
| 23 |
// clang-format off |
| 24 |
var tests = [ |
| 25 |
'', '', |
| 26 |
'f', 'Zg==', |
| 27 |
'fo', 'Zm8=', |
| 28 |
'foo', 'Zm9v', |
| 29 |
'foob', 'Zm9vYg==', |
| 30 |
'fooba', 'Zm9vYmE=', |
| 31 |
'foobar', 'Zm9vYmFy', |
| 32 |
|
| 33 |
// Testing non-ascii characters (1-10 in chinese) |
| 34 |
'\xe4\xb8\x80\xe4\xba\x8c\xe4\xb8\x89\xe5\x9b\x9b\xe4\xba\x94\xe5' + |
| 35 |
'\x85\xad\xe4\xb8\x83\xe5\x85\xab\xe4\xb9\x9d\xe5\x8d\x81', |
| 36 |
'5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2B']; |
| 37 |
// clang-format on |
| 38 |
|
| 39 |
/** |
| 40 |
* Asserts that `encoded` matches `expected` when base64 decoded as byte array. |
| 41 |
* @param {!Array<number>} expected The expected binary decoded content. |
| 42 |
* @param {string} encoded The base64 encoded content to check. |
| 43 |
* @param {boolean=} opt_webSafe True if we should use the web-safe alphabet. |
| 44 |
*/ |
| 45 |
function assertDecodeToByteArrayEquals(expected, encoded, opt_webSafe) { |
| 46 |
var dec = goog.crypt.base64.decodeStringToByteArray(encoded, opt_webSafe); |
| 47 |
assertArrayEquals(expected, dec); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Asserts that `encoded` matches `expected` when base64 decoded as uint8array, |
| 53 |
* if supported on this browser (otherwise this function is a no-op). |
| 54 |
* @param {!Array<number>} expected The expected binary decoded content. |
| 55 |
* @param {string} encoded The base64 encoded content to check. |
| 56 |
*/ |
| 57 |
function assertDecodeToUint8ArrayEquals(expected, encoded) { |
| 58 |
if (goog.global.Uint8Array) { |
| 59 |
var dec = goog.crypt.base64.decodeStringToUint8Array(encoded); |
| 60 |
assertArrayEquals(expected, Array.prototype.slice.call(dec)); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
function testByteArrayEncoding() { |
| 66 |
// Let's see if it's sane by feeding it some well-known values. Index i |
| 67 |
// has the input and index i+1 has the expected value. |
| 68 |
for (var i = 0; i < tests.length; i += 2) { |
| 69 |
var enc = goog.crypt.base64.encodeByteArray( |
| 70 |
goog.crypt.stringToByteArray(tests[i])); |
| 71 |
assertEquals(tests[i + 1], enc); |
| 72 |
var expected = goog.crypt.stringToByteArray(tests[i]); |
| 73 |
assertDecodeToByteArrayEquals(expected, enc); |
| 74 |
assertDecodeToUint8ArrayEquals(expected, enc); |
| 75 |
|
| 76 |
// Check that obsolete websafe param has no effect. |
| 77 |
assertDecodeToByteArrayEquals(expected, enc, true /* websafe */); |
| 78 |
|
| 79 |
// Re-encode as websafe. |
| 80 |
enc = goog.crypt.base64.encodeByteArray( |
| 81 |
goog.crypt.stringToByteArray(tests[i], true /* websafe */)); |
| 82 |
|
| 83 |
// Check that decoding accepts websafe codes. |
| 84 |
assertDecodeToByteArrayEquals(expected, enc); |
| 85 |
assertDecodeToUint8ArrayEquals(expected, enc); |
| 86 |
|
| 87 |
// Check that obsolete websafe param has no effect. |
| 88 |
assertDecodeToByteArrayEquals(expected, enc, true /* websafe */); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
function testOddLengthByteArrayEncoding() { |
| 93 |
var buffer = [0, 0, 0]; |
| 94 |
var encodedBuffer = goog.crypt.base64.encodeByteArray(buffer); |
| 95 |
assertEquals('AAAA', encodedBuffer); |
| 96 |
|
| 97 |
assertDecodeToByteArrayEquals(buffer, encodedBuffer); |
| 98 |
assertDecodeToUint8ArrayEquals(buffer, encodedBuffer); |
| 99 |
} |
| 100 |
|
| 101 |
// Tests that decoding a string where the length is not a multiple of 4 does |
| 102 |
// not produce spurious trailing zeroes. This is a regression test for |
| 103 |
// cl/65120705, which fixes a bug that was introduced when support for |
| 104 |
// non-padded base64 encoding was added in cl/20209336. |
| 105 |
function testOddLengthByteArrayDecoding() { |
| 106 |
// The base-64 encoding of the bytes [97, 98, 99, 100], with no padding. |
| 107 |
// The padded version would be "YWJjZA==" (length 8), or "YWJjZA.." if |
| 108 |
// web-safe. |
| 109 |
var encodedBuffer = 'YWJjZA'; |
| 110 |
|
| 111 |
var expected = goog.crypt.stringToByteArray('abcd'); |
| 112 |
assertDecodeToByteArrayEquals(expected, encodedBuffer); |
| 113 |
assertDecodeToUint8ArrayEquals(expected, encodedBuffer); |
| 114 |
|
| 115 |
// Repeat the test in web-safe decoding mode. |
| 116 |
assertDecodeToByteArrayEquals(expected, encodedBuffer, true /* web-safe */); |
| 117 |
} |
| 118 |
|
| 119 |
function testShortcutPathEncoding() { |
| 120 |
// Test the higher-level API (tests the btoa/atob shortcut path) |
| 121 |
for (var i = 0; i < tests.length; i += 2) { |
| 122 |
var enc = goog.crypt.base64.encodeString(tests[i]); |
| 123 |
assertEquals(tests[i + 1], enc); |
| 124 |
var dec = goog.crypt.base64.decodeString(enc); |
| 125 |
assertEquals(tests[i], dec); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
function testMultipleIterations() { |
| 130 |
// Now run it through its paces |
| 131 |
|
| 132 |
var numIterations = 100; |
| 133 |
for (var i = 0; i < numIterations; i++) { |
| 134 |
var input = []; |
| 135 |
for (var j = 0; j < i; j++) input[j] = j % 256; |
| 136 |
|
| 137 |
var encoded = goog.crypt.base64.encodeByteArray(input); |
| 138 |
assertDecodeToByteArrayEquals(input, encoded); |
| 139 |
assertDecodeToUint8ArrayEquals(input, encoded); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
function testWebSafeEncoding() { |
| 144 |
// Test non-websafe / websafe difference |
| 145 |
var test = '>>>???>>>???=/+'; |
| 146 |
var testArr = goog.crypt.stringToByteArray(test); |
| 147 |
|
| 148 |
var enc = goog.crypt.base64.encodeByteArray(testArr); |
| 149 |
assertEquals('Non-websafe broken?', 'Pj4+Pz8/Pj4+Pz8/PS8r', enc); |
| 150 |
|
| 151 |
enc = goog.crypt.base64.encodeString(test); |
| 152 |
assertEquals('Non-websafe broken?', 'Pj4+Pz8/Pj4+Pz8/PS8r', enc); |
| 153 |
|
| 154 |
enc = goog.crypt.base64.encodeByteArray(testArr, true /* websafe */); |
| 155 |
assertEquals('Websafe encoding broken', 'Pj4-Pz8_Pj4-Pz8_PS8r', enc); |
| 156 |
|
| 157 |
enc = goog.crypt.base64.encodeString(test, true); |
| 158 |
assertEquals('Non-websafe broken?', 'Pj4-Pz8_Pj4-Pz8_PS8r', enc); |
| 159 |
assertDecodeToByteArrayEquals(testArr, enc); |
| 160 |
assertDecodeToUint8ArrayEquals(testArr, enc); |
| 161 |
|
| 162 |
var dec = goog.crypt.base64.decodeString(enc, true /* websafe */); |
| 163 |
assertEquals('Websafe decoding broken', test, dec); |
| 164 |
|
| 165 |
// Test parsing malformed characters |
| 166 |
assertThrows('Didn\'t throw on malformed input', function() { |
| 167 |
goog.crypt.base64.decodeStringToByteArray('foooooo)oooo'); |
| 168 |
}); |
| 169 |
// Test parsing malformed characters |
| 170 |
assertThrows('Didn\'t throw on malformed input', function() { |
| 171 |
goog.crypt.base64.decodeStringToUint8Array('foooooo)oooo'); |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
function testDecodeIgnoresSpace() { |
| 176 |
var spaceTests = [ |
| 177 |
// [encoded, expected decoded] |
| 178 |
[' \n\t\r', ''], ['Z g =\n=', 'f'], ['Zm 8=', 'fo'], [' Zm 9v', 'foo'], |
| 179 |
['Zm9v Yg ==\t ', 'foob'], ['\nZ m9 vYm\n E=', 'fooba'], |
| 180 |
[' \nZ \tm9v YmFy ', 'foobar'] |
| 181 |
]; |
| 182 |
|
| 183 |
for (var i = 0; i < spaceTests.length; i++) { |
| 184 |
var thisTest = spaceTests[i]; |
| 185 |
var encoded = thisTest[0]; |
| 186 |
var expectedStr = thisTest[1]; |
| 187 |
var expected = goog.crypt.stringToByteArray(expectedStr); |
| 188 |
|
| 189 |
assertDecodeToByteArrayEquals(expected, encoded); |
| 190 |
assertDecodeToUint8ArrayEquals(expected, encoded); |
| 191 |
assertEquals(expectedStr, goog.crypt.base64.decodeString(encoded)); |
| 192 |
} |
| 193 |
} |