Skip to content

Commit 13a00c8

Browse files
committed
tests: add basic auth switch helpers to unit test server
1 parent 016b294 commit 13a00c8

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

test/FakeServer.js

+9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ function FakeConnection(socket) {
7171
socket.on('data', this._handleData.bind(this));
7272
}
7373

74+
FakeConnection.prototype.authSwitchRequest = function authSwitchRequest(options) {
75+
this._sendPacket(new Packets.AuthSwitchRequestPacket(options));
76+
};
77+
7478
FakeConnection.prototype.deny = function deny(message, errno) {
7579
message = message || 'Access Denied';
7680
errno = errno || Errors.ER_ACCESS_DENIED_ERROR;
@@ -280,6 +284,11 @@ FakeConnection.prototype._parsePacket = function() {
280284
packet.parse(this._parser);
281285

282286
switch (Packet) {
287+
case Packets.AuthSwitchResponsePacket:
288+
if (!this.emit('authSwitchResponse', packet)) {
289+
this.deny('No auth response handler');
290+
}
291+
break;
283292
case Packets.ClientAuthenticationPacket:
284293
this.database = (packet.database || null);
285294
this.user = (packet.user || null);

test/common.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ common.fakeServerSocket = __dirname + '/fake_server.sock';
1818
common.testDatabase = process.env.MYSQL_DATABASE || 'test';
1919

2020
// Export common modules
21+
common.Auth = require(common.lib + '/protocol/Auth');
2122
common.Charsets = require(common.lib + '/protocol/constants/charsets');
2223
common.ClientConstants = require(common.lib + '/protocol/constants/client');
2324
common.Connection = require(common.lib + '/Connection');

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var connection = common.createConnection({
55
port : common.fakeServerPort,
66
password : 'authswitch'
77
});
8-
var Auth = require(common.lib + '/protocol/Auth');
98

109
var random = Crypto.pseudoRandomBytes || Crypto.randomBytes; // Depends on node.js version
1110
var server = common.createFakeServer();
@@ -28,15 +27,15 @@ server.on('connection', function(incomingConnection) {
2827
random(20, function (err, scramble) {
2928
assert.ifError(err);
3029

30+
incomingConnection.on('authSwitchResponse', function (packet) {
31+
this._sendAuthResponse(packet.data, common.Auth.token('authswitch', scramble));
32+
});
33+
3134
incomingConnection.on('clientAuthentication', function () {
32-
this._sendPacket(new common.Packets.AuthSwitchRequestPacket({
35+
this.authSwitchRequest({
3336
authMethodName : 'mysql_native_password',
3437
authMethodData : scramble
35-
}));
36-
});
37-
38-
incomingConnection.on('AuthSwitchResponsePacket', function (packet) {
39-
this._sendAuthResponse(packet.data, Auth.token('authswitch', scramble));
38+
});
4039
});
4140

4241
incomingConnection.handshake();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ server.listen(common.fakeServerPort, function (err) {
2424

2525
server.on('connection', function(incomingConnection) {
2626
incomingConnection.on('clientAuthentication', function () {
27-
this._sendPacket(new common.Packets.AuthSwitchRequestPacket({
27+
this.authSwitchRequest({
2828
authMethodName : 'foo_plugin_password',
2929
authMethodData : Buffer.alloc(0)
30-
}));
30+
});
3131
});
3232

3333
incomingConnection.handshake();

0 commit comments

Comments
 (0)