-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tz: Add BinaryMarshaler/BinaryUnmarshaler implementation to hash
Closes #47. Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package tz | ||
|
||
import ( | ||
"encoding/binary" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func newDigest() *digest { | ||
d := &digest{} | ||
d.Reset() | ||
return d | ||
} | ||
|
||
func Test_digest_marshaling(t *testing.T) { | ||
var ( | ||
d = newDigest() | ||
marshaledState []byte | ||
hashSum []byte | ||
) | ||
|
||
for i := byte(0); i < 10; i++ { | ||
n, err := d.Write([]byte{i}) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, n) | ||
|
||
t.Run("marshal", func(t *testing.T) { | ||
marshaledState, err = d.MarshalBinary() | ||
|
||
require.NoError(t, err) | ||
require.Len(t, marshaledState, Size) | ||
|
||
hashSum = d.Sum(nil) | ||
require.Len(t, hashSum, Size) | ||
}) | ||
|
||
t.Run("unmarshal", func(t *testing.T) { | ||
unmarshalDigest := newDigest() | ||
err = unmarshalDigest.UnmarshalBinary(marshaledState) | ||
require.NoError(t, err) | ||
|
||
unmarshalDigestHash := unmarshalDigest.Sum(nil) | ||
require.Len(t, unmarshalDigestHash, Size) | ||
|
||
require.Equal(t, hashSum, unmarshalDigestHash) | ||
}) | ||
} | ||
|
||
t.Run("invalid length", func(t *testing.T) { | ||
unmarshalDigest := newDigest() | ||
state := []byte{1, 2, 3} | ||
|
||
err := unmarshalDigest.UnmarshalBinary(state) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("invalid state data", func(t *testing.T) { | ||
someDigest := newDigest() | ||
_, err := d.Write([]byte{1, 2, 3}) | ||
require.NoError(t, err) | ||
|
||
state, err := someDigest.MarshalBinary() | ||
require.NoError(t, err) | ||
|
||
a := uint64(1) << 63 | ||
// broke state data. | ||
binary.BigEndian.PutUint64(state[0:8], a) | ||
unmarshalDigest := newDigest() | ||
|
||
err = unmarshalDigest.UnmarshalBinary(state) | ||
require.Error(t, err) | ||
}) | ||
} |