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.

126 lines
3.6 KiB

6 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, 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. //| PrivKeyEd25519 | tendermint/PrivKeyEd25519 | 0xA3288910 | 0x40 | |
  51. //| PrivKeySecp256k1 | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | 0x20 | |
  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 []byte
  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. // Check (de/en)codings of PubKeys.
  83. pubKey := tc.privKey.PubKey()
  84. var pub2, pub3 crypto.PubKey
  85. checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
  86. assert.EqualValues(t, pubKey, pub2, "tc #%d", tcIndex)
  87. checkAminoJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
  88. assert.EqualValues(t, pubKey, pub3, "tc #%d", tcIndex)
  89. }
  90. }
  91. func TestNilEncodings(t *testing.T) {
  92. // Check nil Signature.
  93. var a, b []byte
  94. checkAminoJSON(t, &a, &b, true)
  95. assert.EqualValues(t, a, b)
  96. // Check nil PubKey.
  97. var c, d crypto.PubKey
  98. checkAminoJSON(t, &c, &d, true)
  99. assert.EqualValues(t, c, d)
  100. // Check nil PrivKey.
  101. var e, f crypto.PrivKey
  102. checkAminoJSON(t, &e, &f, true)
  103. assert.EqualValues(t, e, f)
  104. }
  105. func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
  106. pk, err := PubKeyFromBytes([]byte("foo"))
  107. require.NotNil(t, err, "expecting a non-nil error")
  108. require.Nil(t, pk, "expecting an empty public key on error")
  109. }