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.

119 lines
3.3 KiB

6 years ago
  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, err := tc.privKey.Sign([]byte("something"))
  76. assert.NoError(t, err)
  77. checkAminoBinary(t, sig1, &sig2, -1) // Signature size changes for Secp anyways.
  78. assert.EqualValues(t, sig1, sig2)
  79. checkAminoJSON(t, sig1, &sig3, false) // TODO also check Prefix bytes.
  80. assert.EqualValues(t, sig1, sig3)
  81. // Check (de/en)codings of PubKeys.
  82. pubKey := tc.privKey.PubKey()
  83. var pub2, pub3 PubKey
  84. checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
  85. assert.EqualValues(t, pubKey, pub2)
  86. checkAminoJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
  87. assert.EqualValues(t, pubKey, pub3)
  88. }
  89. }
  90. func TestNilEncodings(t *testing.T) {
  91. // Check nil Signature.
  92. var a, b Signature
  93. checkAminoJSON(t, &a, &b, true)
  94. assert.EqualValues(t, a, b)
  95. // Check nil PubKey.
  96. var c, d PubKey
  97. checkAminoJSON(t, &c, &d, true)
  98. assert.EqualValues(t, c, d)
  99. // Check nil PrivKey.
  100. var e, f PrivKey
  101. checkAminoJSON(t, &e, &f, true)
  102. assert.EqualValues(t, e, f)
  103. }