You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
976 B

  1. package tmhash_test
  2. import (
  3. "crypto/sha256"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto/tmhash"
  8. )
  9. func TestHash(t *testing.T) {
  10. testVector := []byte("abc")
  11. hasher := tmhash.New()
  12. _, err := hasher.Write(testVector)
  13. require.NoError(t, err)
  14. bz := hasher.Sum(nil)
  15. bz2 := tmhash.Sum(testVector)
  16. hasher = sha256.New()
  17. _, err = hasher.Write(testVector)
  18. require.NoError(t, err)
  19. bz3 := hasher.Sum(nil)
  20. assert.Equal(t, bz, bz2)
  21. assert.Equal(t, bz, bz3)
  22. }
  23. func TestHashTruncated(t *testing.T) {
  24. testVector := []byte("abc")
  25. hasher := tmhash.NewTruncated()
  26. _, err := hasher.Write(testVector)
  27. require.NoError(t, err)
  28. bz := hasher.Sum(nil)
  29. bz2 := tmhash.SumTruncated(testVector)
  30. hasher = sha256.New()
  31. _, err = hasher.Write(testVector)
  32. require.NoError(t, err)
  33. bz3 := hasher.Sum(nil)
  34. bz3 = bz3[:tmhash.TruncatedSize]
  35. assert.Equal(t, bz, bz2)
  36. assert.Equal(t, bz, bz3)
  37. }