Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions trie/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package trie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+license

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


import (
"bytes"
"testing"

"github.com/ethereum/go-ethereum/rlp"
)

func newFullNode(v []byte) []interface{} {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the v is not used here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already fixed it.

fullNodeData := []interface{}{}
for i := 0; i < 16; i++ {
k := bytes.Repeat([]byte{byte(i + 1)}, 32)
fullNodeData = append(fullNodeData, k)
}
fullNodeData = append(fullNodeData, []byte("value1"))
return fullNodeData
}

func TestDecodeNestedNode(t *testing.T) {
fullNodeData := newFullNode([]byte("fullnode"))

data := [][]byte{}
for i := 0; i < 16; i++ {
data = append(data, nil)
}
data = append(data, []byte("subnode"))
fullNodeData[15] = data

buf := bytes.NewBuffer([]byte{})
rlp.Encode(buf, fullNodeData)

if _, err := decodeNode([]byte("testdecode"), buf.Bytes()); err != nil {
t.Fatalf("decode nested full node err: %v", err)
}
}

func TestDecodeFullNodeWrongSizeChild(t *testing.T) {
fullNodeData := newFullNode([]byte("wrongsizechild"))
fullNodeData[0] = []byte("00")
buf := bytes.NewBuffer([]byte{})
rlp.Encode(buf, fullNodeData)

_, err := decodeNode([]byte("testdecode"), buf.Bytes())
if _, ok := err.(*decodeError); !ok {
t.Fatalf("decodeNode returned wrong err: %v", err)
}
}

func TestDecodeFullNodeWrongNestedFullNode(t *testing.T) {
fullNodeData := newFullNode([]byte("fullnode"))

data := [][]byte{}
for i := 0; i < 16; i++ {
data = append(data, []byte("123456"))
}
data = append(data, []byte("subnode"))
fullNodeData[15] = data

buf := bytes.NewBuffer([]byte{})
rlp.Encode(buf, fullNodeData)

_, err := decodeNode([]byte("testdecode"), buf.Bytes())
if _, ok := err.(*decodeError); !ok {
t.Fatalf("decodeNode returned wrong err: %v", err)
}
}

func TestDecodeNode(t *testing.T) {
fullNodeData := newFullNode([]byte("decodefullnode"))
buf := bytes.NewBuffer([]byte{})
rlp.Encode(buf, fullNodeData)

_, err := decodeNode([]byte("testdecode"), buf.Bytes())
if err != nil {
t.Fatalf("decode full node err: %v", err)
}
}
13 changes: 0 additions & 13 deletions trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,16 +546,3 @@ func updateString(trie *Trie, k, v string) {
func deleteString(trie *Trie, k string) {
trie.Delete([]byte(k))
}

func TestDecodeNode(t *testing.T) {
t.Parallel()
var (
hash = make([]byte, 20)
elems = make([]byte, 20)
)
for i := 0; i < 5000000; i++ {
rand.Read(hash)
rand.Read(elems)
decodeNode(hash, elems)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test seems useless, and it tests nothing, so i decide to delete it. already rollback.