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.

224 lines
6.6 KiB

  1. package cryptoAmino
  2. import (
  3. "os"
  4. "reflect"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. amino "github.com/tendermint/go-amino"
  9. "github.com/tendermint/tendermint/crypto"
  10. "github.com/tendermint/tendermint/crypto/ed25519"
  11. "github.com/tendermint/tendermint/crypto/multisig"
  12. "github.com/tendermint/tendermint/crypto/secp256k1"
  13. )
  14. type byter interface {
  15. Bytes() []byte
  16. }
  17. func checkAminoBinary(t *testing.T, src, dst interface{}, size int) {
  18. // Marshal to binary bytes.
  19. bz, err := cdc.MarshalBinaryBare(src)
  20. require.Nil(t, err, "%+v", err)
  21. if byterSrc, ok := src.(byter); ok {
  22. // Make sure this is compatible with current (Bytes()) encoding.
  23. assert.Equal(t, byterSrc.Bytes(), bz, "Amino binary vs Bytes() mismatch")
  24. }
  25. // Make sure we have the expected length.
  26. assert.Equal(t, size, len(bz), "Amino binary size mismatch")
  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. // ExamplePrintRegisteredTypes refers to unknown identifier: PrintRegisteredTypes
  46. //nolint:govet
  47. func ExamplePrintRegisteredTypes() {
  48. cdc.PrintTypes(os.Stdout)
  49. // Output: | Type | Name | Prefix | Length | Notes |
  50. //| ---- | ---- | ------ | ----- | ------ |
  51. //| PubKeyEd25519 | tendermint/PubKeyEd25519 | 0x1624DE64 | 0x20 | |
  52. //| PubKeySecp256k1 | tendermint/PubKeySecp256k1 | 0xEB5AE987 | 0x21 | |
  53. //| PubKeyMultisigThreshold | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable | |
  54. //| PrivKeyEd25519 | tendermint/PrivKeyEd25519 | 0xA3288910 | 0x40 | |
  55. //| PrivKeySecp256k1 | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | 0x20 | |
  56. }
  57. func TestKeyEncodings(t *testing.T) {
  58. cases := []struct {
  59. privKey crypto.PrivKey
  60. privSize, pubSize, sigSize int // binary sizes
  61. }{
  62. {
  63. privKey: ed25519.GenPrivKey(),
  64. privSize: 69,
  65. pubSize: 37,
  66. sigSize: 65,
  67. },
  68. {
  69. privKey: secp256k1.GenPrivKey(),
  70. privSize: 37,
  71. pubSize: 38,
  72. sigSize: 65,
  73. },
  74. }
  75. for tcIndex, tc := range cases {
  76. // Check (de/en)codings of PrivKeys.
  77. var priv2, priv3 crypto.PrivKey
  78. checkAminoBinary(t, tc.privKey, &priv2, tc.privSize)
  79. assert.EqualValues(t, tc.privKey, priv2, "tc #%d", tcIndex)
  80. checkAminoJSON(t, tc.privKey, &priv3, false) // TODO also check Prefix bytes.
  81. assert.EqualValues(t, tc.privKey, priv3, "tc #%d", tcIndex)
  82. // Check (de/en)codings of Signatures.
  83. var sig1, sig2 []byte
  84. sig1, err := tc.privKey.Sign([]byte("something"))
  85. assert.NoError(t, err, "tc #%d", tcIndex)
  86. checkAminoBinary(t, sig1, &sig2, tc.sigSize)
  87. assert.EqualValues(t, sig1, sig2, "tc #%d", tcIndex)
  88. // Check (de/en)codings of PubKeys.
  89. pubKey := tc.privKey.PubKey()
  90. var pub2, pub3 crypto.PubKey
  91. checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
  92. assert.EqualValues(t, pubKey, pub2, "tc #%d", tcIndex)
  93. checkAminoJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
  94. assert.EqualValues(t, pubKey, pub3, "tc #%d", tcIndex)
  95. }
  96. }
  97. func TestNilEncodings(t *testing.T) {
  98. // Check nil Signature.
  99. var a, b []byte
  100. checkAminoJSON(t, &a, &b, true)
  101. assert.EqualValues(t, a, b)
  102. // Check nil PubKey.
  103. var c, d crypto.PubKey
  104. checkAminoJSON(t, &c, &d, true)
  105. assert.EqualValues(t, c, d)
  106. // Check nil PrivKey.
  107. var e, f crypto.PrivKey
  108. checkAminoJSON(t, &e, &f, true)
  109. assert.EqualValues(t, e, f)
  110. }
  111. func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
  112. pk, err := PubKeyFromBytes([]byte("foo"))
  113. require.NotNil(t, err)
  114. require.Nil(t, pk)
  115. }
  116. func TestPubkeyAminoName(t *testing.T) {
  117. tests := []struct {
  118. key crypto.PubKey
  119. want string
  120. found bool
  121. }{
  122. {ed25519.PubKeyEd25519{}, ed25519.PubKeyAminoName, true},
  123. {secp256k1.PubKeySecp256k1{}, secp256k1.PubKeyAminoName, true},
  124. {multisig.PubKeyMultisigThreshold{}, multisig.PubKeyMultisigThresholdAminoRoute, true},
  125. }
  126. for i, tc := range tests {
  127. got, found := PubkeyAminoName(cdc, tc.key)
  128. require.Equal(t, tc.found, found, "not equal on tc %d", i)
  129. if tc.found {
  130. require.Equal(t, tc.want, got, "not equal on tc %d", i)
  131. }
  132. }
  133. }
  134. var _ crypto.PrivKey = testPriv{}
  135. var _ crypto.PubKey = testPub{}
  136. var testCdc = amino.NewCodec()
  137. type testPriv []byte
  138. func (privkey testPriv) PubKey() crypto.PubKey { return testPub{} }
  139. func (privkey testPriv) Bytes() []byte {
  140. return testCdc.MustMarshalBinaryBare(privkey)
  141. }
  142. func (privkey testPriv) Sign(msg []byte) ([]byte, error) { return []byte{}, nil }
  143. func (privkey testPriv) Equals(other crypto.PrivKey) bool { return true }
  144. type testPub []byte
  145. func (key testPub) Address() crypto.Address { return crypto.Address{} }
  146. func (key testPub) Bytes() []byte {
  147. return testCdc.MustMarshalBinaryBare(key)
  148. }
  149. func (key testPub) VerifyBytes(msg []byte, sig []byte) bool { return true }
  150. func (key testPub) Equals(other crypto.PubKey) bool { return true }
  151. var (
  152. privAminoName = "registerTest/Priv"
  153. pubAminoName = "registerTest/Pub"
  154. )
  155. func TestRegisterKeyType(t *testing.T) {
  156. RegisterAmino(testCdc)
  157. testCdc.RegisterConcrete(testPriv{}, privAminoName, nil)
  158. testCdc.RegisterConcrete(testPub{}, pubAminoName, nil)
  159. pub := testPub{0x1}
  160. priv := testPriv{0x2}
  161. // Check to make sure key cannot be decoded before registering
  162. _, err := PrivKeyFromBytes(priv.Bytes())
  163. require.Error(t, err)
  164. _, err = PubKeyFromBytes(pub.Bytes())
  165. require.Error(t, err)
  166. // Check that name is not registered
  167. _, found := PubkeyAminoName(testCdc, pub)
  168. require.False(t, found)
  169. // Register key types
  170. RegisterKeyType(testPriv{}, privAminoName)
  171. RegisterKeyType(testPub{}, pubAminoName)
  172. // Name should exist after registering
  173. name, found := PubkeyAminoName(testCdc, pub)
  174. require.True(t, found)
  175. require.Equal(t, name, pubAminoName)
  176. // Decode keys using the encoded bytes from encoding with the other codec
  177. decodedPriv, err := PrivKeyFromBytes(priv.Bytes())
  178. require.NoError(t, err)
  179. require.Equal(t, priv, decodedPriv)
  180. decodedPub, err := PubKeyFromBytes(pub.Bytes())
  181. require.NoError(t, err)
  182. require.Equal(t, pub, decodedPub)
  183. // Reset module codec after testing
  184. cdc = amino.NewCodec()
  185. nameTable = make(map[reflect.Type]string, 3)
  186. RegisterAmino(cdc)
  187. nameTable[reflect.TypeOf(ed25519.PubKeyEd25519{})] = ed25519.PubKeyAminoName
  188. nameTable[reflect.TypeOf(secp256k1.PubKeySecp256k1{})] = secp256k1.PubKeyAminoName
  189. nameTable[reflect.TypeOf(multisig.PubKeyMultisigThreshold{})] = multisig.PubKeyMultisigThresholdAminoRoute
  190. }