aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/test
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2018-11-20 04:43:16 +0800
committerGreg Hysen <greg.hysen@gmail.com>2018-11-29 08:38:10 +0800
commitd70d07366fcc9ed2a750a5427af9d210c486f344 (patch)
tree92092cdec3062619a7f0ae6b3b4bf72fe058854b /packages/utils/test
parent9a0bd05c4ca98dd24d71cdbd53175bfdd5250107 (diff)
downloaddexon-sol-tools-d70d07366fcc9ed2a750a5427af9d210c486f344.tar
dexon-sol-tools-d70d07366fcc9ed2a750a5427af9d210c486f344.tar.gz
dexon-sol-tools-d70d07366fcc9ed2a750a5427af9d210c486f344.tar.bz2
dexon-sol-tools-d70d07366fcc9ed2a750a5427af9d210c486f344.tar.lz
dexon-sol-tools-d70d07366fcc9ed2a750a5427af9d210c486f344.tar.xz
dexon-sol-tools-d70d07366fcc9ed2a750a5427af9d210c486f344.tar.zst
dexon-sol-tools-d70d07366fcc9ed2a750a5427af9d210c486f344.zip
Tests for Static Bytes
Diffstat (limited to 'packages/utils/test')
-rw-r--r--packages/utils/test/abi_encoder_test.ts176
1 files changed, 136 insertions, 40 deletions
diff --git a/packages/utils/test/abi_encoder_test.ts b/packages/utils/test/abi_encoder_test.ts
index 4b85ad938..5fe625092 100644
--- a/packages/utils/test/abi_encoder_test.ts
+++ b/packages/utils/test/abi_encoder_test.ts
@@ -1270,62 +1270,158 @@ describe.only('ABI Encoder', () => {
});
});
- /*
+ describe.only('Static Bytes', () => {
+ it('Single Byte (byte)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Byte', type: 'byte' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ const args = '0x05';
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x0500000000000000000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
+ });
- describe('Static Bytes', () => {
- it('Byte (padded)', async () => {
- const testByteDataItem = { name: 'testStaticBytes', type: 'byte' };
- const byteDataType = new AbiEncoder.Byte(testByteDataItem);
- byteDataType.assignValue('0x05');
- const expectedAbiEncodedByte = '0x0500000000000000000000000000000000000000000000000000000000000000';
- expect(byteDataType.getHexValue()).to.be.equal(expectedAbiEncodedByte);
+ it('Single Byte (bytes1)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes1', type: 'bytes1' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ const args = '0x05';
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x0500000000000000000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
});
- it.skip('Byte (no padding)', async () => {
- const testByteDataItem = { name: 'testStaticBytes', type: 'byte' };
- const byteDataType = new AbiEncoder.Byte(testByteDataItem);
+ it('4 Bytes (bytes4)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes4', type: 'bytes4' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ const args = '0x00010203';
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x0001020300000000000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
+ });
- // @TODO: This does not catch the Error
- expect(byteDataType.assignValue('0x5')).to.throw();
+ it('4 Bytes (bytes4); Encoder must pad input', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes4', type: 'bytes4' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ // Note: There will be padding because this is a bytes32 but we are only passing in 4 bytes.
+ const args = '0x1a18';
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x1a18000000000000000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const paddedArgs = '0x1a180000';
+ const paddedArgsAsJson = JSON.stringify(paddedArgs);
+ expect(decodedArgsAsJson).to.be.equal(paddedArgsAsJson);
});
- it('Bytes1', async () => {
- const testByteDataItem = { name: 'testStaticBytes', type: 'bytes1' };
- const byteDataType = new AbiEncoder.Byte(testByteDataItem);
- byteDataType.assignValue('0x05');
- const expectedAbiEncodedByte = '0x0500000000000000000000000000000000000000000000000000000000000000';
- expect(byteDataType.getHexValue()).to.be.equal(expectedAbiEncodedByte);
+ it('32 Bytes (bytes32)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes32', type: 'bytes32' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ const args = '0x0001020304050607080911121314151617181920212223242526272829303132';
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x0001020304050607080911121314151617181920212223242526272829303132';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
+ });
+
+ it('32 Bytes (bytes32); Encoder must pad input', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes32', type: 'bytes32' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ // Note: There will be padding because this is a bytes32 but we are only passing in 4 bytes.
+ const args = '0x1a18bf61';
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x1a18bf6100000000000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const paddedArgs = '0x1a18bf6100000000000000000000000000000000000000000000000000000000';
+ const paddedArgsAsJson = JSON.stringify(paddedArgs);
+ expect(decodedArgsAsJson).to.be.equal(paddedArgsAsJson);
});
- it('Bytes32 (padded)', async () => {
- const testByteDataItem = { name: 'testStaticBytes', type: 'bytes32' };
- const byteDataType = new AbiEncoder.Byte(testByteDataItem);
- byteDataType.assignValue('0x0001020304050607080911121314151617181920212223242526272829303132');
- const expectedAbiEncodedByte = '0x0001020304050607080911121314151617181920212223242526272829303132';
- expect(byteDataType.getHexValue()).to.be.equal(expectedAbiEncodedByte);
+ it('Should throw when pass in too many bytes (bytes4)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes4', type: 'bytes4' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ const args = '0x0102030405';
+ // Encode Args and validate result
+ expect(() => { dataType.encode(args) }).to.throw('Tried to assign 0x0102030405 (5 bytes), which exceeds max bytes that can be stored in a bytes4');
});
- it('Bytes32 (unpadded)', async () => {
- const testByteDataItem = { name: 'testStaticBytes', type: 'bytes32' };
- const byteDataType = new AbiEncoder.Byte(testByteDataItem);
- byteDataType.assignValue('0x1a18bf61');
- const expectedAbiEncodedByte = '0x1a18bf6100000000000000000000000000000000000000000000000000000000';
- expect(byteDataType.getHexValue()).to.be.equal(expectedAbiEncodedByte);
+ it('Should throw when pass in too many bytes (bytes32)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes32', type: 'bytes32' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ const args = '0x010203040506070809101112131415161718192021222324252627282930313233';
+ // Encode Args and validate result
+ expect(() => { dataType.encode(args) }).to.throw('Tried to assign 0x010203040506070809101112131415161718192021222324252627282930313233 (33 bytes), which exceeds max bytes that can be stored in a bytes32');
});
- it.skip('Bytes32 - Too long', async () => {
- const testByteDataItem = { name: 'testStaticBytes', type: 'bytes32' };
- const byteDataType = new AbiEncoder.Byte(testByteDataItem);
+ it('Should throw when pass in bad hex (no 0x prefix)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes32', type: 'bytes32' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ const args = '0102030405060708091011121314151617181920212223242526272829303132';
+ // Encode Args and validate result
+ expect(() => { dataType.encode(args) }).to.throw("Tried to encode non-hex value. Value must inlcude '0x' prefix.");
+ });
- // @TODO: This does not catch the Error
- expect(
- byteDataType.assignValue('0x000102030405060708091112131415161718192021222324252627282930313233'),
- ).to.throw(
- `Tried to assign 0x000102030405060708091112131415161718192021222324252627282930313233 (33 bytes), which exceeds max bytes that can be stored in a bytes32`,
- );
+ it('Should throw when pass in bad hex (include a half-byte)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes32', type: 'bytes32' };
+ const dataType = new AbiEncoder.Byte(testDataItem);
+ // Construct args to be encoded
+ const args = '0x010';
+ // Encode Args and validate result
+ expect(() => { dataType.encode(args) }).to.throw('Tried to assign 0x010, which is contains a half-byte. Use full bytes only.');
});
});
+ /*
+
+
+
describe('Bytes (Dynamic)', () => {
const testBytesDataItem = { name: 'testBytes', type: 'bytes' };
it('Less than 32 bytes', async () => {