From d7898f9907d389e5bb40d5b52664a1ff341b49b5 Mon Sep 17 00:00:00 2001 From: Aditi Khare <106987683+aditi-khare-mongoDB@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:32:54 -0400 Subject: [PATCH] fix(NODE-6042): Binary.toString output with respect to position (#663) --- src/binary.ts | 10 +++---- test/node/binary.test.ts | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/binary.ts b/src/binary.ts index 84182fb2b..6a892b00a 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -186,15 +186,15 @@ export class Binary extends BSONValue { } toJSON(): string { - return ByteUtils.toBase64(this.buffer); + return ByteUtils.toBase64(this.buffer.subarray(0, this.position)); } toString(encoding?: 'hex' | 'base64' | 'utf8' | 'utf-8'): string { - if (encoding === 'hex') return ByteUtils.toHex(this.buffer); - if (encoding === 'base64') return ByteUtils.toBase64(this.buffer); + if (encoding === 'hex') return ByteUtils.toHex(this.buffer.subarray(0, this.position)); + if (encoding === 'base64') return ByteUtils.toBase64(this.buffer.subarray(0, this.position)); if (encoding === 'utf8' || encoding === 'utf-8') - return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false); - return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false); + return ByteUtils.toUTF8(this.buffer, 0, this.position, false); + return ByteUtils.toUTF8(this.buffer, 0, this.position, false); } /** @internal */ diff --git a/test/node/binary.test.ts b/test/node/binary.test.ts index 1e0b2bd66..2adffc79e 100644 --- a/test/node/binary.test.ts +++ b/test/node/binary.test.ts @@ -144,4 +144,68 @@ describe('class Binary', () => { }); }); }); + + context('toString()', () => { + context('when case is UTF8 (default)', () => { + it('should respect position when converting to string', () => { + const bin = new Binary(); + expect(bin.toString()).to.equal(''); + bin.put(1); + expect(bin.toString()).to.equal('\u0001'); + }); + it('should remain same after round trip', () => { + const bin = new BSON.Binary(); + const serializedBin = BSON.serialize({ bin }); + const roundTrippedBin = BSON.deserialize(serializedBin); + expect(roundTrippedBin.bin.toString()).to.equal(bin.toString()); + }); + }); + + context('when case is hex', () => { + it('should respect position when converting to string', () => { + const bin = new Binary(); + expect(bin.toString('hex')).to.equal(''); + bin.put(1); + expect(bin.toString('hex')).to.equal('01'); + }); + it('should remain same after round trip', () => { + const bin = new BSON.Binary(); + const serializedBin = BSON.serialize({ bin }); + const roundTrippedBin = BSON.deserialize(serializedBin); + expect(roundTrippedBin.bin.toString('hex')).to.equal(bin.toString('hex')); + }); + }); + + context('when case is base64', () => { + it('should respect position when converting to string', () => { + const bin = new Binary(); + expect(bin.toString('base64')).to.equal(''); + bin.put(1); + expect(bin.toString('base64')).to.equal('AQ=='); + }); + it('should remain same after round trip', () => { + const bin = new BSON.Binary(); + const serializedBin = BSON.serialize({ bin }); + const roundTrippedBin = BSON.deserialize(serializedBin); + expect(roundTrippedBin.bin.toString('base64')).to.equal(bin.toString()); + }); + }); + }); + + context('toJSON()', () => { + it('should respect position when converting to JSON', () => { + const bin = new Binary(); + expect(bin.toJSON()).to.equal(''); + bin.put(1); + // toJSON uses base64 + expect(bin.toJSON()).to.equal('AQ=='); + }); + + it('should remain same after round trip', () => { + const bin = new BSON.Binary(); + const serializedBin = BSON.serialize({ bin }); + const roundTrippedBin = BSON.deserialize(serializedBin); + expect(roundTrippedBin.bin.toJSON()).to.equal(bin.toJSON()); + }); + }); });