github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/accounts/abi/abi_test.go (about)

     1  package abi
     2  
     3  import (
     4  	"bytes"
     5  	"math/big"
     6  	"reflect"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/jonasnick/go-ethereum/crypto"
    11  )
    12  
    13  const jsondata = `
    14  [
    15  	{ "name" : "balance", "const" : true },
    16  	{ "name" : "send", "const" : false, "input" : [ { "name" : "amount", "type" : "uint256" } ] }
    17  ]`
    18  
    19  const jsondata2 = `
    20  [
    21  	{ "name" : "balance", "const" : true },
    22  	{ "name" : "send", "const" : false, "input" : [ { "name" : "amount", "type" : "uint256" } ] },
    23  	{ "name" : "test", "const" : false, "input" : [ { "name" : "number", "type" : "uint32" } ] },
    24  	{ "name" : "string", "const" : false, "input" : [ { "name" : "input", "type" : "string" } ] },
    25  	{ "name" : "bool", "const" : false, "input" : [ { "name" : "input", "type" : "bool" } ] },
    26  	{ "name" : "address", "const" : false, "input" : [ { "name" : "input", "type" : "address" } ] },
    27  	{ "name" : "string32", "const" : false, "input" : [ { "name" : "input", "type" : "string32" } ] },
    28  	{ "name" : "uint64[2]", "const" : false, "input" : [ { "name" : "input", "type" : "uint64[2]" } ] },
    29  	{ "name" : "uint64[]", "const" : false, "input" : [ { "name" : "input", "type" : "uint64[]" } ] },
    30  	{ "name" : "foo", "const" : false, "input" : [ { "name" : "input", "type" : "uint32" } ] },
    31  	{ "name" : "bar", "const" : false, "input" : [ { "name" : "input", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
    32  	{ "name" : "slice", "const" : false, "input" : [ { "name" : "input", "type" : "uint32[2]" } ] },
    33  	{ "name" : "slice256", "const" : false, "input" : [ { "name" : "input", "type" : "uint256[2]" } ] }
    34  ]`
    35  
    36  func TestType(t *testing.T) {
    37  	typ, err := NewType("uint32")
    38  	if err != nil {
    39  		t.Error(err)
    40  	}
    41  	if typ.Kind != reflect.Ptr {
    42  		t.Error("expected uint32 to have kind Ptr")
    43  	}
    44  
    45  	typ, err = NewType("uint32[]")
    46  	if err != nil {
    47  		t.Error(err)
    48  	}
    49  	if typ.Kind != reflect.Slice {
    50  		t.Error("expected uint32[] to have type slice")
    51  	}
    52  	if typ.Type != ubig_ts {
    53  		t.Error("expcted uith32[] to have type uint64")
    54  	}
    55  
    56  	typ, err = NewType("uint32[2]")
    57  	if err != nil {
    58  		t.Error(err)
    59  	}
    60  	if typ.Kind != reflect.Slice {
    61  		t.Error("expected uint32[2] to have kind slice")
    62  	}
    63  	if typ.Type != ubig_ts {
    64  		t.Error("expcted uith32[2] to have type uint64")
    65  	}
    66  	if typ.Size != 2 {
    67  		t.Error("expected uint32[2] to have a size of 2")
    68  	}
    69  }
    70  
    71  func TestReader(t *testing.T) {
    72  	Uint256, _ := NewType("uint256")
    73  	exp := ABI{
    74  		Methods: map[string]Method{
    75  			"balance": Method{
    76  				"balance", true, nil, Type{},
    77  			},
    78  			"send": Method{
    79  				"send", false, []Argument{
    80  					Argument{"amount", Uint256},
    81  				}, Type{},
    82  			},
    83  		},
    84  	}
    85  
    86  	abi, err := JSON(strings.NewReader(jsondata))
    87  	if err != nil {
    88  		t.Error(err)
    89  	}
    90  
    91  	// deep equal fails for some reason
    92  	t.Skip()
    93  	if !reflect.DeepEqual(abi, exp) {
    94  		t.Errorf("\nabi: %v\ndoes not match exp: %v", abi, exp)
    95  	}
    96  }
    97  
    98  func TestTestNumbers(t *testing.T) {
    99  	abi, err := JSON(strings.NewReader(jsondata2))
   100  	if err != nil {
   101  		t.Error(err)
   102  		t.FailNow()
   103  	}
   104  
   105  	if _, err := abi.Pack("balance"); err != nil {
   106  		t.Error(err)
   107  	}
   108  
   109  	if _, err := abi.Pack("balance", 1); err == nil {
   110  		t.Error("expected error for balance(1)")
   111  	}
   112  
   113  	if _, err := abi.Pack("doesntexist", nil); err == nil {
   114  		t.Errorf("doesntexist shouldn't exist")
   115  	}
   116  
   117  	if _, err := abi.Pack("doesntexist", 1); err == nil {
   118  		t.Errorf("doesntexist(1) shouldn't exist")
   119  	}
   120  
   121  	if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
   122  		t.Error(err)
   123  	}
   124  
   125  	i := new(int)
   126  	*i = 1000
   127  	if _, err := abi.Pack("send", i); err == nil {
   128  		t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
   129  	}
   130  
   131  	if _, err := abi.Pack("send", 1000); err != nil {
   132  		t.Error("expected send(1000) to cast to big")
   133  	}
   134  
   135  	if _, err := abi.Pack("test", uint32(1000)); err != nil {
   136  		t.Error(err)
   137  	}
   138  }
   139  
   140  func TestTestString(t *testing.T) {
   141  	abi, err := JSON(strings.NewReader(jsondata2))
   142  	if err != nil {
   143  		t.Error(err)
   144  		t.FailNow()
   145  	}
   146  
   147  	if _, err := abi.Pack("string", "hello world"); err != nil {
   148  		t.Error(err)
   149  	}
   150  
   151  	str10 := string(make([]byte, 10))
   152  	if _, err := abi.Pack("string32", str10); err != nil {
   153  		t.Error(err)
   154  	}
   155  
   156  	str32 := string(make([]byte, 32))
   157  	if _, err := abi.Pack("string32", str32); err != nil {
   158  		t.Error(err)
   159  	}
   160  
   161  	str33 := string(make([]byte, 33))
   162  	if _, err := abi.Pack("string32", str33); err == nil {
   163  		t.Error("expected str33 to throw out of bound error")
   164  	}
   165  }
   166  
   167  func TestTestBool(t *testing.T) {
   168  	abi, err := JSON(strings.NewReader(jsondata2))
   169  	if err != nil {
   170  		t.Error(err)
   171  		t.FailNow()
   172  	}
   173  
   174  	if _, err := abi.Pack("bool", true); err != nil {
   175  		t.Error(err)
   176  	}
   177  }
   178  
   179  func TestTestSlice(t *testing.T) {
   180  	abi, err := JSON(strings.NewReader(jsondata2))
   181  	if err != nil {
   182  		t.Error(err)
   183  		t.FailNow()
   184  	}
   185  
   186  	addr := make([]byte, 20)
   187  	if _, err := abi.Pack("address", addr); err != nil {
   188  		t.Error(err)
   189  	}
   190  
   191  	addr = make([]byte, 21)
   192  	if _, err := abi.Pack("address", addr); err == nil {
   193  		t.Error("expected address of 21 width to throw")
   194  	}
   195  
   196  	slice := make([]byte, 2)
   197  	if _, err := abi.Pack("uint64[2]", slice); err != nil {
   198  		t.Error(err)
   199  	}
   200  
   201  	if _, err := abi.Pack("uint64[]", slice); err != nil {
   202  		t.Error(err)
   203  	}
   204  }
   205  
   206  func TestTestAddress(t *testing.T) {
   207  	abi, err := JSON(strings.NewReader(jsondata2))
   208  	if err != nil {
   209  		t.Error(err)
   210  		t.FailNow()
   211  	}
   212  
   213  	addr := make([]byte, 20)
   214  	if _, err := abi.Pack("address", addr); err != nil {
   215  		t.Error(err)
   216  	}
   217  }
   218  
   219  func TestMethodSignature(t *testing.T) {
   220  	String, _ := NewType("string")
   221  	String32, _ := NewType("string32")
   222  	m := Method{"foo", false, []Argument{Argument{"bar", String32}, Argument{"baz", String}}, Type{}}
   223  	exp := "foo(string32,string)"
   224  	if m.String() != exp {
   225  		t.Error("signature mismatch", exp, "!=", m.String())
   226  	}
   227  
   228  	idexp := crypto.Sha3([]byte(exp))[:4]
   229  	if !bytes.Equal(m.Id(), idexp) {
   230  		t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
   231  	}
   232  
   233  	uintt, _ := NewType("uint")
   234  	m = Method{"foo", false, []Argument{Argument{"bar", uintt}}, Type{}}
   235  	exp = "foo(uint256)"
   236  	if m.String() != exp {
   237  		t.Error("signature mismatch", exp, "!=", m.String())
   238  	}
   239  }
   240  
   241  func TestPack(t *testing.T) {
   242  	abi, err := JSON(strings.NewReader(jsondata2))
   243  	if err != nil {
   244  		t.Error(err)
   245  		t.FailNow()
   246  	}
   247  
   248  	sig := crypto.Sha3([]byte("foo(uint32)"))[:4]
   249  	sig = append(sig, make([]byte, 32)...)
   250  	sig[35] = 10
   251  
   252  	packed, err := abi.Pack("foo", uint32(10))
   253  	if err != nil {
   254  		t.Error(err)
   255  		t.FailNow()
   256  	}
   257  
   258  	if !bytes.Equal(packed, sig) {
   259  		t.Errorf("expected %x got %x", sig, packed)
   260  	}
   261  }
   262  
   263  func TestMultiPack(t *testing.T) {
   264  	abi, err := JSON(strings.NewReader(jsondata2))
   265  	if err != nil {
   266  		t.Error(err)
   267  		t.FailNow()
   268  	}
   269  
   270  	sig := crypto.Sha3([]byte("bar(uint32,uint16)"))[:4]
   271  	sig = append(sig, make([]byte, 64)...)
   272  	sig[35] = 10
   273  	sig[67] = 11
   274  
   275  	packed, err := abi.Pack("bar", uint32(10), uint16(11))
   276  	if err != nil {
   277  		t.Error(err)
   278  		t.FailNow()
   279  	}
   280  
   281  	if !bytes.Equal(packed, sig) {
   282  		t.Errorf("expected %x got %x", sig, packed)
   283  	}
   284  }
   285  
   286  func TestPackSlice(t *testing.T) {
   287  	abi, err := JSON(strings.NewReader(jsondata2))
   288  	if err != nil {
   289  		t.Error(err)
   290  		t.FailNow()
   291  	}
   292  
   293  	sig := crypto.Sha3([]byte("slice(uint32[2])"))[:4]
   294  	sig = append(sig, make([]byte, 64)...)
   295  	sig[35] = 1
   296  	sig[67] = 2
   297  
   298  	packed, err := abi.Pack("slice", []uint32{1, 2})
   299  	if err != nil {
   300  		t.Error(err)
   301  		t.FailNow()
   302  	}
   303  
   304  	if !bytes.Equal(packed, sig) {
   305  		t.Errorf("expected %x got %x", sig, packed)
   306  	}
   307  }
   308  
   309  func TestPackSliceBig(t *testing.T) {
   310  	abi, err := JSON(strings.NewReader(jsondata2))
   311  	if err != nil {
   312  		t.Error(err)
   313  		t.FailNow()
   314  	}
   315  
   316  	sig := crypto.Sha3([]byte("slice256(uint256[2])"))[:4]
   317  	sig = append(sig, make([]byte, 64)...)
   318  	sig[35] = 1
   319  	sig[67] = 2
   320  
   321  	packed, err := abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
   322  	if err != nil {
   323  		t.Error(err)
   324  		t.FailNow()
   325  	}
   326  
   327  	if !bytes.Equal(packed, sig) {
   328  		t.Errorf("expected %x got %x", sig, packed)
   329  	}
   330  }