Skip to content

Commit 4e90574

Browse files
committed
tests: improve flexibility of auth unit tests
1 parent af0120f commit 4e90574

File tree

6 files changed

+73
-58
lines changed

6 files changed

+73
-58
lines changed

test/FakeServer.js

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var Packets = common.Packets;
1212
var PacketWriter = common.PacketWriter;
1313
var Parser = common.Parser;
1414
var Types = common.Types;
15-
var Auth = require(common.lib + '/protocol/Auth');
1615
var Errors = common.Errors;
1716
var EventEmitter = require('events').EventEmitter;
1817
var Util = require('util');
@@ -75,14 +74,14 @@ function FakeConnection(socket) {
7574
FakeConnection.prototype.handshake = function(options) {
7675
this._handshakeOptions = options || {};
7776

78-
var packetOpiotns = common.extend({
77+
var packetOptions = common.extend({
7978
scrambleBuff1 : Buffer.from('1020304050607080', 'hex'),
8079
scrambleBuff2 : Buffer.from('0102030405060708090A0B0C', 'hex'),
8180
serverCapabilities1 : 512, // only 1 flag, PROTOCOL_41
8281
protocol41 : true
8382
}, this._handshakeOptions);
8483

85-
this._handshakeInitializationPacket = new Packets.HandshakeInitializationPacket(packetOpiotns);
84+
this._handshakeInitializationPacket = new Packets.HandshakeInitializationPacket(packetOptions);
8685

8786
this._sendPacket(this._handshakeInitializationPacket);
8887
};
@@ -285,31 +284,14 @@ FakeConnection.prototype._parsePacket = function() {
285284
this.database = (packet.database || null);
286285
this.user = (packet.user || null);
287286

288-
if (this._handshakeOptions.oldPassword) {
289-
this._sendPacket(new Packets.UseOldPasswordPacket());
290-
} else if (this._handshakeOptions.authMethodName) {
291-
this._sendPacket(new Packets.AuthSwitchRequestPacket(this._handshakeOptions));
292-
} else if (this._handshakeOptions.password === 'passwd') {
293-
var expected = Buffer.from('3DA0ADA7C9E1BB3A110575DF53306F9D2DE7FD09', 'hex');
294-
this._sendAuthResponse(packet.scrambleBuff, expected);
295-
} else if (this._handshakeOptions.user || this._handshakeOptions.password) {
296-
throw new Error('not implemented');
297-
} else {
287+
if (!this.emit('clientAuthentication', packet)) {
298288
this._sendPacket(new Packets.OkPacket());
299289
this._parser.resetPacketNumber();
300290
}
301291
break;
302292
case Packets.SSLRequestPacket:
303293
this._startTLS();
304294
break;
305-
case Packets.OldPasswordPacket:
306-
var expected = Auth.scramble323(this._handshakeInitializationPacket.scrambleBuff(), this._handshakeOptions.password);
307-
this._sendAuthResponse(packet.scrambleBuff, expected);
308-
break;
309-
case Packets.AuthSwitchResponsePacket:
310-
var expected = Auth.token(this._handshakeOptions.password, Buffer.from('00112233445566778899AABBCCDDEEFF01020304', 'hex'));
311-
this._sendAuthResponse(packet.data, expected);
312-
break;
313295
case Packets.ComQueryPacket:
314296
if (!this.emit('query', packet)) {
315297
this._handleQueryPacket(packet);
@@ -322,6 +304,9 @@ FakeConnection.prototype._parsePacket = function() {
322304
}
323305
break;
324306
case Packets.ComChangeUserPacket:
307+
this.database = (packet.database || null);
308+
this.user = (packet.user || null);
309+
325310
if (!this.emit('changeUser', packet)) {
326311
if (packet.user === 'does-not-exist') {
327312
this._sendPacket(new Packets.ErrorPacket({
@@ -339,9 +324,6 @@ FakeConnection.prototype._parsePacket = function() {
339324
break;
340325
}
341326

342-
this.database = (packet.database || null);
343-
this.user = (packet.user || null);
344-
345327
this._sendPacket(new Packets.OkPacket());
346328
this._parser.resetPacketNumber();
347329
}
@@ -352,7 +334,9 @@ FakeConnection.prototype._parsePacket = function() {
352334
}
353335
break;
354336
default:
355-
throw new Error('Unexpected packet: ' + Packet.name);
337+
if (!this.emit(packet.constructor.name, packet)) {
338+
throw new Error('Unexpected packet: ' + Packet.name);
339+
}
356340
}
357341
};
358342

test/unit/connection/test-auth-switch-native.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
var assert = require('assert');
2-
var Buffer = require('safe-buffer').Buffer;
2+
var Crypto = require('crypto');
33
var common = require('../../common');
44
var connection = common.createConnection({
55
port : common.fakeServerPort,
66
password : 'authswitch'
77
});
8+
var Auth = require(common.lib + '/protocol/Auth');
89

10+
var random = Crypto.pseudoRandomBytes || Crypto.randomBytes; // Depends on node.js version
911
var server = common.createFakeServer();
1012

1113
var connected;
@@ -23,11 +25,21 @@ server.listen(common.fakeServerPort, function (err) {
2325
});
2426

2527
server.on('connection', function(incomingConnection) {
26-
incomingConnection.handshake({
27-
user : connection.config.user,
28-
password : connection.config.password,
29-
authMethodName : 'mysql_native_password',
30-
authMethodData : Buffer.from('00112233445566778899AABBCCDDEEFF0102030400', 'hex')
28+
random(20, function (err, scramble) {
29+
assert.ifError(err);
30+
31+
incomingConnection.on('clientAuthentication', function () {
32+
this._sendPacket(new common.Packets.AuthSwitchRequestPacket({
33+
authMethodName : 'mysql_native_password',
34+
authMethodData : scramble
35+
}));
36+
});
37+
38+
incomingConnection.on('AuthSwitchResponsePacket', function (packet) {
39+
this._sendAuthResponse(packet.data, Auth.token('authswitch', scramble));
40+
});
41+
42+
incomingConnection.handshake();
3143
});
3244
});
3345

test/unit/connection/test-auth-switch-unknown.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ server.listen(common.fakeServerPort, function (err) {
2323
});
2424

2525
server.on('connection', function(incomingConnection) {
26-
incomingConnection.handshake({
27-
user : connection.config.user,
28-
password : connection.config.password,
29-
authMethodName : 'foo_plugin_password',
30-
authMethodData : Buffer.alloc(0)
26+
incomingConnection.on('clientAuthentication', function () {
27+
this._sendPacket(new common.Packets.AuthSwitchRequestPacket({
28+
authMethodName : 'foo_plugin_password',
29+
authMethodData : Buffer.alloc(0)
30+
}));
3131
});
32+
33+
incomingConnection.handshake();
3234
});

test/unit/connection/test-insecure-auth-error.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ server.listen(common.fakeServerPort, function (err) {
2121
});
2222

2323
server.on('connection', function(incomingConnection) {
24-
incomingConnection.handshake({
25-
user : connection.config.user,
26-
password : connection.config.password,
27-
oldPassword : true
24+
incomingConnection.on('clientAuthentication', function () {
25+
this._sendPacket(new common.Packets.UseOldPasswordPacket());
2826
});
27+
28+
incomingConnection.handshake();
2929
});

test/unit/connection/test-old-password.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ var connection = common.createConnection({
55
insecureAuth : true
66
});
77
var assert = require('assert');
8+
var Auth = require(common.lib + '/protocol/Auth');
9+
var Crypto = require('crypto');
810

11+
var random = Crypto.pseudoRandomBytes || Crypto.randomBytes; // Depends on node.js version
912
var server = common.createFakeServer();
1013

1114
var connected;
@@ -23,10 +26,21 @@ server.listen(common.fakeServerPort, function(err) {
2326
});
2427

2528
server.on('connection', function(incomingConnection) {
26-
incomingConnection.handshake({
27-
user : connection.config.user,
28-
password : connection.config.password,
29-
oldPassword : true
29+
random(8, function (err, scramble) {
30+
assert.ifError(err);
31+
32+
incomingConnection.on('clientAuthentication', function () {
33+
this._sendPacket(new common.Packets.UseOldPasswordPacket());
34+
});
35+
36+
incomingConnection.on('OldPasswordPacket', function (packet) {
37+
var expected = Auth.scramble323(scramble, 'oldpw');
38+
this._sendAuthResponse(packet.scrambleBuff, expected);
39+
});
40+
41+
incomingConnection.handshake({
42+
scrambleBuff1: scramble
43+
});
3044
});
3145
});
3246

test/unit/connection/test-password.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,33 @@ var connection = common.createConnection({
44
password : 'passwd'
55
});
66
var assert = require('assert');
7+
var Auth = require(common.lib + '/protocol/Auth');
8+
var Crypto = require('crypto');
79

10+
var random = Crypto.pseudoRandomBytes || Crypto.randomBytes; // Depends on node.js version
811
var server = common.createFakeServer();
912

10-
var connected;
1113
server.listen(common.fakeServerPort, function(err) {
12-
if (err) throw err;
13-
14-
connection.connect(function(err, result) {
15-
if (err) throw err;
16-
17-
connected = result;
14+
assert.ifError(err);
1815

16+
connection.connect(function (err) {
17+
assert.ifError(err);
1918
connection.destroy();
2019
server.destroy();
2120
});
2221
});
2322

2423
server.on('connection', function(incomingConnection) {
25-
incomingConnection.handshake({
26-
user : connection.config.user,
27-
password : connection.config.password
28-
});
29-
});
24+
random(20, function (err, scramble) {
25+
assert.ifError(err);
26+
27+
incomingConnection.on('clientAuthentication', function (packet) {
28+
this._sendAuthResponse(packet.scrambleBuff, Auth.token('passwd', scramble));
29+
});
3030

31-
process.on('exit', function() {
32-
assert.equal(connected.fieldCount, 0);
31+
incomingConnection.handshake({
32+
scrambleBuff1 : scramble.slice(0, 8),
33+
scrambleBuff2 : scramble.slice(8, 20)
34+
});
35+
});
3336
});

0 commit comments

Comments
 (0)