diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-04-18 15:53:53 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:18 +0800 |
commit | a0c7da95c25bbfc324026acb0c8ee162bb25859e (patch) | |
tree | 6cbb516f26167fc43b89c9e0eebb2bc71026df3f /packages | |
parent | 95dbc0f5bcabd1f7a5fda9f4104242d6d068b18b (diff) | |
download | dexon-0x-contracts-a0c7da95c25bbfc324026acb0c8ee162bb25859e.tar dexon-0x-contracts-a0c7da95c25bbfc324026acb0c8ee162bb25859e.tar.gz dexon-0x-contracts-a0c7da95c25bbfc324026acb0c8ee162bb25859e.tar.bz2 dexon-0x-contracts-a0c7da95c25bbfc324026acb0c8ee162bb25859e.tar.lz dexon-0x-contracts-a0c7da95c25bbfc324026acb0c8ee162bb25859e.tar.xz dexon-0x-contracts-a0c7da95c25bbfc324026acb0c8ee162bb25859e.tar.zst dexon-0x-contracts-a0c7da95c25bbfc324026acb0c8ee162bb25859e.zip |
Added tests for (most functions in) LibBytes. The write methods are not currently compatible with our generated contract wrappers.
Diffstat (limited to 'packages')
-rw-r--r-- | packages/contracts/package.json | 4 | ||||
-rw-r--r-- | packages/contracts/src/utils/types.ts | 1 | ||||
-rw-r--r-- | packages/contracts/test/libraries/lib_bytes.ts | 238 |
3 files changed, 241 insertions, 2 deletions
diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 8be09c9f8..db9022373 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -26,8 +26,8 @@ "test:circleci": "yarn test:coverage" }, "config": { - "abis": "../migrations/src/artifacts/@(DummyToken|Exchange|TokenRegistry|MultiSigWallet|MultiSigWalletWithTimeLock|MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress|TokenRegistry|ZRXToken|AssetProxyDispatcher|ERC20Proxy|ERC721Proxy|DummyERC721Token).json", - "contracts": "Exchange,DummyToken,ZRXToken,Token,WETH9,MultiSigWallet,MultiSigWalletWithTimeLock,MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress,MaliciousToken,TokenRegistry,AssetProxyDispatcher,ERC20Proxy,ERC721Proxy,DummyERC721Token", + "abis": "../migrations/src/artifacts/@(DummyToken|Exchange|TokenRegistry|MultiSigWallet|MultiSigWalletWithTimeLock|MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress|TokenRegistry|ZRXToken|AssetProxyDispatcher|ERC20Proxy|ERC721Proxy|DummyERC721Token|LibBytes).json", + "contracts": "Exchange,DummyToken,ZRXToken,Token,WETH9,MultiSigWallet,MultiSigWalletWithTimeLock,MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress,MaliciousToken,TokenRegistry,AssetProxyDispatcher,ERC20Proxy,ERC721Proxy,DummyERC721Token,LibBytes", "contract_dirs": "src/contracts,zeppelin:../../node_modules/zeppelin-solidity" }, "repository": { diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index 0e30ddaee..7de167f27 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -111,6 +111,7 @@ export enum ContractName { ERC20Proxy = 'ERC20Proxy', ERC721Proxy = 'ERC721Proxy', DummyERC721Token = 'DummyERC721Token', + LibBytes = 'LibBytes', } export interface Artifact { diff --git a/packages/contracts/test/libraries/lib_bytes.ts b/packages/contracts/test/libraries/lib_bytes.ts new file mode 100644 index 000000000..b96fb84b0 --- /dev/null +++ b/packages/contracts/test/libraries/lib_bytes.ts @@ -0,0 +1,238 @@ +import { LogWithDecodedArgs, TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js'; +import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; +import { BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import BN = require('bn.js'); +import * as chai from 'chai'; +import ethUtil = require('ethereumjs-util'); +import * as Web3 from 'web3'; + +import { LibBytesContract } from '../../src/contract_wrappers/generated/lib_bytes'; +import { constants } from '../../src/utils/constants'; +import { AssetProxyId, ContractName } from '../../src/utils/types'; +import { chaiSetup } from '../utils/chai_setup'; +import { deployer } from '../utils/deployer'; +import { provider, web3Wrapper } from '../utils/web3_wrapper'; + +chaiSetup.configure(); +const expect = chai.expect; +const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); + +describe('LibBytes', () => { + let owner: string; + let libBytes: LibBytesContract; + const byteArrayShorterThan32Bytes = '0x012345'; + const byteArrayLongerThan32Bytes = + '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; + const byteArrayLongerThan32BytesFirstBytesSwapped = + '0x2301456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; + const byteArrayLongerThan32BytesLastBytesSwapped = + '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abefcd'; + let testAddress: string; + const testBytes32 = '0x102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f01020'; + const testUint256 = new BigNumber(testBytes32, 16); + + before(async () => { + // Setup accounts & addresses + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = accounts[0]; + testAddress = accounts[1]; + // Deploy LibBytes + const libBytesInstance = await deployer.deployAsync(ContractName.LibBytes); + libBytes = new LibBytesContract(libBytesInstance.abi, libBytesInstance.address, provider); + // Verify lengths of test data + const byteArrayShorterThan32BytesLength = ethUtil.toBuffer(byteArrayShorterThan32Bytes).byteLength; + expect(byteArrayShorterThan32BytesLength).to.be.lessThan(32); + const byteArrayLongerThan32BytesLength = ethUtil.toBuffer(byteArrayLongerThan32Bytes).byteLength; + expect(byteArrayLongerThan32BytesLength).to.be.greaterThan(32); + const testBytes32Length = ethUtil.toBuffer(testBytes32).byteLength; + expect(testBytes32Length).to.be.equal(32); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + + describe('areBytesEqual', () => { + it('should return true if byte arrays are equal (both arrays < 32 bytes)', async () => { + const areBytesEqual = await libBytes.areBytesEqual.callAsync( + byteArrayShorterThan32Bytes, + byteArrayShorterThan32Bytes, + ); + return expect(areBytesEqual).to.be.true(); + }); + + it('should return true if byte arrays are equal (both arrays > 32 bytes)', async () => { + const areBytesEqual = await libBytes.areBytesEqual.callAsync( + byteArrayLongerThan32Bytes, + byteArrayLongerThan32Bytes, + ); + return expect(areBytesEqual).to.be.true(); + }); + + it('should return false if byte arrays are not equal (first array < 32 bytes, second array > 32 bytes)', async () => { + const areBytesEqual = await libBytes.areBytesEqual.callAsync( + byteArrayShorterThan32Bytes, + byteArrayLongerThan32Bytes, + ); + return expect(areBytesEqual).to.be.false(); + }); + + it('should return false if byte arrays are not equal (first array > 32 bytes, second array < 32 bytes)', async () => { + const areBytesEqual = await libBytes.areBytesEqual.callAsync( + byteArrayLongerThan32Bytes, + byteArrayShorterThan32Bytes, + ); + return expect(areBytesEqual).to.be.false(); + }); + + it('should return false if byte arrays are not equal (same length, but a byte in first word differs)', async () => { + const areBytesEqual = await libBytes.areBytesEqual.callAsync( + byteArrayLongerThan32BytesFirstBytesSwapped, + byteArrayLongerThan32Bytes, + ); + return expect(areBytesEqual).to.be.false(); + }); + + it('should return false if byte arrays are not equal (same length, but a byte in last word differs)', async () => { + const areBytesEqual = await libBytes.areBytesEqual.callAsync( + byteArrayLongerThan32BytesLastBytesSwapped, + byteArrayLongerThan32Bytes, + ); + return expect(areBytesEqual).to.be.false(); + }); + }); + + describe('readAddress', () => { + it('should successfully read address when the address takes up the whole array)', async () => { + const byteArray = ethUtil.addHexPrefix(testAddress); + const testAddressOffset = new BigNumber(0); + const address = await libBytes.readAddress.callAsync(byteArray, testAddressOffset); + return expect(address).to.be.equal(testAddress); + }); + + it('should successfully read address when it is offset in the array)', async () => { + const addressByteArrayBuffer = ethUtil.toBuffer(testAddress); + const prefixByteArrayBuffer = ethUtil.toBuffer('0xabcdef'); + const combinedByteArrayBuffer = Buffer.concat([prefixByteArrayBuffer, addressByteArrayBuffer]); + const combinedByteArray = ethUtil.bufferToHex(combinedByteArrayBuffer); + const testAddressOffset = new BigNumber(prefixByteArrayBuffer.byteLength); + const address = await libBytes.readAddress.callAsync(combinedByteArray, testAddressOffset); + return expect(address).to.be.equal(testAddress); + }); + + it('should fail if the byte array is too short to hold an address)', async () => { + const shortByteArray = '0xabcdef'; + const offset = new BigNumber(0); + return expect(libBytes.readAddress.callAsync(shortByteArray, offset)).to.be.rejectedWith(constants.REVERT); + }); + + it('should fail if the length between the offset and end of the byte array is too short to hold an address)', async () => { + const byteArray = ethUtil.addHexPrefix(testAddress); + const badOffset = new BigNumber(ethUtil.toBuffer(byteArray).byteLength); + return expect(libBytes.readAddress.callAsync(byteArray, badOffset)).to.be.rejectedWith(constants.REVERT); + }); + }); + + /// @TODO Implement test cases for writeAddress. Test template below. + /// Currently, the generated contract wrappers do not support this library's write methods. + /* + describe('writeAddress', () => { + it('should successfully write address when the address takes up the whole array)', async () => {}); + it('should successfully write address when it is offset in the array)', async () => {}); + it('should fail if the byte array is too short to hold an address)', async () => {}); + it('should fail if the length between the offset and end of the byte array is too short to hold an address)', async () => {}); + }); + */ + + describe('readBytes32', () => { + it('should successfully read bytes32 when the bytes32 takes up the whole array)', async () => { + const testBytes32Offset = new BigNumber(0); + const bytes32 = await libBytes.readBytes32.callAsync(testBytes32, testBytes32Offset); + return expect(bytes32).to.be.equal(testBytes32); + }); + + it('should successfully read bytes32 when it is offset in the array)', async () => { + const bytes32ByteArrayBuffer = ethUtil.toBuffer(testBytes32); + const prefixByteArrayBuffer = ethUtil.toBuffer('0xabcdef'); + const combinedByteArrayBuffer = Buffer.concat([prefixByteArrayBuffer, bytes32ByteArrayBuffer]); + const combinedByteArray = ethUtil.bufferToHex(combinedByteArrayBuffer); + const testAddressOffset = new BigNumber(prefixByteArrayBuffer.byteLength); + const bytes32 = await libBytes.readBytes32.callAsync(combinedByteArray, testAddressOffset); + return expect(bytes32).to.be.equal(testBytes32); + }); + + it('should fail if the byte array is too short to hold a bytes32)', async () => { + const offset = new BigNumber(0); + return expect(libBytes.readBytes32.callAsync(byteArrayShorterThan32Bytes, offset)).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should fail if the length between the offset and end of the byte array is too short to hold a bytes32)', async () => { + const badOffset = new BigNumber(ethUtil.toBuffer(testBytes32).byteLength); + return expect(libBytes.readBytes32.callAsync(testBytes32, badOffset)).to.be.rejectedWith(constants.REVERT); + }); + }); + + /// @TODO Implement test cases for writeBytes32. Test template below. + /// Currently, the generated contract wrappers do not support this library's write methods. + /* + describe('writeBytes32', () => { + it('should successfully write bytes32 when the address takes up the whole array)', async () => {}); + it('should successfully write bytes32 when it is offset in the array)', async () => {}); + it('should fail if the byte array is too short to hold a bytes32)', async () => {}); + it('should fail if the length between the offset and end of the byte array is too short to hold a bytes32)', async () => {}); + }); + */ + + describe('readUint256', () => { + it('should successfully read uint256 when the uint256 takes up the whole array)', async () => { + const formattedTestUint256 = new BN(testUint256.toString(10)); + const testUint256AsBuffer = ethUtil.toBuffer(formattedTestUint256); + const byteArray = ethUtil.bufferToHex(testUint256AsBuffer); + const testUint256Offset = new BigNumber(0); + const uint256 = await libBytes.readUint256.callAsync(byteArray, testUint256Offset); + return expect(uint256).to.bignumber.equal(testUint256); + }); + + it('should successfully read uint256 when it is offset in the array)', async () => { + const prefixByteArrayBuffer = ethUtil.toBuffer('0xabcdef'); + const formattedTestUint256 = new BN(testUint256.toString(10)); + const testUint256AsBuffer = ethUtil.toBuffer(formattedTestUint256); + const combinedByteArrayBuffer = Buffer.concat([prefixByteArrayBuffer, testUint256AsBuffer]); + const combinedByteArray = ethUtil.bufferToHex(combinedByteArrayBuffer); + const testUint256Offset = new BigNumber(prefixByteArrayBuffer.byteLength); + const uint256 = await libBytes.readUint256.callAsync(combinedByteArray, testUint256Offset); + return expect(uint256).to.bignumber.equal(testUint256); + }); + + it('should fail if the byte array is too short to hold a uint256)', async () => { + const offset = new BigNumber(0); + return expect(libBytes.readUint256.callAsync(byteArrayShorterThan32Bytes, offset)).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should fail if the length between the offset and end of the byte array is too short to hold a uint256)', async () => { + const formattedTestUint256 = new BN(testUint256.toString(10)); + const testUint256AsBuffer = ethUtil.toBuffer(formattedTestUint256); + const byteArray = ethUtil.bufferToHex(testUint256AsBuffer); + const badOffset = new BigNumber(testUint256AsBuffer.byteLength); + return expect(libBytes.readUint256.callAsync(byteArray, badOffset)).to.be.rejectedWith(constants.REVERT); + }); + }); + + /// @TODO Implement test cases for writeUint256. Test template below. + /// Currently, the generated contract wrappers do not support this library's write methods. + /* + describe('writeUint256', () => { + it('should successfully write uint256 when the address takes up the whole array)', async () => {}); + it('should successfully write uint256 when it is offset in the array)', async () => {}); + it('should fail if the byte array is too short to hold a uint256)', async () => {}); + it('should fail if the length between the offset and end of the byte array is too short to hold a uint256)', async () => {}); + }); + */ +}); |