Skip to content

Commit

Permalink
up abi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geraldb committed Jan 7, 2023
1 parent 182e6ca commit 093b220
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 23 deletions.
66 changes: 43 additions & 23 deletions abicoder/test/test_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,58 @@

class TestFixtures < MiniTest::Test

def test_basic

def assert_test( test )
types = test['types']
args = test['args']
hex = test['data']

puts "==> testing #{types}...:"
types.zip( args ).each_with_index do |(type,arg),i|
puts " [#{i}] #{type} => #{arg.inspect}"
## pp ABI::Type.parse( type )
end

=begin
## quick hack for bytes / bytes10 etc.
## change encoding to BINARAY / ASCII_8BIT
## not really possible with yaml fixtures
args = types.zip(args).map do |(type,arg)|
arg = arg.b if ['bytes', 'bytes10'].include?( type )
arg
end
=end
if hex
data = hex( hex ) ## convert hex string to binary string
bin = ABI.encode( types, args )
assert bin.encoding == Encoding::BINARY ## note: always check for BINARY encoding too
assert_equal data, bin

assert_equal args, ABI.decode( types, data )
end
assert_equal args, ABI.decode( types, ABI.encode( types, args ))
end


def test_basic
tests = read_yml( '../test/abicoder/basic.yml' )
puts " #{tests.size} test(s) in /test/abicoder/basic.yml"
pp tests

tests.each do |test|
types = test['types']
args = test['args']
hex = test['data']

=begin
## quick hack for bytes / bytes10 etc.
## change encoding to BINARAY / ASCII_8BIT
## not really possible with yaml fixtures
args = types.zip(args).map do |(type,arg)|
arg = arg.b if ['bytes', 'bytes10'].include?( type )
arg
end
=end
if hex
data = hex( hex ) ## convert hex string to binary string
bin = ABI.encode( types, args )
assert bin.encoding == Encoding::BINARY ## note: always check for BINARY encoding too
assert_equal data, bin

assert_equal args, ABI.decode( types, data )
end
assert_equal args, ABI.decode( types, ABI.encode( types, args ))
assert_test test
end
end

def test_more
tests = read_yml( '../test/abicoder/more.yml' )
puts " #{tests.size} test(s) in /test/abicoder/more.yml"
pp tests

tests.each do |test|
assert_test test
end
end

end ## class TestFixtures

250 changes: 250 additions & 0 deletions test/abicoder/more.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
#######################
# more tests converted from ethabi (rust)
# see https://github.com/rust-ethereum/ethabi/blob/master/ethabi/src/encoder.rs

# address
- types: ['address']
args: ['1111111111111111111111111111111111111111']
data: '0000000000000000000000001111111111111111111111111111111111111111'

# dynamic array of addresses
- types: ['address[]']
args: [['1111111111111111111111111111111111111111',
'2222222222222222222222222222222222222222']]
data: '0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222'

## fixed array of addresses
- types: ['address[2]']
args: [['1111111111111111111111111111111111111111',
'2222222222222222222222222222222222222222']]
data: '0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222'

## fixed array of dynamic array of addresses
- types: ['address[][2]'] ## note: in solidity arrays are in reverse - double check if ok here!!!
args: [[['1111111111111111111111111111111111111111',
'2222222222222222222222222222222222222222'],
['3333333333333333333333333333333333333333',
'4444444444444444444444444444444444444444']]]
data: '0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000040
00000000000000000000000000000000000000000000000000000000000000a0
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000003333333333333333333333333333333333333333
0000000000000000000000004444444444444444444444444444444444444444'

## dynamic array of fixed array of addresses
- types: ['address[2][]']
args: [[['1111111111111111111111111111111111111111',
'2222222222222222222222222222222222222222'],
['3333333333333333333333333333333333333333',
'4444444444444444444444444444444444444444']]]
data: '0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
0000000000000000000000003333333333333333333333333333333333333333
0000000000000000000000004444444444444444444444444444444444444444'

## dynamic array of dynamic arrays
- types: ['address[][]']
args: [[['1111111111111111111111111111111111111111'],
['2222222222222222222222222222222222222222']]]
data: '0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000080
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000002222222222222222222222222222222222222222'


## fixed array of fixed arrays
- types: ['address[2][2]']
args: [[['1111111111111111111111111111111111111111',
'2222222222222222222222222222222222222222'],
['3333333333333333333333333333333333333333',
'4444444444444444444444444444444444444444']]]
data: '0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
0000000000000000000000003333333333333333333333333333333333333333
0000000000000000000000004444444444444444444444444444444444444444'


