testing/xpcshell/node-http2/node_modules/http2-protocol/test/framer.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 var expect = require('chai').expect;
michael@0 2 var util = require('./util');
michael@0 3
michael@0 4 var framer = require('../lib/framer');
michael@0 5 var Serializer = framer.Serializer;
michael@0 6 var Deserializer = framer.Deserializer;
michael@0 7
michael@0 8 var frame_types = {
michael@0 9 DATA: ['data'],
michael@0 10 HEADERS: ['priority', 'data'],
michael@0 11 PRIORITY: ['priority'],
michael@0 12 RST_STREAM: ['error'],
michael@0 13 SETTINGS: ['settings'],
michael@0 14 PUSH_PROMISE: ['promised_stream', 'data'],
michael@0 15 PING: ['data'],
michael@0 16 GOAWAY: ['last_stream', 'error'],
michael@0 17 WINDOW_UPDATE: ['window_size'],
michael@0 18 CONTINUATION: ['data']
michael@0 19 };
michael@0 20
michael@0 21 var test_frames = [{
michael@0 22 frame: {
michael@0 23 type: 'DATA',
michael@0 24 flags: { END_STREAM: false, END_SEGMENT: false, RESERVED4: false,
michael@0 25 RESERVED8: false, PAD_LOW: false, PAD_HIGH: false },
michael@0 26 stream: 10,
michael@0 27
michael@0 28 data: new Buffer('12345678', 'hex')
michael@0 29 },
michael@0 30 // length + type + flags + stream + content
michael@0 31 buffer: new Buffer('0004' + '00' + '00' + '0000000A' + '12345678', 'hex')
michael@0 32
michael@0 33 }, {
michael@0 34 frame: {
michael@0 35 type: 'HEADERS',
michael@0 36 flags: { END_STREAM: false, END_SEGMENT: false, END_HEADERS: false,
michael@0 37 PRIORITY: false, PAD_LOW: false, PAD_HIGH: false },
michael@0 38 stream: 15,
michael@0 39
michael@0 40 data: new Buffer('12345678', 'hex')
michael@0 41 },
michael@0 42 buffer: new Buffer('0004' + '01' + '00' + '0000000F' + '12345678', 'hex')
michael@0 43
michael@0 44 }, {
michael@0 45 frame: {
michael@0 46 type: 'HEADERS',
michael@0 47 flags: { END_STREAM: false, END_SEGMENT: false, END_HEADERS: false,
michael@0 48 PRIORITY: true, PAD_LOW: false, PAD_HIGH: false },
michael@0 49 stream: 15,
michael@0 50
michael@0 51 priority: 3,
michael@0 52 data: new Buffer('12345678', 'hex')
michael@0 53 },
michael@0 54 buffer: new Buffer('0008' + '01' + '08' + '0000000F' + '00000003' + '12345678', 'hex')
michael@0 55
michael@0 56 }, {
michael@0 57 frame: {
michael@0 58 type: 'PRIORITY',
michael@0 59 flags: { },
michael@0 60 stream: 10,
michael@0 61
michael@0 62 priority: 3
michael@0 63 },
michael@0 64 buffer: new Buffer('0004' + '02' + '00' + '0000000A' + '00000003', 'hex')
michael@0 65
michael@0 66 }, {
michael@0 67 frame: {
michael@0 68 type: 'RST_STREAM',
michael@0 69 flags: { },
michael@0 70 stream: 10,
michael@0 71
michael@0 72 error: 'INTERNAL_ERROR'
michael@0 73 },
michael@0 74 buffer: new Buffer('0004' + '03' + '00' + '0000000A' + '00000002', 'hex')
michael@0 75
michael@0 76 }, {
michael@0 77 frame: {
michael@0 78 type: 'SETTINGS',
michael@0 79 flags: { ACK: false },
michael@0 80 stream: 10,
michael@0 81
michael@0 82 settings: {
michael@0 83 SETTINGS_HEADER_TABLE_SIZE: 0x12345678,
michael@0 84 SETTINGS_ENABLE_PUSH: true,
michael@0 85 SETTINGS_MAX_CONCURRENT_STREAMS: 0x01234567,
michael@0 86 SETTINGS_INITIAL_WINDOW_SIZE: 0x89ABCDEF
michael@0 87 }
michael@0 88 },
michael@0 89 buffer: new Buffer('0014' + '04' + '00' + '0000000A' + '01' + '12345678' +
michael@0 90 '02' + '00000001' +
michael@0 91 '03' + '01234567' +
michael@0 92 '04' + '89ABCDEF', 'hex')
michael@0 93
michael@0 94 }, {
michael@0 95 frame: {
michael@0 96 type: 'PUSH_PROMISE',
michael@0 97 flags: { RESERVED1: false, RESERVED2: false, END_PUSH_PROMISE: false },
michael@0 98 stream: 15,
michael@0 99
michael@0 100 promised_stream: 3,
michael@0 101 data: new Buffer('12345678', 'hex')
michael@0 102 },
michael@0 103 buffer: new Buffer('0008' + '05' + '00' + '0000000F' + '00000003' + '12345678', 'hex')
michael@0 104
michael@0 105 }, {
michael@0 106 frame: {
michael@0 107 type: 'PING',
michael@0 108 flags: { ACK: false },
michael@0 109 stream: 15,
michael@0 110
michael@0 111 data: new Buffer('1234567887654321', 'hex')
michael@0 112 },
michael@0 113 buffer: new Buffer('0008' + '06' + '00' + '0000000F' + '1234567887654321', 'hex')
michael@0 114
michael@0 115 }, {
michael@0 116 frame: {
michael@0 117 type: 'GOAWAY',
michael@0 118 flags: { },
michael@0 119 stream: 10,
michael@0 120
michael@0 121 last_stream: 0x12345678,
michael@0 122 error: 'PROTOCOL_ERROR'
michael@0 123 },
michael@0 124 buffer: new Buffer('0008' + '07' + '00' + '0000000A' + '12345678' + '00000001', 'hex')
michael@0 125
michael@0 126 }, {
michael@0 127 frame: {
michael@0 128 type: 'WINDOW_UPDATE',
michael@0 129 flags: { },
michael@0 130 stream: 10,
michael@0 131
michael@0 132 window_size: 0x12345678
michael@0 133 },
michael@0 134 buffer: new Buffer('0004' + '08' + '00' + '0000000A' + '12345678', 'hex')
michael@0 135 }, {
michael@0 136 frame: {
michael@0 137 type: 'CONTINUATION',
michael@0 138 flags: { RESERVED1: false, RESERVED2: false, END_HEADERS: true,
michael@0 139 RESERVED8: false, PAD_LOW: false, PAD_HIGH: false },
michael@0 140 stream: 10,
michael@0 141
michael@0 142 data: new Buffer('12345678', 'hex')
michael@0 143 },
michael@0 144 // length + type + flags + stream + content
michael@0 145 buffer: new Buffer('0004' + '09' + '04' + '0000000A' + '12345678', 'hex')
michael@0 146 }];
michael@0 147
michael@0 148 var deserializer_test_frames = test_frames.slice(0);
michael@0 149 var padded_test_frames = [{
michael@0 150 frame: {
michael@0 151 type: 'DATA',
michael@0 152 flags: { END_STREAM: false, END_SEGMENT: false, RESERVED4: false,
michael@0 153 RESERVED8: false, PAD_LOW: true, PAD_HIGH: false },
michael@0 154 stream: 10,
michael@0 155 data: new Buffer('12345678', 'hex')
michael@0 156 },
michael@0 157 // length + type + flags + stream + pad_low control + content + padding
michael@0 158 buffer: new Buffer('000B' + '00' + '10' + '0000000A' + '06' + '12345678' + '000000000000', 'hex')
michael@0 159
michael@0 160 }, {
michael@0 161 frame: {
michael@0 162 type: 'HEADERS',
michael@0 163 flags: { END_STREAM: false, END_SEGMENT: false, END_HEADERS: false,
michael@0 164 PRIORITY: false, PAD_LOW: true, PAD_HIGH: false },
michael@0 165 stream: 15,
michael@0 166
michael@0 167 data: new Buffer('12345678', 'hex')
michael@0 168 },
michael@0 169 buffer: new Buffer('000B' + '01' + '10' + '0000000F' + '06' + '12345678' + '000000000000', 'hex')
michael@0 170
michael@0 171 }, {
michael@0 172 frame: {
michael@0 173 type: 'HEADERS',
michael@0 174 flags: { END_STREAM: false, END_SEGMENT: false, END_HEADERS: false,
michael@0 175 PRIORITY: true, PAD_LOW: true, PAD_HIGH: false },
michael@0 176 stream: 15,
michael@0 177
michael@0 178 priority: 3,
michael@0 179 data: new Buffer('12345678', 'hex')
michael@0 180 },
michael@0 181 buffer: new Buffer('000F' + '01' + '18' + '0000000F' + '06' + '00000003' + '12345678' + '000000000000', 'hex')
michael@0 182
michael@0 183 }, {
michael@0 184 frame: {
michael@0 185 type: 'CONTINUATION',
michael@0 186 flags: { RESERVED1: false, RESERVED2: false, END_HEADERS: true,
michael@0 187 RESERVED8: false, PAD_LOW: true, PAD_HIGH: false },
michael@0 188 stream: 10,
michael@0 189
michael@0 190 data: new Buffer('12345678', 'hex')
michael@0 191 },
michael@0 192 // length + type + flags + stream + content
michael@0 193 buffer: new Buffer('000B' + '09' + '14' + '0000000A' + '06' + '12345678' + '000000000000', 'hex')
michael@0 194 }];
michael@0 195 for (var idx = 0; idx < padded_test_frames.length; idx++) {
michael@0 196 deserializer_test_frames.push(padded_test_frames[idx]);
michael@0 197 }
michael@0 198
michael@0 199
michael@0 200 describe('framer.js', function() {
michael@0 201 describe('Serializer', function() {
michael@0 202 describe('static method .commonHeader({ type, flags, stream }, buffer_array)', function() {
michael@0 203 it('should add the appropriate 8 byte header buffer in front of the others', function() {
michael@0 204 for (var i = 0; i < test_frames.length; i++) {
michael@0 205 var test = test_frames[i];
michael@0 206 var buffers = [test.buffer.slice(8)];
michael@0 207 var header_buffer = test.buffer.slice(0,8);
michael@0 208 Serializer.commonHeader(test.frame, buffers);
michael@0 209 expect(buffers[0]).to.deep.equal(header_buffer);
michael@0 210 }
michael@0 211 });
michael@0 212 });
michael@0 213
michael@0 214 Object.keys(frame_types).forEach(function(type) {
michael@0 215 var tests = test_frames.filter(function(test) { return test.frame.type === type; });
michael@0 216 var frame_shape = '{ ' + frame_types[type].join(', ') + ' }';
michael@0 217 describe('static method .' + type + '(' + frame_shape + ', buffer_array)', function() {
michael@0 218 it('should push buffers to the array that make up a ' + type + ' type payload', function() {
michael@0 219 for (var i = 0; i < tests.length; i++) {
michael@0 220 var test = tests[i];
michael@0 221 var buffers = [];
michael@0 222 Serializer[type](test.frame, buffers);
michael@0 223 expect(util.concat(buffers)).to.deep.equal(test.buffer.slice(8));
michael@0 224 }
michael@0 225 });
michael@0 226 });
michael@0 227 });
michael@0 228
michael@0 229 describe('transform stream', function() {
michael@0 230 it('should transform frame objects to appropriate buffers', function() {
michael@0 231 var stream = new Serializer(util.log);
michael@0 232
michael@0 233 for (var i = 0; i < test_frames.length; i++) {
michael@0 234 var test = test_frames[i];
michael@0 235 stream.write(test.frame);
michael@0 236 var chunk, buffer = new Buffer(0);
michael@0 237 while (chunk = stream.read()) {
michael@0 238 buffer = util.concat([buffer, chunk]);
michael@0 239 }
michael@0 240 expect(buffer).to.be.deep.equal(test.buffer);
michael@0 241 }
michael@0 242 });
michael@0 243 });
michael@0 244 });
michael@0 245
michael@0 246 describe('Deserializer', function() {
michael@0 247 describe('static method .commonHeader(header_buffer, frame)', function() {
michael@0 248 it('should augment the frame object with these properties: { type, flags, stream })', function() {
michael@0 249 for (var i = 0; i < deserializer_test_frames.length; i++) {
michael@0 250 var test = deserializer_test_frames[i], frame = {};
michael@0 251 Deserializer.commonHeader(test.buffer.slice(0,8), frame);
michael@0 252 expect(frame).to.deep.equal({
michael@0 253 type: test.frame.type,
michael@0 254 flags: test.frame.flags,
michael@0 255 stream: test.frame.stream
michael@0 256 });
michael@0 257 }
michael@0 258 });
michael@0 259 });
michael@0 260
michael@0 261 Object.keys(frame_types).forEach(function(type) {
michael@0 262 var tests = deserializer_test_frames.filter(function(test) { return test.frame.type === type; });
michael@0 263 var frame_shape = '{ ' + frame_types[type].join(', ') + ' }';
michael@0 264 describe('static method .' + type + '(payload_buffer, frame)', function() {
michael@0 265 it('should augment the frame object with these properties: ' + frame_shape, function() {
michael@0 266 for (var i = 0; i < tests.length; i++) {
michael@0 267 var test = tests[i];
michael@0 268 var frame = {
michael@0 269 type: test.frame.type,
michael@0 270 flags: test.frame.flags,
michael@0 271 stream: test.frame.stream
michael@0 272 };
michael@0 273 Deserializer[type](test.buffer.slice(8), frame);
michael@0 274 expect(frame).to.deep.equal(test.frame);
michael@0 275 }
michael@0 276 });
michael@0 277 });
michael@0 278 });
michael@0 279
michael@0 280 describe('transform stream', function() {
michael@0 281 it('should transform buffers to appropriate frame object', function() {
michael@0 282 var stream = new Deserializer(util.log);
michael@0 283
michael@0 284 var shuffled = util.shuffleBuffers(deserializer_test_frames.map(function(test) { return test.buffer; }));
michael@0 285 shuffled.forEach(stream.write.bind(stream));
michael@0 286
michael@0 287 for (var j = 0; j < deserializer_test_frames.length; j++) {
michael@0 288 expect(stream.read()).to.be.deep.equal(deserializer_test_frames[j].frame);
michael@0 289 }
michael@0 290 });
michael@0 291 });
michael@0 292 });
michael@0 293
michael@0 294 describe('bunyan formatter', function() {
michael@0 295 describe('`frame`', function() {
michael@0 296 var format = framer.serializers.frame;
michael@0 297 it('should assign a unique ID to each frame', function() {
michael@0 298 var frame1 = { type: 'DATA', data: new Buffer(10) };
michael@0 299 var frame2 = { type: 'PRIORITY', priority: 1 };
michael@0 300 expect(format(frame1).id).to.be.equal(format(frame1));
michael@0 301 expect(format(frame2).id).to.be.equal(format(frame2));
michael@0 302 expect(format(frame1)).to.not.be.equal(format(frame2));
michael@0 303 });
michael@0 304 });
michael@0 305 });
michael@0 306 });

mercurial