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