github.com/ethereum/go-ethereum@v1.14.4-0.20240516095835-473ee8fc07a3/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 t.Parallel() 31 for i, want := range []string{ 32 "0xf880806482520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0a1010000000000000000000000000000000000000000000000000000000000000001801ba0c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549da06180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28", 33 "0xd5c0d3cb84746573742a2a808213378667617a6f6e6b", 34 "0xc780c0c1c0825208", 35 } { 36 var out strings.Builder 37 in := newInStream(bytes.NewReader(common.FromHex(want)), 0) 38 err := rlpToText(in, &out) 39 if err != nil { 40 t.Fatal(err) 41 } 42 text := out.String() 43 rlpBytes, err := textToRlp(strings.NewReader(text)) 44 if err != nil { 45 t.Errorf("test %d: error %v", i, err) 46 continue 47 } 48 have := fmt.Sprintf("%#x", rlpBytes) 49 if have != want { 50 t.Errorf("test %d: have\n%v\nwant:\n%v\n", i, have, want) 51 } 52 } 53 } 54 55 func TestTextToRlp(t *testing.T) { 56 t.Parallel() 57 type tc struct { 58 text string 59 want string 60 } 61 cases := []tc{ 62 { 63 text: `[ 64 "", 65 [], 66 [ 67 [], 68 ], 69 5208, 70 ]`, 71 want: "0xc780c0c1c0825208", 72 }, 73 } 74 for i, tc := range cases { 75 have, err := textToRlp(strings.NewReader(tc.text)) 76 if err != nil { 77 t.Errorf("test %d: error %v", i, err) 78 continue 79 } 80 if hexutil.Encode(have) != tc.want { 81 t.Errorf("test %d:\nhave %v\nwant %v", i, hexutil.Encode(have), tc.want) 82 } 83 } 84 }