From 470e8dbd6fb2aeb27e90a20fa64e8a54705f999a Mon Sep 17 00:00:00 2001 From: lilleman Date: Mon, 6 Apr 2015 23:42:10 +0200 Subject: [PATCH] Better testing --- test/00_start.js | 6 + test/01_encodings.js | 148 ++++++++++++++++++++++ test/02_types.js | 244 ++++++++++++++++++++++++++++++++++++ test/{test.js => 03_pdu.js} | 128 +++---------------- test/04_session.js | 119 ++++++++++++++++++ 5 files changed, 531 insertions(+), 114 deletions(-) create mode 100644 test/00_start.js create mode 100644 test/01_encodings.js create mode 100644 test/02_types.js rename test/{test.js => 03_pdu.js} (63%) create mode 100644 test/04_session.js diff --git a/test/00_start.js b/test/00_start.js new file mode 100644 index 0000000..de6435c --- /dev/null +++ b/test/00_start.js @@ -0,0 +1,6 @@ +'use strict'; + +var log = require('winston'); + +// Remove log output to the console +log.remove(log.transports.Console); \ No newline at end of file diff --git a/test/01_encodings.js b/test/01_encodings.js new file mode 100644 index 0000000..3c51c62 --- /dev/null +++ b/test/01_encodings.js @@ -0,0 +1,148 @@ +'use strict'; + +var assert = require('assert'), + encodings = require('../defs').encodings; + +describe('encodings', function() { + describe('ASCII', function() { + var ASCII = encodings.ASCII, + samples; + + samples = { + '@£$¥': [0, 1, 2, 3], + ' 1a=': [0x20, 0x31, 0x61, 0x3D], + '~^€': [0x1B, 0x3D, 0x1B, 0x14, 0x1B, 0x65] + }; + + describe('#match()', function() { + it('should return true for strings that can be encoded using GSM 03.38 ASCII charset', function() { + assert(ASCII.match('')); + assert(ASCII.match('@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1BÆæßÉ !"#¤%&\'')); + assert(ASCII.match('()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZ')); + assert(ASCII.match('ÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà')); + assert(ASCII.match('\f^{}\\[~]|€')); + }); + + it('should return false for strings that can not be encoded using GSM 03.38 ASCII charset', function() { + assert( ! ASCII.match('`')); + assert( ! ASCII.match('ÁáçÚUÓO')); + assert( ! ASCII.match('تست')); + }); + }); + + describe('#encode', function() { + it('should properly encode the given string using GSM 03.38 ASCII charset', function() { + var str; + + for (str in samples) { + assert.deepEqual(ASCII.encode(str), new Buffer(samples[str])); + } + }); + }); + + describe('#decode', function() { + it('should properly decode the given buffer using GSM 03.38 ASCII charset', function() { + var str; + + for (str in samples) { + assert.deepEqual(ASCII.decode(samples[str]), str); + } + }); + }); + }); + + describe('LATIN1', function() { + var LATIN1 = encodings.LATIN1, + samples; + + samples = { + '@$`Á': [0x40, 0x24, 0x60, 0xC1], + 'áçÚ': [0xE1, 0xE7, 0xDA], + 'UÓO': [0x55, 0xD3, 0x4F] + }; + + describe('#match()', function() { + it('should return false for strings that can be encoded using LATIN1 charset', function() { + assert( ! LATIN1.match('`ÁáçÚUÓO')); + }); + + it('should return false for strings that can not be encoded using LATIN1 charset', function() { + assert( ! LATIN1.match('تست')); + assert( ! LATIN1.match('۱۲۳۴۵۶۷۸۹۰')); + assert( ! LATIN1.match('ʹʺʻʼʽ`')); + }); + }); + + describe('#encode', function() { + it('should properly encode the given string using LATIN1 charset', function() { + var str; + + for (str in samples) { + assert.deepEqual(LATIN1.encode(str), new Buffer(samples[str])); + } + }); + }); + + describe('#decode', function() { + it('should properly decode the given buffer using LATIN1 charset', function() { + var str; + + for (str in samples) { + assert.deepEqual(LATIN1.decode(samples[str]), str); + } + }); + }); + }); + + describe('UCS2', function() { + var UCS2 = encodings.UCS2, + samples; + + samples = { + ' 1a': [0x00, 0x20, 0x00, 0x31, 0x00, 0x61], + '۱۲۳': [0x06, 0xF1, 0x06, 0xF2, 0x06, 0xF3] + }; + + describe('#match()', function() { + it('should always return true', function() { + assert(UCS2.match('')); + assert(UCS2.match('`ÁáçÚUÓO')); + assert(UCS2.match('تست')); + assert(UCS2.match('۱۲۳۴۵۶۷۸۹۰')); + assert(UCS2.match('ʹʺʻʼʽ`')); + }); + }); + + describe('#encode', function() { + it('should properly encode the given string using UCS2 charset', function() { + var str; + + for (str in samples) { + assert.deepEqual(UCS2.encode(str), new Buffer(samples[str])); + } + }); + }); + + describe('#decode', function() { + it('should properly decode the given buffer using UCS2 charset', function() { + var str; + + for (str in samples) { + assert.deepEqual(UCS2.decode(samples[str]), str); + } + }); + }); + }); + + describe('#detect()', function() { + it('should return proper encoding for the given string', function() { + assert.equal(encodings.detect(''), 'ASCII'); + assert.equal(encodings.detect('ÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà(){}[]'), 'ASCII'); + assert.equal(encodings.detect('`ÁáçÚUÓO'), 'UCS2'); + assert.equal(encodings.detect('«©®µ¶±»'), 'UCS2'); + assert.equal(encodings.detect('ʹʺʻʼʽ`'), 'UCS2'); + assert.equal(encodings.detect('تست'), 'UCS2'); + assert.equal(encodings.detect('۱۲۳۴۵۶۷۸۹۰'), 'UCS2'); + }); + }); +}); diff --git a/test/02_types.js b/test/02_types.js new file mode 100644 index 0000000..44f13f7 --- /dev/null +++ b/test/02_types.js @@ -0,0 +1,244 @@ +'use strict'; + +var assert = require('assert'), + types = require('../defs').types; + +describe('int8', function() { + var b = new Buffer([0, 0x65]), expected = 0x65; + + describe('#read()', function() { + it('should read one byte as integer', function() { + var result = types.int8.read(b, 1); + assert.equal(result, expected); + }); + }); + + describe('#size()', function() { + it('should return 1', function() { + assert.equal(types.int8.size(expected), 1); + }); + }); + + describe('#write()', function() { + it('should write one byte to the buffer', function() { + types.int8.write(expected, b, 0); + assert.deepEqual(new Buffer([0x65]), b.slice(0, 1)); + }); + }); +}); + +describe('int16', function() { + var b = new Buffer([0, 0x05, 0x65]), expected = 0x0565; + + describe('#read()', function() { + it('should read 2 bytes as integer', function() { + var result = types.int16.read(b, 1); + assert.equal(result, expected); + }); + }); + + describe('#size()', function() { + it('should return 2', function() { + assert.equal(types.int16.size(expected), 2); + }); + }); + + describe('#write()', function() { + it('should write 2 bytes to the buffer', function() { + types.int16.write(expected, b, 0); + assert.deepEqual(new Buffer([0x05, 0x65]), b.slice(0, 2)); + }); + }); +}); + +describe('int32', function() { + var b = new Buffer([0, 0x10, 0x02, 0x40, 0x45]), expected = 0x10024045; + + describe('#read()', function() { + it('should read 4 bytes as integer', function() { + var result = types.int32.read(b, 1); + assert.equal(result, expected); + }); + }); + + describe('#size()', function() { + it('should return 4', function() { + assert.equal(types.int32.size(expected), 4); + }); + }); + + describe('#write()', function() { + it('should write 4 bytes to the buffer', function() { + types.int32.write(expected, b, 0); + assert.deepEqual(new Buffer([0x10, 0x02, 0x40, 0x45]), b.slice(0, 4)); + }); + }); +}); + +describe('string', function() { + var b = new Buffer(9), expected = 'abcd1234'; + b[0] = 8; + b.write(expected, 1); + + describe('#read()', function() { + it('should read an Octet String from the buffer', function() { + var result = types.string.read(b, 0); + assert.equal(result, expected); + }); + }); + + describe('#size()', function() { + it('should return the length of an Octet String from its first byte', function() { + assert.equal(9, types.string.size(expected)); + }); + }); + + describe('#write()', function() { + it('should write an Octet String to the buffer', function() { + var b2 = new Buffer(9); + types.string.write(expected, b2, 0); + assert.deepEqual(b, b2); + }); + }); +}); + +describe('cstring', function() { + var b = new Buffer(9), expected = 'abcd1234'; + b[8] = 0; + b.write(expected, 0); + + describe('#read()', function() { + it('should read a C-Octet String (null-terminated string) from the buffer', function() { + var result = types.cstring.read(b, 0); + assert.equal(result, expected); + }); + }); + + describe('#size()', function() { + it('should return the length of a C-Octet String (null-terminated string)', function() { + assert.equal(9, types.cstring.size(expected)); + }); + }); + + describe('#write()', function() { + it('should write a C-Octet String (null-terminated string) to the buffer', function() { + var b2 = new Buffer(9); + types.cstring.write(expected, b2, 0); + assert.deepEqual(b, b2); + }); + }); +}); + +describe('buffer', function() { + var b = new Buffer(8), + expected = new Buffer('abcd1234'); + + b.write(expected.toString()); + + describe('#read()', function() { + it('should read a binary field from the buffer', function() { + var result = types.buffer.read(b, 0, b.length); + assert.deepEqual(result, expected); + }); + }); + + describe('#size()', function() { + it('should return the size of a binary field in bytes', function() { + assert.equal(8, types.buffer.size(expected)); + }); + }); + + describe('#write()', function() { + it('should write a binary field to the buffer', function() { + var b2 = new Buffer(8); + types.buffer.write(expected, b2, 0); + assert.deepEqual(b, b2); + }); + }); +}); + +describe('dest_address_array', function() { + var b, + expected; + + b = new Buffer([ + 0x02, 0x01, 0x01, 0x02, 0x31, 0x32, 0x33, 0x00, + 0x02, 0x61, 0x62, 0x63, 0x00 + ]); + + expected = [ + { + dest_addr_ton: 1, + dest_addr_npi: 2, + destination_addr: '123' + }, + { dl_name: 'abc' } + ]; + + describe('#read()', function() { + it('should read all dest_address structures from the buffer', function() { + var result = types.dest_address_array.read(b, 0); + assert.deepEqual(result, expected); + }); + }); + + describe('#size()', function() { + it('should return the size of all dest_address structures in bytes', function() { + assert.equal(types.dest_address_array.size(expected), 13); + }); + }); + + describe('#write()', function() { + it('should write an array of dest_address structures to the buffer', function() { + var b2 = new Buffer(13); + types.dest_address_array.write(expected, b2, 0); + assert.deepEqual(b, b2); + }); + }); +}); + +describe('unsuccess_sme_array', function() { + var b, + expected; + + b = new Buffer([ + 0x02, 0x03, 0x04, 0x61, 0x62, 0x63, 0x00, 0x00, 0x00, 0x00, 0x07, + 0x05, 0x06, 0x31, 0x32, 0x33, 0x00, 0x10, 0x00, 0x00, 0x08 + ]); + + expected = [ + { + dest_addr_ton: 3, + dest_addr_npi: 4, + destination_addr: 'abc', + error_status_code: 0x00000007 + }, + { + dest_addr_ton: 5, + dest_addr_npi: 6, + destination_addr: '123', + error_status_code: 0x10000008 + } + ]; + + describe('#read()', function() { + it('should read all unsuccess_sme structures from the buffer', function() { + var result = types.unsuccess_sme_array.read(b, 0); + assert.deepEqual(result, expected); + }); + }); + + describe('#size()', function() { + it('should return the size of all unsuccess_sme structures in bytes', function() { + assert.equal(types.unsuccess_sme_array.size(expected), 21); + }); + }); + + describe('#write()', function() { + it('should write an array of unsuccess_sme structures to the buffer', function() { + var b2 = new Buffer(21); + types.unsuccess_sme_array.write(expected, b2, 0); + assert.deepEqual(b, b2); + }); + }); +}); diff --git a/test/test.js b/test/03_pdu.js similarity index 63% rename from test/test.js rename to test/03_pdu.js index 3d2a187..fecf415 100644 --- a/test/test.js +++ b/test/03_pdu.js @@ -1,21 +1,7 @@ 'use strict'; var larvitsmpp = require('../larvitsmpp'), - assert = require('assert'), - portfinder = require('portfinder'), - log = require('winston'); - -// Remove log output to the console -log.remove(log.transports.Console); - -// Very advanced auth system -function checkuserpass(username, password, callback) { - if (username === 'foo' && password === 'bar') { - callback(null, true); - } else { - callback(null, false); - } -} + assert = require('assert'); describe('PDU convertion', function() { @@ -136,111 +122,25 @@ describe('PDU convertion', function() { done(); }); }); - }); -}); - -describe('Sessions', function() { - - it('should setup a basic server and client and then directly unbinding again', function(done) { - portfinder.getPort(function(err, freePort) { - assert( ! err, 'Error should be negative'); - - larvitsmpp.server({ - 'port': freePort - }, function(err, serverSession) { + it('should create a submit_sm PDU with UCS2 encoding', function(done) { + larvitsmpp.objToPdu({ + 'cmdName': 'submit_sm', + 'cmdStatus': 'ESME_ROK', + 'seqNr': 12, + 'params': { + 'source_addr': '46701113311', + 'destination_addr': '46709771337', + 'short_message': 'Hello«»world' + } + }, function(err, pdu) { assert( ! err, 'Error should be negative'); - serverSession.on('close', function() { - // Manually destroy the server socket - serverSession.sock.destroy(); - }); - }); - - larvitsmpp.client({ - 'port': freePort - }, function(err, clientSession) { - assert( ! err, 'Error should be negative'); - - // Gracefully close connection - clientSession.unbind(); + assert(pdu.toString('hex') === '0000004f00000004000000000000000c00000034363730313131333331310000003436373039373731333337000000000000000008001800480065006c006c006f00ab00bb0077006f0072006c0064'); done(); }); }); }); - it('should setup a server with auth and client trying to connect with wrong username and password', function(done) { - portfinder.getPort(function(err, freePort) { - assert( ! err, 'Error should be negative'); - - larvitsmpp.server({ - 'port': freePort, - 'checkuserpass': checkuserpass - }, function(err, serverSession) { - assert( ! err, 'Error should be negative'); - - serverSession.on('close', function() { - // Manually destroy the server socket - serverSession.sock.destroy(); - }); - }); - - larvitsmpp.client({ - 'port': freePort - }, function(err) { - assert(err, 'Error should be set since login should have failed'); - - done(); - }); - }); - }); - - it('should setup a server with auth and client trying to connect with correct username and password', function(done) { - portfinder.getPort(function(err, freePort) { - assert( ! err, 'Error should be negative'); - - larvitsmpp.server({ - 'port': freePort, - 'checkuserpass': checkuserpass - }, function(err, serverSession) { - assert( ! err, 'Error should be negative'); - - serverSession.on('close', function() { - // Manually destroy the server socket - serverSession.sock.destroy(); - }); - }); - - larvitsmpp.client({ - 'port': freePort, - 'username': 'foo', - 'password': 'bar' - }, function(err, clientSession) { - assert( ! err, 'Error should be negative'); - - // Gracefully close connection - clientSession.unbind(); - - done(); - }); - }); - }); - - /* - it('should try to send submit_sm while not logged in and get a failure return PDU back', function(done) { - - }); - - it('should send a bind_transceiver to a new session and get logged in', function(done) { - - }); - - it('should send a bind_transceiver to a new session with a login-method and fail due to wrong username and password', function(done) { - - }); - - it('should send a bind_transceiver to a new session with a login-method and succeed with correct username and password', function(done) { - - });*/ -}); +}); \ No newline at end of file diff --git a/test/04_session.js b/test/04_session.js new file mode 100644 index 0000000..4ba904c --- /dev/null +++ b/test/04_session.js @@ -0,0 +1,119 @@ +'use strict'; + +var larvitsmpp = require('../larvitsmpp'), + assert = require('assert'), + portfinder = require('portfinder'); + +// Very advanced auth system +function checkuserpass(username, password, callback) { + if (username === 'foo' && password === 'bar') { + callback(null, true); + } else { + callback(null, false); + } +} + +describe('Sessions', function() { + + it('should setup a basic server and client and then directly unbinding again', function(done) { + portfinder.getPort(function(err, freePort) { + assert( ! err, 'Error should be negative'); + + larvitsmpp.server({ + 'port': freePort + }, function(err, serverSession) { + assert( ! err, 'Error should be negative'); + + serverSession.on('close', function() { + // Manually destroy the server socket + serverSession.sock.destroy(); + }); + }); + + larvitsmpp.client({ + 'port': freePort + }, function(err, clientSession) { + assert( ! err, 'Error should be negative'); + + // Gracefully close connection + clientSession.unbind(); + + done(); + }); + }); + }); + + it('should setup a server with auth and client trying to connect with wrong username and password', function(done) { + portfinder.getPort(function(err, freePort) { + assert( ! err, 'Error should be negative'); + + larvitsmpp.server({ + 'port': freePort, + 'checkuserpass': checkuserpass + }, function(err, serverSession) { + assert( ! err, 'Error should be negative'); + + serverSession.on('close', function() { + // Manually destroy the server socket + serverSession.sock.destroy(); + }); + }); + + larvitsmpp.client({ + 'port': freePort + }, function(err) { + assert(err, 'Error should be set since login should have failed'); + + done(); + }); + }); + }); + + it('should setup a server with auth and client trying to connect with correct username and password', function(done) { + portfinder.getPort(function(err, freePort) { + assert( ! err, 'Error should be negative'); + + larvitsmpp.server({ + 'port': freePort, + 'checkuserpass': checkuserpass + }, function(err, serverSession) { + assert( ! err, 'Error should be negative'); + + serverSession.on('close', function() { + // Manually destroy the server socket + serverSession.sock.destroy(); + }); + }); + + larvitsmpp.client({ + 'port': freePort, + 'username': 'foo', + 'password': 'bar' + }, function(err, clientSession) { + assert( ! err, 'Error should be negative'); + + // Gracefully close connection + clientSession.unbind(); + + done(); + }); + }); + }); + + /* + it('should try to send submit_sm while not logged in and get a failure return PDU back', function(done) { + + }); + + it('should send a bind_transceiver to a new session and get logged in', function(done) { + + }); + + it('should send a bind_transceiver to a new session with a login-method and fail due to wrong username and password', function(done) { + + }); + + it('should send a bind_transceiver to a new session with a login-method and succeed with correct username and password', function(done) { + + });*/ +}); \ No newline at end of file