From 9dcf2855a887ea6d9ad5e6bb700669154668df22 Mon Sep 17 00:00:00 2001 From: Stephen Palmer Date: Wed, 11 Oct 2017 11:48:45 -0500 Subject: [PATCH 1/2] Added npm command to generate and open coverage report for local testing --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index e5ba024..c63003d 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "scripts": { "test": "mocha test", + "coverage": "istanbul cover ./node_modules/mocha/bin/_mocha; open coverage/lcov-report/index.html", "start": "node main.js", "coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, From 7903abade0f3d579ad52aeb7b6d3b2f5b01dea5d Mon Sep 17 00:00:00 2001 From: Stephen Palmer Date: Wed, 11 Oct 2017 11:49:39 -0500 Subject: [PATCH 2/2] Added check to ensure we at least receive more than 1 character for the protocol version check --- lib/constants.js | 1 + lib/server.js | 7 +++++++ test/server.js | 14 ++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/lib/constants.js b/lib/constants.js index ed44024..f8c7838 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -1,6 +1,7 @@ const constants = { VERSION: "5.4.0", PROTOCOL_VERSION: 254, + PROTOCOL_VERSION_MIN_SIZE: 2, UINT32_SIZE: 8, // hex UINT64_SIZE: 16, // hex HASH_SIZE: 16, // bin diff --git a/lib/server.js b/lib/server.js index 7a386cf..cde8f6d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -73,6 +73,13 @@ class CacheServer { // Get the version as the first thing var idx = 0; if (!socket.protocolVersion) { + if (data.length < consts.PROTOCOL_VERSION_MIN_SIZE) + { + // We need more data + socket.pendingData = data; + return false; + } + socket.protocolVersion = helpers.readUInt32(data); let buf = Buffer.allocUnsafe(consts.UINT32_SIZE); if (socket.protocolVersion == consts.PROTOCOL_VERSION) { diff --git a/test/server.js b/test/server.js index 196e7be..8b63987 100644 --- a/test/server.js +++ b/test/server.js @@ -122,6 +122,20 @@ describe("CacheServer protocol", function() { client.write(helpers.encodeInt32(consts.PROTOCOL_VERSION + 1)); }); + + it("should recognize a 2 byte version sent 1 byte at a time", function (done) { + this.slow(250); + + client.on('data', function(data) { + var ver = helpers.readUInt32(data); + assert(ver == consts.PROTOCOL_VERSION, "Expected " + consts.PROTOCOL_VERSION + " Received " + ver); + done(); + }); + + var ver = "fe"; + client.write(ver[0]); + sleep(50).then(() => { client.write(ver[1]); }); + }); }); describe("Transactions", function () {