Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | var assert = require('assert'), |
michael@0 | 2 | spdy = require('../../'), |
michael@0 | 3 | Buffer = require('buffer').Buffer, |
michael@0 | 4 | Stream = require('stream').Stream; |
michael@0 | 5 | |
michael@0 | 6 | suite('A Framer of SPDY module', function() { |
michael@0 | 7 | var inflate, |
michael@0 | 8 | deflate, |
michael@0 | 9 | framer; |
michael@0 | 10 | |
michael@0 | 11 | setup(function() { |
michael@0 | 12 | inflate = spdy.utils.zwrap(spdy.utils.createInflate(2)); |
michael@0 | 13 | deflate = spdy.utils.zwrap(spdy.utils.createDeflate(2)); |
michael@0 | 14 | framer = new spdy.protocol[2].Framer(deflate, inflate); |
michael@0 | 15 | }); |
michael@0 | 16 | |
michael@0 | 17 | /* |
michael@0 | 18 | deflate.on('data', function(b) {console.log(b)}); |
michael@0 | 19 | deflate.write(new Buffer([ |
michael@0 | 20 | 0x00, 0x02, // Number of name+value |
michael@0 | 21 | 0, 0x04, // Name length |
michael@0 | 22 | 0x68, 0x6f, 0x73, 0x74, // 'host' |
michael@0 | 23 | 0, 0x09, // Value length |
michael@0 | 24 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, // 'localhost' |
michael@0 | 25 | 0, 0x06, |
michael@0 | 26 | 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, // 'custom', |
michael@0 | 27 | 0, 0x1, |
michael@0 | 28 | 0x31 // '1' |
michael@0 | 29 | ])); |
michael@0 | 30 | deflate.flush(); |
michael@0 | 31 | */ |
michael@0 | 32 | |
michael@0 | 33 | suite('frame parsing', function() { |
michael@0 | 34 | test('given a SYN_STREAM should return correct frame', function(done) { |
michael@0 | 35 | var body = new Buffer([ |
michael@0 | 36 | 0x00, 0x00, 0x00, 0x01, // Stream ID |
michael@0 | 37 | 0x00, 0x00, 0x00, 0x00, // Associated Stream ID |
michael@0 | 38 | 0x00, 0x00, // Priority + Unused |
michael@0 | 39 | 0x78, 0xbb, 0xdf, 0xa2, 0x51, 0xb2, // Deflated Name/Value pairs |
michael@0 | 40 | 0x62, 0x60, 0x62, 0x60, 0x01, 0xe5, 0x12, |
michael@0 | 41 | 0x06, 0x4e, 0x50, 0x50, 0xe6, 0x80, 0x99, |
michael@0 | 42 | 0x6c, 0xc9, 0xa5, 0xc5, 0x25, 0xf9, 0xb9, |
michael@0 | 43 | 0x0c, 0x8c, 0x86, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff |
michael@0 | 44 | ]); |
michael@0 | 45 | framer.execute({ |
michael@0 | 46 | control: true, |
michael@0 | 47 | type: 1, |
michael@0 | 48 | length: body.length |
michael@0 | 49 | }, body, function(err, frame) { |
michael@0 | 50 | assert.ok(!err); |
michael@0 | 51 | assert.equal(frame.type, 'SYN_STREAM'); |
michael@0 | 52 | assert.equal(frame.id, 1); |
michael@0 | 53 | assert.equal(frame.associated, 0); |
michael@0 | 54 | assert.equal(frame.headers.host, 'localhost'); |
michael@0 | 55 | assert.equal(frame.headers.custom, '1'); |
michael@0 | 56 | done(); |
michael@0 | 57 | }); |
michael@0 | 58 | }); |
michael@0 | 59 | |
michael@0 | 60 | test('given a SYN_REPLY should return correct frame', function(done) { |
michael@0 | 61 | var body = new Buffer([ |
michael@0 | 62 | 0x00, 0x00, 0x00, 0x01, // Stream ID |
michael@0 | 63 | 0x00, 0x00, // Unused |
michael@0 | 64 | 0x78, 0xbb, 0xdf, 0xa2, 0x51, 0xb2, // Deflated Name/Value pairs |
michael@0 | 65 | 0x62, 0x60, 0x62, 0x60, 0x01, 0xe5, 0x12, |
michael@0 | 66 | 0x06, 0x4e, 0x50, 0x50, 0xe6, 0x80, 0x99, |
michael@0 | 67 | 0x6c, 0xc9, 0xa5, 0xc5, 0x25, 0xf9, 0xb9, |
michael@0 | 68 | 0x0c, 0x8c, 0x86, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff |
michael@0 | 69 | ]); |
michael@0 | 70 | framer.execute({ |
michael@0 | 71 | control: true, |
michael@0 | 72 | type: 2, |
michael@0 | 73 | length: body.length |
michael@0 | 74 | }, body, function(err, frame) { |
michael@0 | 75 | assert.ok(!err); |
michael@0 | 76 | assert.equal(frame.type, 'SYN_REPLY'); |
michael@0 | 77 | assert.equal(frame.id, 1); |
michael@0 | 78 | assert.equal(frame.headers.host, 'localhost'); |
michael@0 | 79 | assert.equal(frame.headers.custom, '1'); |
michael@0 | 80 | done(); |
michael@0 | 81 | }); |
michael@0 | 82 | }); |
michael@0 | 83 | |
michael@0 | 84 | test('given a RST_STREAM should return correct frame', function(done) { |
michael@0 | 85 | var body = new Buffer([0, 0, 0, 1, 0, 0, 0, 2]); |
michael@0 | 86 | framer.execute({ |
michael@0 | 87 | control: true, |
michael@0 | 88 | type: 3, |
michael@0 | 89 | length: body.length |
michael@0 | 90 | }, body, function(err, frame) { |
michael@0 | 91 | assert.ok(!err); |
michael@0 | 92 | assert.equal(frame.type, 'RST_STREAM'); |
michael@0 | 93 | assert.equal(frame.id, 1); |
michael@0 | 94 | assert.equal(frame.status, 2); |
michael@0 | 95 | done(); |
michael@0 | 96 | }); |
michael@0 | 97 | }); |
michael@0 | 98 | |
michael@0 | 99 | test('given a NOOP frame should return correct frame', function(done) { |
michael@0 | 100 | framer.execute({ |
michael@0 | 101 | control: true, |
michael@0 | 102 | type: 5, |
michael@0 | 103 | length: 0 |
michael@0 | 104 | }, new Buffer(0), function(err, frame) { |
michael@0 | 105 | assert.ok(!err); |
michael@0 | 106 | assert.equal(frame.type, 'NOOP'); |
michael@0 | 107 | done(); |
michael@0 | 108 | }); |
michael@0 | 109 | }); |
michael@0 | 110 | |
michael@0 | 111 | test('given a PING frame should return correct frame', function(done) { |
michael@0 | 112 | framer.execute({ |
michael@0 | 113 | control: true, |
michael@0 | 114 | type: 6, |
michael@0 | 115 | length: 0 |
michael@0 | 116 | }, new Buffer(0), function(err, frame) { |
michael@0 | 117 | assert.ok(!err); |
michael@0 | 118 | assert.equal(frame.type, 'PING'); |
michael@0 | 119 | done(); |
michael@0 | 120 | }); |
michael@0 | 121 | }); |
michael@0 | 122 | |
michael@0 | 123 | test('given a GOAWAY frame should return correct frame', function(done) { |
michael@0 | 124 | var body = new Buffer([0, 0, 0, 1]); |
michael@0 | 125 | framer.execute({ |
michael@0 | 126 | control: true, |
michael@0 | 127 | type: 7, |
michael@0 | 128 | length: body.length |
michael@0 | 129 | }, body, function(err, frame) { |
michael@0 | 130 | assert.ok(!err); |
michael@0 | 131 | assert.equal(frame.type, 'GOAWAY'); |
michael@0 | 132 | assert.equal(frame.lastId, 1); |
michael@0 | 133 | done(); |
michael@0 | 134 | }); |
michael@0 | 135 | }); |
michael@0 | 136 | }); |
michael@0 | 137 | |
michael@0 | 138 | suite('frame generation', function() { |
michael@0 | 139 | test('.replyFrame() should generate correct frame', function(done) { |
michael@0 | 140 | framer.replyFrame(1, 200, 'ok', {}, function(err, chunks) { |
michael@0 | 141 | assert.equal(err, null); |
michael@0 | 142 | assert.ok(chunks.length > 1); |
michael@0 | 143 | done(); |
michael@0 | 144 | }); |
michael@0 | 145 | }); |
michael@0 | 146 | |
michael@0 | 147 | test('.streamFrame() should generate correct frame', function(done) { |
michael@0 | 148 | framer.streamFrame(2, 1, { url : '/' }, {}, function(err, chunks) { |
michael@0 | 149 | assert.equal(err, null); |
michael@0 | 150 | assert.ok(chunks.length > 1); |
michael@0 | 151 | done(); |
michael@0 | 152 | }); |
michael@0 | 153 | }); |
michael@0 | 154 | |
michael@0 | 155 | test('.dataFrame() w/o fin should generate correct frame', function() { |
michael@0 | 156 | var frame = framer.dataFrame(1, false, new Buffer(123)); |
michael@0 | 157 | assert.equal(frame[4], 0); |
michael@0 | 158 | assert.ok(frame.length > 8); |
michael@0 | 159 | }); |
michael@0 | 160 | |
michael@0 | 161 | test('.dataFrame() with fin should generate correct frame', function() { |
michael@0 | 162 | var frame = framer.dataFrame(1, true, new Buffer(123)); |
michael@0 | 163 | assert.equal(frame[4], 1); |
michael@0 | 164 | assert.ok(frame.length > 8); |
michael@0 | 165 | }); |
michael@0 | 166 | |
michael@0 | 167 | test('.pingFrame() should generate correct frame', function() { |
michael@0 | 168 | var frame = framer.pingFrame(new Buffer([0, 1, 2, 3])); |
michael@0 | 169 | assert.ok(frame.length > 0); |
michael@0 | 170 | }); |
michael@0 | 171 | |
michael@0 | 172 | test('.rstFrame() should generate correct frame', function() { |
michael@0 | 173 | var frame = framer.rstFrame(1, 2); |
michael@0 | 174 | assert.ok(frame.length > 0); |
michael@0 | 175 | |
michael@0 | 176 | // Verify that cache works |
michael@0 | 177 | var frame = framer.rstFrame(1, 2); |
michael@0 | 178 | assert.ok(frame.length > 0); |
michael@0 | 179 | }); |
michael@0 | 180 | }); |
michael@0 | 181 | }); |