@@ -394,37 +394,6 @@ func TestBytes(t *testing.T) {
394394 }
395395}
396396
397- /*
398- func TestReturn(t *testing.T) {
399- const definition = `[
400- { "type" : "function", "name" : "balance", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "hash" } ] },
401- { "type" : "function", "name" : "name", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "address" } ] }]`
402-
403- abi, err := JSON(strings.NewReader(definition))
404- if err != nil {
405- t.Fatal(err)
406- }
407-
408- r := abi.Call(func([]byte) []byte {
409- t := make([]byte, 32)
410- t[0] = 1
411- return t
412- }, "balance")
413- if _, ok := r.(common.Hash); !ok {
414- t.Errorf("expected type common.Hash, got %T", r)
415- }
416-
417- r = abi.Call(func([]byte) []byte {
418- t := make([]byte, 32)
419- t[0] = 1
420- return t
421- }, "name")
422- if _, ok := r.(common.Address); !ok {
423- t.Errorf("expected type common.Address, got %T", r)
424- }
425- }
426- */
427-
428397func TestDefaultFunctionParsing (t * testing.T ) {
429398 const definition = `[{ "name" : "balance" }]`
430399
@@ -550,11 +519,71 @@ func TestMultiReturnWithSlice(t *testing.T) {
550519 }
551520}
552521
522+ func TestMarshalArrays (t * testing.T ) {
523+ const definition = `[
524+ { "name" : "bytes32", "const" : false, "outputs": [ { "type": "bytes32" } ] },
525+ { "name" : "bytes10", "const" : false, "outputs": [ { "type": "bytes10" } ] }
526+ ]`
527+
528+ abi , err := JSON (strings .NewReader (definition ))
529+ if err != nil {
530+ t .Fatal (err )
531+ }
532+
533+ output := common .LeftPadBytes ([]byte {1 }, 32 )
534+
535+ var bytes10 [10 ]byte
536+ err = abi .unmarshal (& bytes10 , "bytes32" , output )
537+ if err == nil || err .Error () != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
538+ t .Error ("expected error or bytes32 not be assignable to bytes10:" , err )
539+ }
540+
541+ var bytes32 [32 ]byte
542+ err = abi .unmarshal (& bytes32 , "bytes32" , output )
543+ if err != nil {
544+ t .Error ("didn't expect error:" , err )
545+ }
546+ if ! bytes .Equal (bytes32 [:], output ) {
547+ t .Error ("expected bytes32[31] to be 1 got" , bytes32 [31 ])
548+ }
549+
550+ type (
551+ B10 [10 ]byte
552+ B32 [32 ]byte
553+ )
554+
555+ var b10 B10
556+ err = abi .unmarshal (& b10 , "bytes32" , output )
557+ if err == nil || err .Error () != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
558+ t .Error ("expected error or bytes32 not be assignable to bytes10:" , err )
559+ }
560+
561+ var b32 B32
562+ err = abi .unmarshal (& b32 , "bytes32" , output )
563+ if err != nil {
564+ t .Error ("didn't expect error:" , err )
565+ }
566+ if ! bytes .Equal (b32 [:], output ) {
567+ t .Error ("expected bytes32[31] to be 1 got" , bytes32 [31 ])
568+ }
569+
570+ output [10 ] = 1
571+ var shortAssignLong [32 ]byte
572+ err = abi .unmarshal (& shortAssignLong , "bytes10" , output )
573+ if err != nil {
574+ t .Error ("didn't expect error:" , err )
575+ }
576+ if ! bytes .Equal (output , shortAssignLong [:]) {
577+ t .Errorf ("expected %x to be %x" , shortAssignLong , output )
578+ }
579+ }
580+
553581func TestUnmarshal (t * testing.T ) {
554582 const definition = `[
555583 { "name" : "int", "const" : false, "outputs": [ { "type": "uint256" } ] },
556584 { "name" : "bool", "const" : false, "outputs": [ { "type": "bool" } ] },
557585 { "name" : "bytes", "const" : false, "outputs": [ { "type": "bytes" } ] },
586+ { "name" : "fixed", "const" : false, "outputs": [ { "type": "bytes32" } ] },
558587 { "name" : "multi", "const" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
559588 { "name" : "mixedBytes", "const" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
560589
@@ -655,6 +684,21 @@ func TestUnmarshal(t *testing.T) {
655684 t .Errorf ("expected %x got %x" , bytesOut , Bytes )
656685 }
657686
687+ // marshal dynamic bytes length 5
688+ buff .Reset ()
689+ buff .Write (common .RightPadBytes ([]byte ("hello" ), 32 ))
690+
691+ var hash common.Hash
692+ err = abi .unmarshal (& hash , "fixed" , buff .Bytes ())
693+ if err != nil {
694+ t .Error (err )
695+ }
696+
697+ helloHash := common .BytesToHash (common .RightPadBytes ([]byte ("hello" ), 32 ))
698+ if hash != helloHash {
699+ t .Errorf ("Expected %x to equal %x" , hash , helloHash )
700+ }
701+
658702 // marshal error
659703 buff .Reset ()
660704 buff .Write (common .Hex2Bytes ("0000000000000000000000000000000000000000000000000000000000000020" ))
0 commit comments