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.

112 lines
2.7 KiB

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