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.

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