Skip to content

Commit 19a8a38

Browse files
committed
Add test for normal password auth
1 parent 7b444d9 commit 19a8a38

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

test/FakeServer.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ FakeConnection.prototype.handshake = function(options) {
5555
this._handshakeOptions = options || {};
5656

5757
this._handshakeInitializationPacket = new Packets.HandshakeInitializationPacket({
58-
scrambleBuff1 : new Buffer(8),
59-
scrambleBuff2 : new Buffer(12),
58+
scrambleBuff1 : new Buffer('1020304050607080', 'hex'),
59+
scrambleBuff2 : new Buffer('0102030405060708090A0B0C', 'hex'),
6060
serverCapabilities1 : 512, // only 1 flag, PROTOCOL_41
6161
protocol41 : true
6262
});
@@ -71,6 +71,21 @@ FakeConnection.prototype.deny = function(message, errno) {
7171
}));
7272
};
7373

74+
FakeConnection.prototype._sendAuthResponse = function(packet, expected) {
75+
var got = packet.scrambleBuff;
76+
77+
if (expected.toString('hex') === got.toString('hex')) {
78+
this._sendPacket(new Packets.OkPacket());
79+
} else {
80+
this._sendPacket(new Packets.ErrorPacket({
81+
message: 'expected ' + expected.toString('hex') + ' got ' + got.toString('hex'),
82+
errno: 1045 // ER_ACCESS_DENIED_ERROR
83+
}));
84+
}
85+
86+
this._parser.resetPacketNumber();
87+
};
88+
7489
FakeConnection.prototype._sendPacket = function(packet) {
7590
var writer = new PacketWriter();
7691
packet.write(writer);
@@ -83,7 +98,7 @@ FakeConnection.prototype._handleData = function(buffer) {
8398

8499
FakeConnection.prototype._parsePacket = function(header) {
85100
var Packet = this._determinePacket(header);
86-
var packet = new Packet();
101+
var packet = new Packet({protocol41: true});
87102

88103
packet.parse(this._parser);
89104

@@ -93,11 +108,12 @@ FakeConnection.prototype._parsePacket = function(header) {
93108

94109
if (this._handshakeOptions.oldPassword) {
95110
this._sendPacket(new Packets.UseOldPasswordPacket());
111+
} else if (this._handshakeOptions.password === 'passwd') {
112+
var expected = new Buffer('3DA0ADA7C9E1BB3A110575DF53306F9D2DE7FD09', 'hex');
113+
this._sendAuthResponse(packet, expected);
114+
} else if (this._handshakeOptions.user || this._handshakeOptions.password) {
115+
throw new Error('not implemented');
96116
} else {
97-
if (this._handshakeOptions.user || this._handshakeOptions.password) {
98-
throw new Error('not implemented');
99-
}
100-
101117
this._sendPacket(new Packets.OkPacket());
102118
this._parser.resetPacketNumber();
103119
}
@@ -106,19 +122,8 @@ FakeConnection.prototype._parsePacket = function(header) {
106122
this._oldPasswordPacket = packet;
107123

108124
var expected = Auth.scramble323(this._handshakeInitializationPacket.scrambleBuff(), this._handshakeOptions.password);
109-
var got = packet.scrambleBuff;
110-
111-
var toString = function(buffer) {
112-
return Array.prototype.slice.call(buffer).join(',');
113-
};
114-
115-
if (toString(expected) === toString(got)) {
116-
this._sendPacket(new Packets.OkPacket());
117-
} else {
118-
this._sendPacket(new Packets.ErrorPacket());
119-
}
120125

121-
this._parser.resetPacketNumber();
126+
this._sendAuthResponse(packet, expected);
122127
break;
123128
case Packets.ComQueryPacket:
124129
this.emit('query', packet);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var common = require('../../common');
2+
var connection = common.createConnection({
3+
port : common.fakeServerPort,
4+
password : 'passwd',
5+
});
6+
var assert = require('assert');
7+
8+
var server = common.createFakeServer();
9+
10+
var connected;
11+
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;
18+
19+
connection.destroy();
20+
server.destroy();
21+
});
22+
});
23+
24+
server.on('connection', function(incomingConnection) {
25+
incomingConnection.handshake({
26+
user : connection.config.user,
27+
password : connection.config.password,
28+
});
29+
});
30+
31+
process.on('exit', function() {
32+
assert.equal(connected.fieldCount, 0);
33+
});

0 commit comments

Comments
 (0)