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