base64streamdecoder_test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. goog.provide('goog.net.streams.Base64StreamDecoderTest');
  15. goog.setTestOnly('goog.net.streams.Base64StreamDecoderTest');
  16. goog.require('goog.net.streams.Base64StreamDecoder');
  17. goog.require('goog.testing.asserts');
  18. goog.require('goog.testing.jsunit');
  19. // Static test data
  20. // clang-format off
  21. var tests = [
  22. '', '',
  23. 'f', 'Zg==',
  24. 'fo', 'Zm8=',
  25. 'foo', 'Zm9v',
  26. 'foob', 'Zm9vYg==',
  27. 'fooba', 'Zm9vYmE=',
  28. 'foobar', 'Zm9vYmFy',
  29. 'foobar', ' Zm 9v \t Ym \n Fy ', // whitespaces will be ignored
  30. '\xfb\xff\xbf\x4d', '+/+/TQ==',
  31. '\xfb\xff\xbf\x4d', '-_-_TQ..', // websafe
  32. // non-ascii characters
  33. '\xe4\xb8\x80\xe4\xba\x8c\xe4\xb8\x89\xe5\x9b\x9b\xe4\xba\x94\xe5' +
  34. '\x85\xad\xe4\xb8\x83\xe5\x85\xab\xe4\xb9\x9d\xe5\x8d\x81',
  35. '5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2B'
  36. ];
  37. // clang-format on
  38. /**
  39. * @param {string} s The string
  40. * @return {!Array<number>} The UTF-16 codes of the characters of the string.
  41. */
  42. function stringCodes(s) {
  43. codes = [];
  44. for (var i = 0; i < s.length; i++) {
  45. codes.push(s.charCodeAt(i));
  46. }
  47. return codes;
  48. }
  49. function testSingleMessage() {
  50. var decoder = new goog.net.streams.Base64StreamDecoder();
  51. for (var i = 0; i < tests.length; i += 2) {
  52. var decoded = decoder.decode(tests[i + 1]);
  53. if (tests[i]) {
  54. assertElementsEquals(stringCodes(tests[i]), decoded);
  55. } else {
  56. assertNull(decoded);
  57. }
  58. }
  59. }
  60. function testBadMessage() {
  61. var decoder = new goog.net.streams.Base64StreamDecoder();
  62. assertThrows(function() { decoder.decode('badchar!'); });
  63. assertFalse(decoder.isInputValid());
  64. // decoder already invalidated
  65. assertThrows(function() { decoder.decode('abc'); });
  66. assertFalse(decoder.isInputValid());
  67. }
  68. function testMessagesInChunks() {
  69. var decoder = new goog.net.streams.Base64StreamDecoder();
  70. assertNull(decoder.decode('Zm'));
  71. assertNull(decoder.decode('9'));
  72. assertElementsEquals(stringCodes('foobar'), decoder.decode('vYmFyZm'));
  73. assertElementsEquals(stringCodes('foo'), decoder.decode('9v'));
  74. assertElementsEquals(stringCodes('barfoo'), decoder.decode('YmFyZm9v'));
  75. assertTrue(decoder.isInputValid());
  76. }