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.

118 lines
3.3 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. //| SignatureEd25519 | tendermint/SignatureEd25519 | 0x2031EA53 | 0x40 | |
  48. //| SignatureSecp256k1 | tendermint/SignatureSecp256k1 | 0x7FC4A495 | variable | |
  49. }
  50. func TestKeyEncodings(t *testing.T) {
  51. cases := []struct {
  52. privKey PrivKey
  53. privSize, pubSize int // binary sizes
  54. }{
  55. {
  56. privKey: GenPrivKeyEd25519(),
  57. privSize: 69,
  58. pubSize: 37,
  59. },
  60. {
  61. privKey: GenPrivKeySecp256k1(),
  62. privSize: 37,
  63. pubSize: 38,
  64. },
  65. }
  66. for _, tc := range cases {
  67. // Check (de/en)codings of PrivKeys.
  68. var priv2, priv3 PrivKey
  69. checkAminoBinary(t, tc.privKey, &priv2, tc.privSize)
  70. assert.EqualValues(t, tc.privKey, priv2)
  71. checkAminoJSON(t, tc.privKey, &priv3, false) // TODO also check Prefix bytes.
  72. assert.EqualValues(t, tc.privKey, priv3)
  73. // Check (de/en)codings of Signatures.
  74. var sig1, sig2, sig3 Signature
  75. sig1 = tc.privKey.Sign([]byte("something"))
  76. checkAminoBinary(t, sig1, &sig2, -1) // Siganture size changes for Secp anyways.
  77. assert.EqualValues(t, sig1, sig2)
  78. checkAminoJSON(t, sig1, &sig3, false) // TODO also check Prefix bytes.
  79. assert.EqualValues(t, sig1, sig3)
  80. // Check (de/en)codings of PubKeys.
  81. pubKey := tc.privKey.PubKey()
  82. var pub2, pub3 PubKey
  83. checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
  84. assert.EqualValues(t, pubKey, pub2)
  85. checkAminoJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
  86. assert.EqualValues(t, pubKey, pub3)
  87. }
  88. }
  89. func TestNilEncodings(t *testing.T) {
  90. // Check nil Signature.
  91. var a, b Signature
  92. checkAminoJSON(t, &a, &b, true)
  93. assert.EqualValues(t, a, b)
  94. // Check nil PubKey.
  95. var c, d PubKey
  96. checkAminoJSON(t, &c, &d, true)
  97. assert.EqualValues(t, c, d)
  98. // Check nil PrivKey.
  99. var e, f PrivKey
  100. checkAminoJSON(t, &e, &f, true)
  101. assert.EqualValues(t, e, f)
  102. }