From 5e7aa692dfb75b7e518c4fda553f97a55e4fa90d Mon Sep 17 00:00:00 2001 From: Daniel Ly Date: Fri, 28 Oct 2011 12:40:08 +0200 Subject: [PATCH] Integrated dumpChunkStream.js in the unit tests. --- examples/dumpChunkStream.js | 21 +++-- examples/dumpMessageStream.js | 16 ++-- examples/dumpTools.js | 6 ++ lib/rtmp/ChunkStream.js | 10 ++- test/TestChunkStream.js | 87 ++++++++++++------- test/data/incoming1-dumpChunkStream.txt | 11 +++ .../{incoming1.txt => incoming1-rtmptool.txt} | 0 test/data/incoming2-dumpChunkStream.txt | 15 ++++ .../{incoming2.txt => incoming2-rtmptool.txt} | 0 .../{incoming3.txt => incoming3-rtmptool.txt} | 0 test/data/outgoing2-dumpChunkStream.txt | 15 ++++ .../{outgoing2.txt => outgoing2-rtmptool.txt} | 0 test/data/outgoing3-dumpChunkStream.txt | 34 ++++++++ .../{outgoing3.txt => outgoing3-rtmptool.txt} | 0 test/fixtures.js | 2 +- 15 files changed, 173 insertions(+), 44 deletions(-) create mode 100644 test/data/incoming1-dumpChunkStream.txt rename test/data/{incoming1.txt => incoming1-rtmptool.txt} (100%) create mode 100644 test/data/incoming2-dumpChunkStream.txt rename test/data/{incoming2.txt => incoming2-rtmptool.txt} (100%) rename test/data/{incoming3.txt => incoming3-rtmptool.txt} (100%) create mode 100644 test/data/outgoing2-dumpChunkStream.txt rename test/data/{outgoing2.txt => outgoing2-rtmptool.txt} (100%) create mode 100644 test/data/outgoing3-dumpChunkStream.txt rename test/data/{outgoing3.txt => outgoing3-rtmptool.txt} (100%) diff --git a/examples/dumpChunkStream.js b/examples/dumpChunkStream.js index 1bd4115..faec7d7 100644 --- a/examples/dumpChunkStream.js +++ b/examples/dumpChunkStream.js @@ -5,16 +5,19 @@ var net = require('net'); var mtrude = require('mtrude'); var ChunkStream = mtrude.rtmp.ChunkStream; var asSocket = mtrude.asSocket; +var dumpTools = require('./dumpTools'); function main() { var optimist = require('optimist') - .usage('Usage: $0 [--debug] [--debugchain] [in [out]]') + .usage('Usage: $0 [--debug] [--debugchain] [--nocolor] [in [out]]') .boolean('debug') .boolean('debugchain') + .boolean('nocolor') .boolean('help') .alias('h', 'help') .describe('debug', 'Set ChunkStream.DBG = true') .describe('debugchain', 'Set BufferChain.DBG = true') + .describe('nocolor', 'Don\'t use colors') ; var argv = optimist.argv; @@ -23,6 +26,8 @@ function main() { return; } + if (argv.nocolor) dumpTools.dontColor(); + console.log('' + 'NOTE! ChunkStream is not complete without MessageStream and might get\n' + 'stuck because of missing coordination with peer. Dump format:\n' @@ -57,11 +62,10 @@ function main() { } -var DumpTools = require('./DumpTools'); -var dump8 = DumpTools.dump8; -var hex2 = DumpTools.hex2; -var hex6 = DumpTools.hex6; -var ascii8 = DumpTools.ascii8; +var dump8 = dumpTools.dump8; +var hex2 = dumpTools.hex2; +var hex6 = dumpTools.hex6; +var ascii8 = dumpTools.ascii8; var dumpChunkStream = function(chunkStream) { chunkStream.on('error', function(errorMessage) { @@ -78,6 +82,9 @@ var dumpChunkStream = function(chunkStream) { chunkStream.on('warn', function(message) { console.log('WARN : %s', ('ChunkStream: ' + message).red); }); + chunkStream.on('info', function(message) { + console.log('INFO : %s', ('ChunkStream: ' + message).magenta); + }); chunkStream.on('chunk', function(chunk) { console.log( 'CHUNK: %s %s %s %s %s:%s %s:%s,%s', @@ -88,7 +95,7 @@ var dumpChunkStream = function(chunkStream) { chunk.rest.toString().blue); if (chunk.typeid == 1) { var chunkSize = chunk.data.readUInt32LE(0); - chunkStream.warn('Setting chunk size to ' + chunkSize); + chunkStream.info('Setting chunk size to ' + chunkSize); chunkStream.chunkSize = chunkSize; } }); diff --git a/examples/dumpMessageStream.js b/examples/dumpMessageStream.js index 4686fef..162dcb0 100644 --- a/examples/dumpMessageStream.js +++ b/examples/dumpMessageStream.js @@ -7,16 +7,19 @@ var rtmp = mtrude.rtmp; var ChunkStream = rtmp.ChunkStream; var MessageStream = rtmp.MessageStream; var asSocket = mtrude.asSocket; +var dumpTools = require('./dumpTools'); function main() { var optimist = require('optimist') - .usage('Usage: $0 [--debug] [--chunks] [in [out]]') + .usage('Usage: $0 [--debug] [--chunks] [--nocolor] [in [out]]') .boolean('debug') .boolean('chunks') + .boolean('nocolor') .boolean('help') .alias('h', 'help') .describe('debug', 'Set MessageStream.DBG = true') .describe('chunks', 'Also dump chunks') + .describe('nocolor', 'Don\'t use colors') ; var argv = optimist.argv; @@ -25,6 +28,8 @@ function main() { return; } + if (argv.nocolor) dumpTools.dontColor(); + console.log('Dump format:\n' + 'MSG : %s %s %s %s %s:%s %s\n' + 'PING : %s (%s) %s %s%s%s:%s\n', @@ -61,11 +66,10 @@ function main() { dumpMessageStream(messageStream); } -var DumpTools = require('./DumpTools'); -var dump8 = DumpTools.dump8; -var hex2 = DumpTools.hex2; -var hex6 = DumpTools.hex6; -var ascii8 = DumpTools.ascii8; +var dump8 = dumpTools.dump8; +var hex2 = dumpTools.hex2; +var hex6 = dumpTools.hex6; +var ascii8 = dumpTools.ascii8; function dumpMessageStream(messageStream) { messageStream.on('error', function(errorMessage) { diff --git a/examples/dumpTools.js b/examples/dumpTools.js index a2e7a63..8f56fd6 100644 --- a/examples/dumpTools.js +++ b/examples/dumpTools.js @@ -23,3 +23,9 @@ var ascii8 = exports.ascii8 = function(data) { return b(0) + b(1) + b(2) + b(3) + ' ' + b(4) + b(5) + b(6) + b(7); } +var dontColor = exports.dontColor = function() { + var colors = 'cyan yellow blue grey white magenta red green'.split(' '); + for (var i in colors) + Object.defineProperty(String.prototype, colors[i], + { get: function() { return this } }); +} diff --git a/lib/rtmp/ChunkStream.js b/lib/rtmp/ChunkStream.js index 81d4b88..7d84afa 100644 --- a/lib/rtmp/ChunkStream.js +++ b/lib/rtmp/ChunkStream.js @@ -24,6 +24,8 @@ function dbg() { if (ChunkStream.DBG) console.log.apply(console, arguments) } * Emitted on networking or protocol errors * 'warn': function(message) {} * Emitted on potential problems + * 'info': function(message) {} + * Emitted if something special has been found but which poses no problem * 'end' function(graceful) {} * Emitted on disconnect from other end (graceful or not) * 'handshake' function(deltaTime) {} @@ -125,6 +127,10 @@ p.warn = function(warnText) { this.emit('warn', warnText); } +p.info = function(warnText) { + this.emit('info', warnText); +} + p.close = function() { var it = this; @@ -373,7 +379,7 @@ s.chunkType2 = function() { // RTMCSP 6.1.2.4 Chunk Type 2; additional chunk chunks.timestamp = this.buffers.consumeUInt24BE(); resetLengthIfNewMessage(chunks); - this.warn('chunkType2 timestamp 0x' + chunks.timestamp.toString(16)); + this.info('chunkType2 timestamp 0x' + chunks.timestamp.toString(16)); if (chunks.timestamp === 0xffffff) return this.error("Extended Timestamp not supported"); @@ -404,6 +410,8 @@ s.chunkData = function() { // RTMCSP 6.1 Chunk Data var data = this.buffers.consumeBuffers(length); var chunk = newChunk(chunks, chunks.msid, chunks.timestamp, chunks.length, chunks.typeid, data, rest); + dbg('emit : chunk timestamp=%s typeid=%s %s:%s length=', + chunk.timestamp, chunk.typeid, chunk.csid, chunk.msid, chunk.data.length); this.emit('chunk', chunk); this.state = 'chunk'; diff --git a/test/TestChunkStream.js b/test/TestChunkStream.js index 2ae42be..0880ddd 100644 --- a/test/TestChunkStream.js +++ b/test/TestChunkStream.js @@ -1,52 +1,65 @@ "use strict"; -var assert = require('assert'); var fs = require('fs'); var mtrude = require('mtrude'); var ChunkStream = mtrude.rtmp.ChunkStream; +var spawn = require('child_process').spawn; var mockSocket = require('./fixtures').mockSocket; -function fix(assert) { - for (var key in assert) assert.ok[key] = assert[key]; - return assert.ok; -} - -function incoming(filename, chunkCount, exit) { +function incoming(filename, chunkCount, exit, assert) { var handshake = false; var end = false; var length = 0; var chunkIndex = 0; var cs = new ChunkStream(mockSocket(filename)); - cs.on('error', function(err) { - assert(false, filename + ': error emitted: ' + err); + cs.on('error', function(errorMessage) { + assert.ok(false, filename + ': error emitted: ' + errorMessage); + }); + cs.on('warn', function(warnMessage) { + assert.ok(false, filename + ': warn emitted: ' + warnMessage); }); - cs.on('warn', function(message) { console.log('WARN', message); }); cs.on('handshake', function() { handshake = true; }); cs.on('end', function(graceful) { - assert(graceful, filename + ': not a graceful end'); + assert.ok(graceful, filename + ': not a graceful end'); assert.equal(chunkIndex, chunkCount, filename + ': ' + chunkIndex + ' == ' + chunkCount); end = true; }); cs.on('chunk', function(chunk) { - assert(handshake, filename + + assert.ok(handshake, filename + ': handshake must be emitted before any chunks'); chunkIndex++; }); exit(function() { - assert(handshake, filename + ': handshake has not been emitted'); - assert(end, filename + ': end has not been emitted'); + assert.ok(handshake, filename + ': handshake has not been emitted'); + assert.ok(end, filename + ': end has not been emitted'); }); return cs; } +function dumpChunkStream(filename, assert) { + var iFile = 'test/data/' + filename + '.raw'; + var tFile = 'test/data/' + filename + '-dumpChunkStream.txt'; + var js = 'examples/dumpChunkStream.js'; + var child = spawn('node', [js, '--nocolor', iFile, '/dev/null']); + var text = ''; + + child.stdout.on('data', function(data) { text += data }); + child.on('exit', function(code) { + assert.equal(code, 0); + fs.readFile(tFile, function(err, testData) { + assert.equal(err, null, err + ''); + assert.equal(text, testData, js + ' ' + iFile); + }); + }); +} + + module.exports = { 'test incoming1': function(exit, assert) { - assert = fix(assert); - - var cs = incoming('incoming1', 3, exit); + var cs = incoming('incoming1', 3, exit, assert); var chunkIndex = 0; cs.on('chunk', function(chunk) { @@ -61,9 +74,7 @@ module.exports = { }, 'test incoming2': function(exit, assert) { - assert = fix(assert); - - var cs = incoming('incoming2', 7, exit); + var cs = incoming('incoming2', 7, exit, assert); var chunkIndex = 0; cs.on('chunk', function(chunk) { @@ -78,20 +89,38 @@ module.exports = { }, 'test incoming3': function(exit, assert) { - assert = fix(assert); - var cs = incoming('incoming3', 8, exit); + var cs = incoming('incoming3', 8, exit, assert); }, 'test outgoing2': function(exit, assert) { - assert = fix(assert); - var cs = incoming('outgoing2', 7, exit); + var cs = incoming('outgoing2', 7, exit, assert); }, - // Implement ChunkStream.chunkType2 - /* 'test outgoing3': function(exit, assert) { - assert = fix(assert); - var cs = incoming('outgoing3', 7, exit); + var cs = incoming('outgoing3', 25, exit, assert); + var chunkIndex = 0; + + cs.on('chunk', function(chunk) { + if (chunk.typeid == 1) cs.chunkSize = chunk.data.readUInt32LE(0); + assert.equal(chunk.csid, [2, 2, 2, 3, 3, 3, 3, 2, 2, 20, 20, 20, 20, 20, + 20, 20, 2, 21, 21, 20, 20, 2, 20, 20, 2][chunkIndex]); + chunkIndex++; + }); + }, + + 'dump incoming1': function(exit, assert) { + dumpChunkStream('outgoing3', assert); + }, + + 'dump incoming2': function(exit, assert) { + dumpChunkStream('outgoing3', assert); + }, + + 'dump outgoing2': function(exit, assert) { + dumpChunkStream('outgoing2', assert); + }, + + 'dump outgoing3': function(exit, assert) { + dumpChunkStream('outgoing3', assert); }, - */ } diff --git a/test/data/incoming1-dumpChunkStream.txt b/test/data/incoming1-dumpChunkStream.txt new file mode 100644 index 0000000..7901518 --- /dev/null +++ b/test/data/incoming1-dumpChunkStream.txt @@ -0,0 +1,11 @@ +NOTE! ChunkStream is not complete without MessageStream and might get +stuck because of missing coordination with peer. Dump format: +CHUNK: data-1st -8-bytes asci i-8c tstamp ti msg-id:chs-id msglen:chklen,rest + +FILE : Reading test/data/incoming1.raw +FILE : Writing out +HANDS: Handshake completed successfully +CHUNK: 02000763 6f6e6e65 ···c onne 000000 14 000003:000000 235:128,107 +CHUNK: 00000b61 7564696f ···a udio 000000 14 000003:000000 235:107,0 +CHUNK: 002625a0 ·&%· ···· ca90ea 05 000002:000000 4:4,0 +END : ChunkStream gracefully ended diff --git a/test/data/incoming1.txt b/test/data/incoming1-rtmptool.txt similarity index 100% rename from test/data/incoming1.txt rename to test/data/incoming1-rtmptool.txt diff --git a/test/data/incoming2-dumpChunkStream.txt b/test/data/incoming2-dumpChunkStream.txt new file mode 100644 index 0000000..20aeff2 --- /dev/null +++ b/test/data/incoming2-dumpChunkStream.txt @@ -0,0 +1,15 @@ +NOTE! ChunkStream is not complete without MessageStream and might get +stuck because of missing coordination with peer. Dump format: +CHUNK: data-1st -8-bytes asci i-8c tstamp ti msg-id:chs-id msglen:chklen,rest + +FILE : Reading test/data/incoming2.raw +FILE : Writing out +HANDS: Handshake completed successfully +CHUNK: 02000763 6f6e6e65 ···c onne 000001 14 000003:000000 235:128,107 +CHUNK: 00000b61 7564696f ···a udio 000001 14 000003:000000 235:107,0 +CHUNK: 002625a0 ·&%· ···· 603010 05 000002:000000 4:4,0 +CHUNK: 0002000c 63726561 ···· crea 003442 11 000003:000000 26:26,0 +CHUNK: 00030000 00000000 ···· ···· 000000 04 000002:000000 10:10,0 +CHUNK: 00030000 00010000 ···· ···· 000000 04 000002:000000 10:10,0 +CHUNK: 00020004 706c6179 ···· play 005ef4 11 000008:000000 43:43,0 +END : ChunkStream gracefully ended diff --git a/test/data/incoming2.txt b/test/data/incoming2-rtmptool.txt similarity index 100% rename from test/data/incoming2.txt rename to test/data/incoming2-rtmptool.txt diff --git a/test/data/incoming3.txt b/test/data/incoming3-rtmptool.txt similarity index 100% rename from test/data/incoming3.txt rename to test/data/incoming3-rtmptool.txt diff --git a/test/data/outgoing2-dumpChunkStream.txt b/test/data/outgoing2-dumpChunkStream.txt new file mode 100644 index 0000000..5eec0cd --- /dev/null +++ b/test/data/outgoing2-dumpChunkStream.txt @@ -0,0 +1,15 @@ +NOTE! ChunkStream is not complete without MessageStream and might get +stuck because of missing coordination with peer. Dump format: +CHUNK: data-1st -8-bytes asci i-8c tstamp ti msg-id:chs-id msglen:chklen,rest + +FILE : Reading test/data/outgoing2.raw +FILE : Writing /dev/null +HANDS: Handshake completed successfully +CHUNK: 002625a0 ·&%· ···· 000000 05 000002:000000 4:4,0 +CHUNK: 002625a0 02 ·&%· ···· 000000 06 000002:000000 5:5,0 +CHUNK: 00000000 0000 ···· ···· 000000 04 000002:000000 6:6,0 +CHUNK: 0200075f 72657375 ···_ resu 000000 14 000003:000000 189:128,61 +CHUNK: 65736372 69707469 escr ipti 000000 14 000003:000000 189:61,0 +CHUNK: 0200086f 6e425744 ···o nBWD 000000 14 000003:000000 30:30,0 +CHUNK: 0200075f 72657375 ···_ resu 000000 14 000003:000000 29:29,0 +END : ChunkStream gracefully ended diff --git a/test/data/outgoing2.txt b/test/data/outgoing2-rtmptool.txt similarity index 100% rename from test/data/outgoing2.txt rename to test/data/outgoing2-rtmptool.txt diff --git a/test/data/outgoing3-dumpChunkStream.txt b/test/data/outgoing3-dumpChunkStream.txt new file mode 100644 index 0000000..ea02b9c --- /dev/null +++ b/test/data/outgoing3-dumpChunkStream.txt @@ -0,0 +1,34 @@ +NOTE! ChunkStream is not complete without MessageStream and might get +stuck because of missing coordination with peer. Dump format: +CHUNK: data-1st -8-bytes asci i-8c tstamp ti msg-id:chs-id msglen:chklen,rest + +FILE : Reading test/data/outgoing3.raw +FILE : Writing /dev/null +HANDS: Handshake completed successfully +CHUNK: 002625a0 ·&%· ···· 000000 05 000002:000000 4:4,0 +CHUNK: 002625a0 02 ·&%· ···· 000000 06 000002:000000 5:5,0 +CHUNK: 00000000 0000 ···· ···· 000000 04 000002:000000 6:6,0 +CHUNK: 0200075f 72657375 ···_ resu 000000 14 000003:000000 189:128,61 +CHUNK: 65736372 69707469 escr ipti 000000 14 000003:000000 189:61,0 +CHUNK: 0200086f 6e425744 ···o nBWD 000000 14 000003:000000 30:30,0 +CHUNK: 0200075f 72657375 ···_ resu 000000 14 000003:000000 29:29,0 +CHUNK: 00040000 0001 ···· ···· 000000 04 000002:000000 6:6,0 +CHUNK: 00000000 0001 ···· ···· 000000 04 000002:000000 6:6,0 +CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 134:128,6 +CHUNK: 37383400 0009 784· ···· 000000 14 000014:000000 134:6,0 +CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 134:128,6 +CHUNK: 37383400 0009 784· ···· 000000 14 000014:000000 134:6,0 +CHUNK: 0200117c 52746d70 ···| Rtmp 000000 12 000014:000000 24:24,0 +CHUNK: 0200086f 6e537461 ···o nSta 000000 12 000014:000000 44:44,0 +CHUNK: 02000a6f 6e4d6574 ···o nMet 000000 12 000014:000000 105:105,0 +CHUNK: 00400000 ·@·· ···· 000000 01 000002:000000 4:4,0 +INFO : ChunkStream: Setting chunk size to 16384 +CHUNK: 12000084 030299c7 ···· ···· 000000 09 000015:000000 1376:1376,0 +CHUNK: 32000084 074298dc 2··· ·B·· 00004e 09 000015:000000 901:901,0 +CHUNK: 02000c6f 6e506c61 ···o nPla 000000 12 000014:000000 102:102,0 +CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 143:143,0 +CHUNK: 00010000 0001 ···· ···· 000000 04 000002:000000 6:6,0 +CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 136:136,0 +CHUNK: 0200086f 6e537461 ···o nSta 000000 14 000014:000000 143:143,0 +CHUNK: 00010000 0001 ···· ···· 000000 04 000002:000000 6:6,0 +END : ChunkStream gracefully ended diff --git a/test/data/outgoing3.txt b/test/data/outgoing3-rtmptool.txt similarity index 100% rename from test/data/outgoing3.txt rename to test/data/outgoing3-rtmptool.txt diff --git a/test/fixtures.js b/test/fixtures.js index 4d2a670..a8a6729 100644 --- a/test/fixtures.js +++ b/test/fixtures.js @@ -11,7 +11,7 @@ exports.mockSocket = function(name, assert) { var incoming = dir + name + '.raw'; var outgoing = dir + 'out-' + new Date().getTime().toString(36) + '.raw'; return asSocket(incoming, outgoing - , function() { console.log('mockSocket(): reading ' + incoming); } + , function() { /* console.log('mockSocket(): reading ' + incoming); */ } , function() { fs.unlink(outgoing); } ); }