pbstreamparser_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.PbStreamParserTest');
  15. goog.setTestOnly('goog.net.streams.PbStreamParserTest');
  16. goog.require('goog.net.streams.PbStreamParser');
  17. goog.require('goog.object');
  18. goog.require('goog.testing.asserts');
  19. goog.require('goog.testing.jsunit');
  20. // clang-format off
  21. var testMessage1 = {
  22. data: [
  23. 0x0a, 0x00, // msg: ''
  24. 0x0a, 0x07, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, // msg: 'abcdefg'
  25. 0x7a, 0x04, 0x70, 0x61, 0x64, 0x31, // padding: 'pad1'
  26. 0x0a, 0x08, // msg: (special chars)
  27. 0x00, 0x01, 0x02, 0x03, 0x0a, 0xff, 0xfe, 0xfd, 0x7a, 0x00, // padding: ''
  28. 0x12, 0x17, // status: (23 bytes long sub-message)
  29. 0x08, 0xc8, 0x01, 0x12, 0x12, 0x73, 0x6f, 0x6d, 0x65, 0x74,
  30. 0x68, 0x69, 0x6e, 0x67, 0x2d, 0x69, 0x73, 0x2d, 0x77, 0x72,
  31. 0x6f, 0x6e, 0x67,
  32. 0x7a, 0x02, 0x00, 0x00 // padding: {0x00, 0x00}
  33. ],
  34. parsed: [
  35. {1: []},
  36. {1: [0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67]},
  37. {1: [0x00, 0x01, 0x02, 0x03, 0x0a, 0xff, 0xfe, 0xfd]},
  38. {2: [0x08, 0xc8, 0x01, 0x12, 0x12, 0x73, 0x6f, 0x6d, 0x65, 0x74, 0x68, 0x69,
  39. 0x6e, 0x67, 0x2d, 0x69, 0x73, 0x2d, 0x77, 0x72, 0x6f, 0x6e, 0x67]}
  40. ]
  41. };
  42. // clang-format on
  43. /**
  44. * @param {!Array<number>} bytes The input bytes
  45. * @return {!ArrayBuffer|!Array<number>} The input bytes in an ArrayBuffer.
  46. * Falls back to native array if ArrayBuffer is not supported.
  47. */
  48. function getInput(bytes) {
  49. if (typeof Uint8Array !== 'undefined') {
  50. return (new Uint8Array(bytes)).buffer;
  51. }
  52. return bytes;
  53. }
  54. function testSingleMessage() {
  55. var parser = new goog.net.streams.PbStreamParser();
  56. var input = getInput([0x0a, 0x05, 0xFF, 0xFE, 0x00, 0x01, 0x77]);
  57. var result = parser.parse(input);
  58. assertEquals(1, result.length);
  59. assertElementsEquals(['1'], goog.object.getKeys(result[0]));
  60. assertElementsEquals([0xFF, 0xFE, 0x00, 0x01, 0x77], result[0][1]);
  61. result = parser.parse(getInput([]));
  62. assertNull(result);
  63. }
  64. function testMultipleMessagesWithPadding() {
  65. var parser = new goog.net.streams.PbStreamParser();
  66. var result = parser.parse(getInput(testMessage1.data));
  67. var expected = testMessage1.parsed;
  68. assertEquals(expected.length, result.length);
  69. for (var i = 0; i < expected.length; i++) {
  70. keys = goog.object.getKeys(result[i]);
  71. assertElementsEquals(goog.object.getKeys(expected[i]), keys);
  72. assertEquals(1, keys.length);
  73. assertElementsEquals(expected[i][keys[0]], result[i][keys[0]]);
  74. if (typeof Uint8Array !== 'undefined') {
  75. assertTrue(result[i][keys[0]] instanceof Uint8Array);
  76. } else {
  77. assertTrue(result[i][keys[0]] instanceof Array);
  78. }
  79. }
  80. }
  81. function testMessagesInChunks() {
  82. // clang-format off
  83. var data = [
  84. 0x0a, 0x03, 0x61, 0x62, 0x63,
  85. 0x0a, 0x03, 0x64, 0x65, 0x66,
  86. 0x12, 0x03, 0x67, 0x68, 0x69
  87. ];
  88. // clang-format on
  89. var parser = new goog.net.streams.PbStreamParser();
  90. var result = parser.parse(getInput(data.slice(0, 3)));
  91. assertNull(result);
  92. result = parser.parse(getInput(data.slice(3, 8)));
  93. assertEquals(1, result.length);
  94. assertElementsEquals(['1'], goog.object.getKeys(result[0]));
  95. assertElementsEquals([0x61, 0x62, 0x63], result[0][1]);
  96. result = parser.parse(getInput(data.slice(8, 10)));
  97. assertEquals(1, result.length);
  98. assertElementsEquals(['1'], goog.object.getKeys(result[0]));
  99. assertElementsEquals([0x64, 0x65, 0x66], result[0][1]);
  100. result = parser.parse(getInput(data.slice(10)));
  101. assertEquals(1, result.length);
  102. assertElementsEquals(['2'], goog.object.getKeys(result[0]));
  103. assertElementsEquals([0x67, 0x68, 0x69], result[0][2]);
  104. }
  105. function testInvalidInputs() {
  106. var parser;
  107. // wrong wire type
  108. parser = new goog.net.streams.PbStreamParser();
  109. assertThrows(function() { parser.parse(getInput([0x0b])); });
  110. // parser already invalidated
  111. assertThrows(function() { parser.parse(getInput([0x0a])); });
  112. // unknown tag
  113. parser = new goog.net.streams.PbStreamParser();
  114. assertThrows(function() { parser.parse([0x1a]); });
  115. // length too long
  116. parser = new goog.net.streams.PbStreamParser();
  117. assertThrows(function() {
  118. parser.parse(getInput([0x0a, 0xff, 0xff, 0xff, 0xff, 0x10]));
  119. });
  120. // length is going to be too long since more varint bytes are comming
  121. parser = new goog.net.streams.PbStreamParser();
  122. assertThrows(function() {
  123. parser.parse(getInput([0x0a, 0xff, 0xff, 0xff, 0xff, 0x80]));
  124. });
  125. }