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.

105 lines
2.6 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 = tc.privKey.Sign([]byte("something"))
  64. checkAminoBinary(t, sig1, &sig2, -1) // Siganture size changes for Secp anyways.
  65. assert.EqualValues(t, sig1, sig2)
  66. checkAminoJSON(t, sig1, &sig3, false) // TODO also check Prefix bytes.
  67. assert.EqualValues(t, sig1, sig3)
  68. // Check (de/en)codings of PubKeys.
  69. pubKey := tc.privKey.PubKey()
  70. var pub2, pub3 PubKey
  71. checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
  72. assert.EqualValues(t, pubKey, pub2)
  73. checkAminoJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
  74. assert.EqualValues(t, pubKey, pub3)
  75. }
  76. }
  77. func TestNilEncodings(t *testing.T) {
  78. // Check nil Signature.
  79. var a, b Signature
  80. checkAminoJSON(t, &a, &b, true)
  81. assert.EqualValues(t, a, b)
  82. // Check nil PubKey.
  83. var c, d PubKey
  84. checkAminoJSON(t, &c, &d, true)
  85. assert.EqualValues(t, c, d)
  86. // Check nil PrivKey.
  87. var e, f PrivKey
  88. checkAminoJSON(t, &e, &f, true)
  89. assert.EqualValues(t, e, f)
  90. }