From 132325de5942914634e8f4e95a9299b046bb68e4 Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya Date: Wed, 13 Nov 2019 12:36:17 +0530 Subject: [PATCH 1/2] fix: safe integer bound and added test cases --- src/mutation.ts | 2 +- test/mutation.ts | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/mutation.ts b/src/mutation.ts index 44a377ba8..add83f4b6 100644 --- a/src/mutation.ts +++ b/src/mutation.ts @@ -111,7 +111,7 @@ export class Mutation { // tslint:disable-next-line no-any const num = Long.fromBytes(buf as any).toNumber(); - if (Number.MIN_SAFE_INTEGER < num && num < Number.MAX_SAFE_INTEGER) { + if (Number.isSafeInteger(num)) { return num; } } diff --git a/test/mutation.ts b/test/mutation.ts index df5d993ec..8ca5fc5a5 100644 --- a/test/mutation.ts +++ b/test/mutation.ts @@ -56,6 +56,31 @@ describe('Bigtable/Mutation', function() { assert.strictEqual(num, decoded); }); + + it('should convert a base64 encoded MIN_SAFE_INTEGER number when true', function() { + const num = Number.MIN_SAFE_INTEGER; + const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString( + 'base64' + ); + const decoded = Mutation.convertFromBytes(encoded, { + isPossibleNumber: true, + }); + + assert.strictEqual(num, decoded); + }); + + it('should convert a base64 encoded MAX_SAFE_INTEGER number when true', function() { + const num = Number.MAX_SAFE_INTEGER; + const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString( + 'base64' + ); + const decoded = Mutation.convertFromBytes(encoded, { + isPossibleNumber: true, + }); + + assert.strictEqual(num, decoded); + }); + it('should not convert a base64 encoded number when false', function() { const num = 10; const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString( From 135c6106da6b7296cb20c1bb8c803d361060042b Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya Date: Thu, 14 Nov 2019 11:03:11 +0530 Subject: [PATCH 2/2] test: added more unit tests for outside of bounds --- test/mutation.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/mutation.ts b/test/mutation.ts index 8ca5fc5a5..5ab05a72a 100644 --- a/test/mutation.ts +++ b/test/mutation.ts @@ -81,6 +81,30 @@ describe('Bigtable/Mutation', function() { assert.strictEqual(num, decoded); }); + it('should not convert a base64 encoded smaller than MIN_SAFE_INTEGER number when true', function() { + const num = Number.MIN_SAFE_INTEGER - 100; + const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString( + 'base64' + ); + const decoded = Mutation.convertFromBytes(encoded, { + isPossibleNumber: true, + }); + + assert.notStrictEqual(num, decoded); + }); + + it('should not convert a base64 encoded larger than MAX_SAFE_INTEGER number when true', function() { + const num = Number.MAX_SAFE_INTEGER + 100; + const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString( + 'base64' + ); + const decoded = Mutation.convertFromBytes(encoded, { + isPossibleNumber: true, + }); + + assert.notStrictEqual(num, decoded); + }); + it('should not convert a base64 encoded number when false', function() { const num = 10; const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString(