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.

128 lines
3.2 KiB

  1. package crypto
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. data "github.com/tendermint/go-data"
  9. wire "github.com/tendermint/go-wire"
  10. )
  11. type byter interface {
  12. Bytes() []byte
  13. }
  14. // go to wire encoding and back
  15. func checkWire(t *testing.T, in byter, reader interface{}, typ byte) {
  16. // test to and from binary
  17. bin, err := data.ToWire(in)
  18. require.Nil(t, err, "%+v", err)
  19. assert.Equal(t, typ, bin[0])
  20. // make sure this is compatible with current (Bytes()) encoding
  21. assert.Equal(t, in.Bytes(), bin)
  22. err = data.FromWire(bin, reader)
  23. require.Nil(t, err, "%+v", err)
  24. }
  25. // go to json encoding and back
  26. func checkJSON(t *testing.T, in interface{}, reader interface{}, typ string) {
  27. // test to and from binary
  28. js, err := data.ToJSON(in)
  29. require.Nil(t, err, "%+v", err)
  30. styp := `"` + typ + `"`
  31. assert.True(t, strings.Contains(string(js), styp))
  32. err = data.FromJSON(js, reader)
  33. require.Nil(t, err, "%+v", err)
  34. // also check text format
  35. text, err := data.ToText(in)
  36. require.Nil(t, err, "%+v", err)
  37. parts := strings.Split(text, ":")
  38. require.Equal(t, 2, len(parts))
  39. // make sure the first part is the typ string
  40. assert.Equal(t, typ, parts[0])
  41. // and the data is also present in the json
  42. assert.True(t, strings.Contains(string(js), parts[1]))
  43. }
  44. // make sure go-wire json can still figure this out...
  45. func checkWireJSON(t *testing.T, in interface{}, reader interface{}, typ byte) {
  46. // test to and from binary
  47. var err error
  48. js := wire.JSONBytes(in)
  49. btyp := fmt.Sprintf("[%d,", typ)
  50. assert.True(t, strings.HasPrefix(string(js), btyp), string(js))
  51. wire.ReadJSON(reader, js, &err)
  52. require.Nil(t, err, "%+v", err)
  53. }
  54. func TestKeyEncodings(t *testing.T) {
  55. cases := []struct {
  56. privKey PrivKey
  57. keyType byte
  58. keyName string
  59. }{
  60. {
  61. privKey: GenPrivKeyEd25519().Wrap(),
  62. keyType: TypeEd25519,
  63. keyName: NameEd25519,
  64. },
  65. {
  66. privKey: GenPrivKeySecp256k1().Wrap(),
  67. keyType: TypeSecp256k1,
  68. keyName: NameSecp256k1,
  69. },
  70. }
  71. for _, tc := range cases {
  72. // check (de/en)codings of private key
  73. var priv2, priv3, priv4 PrivKey
  74. checkWire(t, tc.privKey, &priv2, tc.keyType)
  75. assert.EqualValues(t, tc.privKey, priv2)
  76. checkJSON(t, tc.privKey, &priv3, tc.keyName)
  77. assert.EqualValues(t, tc.privKey, priv3)
  78. checkWireJSON(t, tc.privKey, &priv4, tc.keyType)
  79. assert.EqualValues(t, tc.privKey, priv4)
  80. // check (de/en)codings of public key
  81. pubKey := tc.privKey.PubKey()
  82. var pub2, pub3, pub4 PubKey
  83. checkWire(t, pubKey, &pub2, tc.keyType)
  84. assert.EqualValues(t, pubKey, pub2)
  85. checkJSON(t, pubKey, &pub3, tc.keyName)
  86. assert.EqualValues(t, pubKey, pub3)
  87. checkWireJSON(t, pubKey, &pub4, tc.keyType)
  88. assert.EqualValues(t, pubKey, pub4)
  89. }
  90. }
  91. func toFromJSON(t *testing.T, in interface{}, recvr interface{}) {
  92. js, err := data.ToJSON(in)
  93. require.Nil(t, err, "%+v", err)
  94. err = data.FromJSON(js, recvr)
  95. require.Nil(t, err, "%+v", err)
  96. }
  97. func TestNilEncodings(t *testing.T) {
  98. // make sure sigs are okay with nil
  99. var a, b Signature
  100. toFromJSON(t, a, &b)
  101. assert.EqualValues(t, a, b)
  102. // make sure sigs are okay with nil
  103. var c, d PubKey
  104. toFromJSON(t, c, &d)
  105. assert.EqualValues(t, c, d)
  106. // make sure sigs are okay with nil
  107. var e, f PrivKey
  108. toFromJSON(t, e, &f)
  109. assert.EqualValues(t, e, f)
  110. }