Initial work on the server

This commit is contained in:
Abhishek Das
2013-03-25 05:57:47 +05:30
parent 93400554fd
commit 0485272d93
626 changed files with 121311 additions and 1 deletions
+125
View File
@@ -0,0 +1,125 @@
var common = require('../../common');
var test = require('utest');
var assert = require('assert');
var Parser = require(common.lib + '/protocol/Parser');
function packet(bytes) {
var buffer = new Buffer(bytes);
var parser = new Parser();
parser.append(buffer);
return parser;
}
test('Parser', {
"parseBuffer: buffer won\'t change after appending another one": function() {
var startBuffer = new Buffer(5);
startBuffer.fill('a');
var parser = new Parser();
parser.append(startBuffer);
var value = parser.parseBuffer(4);
assert.equal(value.toString(), 'aaaa');
parser.append(new Buffer('b'));
assert.equal(value.toString(), 'aaaa');
},
'parseUnsignedNumber: 1 byte': function() {
var value = packet([5]).parseUnsignedNumber(1);
assert.equal(value, 5);
},
'parseUnsignedNumber: 2 bytes': function() {
var value = packet([1, 1]).parseUnsignedNumber(2);
assert.equal(value, 256 + 1);
},
'parseUnsignedNumber: honors offsets': function() {
var parser = packet([1, 2]);
assert.equal(parser.parseUnsignedNumber(1), 1);
assert.equal(parser.parseUnsignedNumber(1), 2);
},
'parseLengthCodedNumber: 1 byte': function() {
var parser = packet([250]);
assert.strictEqual(parser.parseLengthCodedNumber(), 250);
},
'parseLengthCodedNumber: 251 = null': function() {
var parser = packet([251]);
assert.strictEqual(parser.parseLengthCodedNumber(), null);
},
'parseLengthCodedNumber: 252 = 16 bit': function() {
var parser = packet([252, 2, 1]);
var expected =
2 * Math.pow(256, 0) +
1 * Math.pow(256, 1);
assert.strictEqual(parser.parseLengthCodedNumber(), expected);
},
'parseLengthCodedNumber: 253 = 24 bit': function() {
var parser = packet([253, 3, 2, 1]);
var expected =
3 * Math.pow(256, 0) +
2 * Math.pow(256, 1) +
1 * Math.pow(256, 2);
assert.strictEqual(parser.parseLengthCodedNumber(), expected);
},
'parseLengthCodedNumber: 254 = 64 bit': function() {
var parser = packet([254, 8, 7, 6, 5, 4, 3, 2, 0]);
var expected =
8 * Math.pow(256, 0) +
7 * Math.pow(256, 1) +
6 * Math.pow(256, 2) +
5 * Math.pow(256, 3) +
4 * Math.pow(256, 4) +
3 * Math.pow(256, 5) +
2 * Math.pow(256, 6) +
0 * Math.pow(256, 7);
assert.strictEqual(parser.parseLengthCodedNumber(), expected);
},
'parseLengthCodedNumber: < 53 bit = no problemo': function() {
var parser = packet([254, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00]);
assert.strictEqual(parser.parseLengthCodedNumber(), Math.pow(2, 53) - 1);
},
'parseLengthCodedNumber: 53 bit = Error': function() {
var parser = packet([254, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00]);
assert.throws(function() {
parser.parseLengthCodedNumber();
}, /precision/i);
},
'parseLengthCodedNumber: 255 = Error': function() {
var parser = packet([255]);
assert.throws(function() {
parser.parseLengthCodedNumber();
}, /unexpected/i);
},
'parsePacketTerminatedString: regular case': function() {
var parser = packet([0x48, 0x69]);
parser._packetEnd = 2;
var str = parser.parsePacketTerminatedString();
assert.equal(str, 'Hi');
},
'parsePacketTerminatedString: 0x00 terminated': function() {
var parser = packet([0x48, 0x69, 0x00]);
parser._packetEnd = 2;
var str = parser.parsePacketTerminatedString();
assert.equal(str, 'Hi');
},
});
+132
View File
@@ -0,0 +1,132 @@
var common = require('../../common');
var test = require('utest');
var assert = require('assert');
var SqlString = require(common.lib + '/protocol/SqlString');
test('SqlString.escape', {
'undefined -> NULL': function() {
assert.equal(SqlString.escape(undefined), 'NULL');
},
'null -> NULL': function() {
assert.equal(SqlString.escape(null), 'NULL');
},
'booleans convert to strings': function() {
assert.equal(SqlString.escape(false), 'false');
assert.equal(SqlString.escape(true), 'true');
},
'numbers convert to strings': function() {
assert.equal(SqlString.escape(5), '5');
},
'objects are turned into key value pairs': function() {
assert.equal(SqlString.escape({a: 'b', c: 'd'}), "`a` = 'b', `c` = 'd'");
},
'objects function properties are ignored': function() {
assert.equal(SqlString.escape({a: 'b', c: function() {}}), "`a` = 'b'");
},
'nested objects are cast to strings': function() {
assert.equal(SqlString.escape({a: {nested: true}}), "`a` = '[object Object]'");
},
'arrays are turned into lists': function() {
assert.equal(SqlString.escape([1, 2, 'c']), "1, 2, 'c'");
},
'nested arrays are turned into grouped lists': function() {
assert.equal(SqlString.escape([[1,2,3], [4,5,6], ['a', 'b', {nested: true}]]), "(1, 2, 3), (4, 5, 6), ('a', 'b', '[object Object]')");
},
'nested objects inside arrays are cast to strings': function() {
assert.equal(SqlString.escape([1, {nested: true}, 2]), "1, '[object Object]', 2");
},
'strings are quoted': function() {
assert.equal(SqlString.escape('Super'), "'Super'");
},
'\0 gets escaped': function() {
assert.equal(SqlString.escape('Sup\0er'), "'Sup\\0er'");
},
'\b gets escaped': function() {
assert.equal(SqlString.escape('Sup\ber'), "'Sup\\ber'");
},
'\n gets escaped': function() {
assert.equal(SqlString.escape('Sup\ner'), "'Sup\\ner'");
},
'\r gets escaped': function() {
assert.equal(SqlString.escape('Sup\rer'), "'Sup\\rer'");
},
'\t gets escaped': function() {
assert.equal(SqlString.escape('Sup\ter'), "'Sup\\ter'");
},
'\\ gets escaped': function() {
assert.equal(SqlString.escape('Sup\\er'), "'Sup\\\\er'");
},
'\u001a (ascii 26) gets replaced with \\Z': function() {
assert.equal(SqlString.escape('Sup\u001aer'), "'Sup\\Zer'");
},
'single quotes get escaped': function() {
assert.equal(SqlString.escape('Sup\'er'), "'Sup\\'er'");
},
'double quotes get escaped': function() {
assert.equal(SqlString.escape('Sup"er'), "'Sup\\\"er'");
},
'dates are converted to YYYY-MM-DD HH:II:SS': function() {
var expected = '2012-05-07 11:42:03';
var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3));
var string = SqlString.escape(date);
assert.strictEqual(string, "'" + expected + "'");
},
'buffers are converted to hex': function() {
var buffer = new Buffer([0, 1, 254, 255]);
var string = SqlString.escape(buffer);
assert.strictEqual(string, "X'0001feff'");
},
'NaN -> NaN': function() {
assert.equal(SqlString.escape(NaN), 'NaN');
},
'Infinity -> Infinity': function() {
assert.equal(SqlString.escape(Infinity), 'Infinity');
}
});
test('SqlString.format', {
'question marks are replaced with escaped array values': function() {
var sql = SqlString.format('? and ?', ['a', 'b']);
assert.equal(sql, "'a' and 'b'");
},
'extra question marks are left untouched': function() {
var sql = SqlString.format('? and ?', ['a']);
assert.equal(sql, "'a' and ?");
},
'extra arguments are not used': function() {
var sql = SqlString.format('? and ?', ['a', 'b', 'c']);
assert.equal(sql, "'a' and 'b'");
},
'question marks within values do not cause issues': function() {
var sql = SqlString.format('? and ?', ['hello?', 'b']);
assert.equal(sql, "'hello?' and 'b'");
},
});