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.8 KiB

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