Better testing

This commit is contained in:
2015-04-06 23:42:10 +02:00
parent 86051e4cd7
commit 470e8dbd6f
5 changed files with 531 additions and 114 deletions
+6
View File
@@ -0,0 +1,6 @@
'use strict';
var log = require('winston');
// Remove log output to the console
log.remove(log.transports.Console);
+148
View File
@@ -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');
});
});
});
+244
View File
@@ -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);
});
});
});
+14 -114
View File
@@ -1,21 +1,7 @@
'use strict'; 'use strict';
var larvitsmpp = require('../larvitsmpp'), var larvitsmpp = require('../larvitsmpp'),
assert = require('assert'), 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);
}
}
describe('PDU convertion', function() { describe('PDU convertion', function() {
@@ -136,111 +122,25 @@ describe('PDU convertion', function() {
done(); done();
}); });
}); });
});
}); it('should create a submit_sm PDU with UCS2 encoding', function(done) {
larvitsmpp.objToPdu({
describe('Sessions', function() { 'cmdName': 'submit_sm',
'cmdStatus': 'ESME_ROK',
it('should setup a basic server and client and then directly unbinding again', function(done) { 'seqNr': 12,
portfinder.getPort(function(err, freePort) { 'params': {
assert( ! err, 'Error should be negative'); 'source_addr': '46701113311',
'destination_addr': '46709771337',
larvitsmpp.server({ 'short_message': 'Hello«»world'
'port': freePort }
}, function(err, serverSession) { }, function(err, pdu) {
assert( ! err, 'Error should be negative'); assert( ! err, 'Error should be negative');
serverSession.on('close', function() { assert(pdu.toString('hex') === '0000004f00000004000000000000000c00000034363730313131333331310000003436373039373731333337000000000000000008001800480065006c006c006f00ab00bb0077006f0072006c0064');
// 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(); 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) {
});*/
});
+119
View File
@@ -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) {
});*/
});