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.

121 lines
3.4 KiB

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