## fixed array of static tuples followed by dynamic type
- types: ['(uint256,uint256,address)[2]','string']
args: [[[93523141, 352332135, '4444444444444444444444444444444444444444'],
[12411, 451, '2222222222222222222222222222222222222222']],
'gavofyork']
data: '0000000000000000000000000000000000000000000000000000000005930cc5
0000000000000000000000000000000000000000000000000000000015002967
0000000000000000000000004444444444444444444444444444444444444444
000000000000000000000000000000000000000000000000000000000000307b
00000000000000000000000000000000000000000000000000000000000001c3
0000000000000000000000002222222222222222222222222222222222222222
00000000000000000000000000000000000000000000000000000000000000e0
0000000000000000000000000000000000000000000000000000000000000009
6761766f66796f726b0000000000000000000000000000000000000000000000'

## empty arrays
- types: ['uint256[]','uint256[]']
args: [[],[]]
data: '0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000060
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000'


## nested empty arrays
- types: ['uint256[][]','uint256[][]']
args: [[[]],[[]]]
data: '0000000000000000000000000000000000000000000000000000000000000040
00000000000000000000000000000000000000000000000000000000000000a0
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000000'

## bool
- types: ['bool']
args: [true]
data: '0000000000000000000000000000000000000000000000000000000000000001'

## bool2
- types: ['bool']
args: [false]
data: '0000000000000000000000000000000000000000000000000000000000000000'


## comprehensive test2
- types: ['int256','string','int256','int256','int256','int256[]' ]
args: [1, 'gavofyork', 2, 3, 4, [5,6,7]]
data: '0000000000000000000000000000000000000000000000000000000000000001
00000000000000000000000000000000000000000000000000000000000000c0
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000004
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000009
6761766f66796f726b0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000005
0000000000000000000000000000000000000000000000000000000000000006
0000000000000000000000000000000000000000000000000000000000000007'


## static tuple of addresses
- types: ['(address,address)']
args: [['1111111111111111111111111111111111111111',
'2222222222222222222222222222222222222222']]
data: '0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222'

## dynamic tuple
- types: ['(string,string)']
args: [['gavofyork', 'gavofyork']]
data: '0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000080
0000000000000000000000000000000000000000000000000000000000000009
6761766f66796f726b0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000009
6761766f66796f726b0000000000000000000000000000000000000000000000'


## complex tuple
- types: ['(uint256,string,address,address)']
args: [[0x1111111111111111111111111111111111111111111111111111111111111111,
'gavofyork',
'1111111111111111111111111111111111111111',
'2222222222222222222222222222222222222222']]
data: '0000000000000000000000000000000000000000000000000000000000000020
1111111111111111111111111111111111111111111111111111111111111111
0000000000000000000000000000000000000000000000000000000000000080
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
0000000000000000000000000000000000000000000000000000000000000009
6761766f66796f726b0000000000000000000000000000000000000000000000'


# nested tuple
- types: ['(string,bool,string,(string,string,(string,string)))']
args: [['test', true, 'cyborg',
['night', 'day', ['weee', 'funtests']]]]
data: '0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000080
0000000000000000000000000000000000000000000000000000000000000001
00000000000000000000000000000000000000000000000000000000000000c0
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000000004
7465737400000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000006
6379626f72670000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000060
00000000000000000000000000000000000000000000000000000000000000a0
00000000000000000000000000000000000000000000000000000000000000e0
0000000000000000000000000000000000000000000000000000000000000005
6e69676874000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000003
6461790000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000080
0000000000000000000000000000000000000000000000000000000000000004
7765656500000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000008
66756e7465737473000000000000000000000000000000000000000000000000'


# params containing dynamic tuple
- types: ['address', '(bool,string,string)', 'address', 'address', 'bool' ]
args: ['2222222222222222222222222222222222222222',
[true, 'spaceship', 'cyborg'],
'3333333333333333333333333333333333333333',
'4444444444444444444444444444444444444444',
false]
data: '0000000000000000000000002222222222222222222222222222222222222222
00000000000000000000000000000000000000000000000000000000000000a0
0000000000000000000000003333333333333333333333333333333333333333
0000000000000000000000004444444444444444444444444444444444444444
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000060
00000000000000000000000000000000000000000000000000000000000000a0
0000000000000000000000000000000000000000000000000000000000000009
7370616365736869700000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000006
6379626f72670000000000000000000000000000000000000000000000000000'


# params containing static tuple
- types: ['address', '(address,bool,bool)', 'address', 'address']
args: ['1111111111111111111111111111111111111111',
['2222222222222222222222222222222222222222', true, false],
'3333333333333333333333333333333333333333',
'4444444444444444444444444444444444444444']
data: '0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000003333333333333333333333333333333333333333
0000000000000000000000004444444444444444444444444444444444444444'

# dynamic tuple with nested static tuples
- types: ['(((bool,uint256)),uint256[]))']
args: [[[[false,0x777]],[0x42,0x1337]]]
data: '0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000777
0000000000000000000000000000000000000000000000000000000000000060
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000042
0000000000000000000000000000000000000000000000000000000000001337'

0 comments on commit 093b220

Please sign in to comment.