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