From 18c9bcfcf4bf962594f38598248e25278a8c12f8 Mon Sep 17 00:00:00 2001 From: quim Date: Sun, 26 Apr 2015 22:28:51 +0100 Subject: [PATCH] add lab/code for testing --- .gitignore | 2 +- index.js | 13 ++-- lib/client.js | 3 +- lib/error.js | 19 ++++++ lib/server.js | 165 ++++++++++++++++++++++++------------------------- lib/util.js | 6 +- package.json | 21 ++++--- test/index.js | 67 ++++++++++---------- test/server.js | 69 +++++++++++++++++++++ 9 files changed, 228 insertions(+), 137 deletions(-) create mode 100644 lib/error.js create mode 100644 test/server.js diff --git a/.gitignore b/.gitignore index 9962140..025d874 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -node_modules/* +node_modules test.* *.csv .DS_Store diff --git a/index.js b/index.js index ab0f6bc..f7b58aa 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,10 @@ 'use strict' module.exports = { - server : require('./lib/server'), - client : require('./lib/client'), - udpClient : require('./lib/udp_client'), - udpServer : require('./lib/udp_server'), - portscan : require('./lib/portscan') -}; + server : require('./lib/server'), + client : require('./lib/client'), + udpClient : require('./lib/udp_client'), + udpServer : require('./lib/udp_server'), + portscan : require('./lib/portscan'), + NetcatError : require('./lib/error') +} diff --git a/lib/client.js b/lib/client.js index ceb8e7b..e8e4632 100644 --- a/lib/client.js +++ b/lib/client.js @@ -21,7 +21,7 @@ inherits(Client, EventEmitter); Client.init = function(port, host, options) { if (!parseInt(port)) { - throw new Error('in node-nc server the port is mandatory!'); + throw new Error('in node-nc server the port is mandatory!'); } // check args @@ -120,5 +120,4 @@ Client.prototype.send = function(msg, end, cb) { Client.prototype.close = function() { this._client.end(); - return this; }; diff --git a/lib/error.js b/lib/error.js new file mode 100644 index 0000000..1fc6af9 --- /dev/null +++ b/lib/error.js @@ -0,0 +1,19 @@ +'use strict' + +var util = require('util') + +module.exports = NetcatError + +function NetcatError(error) { + Error.call(this) + Error.captureStackTrace(this, this.constructor) + + if (error instanceof Error) { + error = util.format('%s: %s', error.name, error.message) + } + + this.name = this.constructor.name + this.message = error +} + +util.inherits(NetcatError, Error) diff --git a/lib/server.js b/lib/server.js index 8028ac4..1d3b64a 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,141 +1,140 @@ -'use strict'; +'use strict' -var net = require('net'); -var inherits = require('util').inherits; -var EventEmitter = require('events').EventEmitter; -var _ = require('underscore'); -var ut = require('./util'); +var net = require('net') +var inherits = require('util').inherits +var EE = require('events').EventEmitter +var _ = require('underscore') +var isJsObject = require('is-js-object') +var ut = require('./util') +var NetcatError = require('./error') -module.exports = Server; +module.exports = Server -function Server(port, host, options) { +function Server (port, host, options) { if (!(this instanceof Server)) { - return new Server(port, host, options); + return new Server(port, host, options) } - EventEmitter.call(this); - Server.init.call(this, port, host, options); + EE.call(this) + Server.init.call(this, port, host, options) } -inherits(Server, EventEmitter); +inherits(Server, EE) -Server.init = function(port, host, options) { - var self = this; +Server.init = function init (port, host, options) { + var self = this if (!parseInt(port)) { - throw new Error('in node-nc server the port is mandatory!'); + throw new NetcatError('the port is required') } // check args - if (_.isObject(host)) { - options = host; - host = 'localhost'; + if (isJsObject(host)) { + options = host + host = 'localhost' } - this._clients = {}; - this._port = port; - this._host = host || 'localhost'; - this.timeout = (options && options.timeout) || 3600000; + this._clients = {} + this._port = port + this._host = host || 'localhost' + this._timeout = (options && options.timeout) || 3600000 - function handler(socket) { - var client = socket.remoteAddress + ':' + socket.remotePort; + function handler (socket) { + var client = socket.remoteAddress + ':' + socket.remotePort // socket configurations - socket.setKeepAlive(true, 60000); - socket.setTimeout(this._timeout); + socket.setKeepAlive(true, 60000) + socket.setTimeout(this._timeout) if (options && options.readEncoding) { - socket.setEncoding(options.readEncoding); + socket.setEncoding(options.readEncoding) } // // client events handlers // - function data(chunk) { - self.emit('data', client, chunk); + function data (chunk) { + self.emit('data', client, chunk) } - function timeout() { - socket.destroy(); + function timeout () { + socket.destroy() } - function error(err, cl, chunk) { - self.emit('client_error', err, cl, chunk); + function error (err, cl, chunk) { + self.emit('client_error', err, cl, chunk) } - function close() { - delete self._clients[client]; - self.emit('client_off', client); + function close () { + self.emit('client_off', client) + delete self._clients[client] } // - // register events + // register the client events // - process.nextTick(function register() { - socket.on('data', data); - socket.on('timeout', timeout); - socket.on('error', error); - socket.on('close', close); - }); + socket.on('data', data) + socket.on('timeout', timeout) + socket.on('error', error) + socket.on('close', close) } - this._server = net.createServer(handler); + this._server = net.createServer(handler) // // server events handlers // - function listening() { - self.emit('ready'); + function listening () { + self.emit('ready') } - function error(err) { - self.emit('error', err); + function error (err) { + self.emit('error', err) } - function close() { - self.emit('close'); + function close () { + self.emit('close') } - function conn(socket) { - self._clients[socket.remoteAddress + ':' + socket.remotePort] = socket; - self.emit('client_on', socket.remoteAddress + ':' + socket.remotePort); + function conn (socket) { + self._clients[socket.remoteAddress + ':' + socket.remotePort] = socket + self.emit('client_on', socket.remoteAddress + ':' + socket.remotePort) } // - // register events + // register the server events // - process.nextTick(function register() { - this._server.on('listening', listening); - this._server.on('connection', conn); - this._server.on('close', close); - this._server.on('error', error); - }, this); -}; + this._server.on('listening', listening) + this._server.on('connection', conn) + this._server.on('close', close) + this._server.on('error', error) +} -Server.prototype.listen = function() { - this._server.listen(this._port, this._host); -}; +Server.prototype.listen = function listen () { + this._server.listen(this._port, this._host) +} -Server.prototype.close = function(cb) { - cb = cb || ut.noop; - this._server.close(cb); -}; +Server.prototype.close = function close (cb) { + cb = cb || ut.noop + this._server.close(cb) +} -Server.prototype.send = function(client, msg, end, cb) { +Server.prototype.send = function send (client, msg, end, cb) { if (typeof end === 'function') { - cb = end; - end = false; + cb = end + end = false + } else { + cb = cb || ut.noop } - cb = cb || ut.noop; - msg = msg || new Buffer([0x00]); + msg = msg || new Buffer([0x00]) if (!Buffer.isBuffer(msg)) { - msg = new Buffer(msg.toString()); + msg = new Buffer(msg.toString()) } // check client exists @@ -143,20 +142,20 @@ Server.prototype.send = function(client, msg, end, cb) { // client exists // send and close connection if (end) { - this._clients[client].end(msg); - cb(); + this._clients[client].end(msg) + cb() } else { - this._clients[client].write(msg, cb); + this._clients[client].write(msg, cb) } } else { this._clients[client].emit('error', new Error(), client, - msg); - cb(); + msg) + cb() } -}; +} -Server.prototype.getClients = function() { - return _.keys(this._clients); -}; +Server.prototype.getClients = function getClients () { + return _.keys(this._clients) +} diff --git a/lib/util.js b/lib/util.js index 0949c04..ffdd1d3 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,9 +1,9 @@ -'use strict'; +'use strict' module.exports = { noop: noop -}; +} function noop() { - return function() {}; + return function() {} } diff --git a/package.json b/package.json index 0c9502a..2c73da9 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "node-netcat", - "version": "1.5.8", + "version": "2.0.0-1", "description": "nc/netcat - arbitrary TCP and UDP connections and listens in node.js", "main": "index.js", "scripts": { - "test": "istanbul cover tape test/*.js", + "test": "lab -v -c", "jshint": "jshint -c .jshintrc lib/*.js test/*.js", "code-style": "jscs -p google lib/*.js test/*.js", - "check-coverage": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100", - "open-coverage": "open coverage/lcov-report/index.html" + "check-coverage": "lab -t 100 -r html -o coverage.html", + "open-coverage": "open coverage.html" }, "files": [ "LICENSE", @@ -30,13 +30,16 @@ "author": "@joaquimserafim", "license": "ISC", "devDependencies": { - "istanbul": "^0.3.5", - "jscs": "^1.10.0", - "jshint": "^2.6.0", - "pre-commit": "^1.0.1", - "tape": "^3.5.0" + "code": "^1.4.0", + "istanbul": "^0.3.13", + "jscs": "^1.12.0", + "jshint": "^2.7.0", + "lab": "^5.7.0", + "pre-commit": "^1.0.6", + "tape": "^4.0.0" }, "dependencies": { + "is-js-object": "^1.0.0", "lasync": "^1.1.0", "underscore": "^1.7.0" }, diff --git a/test/index.js b/test/index.js index 58d8e87..d656991 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,5 @@ 'use strict'; +return; var test = require('tape'); var Netcat = require('../'); @@ -16,22 +17,22 @@ test('nc server constructor - dont pass any param', function(assert) { } }); -test('nc server constructor - pass only port & options', function(assert) { +test('nc server constructor - pass only port & options', function(assert) { server = Netcat.server(4000, { readEncoding: 'utf8', timeout: 3000 }); - + assert.ok(server); server.listen(); - server.once('ready', function() { - assert.pass('server, ready'); + server.once('ready', function() { + assert.pass('server, ready'); server.close(function() { assert.pass('server closed'); assert.end(); }); - }); + }); }); test('tx to a client has disconnected', function(assert) { @@ -44,19 +45,19 @@ test('tx to a client has disconnected', function(assert) { server.listen(); - server.once('ready', function() { + server.once('ready', function() { assert.pass('server, ready'); client.start(); }); - server.on('client_on', function(client) { + server.on('client_on', function(client) { assert.ok(client, 'server, client connect ' + client); clients = server.getClients(); assert.equal(clients.length, 1); - assert.equal(clients[0], client); + assert.equal(clients[0], client); }); - server.on('client_off', function(client) { + server.on('client_off', function(client) { assert.ok(client, 'server, client disconnet ' + client); }); @@ -65,7 +66,7 @@ test('tx to a client has disconnected', function(assert) { }); server.once('error', assert.fail); - + server.once('close', function() { assert.pass('server, closed'); @@ -76,8 +77,8 @@ test('tx to a client has disconnected', function(assert) { assert.pass('client, ready'); client.close(); server.send(client, 'Hello World!', function() { - - //server.close(); + + //server.close(); }); }); }); @@ -85,17 +86,17 @@ return; test('server & client using binary data', function(t) { t.plan(15); - + server = Netcat.server(4000); client = Netcat.client(4000); - server.once('ready', function() { + server.once('ready', function() { t.pass('server, ready'); client.start(); }); server.on('data', function(client, data) { - t.equal(data.length > 0, + t.equal(data.length > 0, true, 'server, receive data: "' + data + '" from client ' + client); @@ -131,12 +132,12 @@ test('server & client using binary data', function(t) { setTimeout(function() { server.close(); }, 1000); }); - server.on('client_on', function(client) { - t.ok(client, 'server, client connect ' + client); + server.on('client_on', function(client) { + t.ok(client, 'server, client connect ' + client); }); - server.on('client_off', function(client) { - t.ok(client, 'server, client disconnet ' + client); + server.on('client_off', function(client) { + t.ok(client, 'server, client disconnet ' + client); }); server.once('error', function(err) { t.error(err !== null, err); }); @@ -144,8 +145,8 @@ test('server & client using binary data', function(t) { server.listen(); // client - client.once('open', function() { - t.pass('client, connected'); + client.once('open', function() { + t.pass('client, connected'); setTimeout(function () { client.send('Hello World', function() { t.pass('client, send message'); @@ -168,17 +169,17 @@ test('server & client using binary data', function(t) { test('server & client using utf8 encoding', function(t) { t.plan(15); - + server = Netcat.server(4000, {readEncoding: 'utf8'}); client = Netcat.client(4000, {readEncoding: 'utf8'}); - server.once('ready', function() { + server.once('ready', function() { t.pass('server, ready'); client.start(); }); server.on('data', function(client, data) { - t.equal(data.length > 0, + t.equal(data.length > 0, true, 'server, receive data: "' + data + '" from client ' + client); @@ -195,7 +196,7 @@ test('server & client using utf8 encoding', function(t) { server.send(clients[client], data); t.pass('server, send "' + data + '" to client ' + clients[client]); }); - + // first send some messages without closing the conn Object.keys(clients).forEach(function(client) { server.send(clients[client], data, function() { @@ -213,12 +214,12 @@ test('server & client using utf8 encoding', function(t) { setTimeout(function() { server.close(); }, 1000); }); - server.on('client_on', function(client) { - t.ok(client, 'server, client connect ' + client); + server.on('client_on', function(client) { + t.ok(client, 'server, client connect ' + client); }); - server.on('client_off', function(client) { - t.ok(client, 'server, client disconnet ' + client); + server.on('client_off', function(client) { + t.ok(client, 'server, client disconnet ' + client); }); server.once('error', function(err) { t.error(err !== null, err); }); @@ -227,8 +228,8 @@ test('server & client using utf8 encoding', function(t) { // client - client.once('open', function() { - t.pass('client, connected'); + client.once('open', function() { + t.pass('client, connected'); setTimeout(function () { client.send('Hello World', function() { t.pass('client, send message'); @@ -259,7 +260,7 @@ test('portscan', function(t) { // will fail in port 81 if (err) return t.ok(err, err); - t.ok(res, res); + t.ok(res, res); }); }); @@ -284,7 +285,7 @@ test('upd', function(t) { setTimeout(function() { client.close(); }, 1000); }, 1000); }); - + server.once('error', function(err) { t.error(err !== null, err); }); server.once('close', function() { t.pass('server, closed'); }); server.bind(); diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000..c2fc68a --- /dev/null +++ b/test/server.js @@ -0,0 +1,69 @@ +'use strict' + +var Lab = require('lab') +var Code = require('code') + +var lab = module.exports.lab = Lab.script() + +var describe = lab.describe +var it = lab.it +var expect = Code.expect + +var Server = require('../').server +var NetcatError = require('../').NetcatError + +describe('Server', function() { + + describe('interface', function() { + it('miss port arg should throw', function(done) { + var throws = function throws () { + var s = new Server() + } + + expect(throws).to.throw(NetcatError, 'the port is required') + done() + }) + + it('pass the port should init the Server', function(done) { + var s = new Server(3000) + expect(s).to.be.an.instanceof(Server) + done() + }) + + it('not using the "new" to init the Server', function(done) { + var s = Server(3000) + expect(s).to.be.an.instanceof(Server) + done() + }) + + it('passing port and host', function(done) { + var s = Server(3000, 'localhost') + expect(s).to.be.an.instanceof(Server) + done() + }) + + it('passing port and an object for the options', function(done) { + var s = new Server(3000, {timeout: 60000, readEncoding: 'asccii'}) + expect(s).to.be.an.instanceof(Server) + done() + }) + }) + + describe('test server initiation', function() { + var server + + it('init', function(done) { + server = Server(3000) + server.on('ready', done) + server.listen() + }) + + it('client on', function(done) { + + server.on('client_on', done) + }) + + + }) + +}) \ No newline at end of file