-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
tag_test.js
221 lines (188 loc) · 7.84 KB
/
tag_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* @fileoverview Tests for tag.js.
*/
goog.module('protobuf.binary.TagTests');
const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
const WireType = goog.require('protobuf.binary.WireType');
const {CHECK_CRITICAL_STATE} = goog.require('protobuf.internal.checks');
const {createTag, get32BitVarintLength, skipField, tagToFieldNumber, tagToWireType} = goog.require('protobuf.binary.tag');
goog.setTestOnly();
/**
* @param {...number} bytes
* @return {!ArrayBuffer}
*/
function createArrayBuffer(...bytes) {
return new Uint8Array(bytes).buffer;
}
describe('skipField', () => {
it('skips varints', () => {
const bufferDecoder =
BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x00));
skipField(bufferDecoder, WireType.VARINT, 1);
expect(bufferDecoder.cursor()).toBe(2);
});
it('throws for out of bounds varints', () => {
const bufferDecoder =
BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x00));
bufferDecoder.setCursor(2);
if (CHECK_CRITICAL_STATE) {
expect(() => skipField(bufferDecoder, WireType.VARINT, 1)).toThrowError();
}
});
it('skips fixed64', () => {
const bufferDecoder = BufferDecoder.fromArrayBuffer(
createArrayBuffer(0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
skipField(bufferDecoder, WireType.FIXED64, 1);
expect(bufferDecoder.cursor()).toBe(8);
});
it('throws for fixed64 if length is too short', () => {
const bufferDecoder =
BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x00));
if (CHECK_CRITICAL_STATE) {
expect(() => skipField(bufferDecoder, WireType.FIXED64, 1))
.toThrowError();
}
});
it('skips fixed32', () => {
const bufferDecoder = BufferDecoder.fromArrayBuffer(
createArrayBuffer(0x80, 0x00, 0x00, 0x00));
skipField(bufferDecoder, WireType.FIXED32, 1);
expect(bufferDecoder.cursor()).toBe(4);
});
it('throws for fixed32 if length is too short', () => {
const bufferDecoder =
BufferDecoder.fromArrayBuffer(createArrayBuffer(0x80, 0x00));
if (CHECK_CRITICAL_STATE) {
expect(() => skipField(bufferDecoder, WireType.FIXED32, 1))
.toThrowError();
}
});
it('skips length delimited', () => {
const bufferDecoder = BufferDecoder.fromArrayBuffer(
createArrayBuffer(0x03, 0x00, 0x00, 0x00));
skipField(bufferDecoder, WireType.DELIMITED, 1);
expect(bufferDecoder.cursor()).toBe(4);
});
it('throws for length delimited if length is too short', () => {
const bufferDecoder =
BufferDecoder.fromArrayBuffer(createArrayBuffer(0x03, 0x00, 0x00));
if (CHECK_CRITICAL_STATE) {
expect(() => skipField(bufferDecoder, WireType.DELIMITED, 1))
.toThrowError();
}
});
it('skips groups', () => {
const bufferDecoder = BufferDecoder.fromArrayBuffer(
createArrayBuffer(0x0B, 0x08, 0x01, 0x0C));
bufferDecoder.setCursor(1);
skipField(bufferDecoder, WireType.START_GROUP, 1);
expect(bufferDecoder.cursor()).toBe(4);
});
it('skips group in group', () => {
const buffer = createArrayBuffer(
0x0B, // start outer
0x10, 0x01, // field: 2, value: 1
0x0B, // start inner group
0x10, 0x01, // payload inner group
0x0C, // stop inner group
0x0C // end outer
);
const bufferDecoder = BufferDecoder.fromArrayBuffer(buffer);
bufferDecoder.setCursor(1);
skipField(bufferDecoder, WireType.START_GROUP, 1);
expect(bufferDecoder.cursor()).toBe(8);
});
it('throws for group if length is too short', () => {
// no closing group
const bufferDecoder =
BufferDecoder.fromArrayBuffer(createArrayBuffer(0x0B, 0x00, 0x00));
if (CHECK_CRITICAL_STATE) {
expect(() => skipField(bufferDecoder, WireType.START_GROUP, 1))
.toThrowError();
}
});
});
describe('tagToWireType', () => {
it('decodes numbers ', () => {
// simple numbers
expect(tagToWireType(0x00)).toBe(WireType.VARINT);
expect(tagToWireType(0x01)).toBe(WireType.FIXED64);
expect(tagToWireType(0x02)).toBe(WireType.DELIMITED);
expect(tagToWireType(0x03)).toBe(WireType.START_GROUP);
expect(tagToWireType(0x04)).toBe(WireType.END_GROUP);
expect(tagToWireType(0x05)).toBe(WireType.FIXED32);
// upper bits should not matter
expect(tagToWireType(0x08)).toBe(WireType.VARINT);
expect(tagToWireType(0x09)).toBe(WireType.FIXED64);
expect(tagToWireType(0x0A)).toBe(WireType.DELIMITED);
expect(tagToWireType(0x0B)).toBe(WireType.START_GROUP);
expect(tagToWireType(0x0C)).toBe(WireType.END_GROUP);
expect(tagToWireType(0x0D)).toBe(WireType.FIXED32);
// upper bits should not matter
expect(tagToWireType(0xF8)).toBe(WireType.VARINT);
expect(tagToWireType(0xF9)).toBe(WireType.FIXED64);
expect(tagToWireType(0xFA)).toBe(WireType.DELIMITED);
expect(tagToWireType(0xFB)).toBe(WireType.START_GROUP);
expect(tagToWireType(0xFC)).toBe(WireType.END_GROUP);
expect(tagToWireType(0xFD)).toBe(WireType.FIXED32);
// negative numbers work
expect(tagToWireType(-8)).toBe(WireType.VARINT);
expect(tagToWireType(-7)).toBe(WireType.FIXED64);
expect(tagToWireType(-6)).toBe(WireType.DELIMITED);
expect(tagToWireType(-5)).toBe(WireType.START_GROUP);
expect(tagToWireType(-4)).toBe(WireType.END_GROUP);
expect(tagToWireType(-3)).toBe(WireType.FIXED32);
});
});
describe('tagToFieldNumber', () => {
it('returns fieldNumber', () => {
expect(tagToFieldNumber(0x08)).toBe(1);
expect(tagToFieldNumber(0x09)).toBe(1);
expect(tagToFieldNumber(0x10)).toBe(2);
expect(tagToFieldNumber(0x12)).toBe(2);
});
});
describe('createTag', () => {
it('combines fieldNumber and wireType', () => {
expect(createTag(WireType.VARINT, 1)).toBe(0x08);
expect(createTag(WireType.FIXED64, 1)).toBe(0x09);
expect(createTag(WireType.DELIMITED, 1)).toBe(0x0A);
expect(createTag(WireType.START_GROUP, 1)).toBe(0x0B);
expect(createTag(WireType.END_GROUP, 1)).toBe(0x0C);
expect(createTag(WireType.FIXED32, 1)).toBe(0x0D);
expect(createTag(WireType.VARINT, 2)).toBe(0x10);
expect(createTag(WireType.FIXED64, 2)).toBe(0x11);
expect(createTag(WireType.DELIMITED, 2)).toBe(0x12);
expect(createTag(WireType.START_GROUP, 2)).toBe(0x13);
expect(createTag(WireType.END_GROUP, 2)).toBe(0x14);
expect(createTag(WireType.FIXED32, 2)).toBe(0x15);
expect(createTag(WireType.VARINT, 0x1FFFFFFF)).toBe(0xFFFFFFF8 >>> 0);
expect(createTag(WireType.FIXED64, 0x1FFFFFFF)).toBe(0xFFFFFFF9 >>> 0);
expect(createTag(WireType.DELIMITED, 0x1FFFFFFF)).toBe(0xFFFFFFFA >>> 0);
expect(createTag(WireType.START_GROUP, 0x1FFFFFFF)).toBe(0xFFFFFFFB >>> 0);
expect(createTag(WireType.END_GROUP, 0x1FFFFFFF)).toBe(0xFFFFFFFC >>> 0);
expect(createTag(WireType.FIXED32, 0x1FFFFFFF)).toBe(0xFFFFFFFD >>> 0);
});
});
describe('get32BitVarintLength', () => {
it('length of tag', () => {
expect(get32BitVarintLength(0)).toBe(1);
expect(get32BitVarintLength(1)).toBe(1);
expect(get32BitVarintLength(1)).toBe(1);
expect(get32BitVarintLength(Math.pow(2, 7) - 1)).toBe(1);
expect(get32BitVarintLength(Math.pow(2, 7))).toBe(2);
expect(get32BitVarintLength(Math.pow(2, 14) - 1)).toBe(2);
expect(get32BitVarintLength(Math.pow(2, 14))).toBe(3);
expect(get32BitVarintLength(Math.pow(2, 21) - 1)).toBe(3);
expect(get32BitVarintLength(Math.pow(2, 21))).toBe(4);
expect(get32BitVarintLength(Math.pow(2, 28) - 1)).toBe(4);
expect(get32BitVarintLength(Math.pow(2, 28))).toBe(5);
expect(get32BitVarintLength(Math.pow(2, 31) - 1)).toBe(5);
expect(get32BitVarintLength(-1)).toBe(5);
expect(get32BitVarintLength(-Math.pow(2, 31))).toBe(5);
expect(get32BitVarintLength(createTag(WireType.VARINT, 0x1fffffff)))
.toBe(5);
expect(get32BitVarintLength(createTag(WireType.FIXED32, 0x1fffffff)))
.toBe(5);
});
});