github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/cmd/rlpdump/rlpdump_test.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/common/hexutil"
    27  )
    28  
    29  func TestRoundtrip(t *testing.T) {
    30  	for i, want := range []string{
    31  		"0xf880806482520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0a1010000000000000000000000000000000000000000000000000000000000000001801ba0c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549da06180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28",
    32  		"0xd5c0d3cb84746573742a2a808213378667617a6f6e6b",
    33  		"0xc780c0c1c0825208",
    34  	} {
    35  		var out strings.Builder
    36  		err := rlpToText(bytes.NewReader(common.FromHex(want)), &out)
    37  		if err != nil {
    38  			t.Fatal(err)
    39  		}
    40  		text := out.String()
    41  		rlpBytes, err := textToRlp(strings.NewReader(text))
    42  		if err != nil {
    43  			t.Errorf("test %d: error %v", i, err)
    44  			continue
    45  		}
    46  		have := fmt.Sprintf("%#x", rlpBytes)
    47  		if have != want {
    48  			t.Errorf("test %d: have\n%v\nwant:\n%v\n", i, have, want)
    49  		}
    50  	}
    51  }
    52  
    53  func TestTextToRlp(t *testing.T) {
    54  	type tc struct {
    55  		text string
    56  		want string
    57  	}
    58  	cases := []tc{
    59  		{
    60  			text: `[
    61    "",
    62    [],
    63  [     
    64   [],
    65      ],
    66    5208,
    67  ]`,
    68  			want: "0xc780c0c1c0825208",
    69  		},
    70  	}
    71  	for i, tc := range cases {
    72  		have, err := textToRlp(strings.NewReader(tc.text))
    73  		if err != nil {
    74  			t.Errorf("test %d: error %v", i, err)
    75  			continue
    76  		}
    77  		if hexutil.Encode(have) != tc.want {
    78  			t.Errorf("test %d:\nhave %v\nwant %v", i, hexutil.Encode(have), tc.want)
    79  		}
    80  	}
    81  }