github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/trie/node_test.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The go-ethereum library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package trie 18 19 import ( 20 "bytes" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/rlp" 24 ) 25 26 func newTestFullNode(v []byte) []interface{} { 27 fullNodeData := []interface{}{} 28 for i := 0; i < 16; i++ { 29 k := bytes.Repeat([]byte{byte(i + 1)}, 32) 30 fullNodeData = append(fullNodeData, k) 31 } 32 fullNodeData = append(fullNodeData, v) 33 return fullNodeData 34 } 35 36 func TestDecodeNestedNode(t *testing.T) { 37 fullNodeData := newTestFullNode([]byte("fullnode")) 38 39 data := [][]byte{} 40 for i := 0; i < 16; i++ { 41 data = append(data, nil) 42 } 43 data = append(data, []byte("subnode")) 44 fullNodeData[15] = data 45 46 buf := bytes.NewBuffer([]byte{}) 47 rlp.Encode(buf, fullNodeData) 48 49 if _, err := decodeNode([]byte("testdecode"), buf.Bytes()); err != nil { 50 t.Fatalf("decode nested full node err: %v", err) 51 } 52 } 53 54 func TestDecodeFullNodeWrongSizeChild(t *testing.T) { 55 fullNodeData := newTestFullNode([]byte("wrongsizechild")) 56 fullNodeData[0] = []byte("00") 57 buf := bytes.NewBuffer([]byte{}) 58 rlp.Encode(buf, fullNodeData) 59 60 _, err := decodeNode([]byte("testdecode"), buf.Bytes()) 61 if _, ok := err.(*decodeError); !ok { 62 t.Fatalf("decodeNode returned wrong err: %v", err) 63 } 64 } 65 66 func TestDecodeFullNodeWrongNestedFullNode(t *testing.T) { 67 fullNodeData := newTestFullNode([]byte("fullnode")) 68 69 data := [][]byte{} 70 for i := 0; i < 16; i++ { 71 data = append(data, []byte("123456")) 72 } 73 data = append(data, []byte("subnode")) 74 fullNodeData[15] = data 75 76 buf := bytes.NewBuffer([]byte{}) 77 rlp.Encode(buf, fullNodeData) 78 79 _, err := decodeNode([]byte("testdecode"), buf.Bytes()) 80 if _, ok := err.(*decodeError); !ok { 81 t.Fatalf("decodeNode returned wrong err: %v", err) 82 } 83 } 84 85 func TestDecodeFullNode(t *testing.T) { 86 fullNodeData := newTestFullNode([]byte("decodefullnode")) 87 buf := bytes.NewBuffer([]byte{}) 88 rlp.Encode(buf, fullNodeData) 89 90 _, err := decodeNode([]byte("testdecode"), buf.Bytes()) 91 if err != nil { 92 t.Fatalf("decode full node err: %v", err) 93 } 94 }