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.

129 lines
3.7 KiB

  1. package cryptoAmino
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. "github.com/tendermint/tendermint/crypto/secp256k1"
  10. )
  11. type byter interface {
  12. Bytes() []byte
  13. }
  14. func checkAminoBinary(t *testing.T, src, dst interface{}, size int) {
  15. // Marshal to binary bytes.
  16. bz, err := cdc.MarshalBinaryBare(src)
  17. require.Nil(t, err, "%+v", err)
  18. if byterSrc, ok := src.(byter); ok {
  19. // Make sure this is compatible with current (Bytes()) encoding.
  20. assert.Equal(t, byterSrc.Bytes(), bz, "Amino binary vs Bytes() mismatch")
  21. }
  22. // Make sure we have the expected length.
  23. if size != -1 {
  24. assert.Equal(t, size, len(bz), "Amino binary size mismatch")
  25. }
  26. // Unmarshal.
  27. err = cdc.UnmarshalBinaryBare(bz, dst)
  28. require.Nil(t, err, "%+v", err)
  29. }
  30. func checkAminoJSON(t *testing.T, src interface{}, dst interface{}, isNil bool) {
  31. // Marshal to JSON bytes.
  32. js, err := cdc.MarshalJSON(src)
  33. require.Nil(t, err, "%+v", err)
  34. if isNil {
  35. assert.Equal(t, string(js), `null`)
  36. } else {
  37. assert.Contains(t, string(js), `"type":`)
  38. assert.Contains(t, string(js), `"value":`)
  39. }
  40. // Unmarshal.
  41. err = cdc.UnmarshalJSON(js, dst)
  42. require.Nil(t, err, "%+v", err)
  43. }
  44. func ExamplePrintRegisteredTypes() {
  45. cdc.PrintTypes(os.Stdout)
  46. // Output: | Type | Name | Prefix | Length | Notes |
  47. //| ---- | ---- | ------ | ----- | ------ |
  48. //| PubKeyEd25519 | tendermint/PubKeyEd25519 | 0x1624DE64 | 0x20 | |
  49. //| PubKeySecp256k1 | tendermint/PubKeySecp256k1 | 0xEB5AE987 | 0x21 | |
  50. //| PubKeyMultisigThreshold | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable | |
  51. //| PrivKeyEd25519 | tendermint/PrivKeyEd25519 | 0xA3288910 | 0x40 | |
  52. //| PrivKeySecp256k1 | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | 0x20 | |
  53. }
  54. func TestKeyEncodings(t *testing.T) {
  55. cases := []struct {
  56. privKey crypto.PrivKey
  57. privSize, pubSize, sigSize int // binary sizes
  58. }{
  59. {
  60. privKey: ed25519.GenPrivKey(),
  61. privSize: 69,
  62. pubSize: 37,
  63. sigSize: 65,
  64. },
  65. {
  66. privKey: secp256k1.GenPrivKey(),
  67. privSize: 37,
  68. pubSize: 38,
  69. sigSize: 65,
  70. },
  71. }
  72. for tcIndex, tc := range cases {
  73. // Check (de/en)codings of PrivKeys.
  74. var priv2, priv3 crypto.PrivKey
  75. checkAminoBinary(t, tc.privKey, &priv2, tc.privSize)
  76. assert.EqualValues(t, tc.privKey, priv2, "tc #%d", tcIndex)
  77. checkAminoJSON(t, tc.privKey, &priv3, false) // TODO also check Prefix bytes.
  78. assert.EqualValues(t, tc.privKey, priv3, "tc #%d", tcIndex)
  79. // Check (de/en)codings of Signatures.
  80. var sig1, sig2 []byte
  81. sig1, err := tc.privKey.Sign([]byte("something"))
  82. assert.NoError(t, err, "tc #%d", tcIndex)
  83. checkAminoBinary(t, sig1, &sig2, tc.sigSize)
  84. assert.EqualValues(t, sig1, sig2, "tc #%d", tcIndex)
  85. // Check (de/en)codings of PubKeys.
  86. pubKey := tc.privKey.PubKey()
  87. var pub2, pub3 crypto.PubKey
  88. checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
  89. assert.EqualValues(t, pubKey, pub2, "tc #%d", tcIndex)
  90. checkAminoJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
  91. assert.EqualValues(t, pubKey, pub3, "tc #%d", tcIndex)
  92. }
  93. }
  94. func TestNilEncodings(t *testing.T) {
  95. // Check nil Signature.
  96. var a, b []byte
  97. checkAminoJSON(t, &a, &b, true)
  98. assert.EqualValues(t, a, b)
  99. // Check nil PubKey.
  100. var c, d crypto.PubKey
  101. checkAminoJSON(t, &c, &d, true)
  102. assert.EqualValues(t, c, d)
  103. // Check nil PrivKey.
  104. var e, f crypto.PrivKey
  105. checkAminoJSON(t, &e, &f, true)
  106. assert.EqualValues(t, e, f)
  107. }
  108. func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
  109. pk, err := PubKeyFromBytes([]byte("foo"))
  110. require.NotNil(t, err, "expecting a non-nil error")
  111. require.Nil(t, pk, "expecting an empty public key on error")
  112. }