Debian Codegraph @

closure-library/closure/goog/net/streams/base64pbstreamparser_test.js

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