Skip to content

Commit

Permalink
fix: fix ntlm connection state bug (#1446)
Browse files Browse the repository at this point in the history
  • Loading branch information
mShan0 authored Jun 18, 2022
1 parent 57a50d4 commit 7ebd236
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3411,9 +3411,9 @@ Connection.prototype.STATE = {
if (handler.loginAckReceived) {
if (handler.routingData) {
this.routingData = handler.routingData;
this.transitionTo(this.STATE.REROUTING);
return this.transitionTo(this.STATE.REROUTING);
} else {
this.transitionTo(this.STATE.LOGGED_IN_SENDING_INITIAL_SQL);
return this.transitionTo(this.STATE.LOGGED_IN_SENDING_INITIAL_SQL);
}
} else if (this.ntlmpacket) {
try {
Expand Down Expand Up @@ -3442,19 +3442,19 @@ Connection.prototype.STATE = {
} else {
throw error;
}
this.transitionTo(this.STATE.FINAL);
return this.transitionTo(this.STATE.FINAL);
}
} else if (this.loginError) {
if (isTransientError(this.loginError)) {
this.debug.log('Initiating retry on transient error');
this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY);
return this.transitionTo(this.STATE.TRANSIENT_FAILURE_RETRY);
} else {
this.emit('connect', this.loginError);
this.transitionTo(this.STATE.FINAL);
return this.transitionTo(this.STATE.FINAL);
}
} else {
this.emit('connect', new ConnectionError('Login failed.', 'ELOGIN'));
this.transitionTo(this.STATE.FINAL);
return this.transitionTo(this.STATE.FINAL);
}
}

Expand Down
14 changes: 12 additions & 2 deletions test/integration/child-processes/ntlm-connect-node17.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Connection } = require('../../../');
const { Connection, Request } = require('../../../');
const fs = require('fs');
const homedir = require('os').homedir();

Expand All @@ -7,6 +7,15 @@ function getNtlmConfig() {
fs.readFileSync(homedir + '/.tedious/test-connection.json', 'utf8')
).ntlm;
}

const request = new Request('select 1; select 2;', function(err, rowCount) {
if (err) {
throw err;
}
connection.close();
});


const ntlmConfig = getNtlmConfig();

const connection = new Connection(ntlmConfig);
Expand All @@ -19,5 +28,6 @@ connection.connect(function(err) {
throw err;
}
}
connection.close();

connection.execSql(request);
});
29 changes: 25 additions & 4 deletions test/integration/connection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,34 @@ describe('Ntlm Test', function() {
assert.ok(false, 'Unexpected value for domainCase: ' + domainCase);
}

const connection = new Connection(ntlmConfig);
let row = 0;

connection.connect(function(err) {
const request = new Request('select 1; select 2;', function(err, rowCount) {
assert.ifError(err);
assert.strictEqual(rowCount, 2);

connection.close();
});

request.on('doneInProc', function(rowCount, more) {
assert.strictEqual(rowCount, 1);
});

request.on('columnMetadata', function(columnsMetadata) {
assert.strictEqual(columnsMetadata.length, 1);
});

request.on('row', function(columns) {
assert.strictEqual(columns[0].value, ++row);
});

const connection = new Connection(ntlmConfig);

connection.connect(function(err) {
assert.ifError(err);
connection.execSql(request);
});

connection.on('end', function() {
done();
});
Expand Down Expand Up @@ -471,8 +491,9 @@ describe('Ntlm Test', function() {
});

} else {
const ntlmConfig = getNtlmConfig();

it('should throw an aggregate error with node 17', () => {
(ntlmConfig ? it : it.skip)('should throw an aggregate error with node 17', () => {
const child = childProcess.spawnSync(process.execPath,
['./test/integration/child-processes/ntlm-connect-node17.js'],
{ encoding: 'utf8' });
Expand All @@ -483,7 +504,7 @@ describe('Ntlm Test', function() {
assert.strictEqual(child.status, 1);
});

it('should ntlm with node 17 when `--openssl-legacy-provider` flag enabled', () => {
(ntlmConfig ? it : it.skip)('should ntlm with node 17 when `--openssl-legacy-provider` flag enabled', () => {
const child = childProcess.spawnSync(process.execPath,
['--openssl-legacy-provider',
'./test/integration/child-processes/ntlm-connect-node17.js'],
Expand Down

0 comments on commit 7ebd236

Please sign in to comment.