-
-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle prepare response with actual number of parameter definiti…
…on less than reported in the prepare header. Fixes #2052 * chore: route synchronous exceptions from packet deserialisation as a fatal connection error * chore: initial steps in migration to builtin node test runner * chore: add test:builtin-node-runner npm script * add a test case reproducing #2052 issue * add mysql:8.0.33 to linux matrix * debug test execution * cleanup debug log * simple connection test * simple connection test * end connection in the test * add timeout * fix: handle prepare response with actual number of parameter definition less than reported in the prepare header. Fixes #2052 * test commit * lint
- Loading branch information
Showing
5 changed files
with
163 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,147 @@ | ||
import { describe, it } from 'node:test'; | ||
import { describe, it, beforeEach, afterEach } from 'node:test'; | ||
import assert from 'node:assert'; | ||
// import common from '../../common.js'; | ||
|
||
describe('Prepare result when CLIENT_OPTIONAL_RESULTSET_METADATA is set or metadata_follows is unset', () => { | ||
it('should not throw an exception', (t, done) => { | ||
assert(true); | ||
done(null); | ||
/* | ||
const connection = common.createConnection({ | ||
database: 'mysql', | ||
import common from '../../common.js'; | ||
import PrepareCommand from '../../../lib/commands/prepare.js'; | ||
import packets from '../../../lib/packets/index.js'; | ||
|
||
describe( | ||
'Unit test - prepare result with number of parameters incorrectly reported by the server', | ||
{ timeout: 1000 }, | ||
() => { | ||
it('should report 0 actual parameters when 1 placeholder is used in ORDER BY ?', (t, done) => { | ||
const connection = { | ||
constructor: { | ||
statementKey: () => 0, | ||
}, | ||
_handshakePacket: {}, | ||
_resetSequenceId: () => {}, | ||
_statements: new Map(), | ||
serverEncoding: 'utf8', | ||
clientEncoding: 'utf8', | ||
config: { | ||
charsetNumber: 33, | ||
}, | ||
writePacket: (packet) => { | ||
// client -> server COM_PREPARE | ||
assert.equal( | ||
packet.buffer.toString('hex'), | ||
'000000001673656c656374202a2066726f6d207573657273206f72646572206279203f' | ||
); | ||
}, | ||
}; | ||
const prepareCommand = new PrepareCommand( | ||
{ sql: 'select * from users order by ?' }, | ||
(err, result) => { | ||
assert.equal(err, null); | ||
debugger; | ||
assert.equal(result.parameters.length, 0); | ||
assert.equal(result.columns.length, 51); | ||
assert.equal(result.id, 1); | ||
done(null); | ||
} | ||
); | ||
|
||
prepareCommand.execute(null, connection); | ||
const headerPacket = new packets.Packet( | ||
0, | ||
Buffer.from('0000000000010000003300010000000005000002', 'hex'), | ||
0, | ||
20 | ||
); | ||
prepareCommand.execute(headerPacket, connection); | ||
const paramsEofPacket = new packets.Packet(0, Buffer.from('00000000fe000002002b000004', 'hex'), 0, 11); | ||
prepareCommand.execute(paramsEofPacket, connection); | ||
for (let i = 0; i < 51; ++i) { | ||
const columnDefinitionPacket = new packets.Packet( | ||
0, | ||
Buffer.from( | ||
'0000000003646566056d7973716c0475736572047573657204486f737404486f73740ce000fc030000fe034000000005000005', 'hex' | ||
), | ||
0, | ||
47 | ||
); | ||
prepareCommand.execute(columnDefinitionPacket, connection); | ||
} | ||
const columnsEofPacket = new packets.Packet(0, Buffer.from('00000000fe000002002b000004', 'hex'), 0, 11); | ||
prepareCommand.execute(columnsEofPacket, connection); | ||
}); | ||
} | ||
); | ||
|
||
describe('E2E Prepare result with number of parameters incorrectly reported by the server', { timeout: 1000 }, () => { | ||
let connection; | ||
|
||
function isNewerThan8_0_22() { | ||
const { serverVersion } = connection._handshakePacket; | ||
const [major, minor, patch] = serverVersion.split('.').map((x) => parseInt(x, 10)); | ||
if (major > 8) { | ||
return true; | ||
} | ||
if (major === 8 && minor > 0) { | ||
return true; | ||
} | ||
if (major === 8 && minor === 0 && patch >= 22) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
beforeEach((t, done) => { | ||
connection = common.createConnection({ | ||
database: 'mysql', | ||
}); | ||
connection.on('error', (err) => { | ||
done(err); | ||
}); | ||
connection.on('connect', () => { | ||
done(null); | ||
}); | ||
}); | ||
|
||
afterEach((t, done) => { | ||
connection.end(err => { | ||
done(null) | ||
}); | ||
}); | ||
|
||
// see https://github.com/sidorares/node-mysql2/issues/2052#issuecomment-1620318928 | ||
it('should report 0 actual parameters when 1 placeholder is used in ORDER BY ?', (t, done) => { | ||
connection.prepare('select * from user order by ?', (err, stmt) => { | ||
console.log(err); | ||
if (err) { | ||
if (!err.fatal) { | ||
connection.end(); | ||
} | ||
done(err); | ||
} else { | ||
if(isNewerThan8_0_22()) { | ||
// mysql 8.0.22 and newer report 0 parameters | ||
assert.equal(stmt.parameters.length, 0); | ||
} else { | ||
// mariadb, mysql 8.0.21 and older report 1 parameter | ||
assert.equal(stmt.parameters.length, 1); | ||
} | ||
done(null); | ||
} | ||
}); | ||
}); | ||
|
||
it('should report 1 actual parameters when 2 placeholders used in ORDER BY?', (t, done) => { | ||
connection.prepare('select * from user where user.User like ? order by ?', (err, stmt) => { | ||
if (err) { | ||
if (!err.fatal) { | ||
connection.end(); | ||
} | ||
done(err); | ||
} else { | ||
if(isNewerThan8_0_22()) { | ||
// mysql 8.0.22 and newer report 1 parameter | ||
assert.equal(stmt.parameters.length, 1); | ||
} else { | ||
// mariadb, mysql 8.0.21 and older report 2 parameters | ||
assert.equal(stmt.parameters.length, 2); | ||
} | ||
done(null); | ||
} | ||
}); | ||
*/ | ||
}); | ||
}); |