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 20, 2017
1 parent 65f9ddb commit fb9e95e
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions crypto/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 [32]byte // treeNonce is treeID in KT, it should be a 32-byte array for cross-project compatibility
index []byte
depth uint32
leaf []byte
want Hash
}{
{treeNonce: [32]byte{0}, index: []byte("foo"), depth: 128, leaf: []byte("leaf"), want: h2h("65e7f29787a6168affd016656bb1f4f03af91cf7416270f5015005f8594d3eb6")},
} {
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 [32]byte // treeNonce is treeID in KT, it should be a 32-byte array for cross-project compatibility
index []byte
depth uint32
want Hash
}{
{treeNonce: [32]byte{0}, index: []byte("foo"), depth: 128, want: h2h("1a6b0eb739b32a46e7d679a9be03f522e907f53423aacb82e550bf657d1afb10")},
} {
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 fb9e95e

Please sign in to comment.