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.
michael@0 | 1 | var expect = require('chai').expect; |
michael@0 | 2 | var util = require('./util'); |
michael@0 | 3 | |
michael@0 | 4 | var Connection = require('../lib/connection').Connection; |
michael@0 | 5 | |
michael@0 | 6 | var settings = { |
michael@0 | 7 | SETTINGS_MAX_CONCURRENT_STREAMS: 100, |
michael@0 | 8 | SETTINGS_INITIAL_WINDOW_SIZE: 100000 |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | var MAX_PRIORITY = Math.pow(2, 31) - 1; |
michael@0 | 12 | var MAX_RANDOM_PRIORITY = 10; |
michael@0 | 13 | |
michael@0 | 14 | function randomPriority() { |
michael@0 | 15 | return Math.floor(Math.random() * (MAX_RANDOM_PRIORITY + 1)); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | function expectPriorityOrder(priorities) { |
michael@0 | 19 | priorities.forEach(function(bucket, priority) { |
michael@0 | 20 | bucket.forEach(function(stream) { |
michael@0 | 21 | expect(stream._priority).to.be.equal(priority); |
michael@0 | 22 | }); |
michael@0 | 23 | }); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | describe('connection.js', function() { |
michael@0 | 27 | describe('Connection class', function() { |
michael@0 | 28 | describe('method ._insert(stream)', function() { |
michael@0 | 29 | it('should insert the stream in _streamPriorities in a place determined by stream._priority', function() { |
michael@0 | 30 | var streams = []; |
michael@0 | 31 | var connection = Object.create(Connection.prototype, { _streamPriorities: { value: streams }}); |
michael@0 | 32 | var streamCount = 10; |
michael@0 | 33 | |
michael@0 | 34 | for (var i = 0; i < streamCount; i++) { |
michael@0 | 35 | var stream = { _priority: randomPriority() }; |
michael@0 | 36 | connection._insert(stream, stream._priority); |
michael@0 | 37 | expect(connection._streamPriorities[stream._priority]).to.include(stream); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | expectPriorityOrder(connection._streamPriorities); |
michael@0 | 41 | }); |
michael@0 | 42 | }); |
michael@0 | 43 | describe('method ._reprioritize(stream)', function() { |
michael@0 | 44 | it('should eject and then insert the stream in _streamPriorities in a place determined by stream._priority', function() { |
michael@0 | 45 | var streams = []; |
michael@0 | 46 | var connection = Object.create(Connection.prototype, { _streamPriorities: { value: streams }}); |
michael@0 | 47 | var streamCount = 10; |
michael@0 | 48 | var oldPriority, newPriority, stream; |
michael@0 | 49 | |
michael@0 | 50 | for (var i = 0; i < streamCount; i++) { |
michael@0 | 51 | oldPriority = randomPriority(); |
michael@0 | 52 | while ((newPriority = randomPriority()) === oldPriority); |
michael@0 | 53 | stream = { _priority: oldPriority }; |
michael@0 | 54 | connection._insert(stream, oldPriority); |
michael@0 | 55 | connection._reprioritize(stream, newPriority); |
michael@0 | 56 | stream._priority = newPriority; |
michael@0 | 57 | |
michael@0 | 58 | expect(connection._streamPriorities[newPriority]).to.include(stream); |
michael@0 | 59 | expect(connection._streamPriorities[oldPriority] || []).to.not.include(stream); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | expectPriorityOrder(streams); |
michael@0 | 63 | }); |
michael@0 | 64 | }); |
michael@0 | 65 | describe('invalid operation', function() { |
michael@0 | 66 | describe('unsolicited ping answer', function() { |
michael@0 | 67 | it('should be ignored', function() { |
michael@0 | 68 | var connection = new Connection(util.log, 1, settings); |
michael@0 | 69 | |
michael@0 | 70 | connection._receivePing({ |
michael@0 | 71 | stream: 0, |
michael@0 | 72 | type: 'PING', |
michael@0 | 73 | flags: { |
michael@0 | 74 | 'PONG': true |
michael@0 | 75 | }, |
michael@0 | 76 | data: new Buffer(8) |
michael@0 | 77 | }); |
michael@0 | 78 | }); |
michael@0 | 79 | }); |
michael@0 | 80 | }); |
michael@0 | 81 | }); |
michael@0 | 82 | describe('test scenario', function() { |
michael@0 | 83 | var c, s; |
michael@0 | 84 | beforeEach(function() { |
michael@0 | 85 | c = new Connection(util.log.child({ role: 'client' }), 1, settings); |
michael@0 | 86 | s = new Connection(util.log.child({ role: 'client' }), 2, settings); |
michael@0 | 87 | c.pipe(s).pipe(c); |
michael@0 | 88 | }); |
michael@0 | 89 | |
michael@0 | 90 | describe('connection setup', function() { |
michael@0 | 91 | it('should work as expected', function(done) { |
michael@0 | 92 | setTimeout(function() { |
michael@0 | 93 | // If there are no exception until this, then we're done |
michael@0 | 94 | done(); |
michael@0 | 95 | }, 10); |
michael@0 | 96 | }); |
michael@0 | 97 | }); |
michael@0 | 98 | describe('sending/receiving a request', function() { |
michael@0 | 99 | it('should work as expected', function(done) { |
michael@0 | 100 | // Request and response data |
michael@0 | 101 | var request_headers = { |
michael@0 | 102 | ':method': 'GET', |
michael@0 | 103 | ':path': '/' |
michael@0 | 104 | }; |
michael@0 | 105 | var request_data = new Buffer(0); |
michael@0 | 106 | var response_headers = { |
michael@0 | 107 | ':status': '200' |
michael@0 | 108 | }; |
michael@0 | 109 | var response_data = new Buffer('12345678', 'hex'); |
michael@0 | 110 | |
michael@0 | 111 | // Setting up server |
michael@0 | 112 | s.on('stream', function(server_stream) { |
michael@0 | 113 | server_stream.on('headers', function(headers) { |
michael@0 | 114 | expect(headers).to.deep.equal(request_headers); |
michael@0 | 115 | server_stream.headers(response_headers); |
michael@0 | 116 | server_stream.end(response_data); |
michael@0 | 117 | }); |
michael@0 | 118 | }); |
michael@0 | 119 | |
michael@0 | 120 | // Sending request |
michael@0 | 121 | var client_stream = c.createStream(); |
michael@0 | 122 | client_stream.headers(request_headers); |
michael@0 | 123 | client_stream.end(request_data); |
michael@0 | 124 | |
michael@0 | 125 | // Waiting for answer |
michael@0 | 126 | done = util.callNTimes(2, done); |
michael@0 | 127 | client_stream.on('headers', function(headers) { |
michael@0 | 128 | expect(headers).to.deep.equal(response_headers); |
michael@0 | 129 | done(); |
michael@0 | 130 | }); |
michael@0 | 131 | client_stream.on('readable', function() { |
michael@0 | 132 | expect(client_stream.read()).to.deep.equal(response_data); |
michael@0 | 133 | done(); |
michael@0 | 134 | }); |
michael@0 | 135 | }); |
michael@0 | 136 | }); |
michael@0 | 137 | describe('server push', function() { |
michael@0 | 138 | it('should work as expected', function(done) { |
michael@0 | 139 | var request_headers = { ':method': 'get', ':path': '/' }; |
michael@0 | 140 | var response_headers = { ':status': '200' }; |
michael@0 | 141 | var push_request_headers = { ':method': 'get', ':path': '/x' }; |
michael@0 | 142 | var push_response_headers = { ':status': '200' }; |
michael@0 | 143 | var response_content = new Buffer(10); |
michael@0 | 144 | var push_content = new Buffer(10); |
michael@0 | 145 | |
michael@0 | 146 | done = util.callNTimes(5, done); |
michael@0 | 147 | |
michael@0 | 148 | s.on('stream', function(response) { |
michael@0 | 149 | response.headers(response_headers); |
michael@0 | 150 | |
michael@0 | 151 | var pushed = response.promise(push_request_headers); |
michael@0 | 152 | pushed.headers(push_response_headers); |
michael@0 | 153 | pushed.end(push_content); |
michael@0 | 154 | |
michael@0 | 155 | response.end(response_content); |
michael@0 | 156 | }); |
michael@0 | 157 | |
michael@0 | 158 | var request = c.createStream(); |
michael@0 | 159 | request.headers(request_headers); |
michael@0 | 160 | request.end(); |
michael@0 | 161 | request.on('headers', function(headers) { |
michael@0 | 162 | expect(headers).to.deep.equal(response_headers); |
michael@0 | 163 | done(); |
michael@0 | 164 | }); |
michael@0 | 165 | request.on('readable', function() { |
michael@0 | 166 | expect(request.read()).to.deep.equal(response_content); |
michael@0 | 167 | done(); |
michael@0 | 168 | }); |
michael@0 | 169 | request.on('promise', function(pushed, headers) { |
michael@0 | 170 | expect(headers).to.deep.equal(push_request_headers); |
michael@0 | 171 | pushed.on('headers', function(headers) { |
michael@0 | 172 | expect(headers).to.deep.equal(response_headers); |
michael@0 | 173 | done(); |
michael@0 | 174 | }); |
michael@0 | 175 | pushed.on('readable', function() { |
michael@0 | 176 | expect(pushed.read()).to.deep.equal(push_content); |
michael@0 | 177 | done(); |
michael@0 | 178 | }); |
michael@0 | 179 | pushed.on('end', function() { |
michael@0 | 180 | done(); |
michael@0 | 181 | }); |
michael@0 | 182 | }); |
michael@0 | 183 | }); |
michael@0 | 184 | }); |
michael@0 | 185 | describe('ping from client', function() { |
michael@0 | 186 | it('should work as expected', function(done) { |
michael@0 | 187 | c.ping(function() { |
michael@0 | 188 | done(); |
michael@0 | 189 | }); |
michael@0 | 190 | }); |
michael@0 | 191 | }); |
michael@0 | 192 | describe('ping from server', function() { |
michael@0 | 193 | it('should work as expected', function(done) { |
michael@0 | 194 | s.ping(function() { |
michael@0 | 195 | done(); |
michael@0 | 196 | }); |
michael@0 | 197 | }); |
michael@0 | 198 | }); |
michael@0 | 199 | describe('creating two streams and then using them in reverse order', function() { |
michael@0 | 200 | it('should not result in non-monotonous local ID ordering', function() { |
michael@0 | 201 | var s1 = c.createStream(); |
michael@0 | 202 | var s2 = c.createStream(); |
michael@0 | 203 | s2.headers({ ':method': 'get', ':path': '/' }); |
michael@0 | 204 | s1.headers({ ':method': 'get', ':path': '/' }); |
michael@0 | 205 | }); |
michael@0 | 206 | }); |
michael@0 | 207 | describe('creating two promises and then using them in reverse order', function() { |
michael@0 | 208 | it('should not result in non-monotonous local ID ordering', function(done) { |
michael@0 | 209 | s.on('stream', function(response) { |
michael@0 | 210 | response.headers({ ':status': '200' }); |
michael@0 | 211 | |
michael@0 | 212 | var p1 = s.createStream(); |
michael@0 | 213 | var p2 = s.createStream(); |
michael@0 | 214 | response.promise(p2, { ':method': 'get', ':path': '/p2' }); |
michael@0 | 215 | response.promise(p1, { ':method': 'get', ':path': '/p1' }); |
michael@0 | 216 | p2.headers({ ':status': '200' }); |
michael@0 | 217 | p1.headers({ ':status': '200' }); |
michael@0 | 218 | }); |
michael@0 | 219 | |
michael@0 | 220 | var request = c.createStream(); |
michael@0 | 221 | request.headers({ ':method': 'get', ':path': '/' }); |
michael@0 | 222 | |
michael@0 | 223 | done = util.callNTimes(2, done); |
michael@0 | 224 | request.on('promise', function() { |
michael@0 | 225 | done(); |
michael@0 | 226 | }); |
michael@0 | 227 | }); |
michael@0 | 228 | }); |
michael@0 | 229 | describe('closing the connection on one end', function() { |
michael@0 | 230 | it('should result in closed streams on both ends', function(done) { |
michael@0 | 231 | done = util.callNTimes(2, done); |
michael@0 | 232 | c.on('end', done); |
michael@0 | 233 | s.on('end', done); |
michael@0 | 234 | |
michael@0 | 235 | c.close(); |
michael@0 | 236 | }); |
michael@0 | 237 | }); |
michael@0 | 238 | }); |
michael@0 | 239 | }); |