github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/cmd/rlpdump/rlpdump_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/ethereum/go-ethereum/common"
    10  	"github.com/ethereum/go-ethereum/common/hexutil"
    11  )
    12  
    13  func TestRoundtrip(t *testing.T) {
    14  	for i, want := range []string{
    15  		"0xf880806482520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0a1010000000000000000000000000000000000000000000000000000000000000001801ba0c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549da06180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28",
    16  		"0xd5c0d3cb84746573742a2a808213378667617a6f6e6b",
    17  		"0xc780c0c1c0825208",
    18  	} {
    19  		var out strings.Builder
    20  		err := rlpToText(bytes.NewReader(common.FromHex(want)), &out)
    21  		if err != nil {
    22  			t.Fatal(err)
    23  		}
    24  		text := out.String()
    25  		rlpBytes, err := textToRlp(strings.NewReader(text))
    26  		if err != nil {
    27  			t.Errorf("test %d: error %v", i, err)
    28  			continue
    29  		}
    30  		have := fmt.Sprintf("0x%x", rlpBytes)
    31  		if have != want {
    32  			t.Errorf("test %d: have\n%v\nwant:\n%v\n", i, have, want)
    33  		}
    34  	}
    35  }
    36  
    37  func TestTextToRlp(t *testing.T) {
    38  	type tc struct {
    39  		text string
    40  		want string
    41  	}
    42  	cases := []tc{
    43  		{
    44  			text: `[
    45    "",
    46    [],
    47  [     
    48   [],
    49      ],
    50    5208,
    51  ]`,
    52  			want: "0xc780c0c1c0825208",
    53  		},
    54  	}
    55  	for i, tc := range cases {
    56  		have, err := textToRlp(strings.NewReader(tc.text))
    57  		if err != nil {
    58  			t.Errorf("test %d: error %v", i, err)
    59  			continue
    60  		}
    61  		if hexutil.Encode(have) != tc.want {
    62  			t.Errorf("test %d:\nhave %v\nwant %v", i, hexutil.Encode(have), tc.want)
    63  		}
    64  	}
    65  }