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.

107 lines
2.7 KiB

  1. package crypto
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. )
  7. type byter interface {
  8. Bytes() []byte
  9. }
  10. func checkAminoBinary(t *testing.T, src byter, dst interface{}, size int) {
  11. // Marshal to binary bytes.
  12. bz, err := cdc.MarshalBinaryBare(src)
  13. require.Nil(t, err, "%+v", err)
  14. // Make sure this is compatible with current (Bytes()) encoding.
  15. assert.Equal(t, src.Bytes(), bz, "Amino binary vs Bytes() mismatch")
  16. // Make sure we have the expected length.
  17. if size != -1 {
  18. assert.Equal(t, size, len(bz), "Amino binary size mismatch")
  19. }
  20. // Unmarshal.
  21. err = cdc.UnmarshalBinaryBare(bz, dst)
  22. require.Nil(t, err, "%+v", err)
  23. }
  24. func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool) {
  25. // Marshal to JSON bytes.
  26. js, err := cdc.MarshalJSON(src)
  27. require.Nil(t, err, "%+v", err)
  28. if isNil {
  29. assert.Equal(t, string(js), `null`)
  30. } else {
  31. assert.Contains(t, string(js), `"type":`)
  32. assert.Contains(t, string(js), `"value":`)
  33. }
  34. // Unmarshal.
  35. err = cdc.UnmarshalJSON(js, dst)
  36. require.Nil(t, err, "%+v", err)
  37. }
  38. func TestKeyEncodings(t *testing.T) {
  39. cases := []struct {
  40. privKey PrivKey
  41. privSize, pubSize int // binary sizes
  42. }{
  43. {
  44. privKey: GenPrivKeyEd25519(),
  45. privSize: 69,
  46. pubSize: 37,
  47. },
  48. {
  49. privKey: GenPrivKeySecp256k1(),
  50. privSize: 37,
  51. pubSize: 38,
  52. },
  53. }
  54. for _, tc := range cases {
  55. // Check (de/en)codings of PrivKeys.
  56. var priv2, priv3 PrivKey
  57. checkAminoBinary(t, tc.privKey, &priv2, tc.privSize)
  58. assert.EqualValues(t, tc.privKey, priv2)
  59. checkAminoJSON(t, tc.privKey, &priv3, false) // TODO also check Prefix bytes.
  60. assert.EqualValues(t, tc.privKey, priv3)
  61. // Check (de/en)codings of Signatures.
  62. var sig1, sig2, sig3 Signature
  63. sig1, err := tc.privKey.Sign([]byte("something"))
  64. assert.NoError(t, err)
  65. checkAminoBinary(t, sig1, &sig2, -1) // Siganture size changes for Secp anyways.
  66. assert.EqualValues(t, sig1, sig2)
  67. checkAminoJSON(t, sig1, &sig3, false) // TODO also check Prefix bytes.
  68. assert.EqualValues(t, sig1, sig3)
  69. // Check (de/en)codings of PubKeys.
  70. pubKey, err := tc.privKey.PubKey()
  71. assert.NoError(t, err)
  72. var pub2, pub3 PubKey
  73. checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
  74. assert.EqualValues(t, pubKey, pub2)
  75. checkAminoJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
  76. assert.EqualValues(t, pubKey, pub3)
  77. }
  78. }
  79. func TestNilEncodings(t *testing.T) {
  80. // Check nil Signature.
  81. var a, b Signature
  82. checkAminoJSON(t, &a, &b, true)
  83. assert.EqualValues(t, a, b)
  84. // Check nil PubKey.
  85. var c, d PubKey
  86. checkAminoJSON(t, &c, &d, true)
  87. assert.EqualValues(t, c, d)
  88. // Check nil PrivKey.
  89. var e, f PrivKey
  90. checkAminoJSON(t, &e, &f, true)
  91. assert.EqualValues(t, e, f)
  92. }