base64pbstreamparser_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.module('goog.net.streams.Base64PbStreamParserTest');
  15. goog.setTestOnly('goog.net.streams.Base64PbStreamParserTest');
  16. var Base64PbStreamParser = goog.require('goog.net.streams.Base64PbStreamParser');
  17. var base64 = goog.require('goog.crypt.base64');
  18. var object = goog.require('goog.object');
  19. var testSuite = goog.require('goog.testing.testSuite');
  20. // Static test data
  21. // clang-format off
  22. var testMessage1 = {
  23. data: [
  24. 0x0a, 0x00, // msg: ''
  25. 0x0a, 0x07, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, // msg: 'abcdefg'
  26. 0x7a, 0x04, 0x70, 0x61, 0x64, 0x31, // padding: 'pad1'
  27. 0x0a, 0x08, // msg: (special chars)
  28. 0x00, 0x01, 0x02, 0x03, 0x0a, 0xff, 0xfe, 0xfd, 0x7a, 0x00, // padding: ''
  29. 0x12, 0x17, // status: (23 bytes long sub-message)
  30. 0x08, 0xc8, 0x01, 0x12, 0x12, 0x73, 0x6f, 0x6d, 0x65, 0x74,
  31. 0x68, 0x69, 0x6e, 0x67, 0x2d, 0x69, 0x73, 0x2d, 0x77, 0x72,
  32. 0x6f, 0x6e, 0x67,
  33. 0x7a, 0x02, 0x00, 0x00 // padding: {0x00, 0x00}
  34. ],
  35. parsed: [
  36. {1: []},
  37. {1: [0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67]},
  38. {1: [0x00, 0x01, 0x02, 0x03, 0x0a, 0xff, 0xfe, 0xfd]},
  39. {2: [0x08, 0xc8, 0x01, 0x12, 0x12, 0x73, 0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69,
  40. 0x6e, 0x67, 0x2d, 0x69, 0x73, 0x2d, 0x77, 0x72, 0x6f, 0x6e, 0x67]}
  41. ]
  42. };
  43. // clang-format on
  44. /**
  45. * Encodes input with base64 encoding.
  46. * @param {!Array<number>} input The input bytes
  47. * @return {string} The encoded string
  48. */
  49. function encodeBytes(input) {
  50. return base64.encodeByteArray(input, true /* websafe */);
  51. }
  52. testSuite({
  53. testSingleMessage: function() {
  54. var parser = new Base64PbStreamParser();
  55. var input = 'CgX__gABdw==';
  56. var result = parser.parse(input);
  57. assertEquals(1, result.length);
  58. assertElementsEquals(['1'], object.getKeys(result[0]));
  59. assertElementsEquals([0xFF, 0xFE, 0x00, 0x01, 0x77], result[0][1]);
  60. if (typeof Uint8Array !== 'undefined') {
  61. assertTrue(result[0][1] instanceof Uint8Array);
  62. } else {
  63. assertTrue(result[0][1] instanceof Array);
  64. }
  65. result = parser.parse('');
  66. assertNull(result);
  67. assertTrue(parser.isInputValid());
  68. },
  69. testMultipleMessages: function() {
  70. var parser = new Base64PbStreamParser();
  71. var input = encodeBytes(testMessage1.data);
  72. var expected = testMessage1.parsed;
  73. var result = parser.parse(input);
  74. assertEquals(expected.length, result.length);
  75. for (var i = 0; i < expected.length; i++) {
  76. var keys = object.getKeys(result[i]);
  77. assertElementsEquals(object.getKeys(expected[i]), keys);
  78. assertEquals(1, keys.length);
  79. assertElementsEquals(expected[i][keys[0]], result[i][keys[0]]);
  80. }
  81. },
  82. testInvalidInputs: function() {
  83. var parser1 = new Base64PbStreamParser();
  84. // invalid base-64 character
  85. assertThrows(function() { parser1.parse('badchar!'); });
  86. assertFalse(parser1.isInputValid());
  87. // parser already invalidated
  88. assertThrows(function() { parser1.parse('CgX__gABdw=='); });
  89. assertFalse(parser1.isInputValid());
  90. var parser2 = new Base64PbStreamParser();
  91. // invalid message tag
  92. assertThrows(function() { parser2.parse('GgGq'); });
  93. assertFalse(parser2.isInputValid());
  94. var parser3 = new Base64PbStreamParser();
  95. // message length too long
  96. assertThrows(function() { parser3.parse('Cv____8Q'); });
  97. assertFalse(parser3.isInputValid());
  98. },
  99. testMessagesInChunks: function() {
  100. // clang-format off
  101. var data = [
  102. 0x0a, 0x03, 0x61, 0x62, 0x63,
  103. 0x0a, 0x03, 0x64, 0x65, 0x66,
  104. 0x12, 0x03, 0x67, 0x68, 0x69
  105. ];
  106. // clang-format on
  107. var parser = new Base64PbStreamParser();
  108. var result = parser.parse(encodeBytes(data.slice(0, 3)));
  109. assertNull(result);
  110. result = parser.parse(encodeBytes(data.slice(3, 12)));
  111. assertEquals(2, result.length);
  112. assertElementsEquals(['1'], object.getKeys(result[0]));
  113. assertElementsEquals([0x61, 0x62, 0x63], result[0][1]);
  114. assertElementsEquals(['1'], object.getKeys(result[1]));
  115. assertElementsEquals([0x64, 0x65, 0x66], result[1][1]);
  116. result = parser.parse(encodeBytes(data.slice(12)));
  117. assertEquals(1, result.length);
  118. assertElementsEquals(['2'], object.getKeys(result[0]));
  119. assertElementsEquals([0x67, 0x68, 0x69], result[0][2]);
  120. }
  121. });