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.

65 lines
1.1 KiB

  1. package crypto
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. wire "github.com/tendermint/go-wire"
  8. )
  9. type BadKey struct {
  10. PrivKeyEd25519
  11. }
  12. // Wrap fulfils interface for PrivKey struct
  13. func (pk BadKey) Wrap() PrivKey {
  14. return PrivKey{pk}
  15. }
  16. func (pk BadKey) Bytes() []byte {
  17. return wire.BinaryBytes(pk.Wrap())
  18. }
  19. func (pk BadKey) ValidateKey() error {
  20. return fmt.Errorf("fuggly key")
  21. }
  22. func init() {
  23. PrivKeyMapper.
  24. RegisterImplementation(BadKey{}, "bad", 0x66)
  25. }
  26. func TestReadPrivKey(t *testing.T) {
  27. assert, require := assert.New(t), require.New(t)
  28. // garbage in, garbage out
  29. garbage := []byte("hjgewugfbiewgofwgewr")
  30. _, err := PrivKeyFromBytes(garbage)
  31. require.Error(err)
  32. edKey := GenPrivKeyEd25519()
  33. badKey := BadKey{edKey}
  34. cases := []struct {
  35. key PrivKey
  36. valid bool
  37. }{
  38. {edKey.Wrap(), true},
  39. {badKey.Wrap(), false},
  40. }
  41. for i, tc := range cases {
  42. data := tc.key.Bytes()
  43. key, err := PrivKeyFromBytes(data)
  44. if tc.valid {
  45. assert.NoError(err, "%d", i)
  46. assert.Equal(tc.key, key, "%d", i)
  47. } else {
  48. assert.Error(err, "%d: %#v", i, key)
  49. }
  50. }
  51. }