From 822e319efea9c862702ddf589eb2344ff02e5bc5 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Mon, 21 May 2018 15:59:58 -0700 Subject: Use last byte of signature as signature type --- packages/contracts/test/libraries/lib_bytes.ts | 47 ++++++++++++-------------- 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'packages/contracts/test/libraries/lib_bytes.ts') diff --git a/packages/contracts/test/libraries/lib_bytes.ts b/packages/contracts/test/libraries/lib_bytes.ts index c85cee5e3..4eedad25d 100644 --- a/packages/contracts/test/libraries/lib_bytes.ts +++ b/packages/contracts/test/libraries/lib_bytes.ts @@ -22,6 +22,7 @@ describe('LibBytes', () => { let owner: string; let libBytes: TestLibBytesContract; const byteArrayShorterThan32Bytes = '0x012345'; + const byteArrayShorterThan20Bytes = byteArrayShorterThan32Bytes; const byteArrayLongerThan32Bytes = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; const byteArrayLongerThan32BytesFirstBytesSwapped = @@ -60,39 +61,35 @@ describe('LibBytes', () => { await blockchainLifecycle.revertAsync(); }); - describe('deepCopyBytes', () => { - const byteArrayLongerThan32BytesLen = (byteArrayLongerThan32Bytes.length - 2) / 2; - it('should return a byte array of length 0 if len is 0', async () => { - const index = new BigNumber(0); - const len = new BigNumber(0); - const copy = await libBytes.publicDeepCopyBytes.callAsync(byteArrayLongerThan32Bytes, index, len); - expect(copy).to.equal(constants.NULL_BYTES); + describe('popByte', () => { + it('should revert if length is 0', async () => { + return expect(libBytes.publicPopByte.callAsync(constants.NULL_BYTES)).to.be.rejectedWith(constants.REVERT); }); - it('should throw if start index + length to copy is greater than length of byte array', async () => { - const index = new BigNumber(0); - const len = new BigNumber(byteArrayLongerThan32BytesLen + 1); - return expect( - libBytes.publicDeepCopyBytes.callAsync(byteArrayLongerThan32Bytes, index, len), - ).to.be.rejectedWith(constants.REVERT); + it('should pop the last byte from the input and return it', async () => { + const [newBytes, poppedByte] = await libBytes.publicPopByte.callAsync(byteArrayLongerThan32Bytes); + const expectedNewBytes = byteArrayLongerThan32Bytes.slice(0, -2); + const expectedPoppedByte = `0x${byteArrayLongerThan32Bytes.slice(-2)}`; + expect(newBytes).to.equal(expectedNewBytes); + expect(poppedByte).to.equal(expectedPoppedByte); }); + }); - it('should copy the entire byte array if index = 0 and len = b.length', async () => { - const index = new BigNumber(0); - const len = new BigNumber(byteArrayLongerThan32BytesLen); - const copy = await libBytes.publicDeepCopyBytes.callAsync(byteArrayLongerThan32Bytes, index, len); - expect(copy).to.equal(byteArrayLongerThan32Bytes); + describe('popAddress', () => { + it('should revert if length is less than 20', async () => { + return expect(libBytes.publicPopAddress.callAsync(byteArrayShorterThan20Bytes)).to.be.rejectedWith( + constants.REVERT, + ); }); - it('should copy part of the byte array if area to copy is less than b.length', async () => { - const index = new BigNumber(10); - const len = new BigNumber(4); - const copy = await libBytes.publicDeepCopyBytes.callAsync(byteArrayLongerThan32Bytes, index, len); - const expectedCopy = `0x${byteArrayLongerThan32Bytes.slice(22, 30)}`; - expect(copy).to.equal(expectedCopy); + it('should pop the last 20 bytes from the input and return it', async () => { + const [newBytes, poppedAddress] = await libBytes.publicPopAddress.callAsync(byteArrayLongerThan32Bytes); + const expectedNewBytes = byteArrayLongerThan32Bytes.slice(0, -40); + const expectedPoppedAddress = `0x${byteArrayLongerThan32Bytes.slice(-40)}`; + expect(newBytes).to.equal(expectedNewBytes); + expect(poppedAddress).to.equal(expectedPoppedAddress); }); }); - describe('areBytesEqual', () => { it('should return true if byte arrays are equal (both arrays < 32 bytes)', async () => { const areBytesEqual = await libBytes.publicAreBytesEqual.callAsync( -- cgit v1.2.3