| 1 |
// Copyright 2016 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.net.streams.Base64StreamDecoderTest'); |
| 16 |
goog.setTestOnly('goog.net.streams.Base64StreamDecoderTest'); |
| 17 |
|
| 18 |
goog.require('goog.net.streams.Base64StreamDecoder'); |
| 19 |
goog.require('goog.testing.asserts'); |
| 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 |
'foobar', ' Zm 9v \t Ym \n Fy ', // whitespaces will be ignored |
| 33 |
|
| 34 |
'\xfb\xff\xbf\x4d', '+/+/TQ==', |
| 35 |
'\xfb\xff\xbf\x4d', '-_-_TQ..', // websafe |
| 36 |
|
| 37 |
// non-ascii characters |
| 38 |
'\xe4\xb8\x80\xe4\xba\x8c\xe4\xb8\x89\xe5\x9b\x9b\xe4\xba\x94\xe5' + |
| 39 |
'\x85\xad\xe4\xb8\x83\xe5\x85\xab\xe4\xb9\x9d\xe5\x8d\x81', |
| 40 |
'5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2B' |
| 41 |
]; |
| 42 |
// clang-format on |
| 43 |
|
| 44 |
/** |
| 45 |
* @param {string} s The string |
| 46 |
* @return {!Array<number>} The UTF-16 codes of the characters of the string. |
| 47 |
*/ |
| 48 |
function stringCodes(s) { |
| 49 |
codes = []; |
| 50 |
for (var i = 0; i < s.length; i++) { |
| 51 |
codes.push(s.charCodeAt(i)); |
| 52 |
} |
| 53 |
return codes; |
| 54 |
} |
| 55 |
|
| 56 |
function testSingleMessage() { |
| 57 |
var decoder = new goog.net.streams.Base64StreamDecoder(); |
| 58 |
|
| 59 |
for (var i = 0; i < tests.length; i += 2) { |
| 60 |
var decoded = decoder.decode(tests[i + 1]); |
| 61 |
if (tests[i]) { |
| 62 |
assertElementsEquals(stringCodes(tests[i]), decoded); |
| 63 |
} else { |
| 64 |
assertNull(decoded); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function testBadMessage() { |
| 70 |
var decoder = new goog.net.streams.Base64StreamDecoder(); |
| 71 |
|
| 72 |
assertThrows(function() { decoder.decode('badchar!'); }); |
| 73 |
assertFalse(decoder.isInputValid()); |
| 74 |
|
| 75 |
// decoder already invalidated |
| 76 |
assertThrows(function() { decoder.decode('abc'); }); |
| 77 |
assertFalse(decoder.isInputValid()); |
| 78 |
} |
| 79 |
|
| 80 |
function testMessagesInChunks() { |
| 81 |
var decoder = new goog.net.streams.Base64StreamDecoder(); |
| 82 |
|
| 83 |
assertNull(decoder.decode('Zm')); |
| 84 |
assertNull(decoder.decode('9')); |
| 85 |
assertElementsEquals(stringCodes('foobar'), decoder.decode('vYmFyZm')); |
| 86 |
assertElementsEquals(stringCodes('foo'), decoder.decode('9v')); |
| 87 |
assertElementsEquals(stringCodes('barfoo'), decoder.decode('YmFyZm9v')); |
| 88 |
assertTrue(decoder.isInputValid()); |
| 89 |
} |