Skip to content

Commit

Permalink
Add tests for hasher
Browse files Browse the repository at this point in the history
* Part of #177
  • Loading branch information
vqhuy committed Jun 19, 2017
1 parent 667eba4 commit 8cae06d
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions merkletree/hasher/hasher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package hasher

import (
"encoding/hex"
"testing"
)

// h2h converts a hex string into its Hash object.
func h2h(h string) Hash {
b, err := hex.DecodeString(h)
if err != nil {
panic("invalid hex string")
}
var ret Hash
copy(ret[:], b)
return ret
}

// s2h converts a byte slice into its Hash object.
func s2h(s []byte) Hash {
var ret Hash
copy(ret[:], s)
return ret
}

func TestHashLeafVectors(t *testing.T) {
for _, tc := range []struct {
treeNonce []byte // treeNonce is treeID in KT, it should be a 8-byte slice
index []byte
depth uint32
leaf []byte
want Hash
}{
{treeNonce: make([]byte, 8), index: []byte("foo"), depth: 128, leaf: []byte("leaf"), want: h2h("d7e63cd03f40c88dcddd11cd33d060ba555adf5a792846fb47fea12d95d3501c")},
} {
if got, want := s2h(Default.HashLeaf(tc.treeNonce, tc.index, tc.depth, tc.leaf)), tc.want; got != want {
t.Errorf("HashLeaf(%v, %s, %v, %s): %x, want %x", tc.treeNonce, tc.index, tc.depth, tc.leaf, got, want)
}
}
}

func TestHashEmptyVectors(t *testing.T) {
for _, tc := range []struct {
treeNonce []byte // treeNonce is treeID in KT, it should be a 8-byte slice
index []byte
depth uint32
want Hash
}{
{treeNonce: make([]byte, 8), index: []byte("foo"), depth: 128, want: h2h("ee50dbada0aeb067724550e453101b802034a277bc0565d751c22ee1dce4998f")},
} {
if got, want := s2h(Default.HashEmpty(tc.treeNonce, tc.index, tc.depth)), tc.want; got != want {
t.Errorf("HashLeaf(%v, %s, %v): %x, want %x", tc.treeNonce, tc.index, tc.depth, got, want)
}
}
}

0 comments on commit 8cae06d

Please sign in to comment.