Skip to content

Commit

Permalink
95.7% test coverage for internal/trie/node
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 16, 2021
1 parent 9c73b48 commit 61baed1
Show file tree
Hide file tree
Showing 12 changed files with 980 additions and 27 deletions.
4 changes: 2 additions & 2 deletions internal/trie/node/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func NewBranch(key, value []byte, dirty bool, generation uint64) *Branch {

func (b *Branch) String() string {
if len(b.Value) > 1024 {
return fmt.Sprintf("key=%x childrenBitmap=%16b value (hashed)=%x dirty=%v",
return fmt.Sprintf("branch key=0x%x childrenBitmap=%b value (hashed)=0x%x dirty=%t",
b.Key, b.ChildrenBitmap(), common.MustBlake2bHash(b.Value), b.dirty)
}
return fmt.Sprintf("key=%x childrenBitmap=%16b value=%v dirty=%v",
return fmt.Sprintf("branch key=0x%x childrenBitmap=%b value=0x%x dirty=%t",
b.Key, b.ChildrenBitmap(), b.Value, b.dirty)
}
128 changes: 128 additions & 0 deletions internal/trie/node/branch_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,134 @@ import (
"github.com/stretchr/testify/require"
)

func Test_Branch_ScaleEncodeHash(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
branch *Branch
encoding []byte
wrappedErr error
errMessage string
}{
"empty branch": {
branch: &Branch{},
encoding: []byte{0xc, 0x80, 0x0, 0x0},
},
"non empty branch": {
branch: &Branch{
Key: []byte{1, 2},
Value: []byte{3, 4},
Children: [16]Node{
nil, nil, &Leaf{Key: []byte{9}},
},
},
encoding: []byte{0x2c, 0xc2, 0x12, 0x4, 0x0, 0x8, 0x3, 0x4, 0xc, 0x41, 0x9, 0x0},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

encoding, err := testCase.branch.ScaleEncodeHash()

if testCase.wrappedErr != nil {
assert.ErrorIs(t, err, testCase.wrappedErr)
assert.EqualError(t, err, testCase.errMessage)
} else {
require.NoError(t, err)
}
assert.Equal(t, testCase.encoding, encoding)
})
}
}

func Test_Branch_hash(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
branch *Branch
write writeCall
errWrapped error
errMessage string
}{
"empty branch": {
branch: &Branch{},
write: writeCall{
written: []byte{128, 0, 0},
},
},
"less than 32 bytes encoding": {
branch: &Branch{
Key: []byte{1, 2},
},
write: writeCall{
written: []byte{130, 18, 0, 0},
},
},
"less than 32 bytes encoding write error": {
branch: &Branch{
Key: []byte{1, 2},
},
write: writeCall{
written: []byte{130, 18, 0, 0},
err: errTest,
},
errWrapped: errTest,
errMessage: "cannot write encoded branch to buffer: test error",
},
"more than 32 bytes encoding": {
branch: &Branch{
Key: repeatBytes(100, 1),
},
write: writeCall{
written: []byte{
70, 102, 188, 24, 31, 68, 86, 114,
95, 156, 225, 138, 175, 254, 176, 251,
81, 84, 193, 40, 11, 234, 142, 233,
69, 250, 158, 86, 72, 228, 66, 46},
},
},
"more than 32 bytes encoding write error": {
branch: &Branch{
Key: repeatBytes(100, 1),
},
write: writeCall{
written: []byte{
70, 102, 188, 24, 31, 68, 86, 114,
95, 156, 225, 138, 175, 254, 176, 251,
81, 84, 193, 40, 11, 234, 142, 233,
69, 250, 158, 86, 72, 228, 66, 46},
err: errTest,
},
errWrapped: errTest,
errMessage: "cannot write hash sum of branch to buffer: test error",
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)

digestBuffer := NewMockWriter(ctrl)
digestBuffer.EXPECT().Write(testCase.write.written).
Return(testCase.write.n, testCase.write.err)

err := testCase.branch.hash(digestBuffer)

if testCase.errWrapped != nil {
assert.ErrorIs(t, err, testCase.errWrapped)
assert.EqualError(t, err, testCase.errMessage)
} else {
require.NoError(t, err)
}
})
}
}

func Test_Branch_Encode(t *testing.T) {
t.Parallel()

Expand Down
95 changes: 95 additions & 0 deletions internal/trie/node/branch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package node

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_NewBranch(t *testing.T) {
t.Parallel()

key := []byte{1, 2}
value := []byte{3, 4}
const dirty = true
const generation = 9

branch := NewBranch(key, value, dirty, generation)

expectedBranch := &Branch{
Key: key,
Value: value,
dirty: dirty,
generation: generation,
}
assert.Equal(t, expectedBranch, branch)

// Check modifying passed slice modifies branch slices
key[0] = 11
value[0] = 13
assert.Equal(t, expectedBranch, branch)
}

func Test_Branch_String(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
branch *Branch
s string
}{
"empty branch": {
branch: &Branch{},
s: "branch key=0x childrenBitmap=0 value=0x dirty=false",
},
"branch with value smaller than 1024": {
branch: &Branch{
Key: []byte{1, 2},
Value: []byte{3, 4},
dirty: true,
Children: [16]Node{
nil, nil, nil,
&Leaf{},
nil, nil, nil,
&Branch{},
nil, nil, nil,
&Leaf{},
nil, nil, nil, nil,
},
},
s: "branch key=0x0102 childrenBitmap=100010001000 value=0x0304 dirty=true",
},
"branch with value higher than 1024": {
branch: &Branch{
Key: []byte{1, 2},
Value: make([]byte, 1025),
dirty: true,
Children: [16]Node{
nil, nil, nil,
&Leaf{},
nil, nil, nil,
&Branch{},
nil, nil, nil,
&Leaf{},
nil, nil, nil, nil,
},
},
s: "branch key=0x0102 childrenBitmap=100010001000 " +
"value (hashed)=0x307861663233363133353361303538646238383034626337353735323831663131663735313265326331346336373032393864306232336630396538386266333066 " + //nolint:lll
"dirty=true",
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

s := testCase.branch.String()

assert.Equal(t, testCase.s, s)
})
}
}
3 changes: 3 additions & 0 deletions internal/trie/node/copy_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package node

import (
Expand Down
Loading

0 comments on commit 61baed1

Please sign in to comment.