8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 wire "github.com/tendermint/go-wire"
11 data "github.com/tendermint/go-wire/data"
14 type byter interface {
18 // go to wire encoding and back
19 func checkWire(t *testing.T, in byter, reader interface{}, typ byte, size int) {
20 // test to and from binary
21 bin, err := data.ToWire(in)
22 require.Nil(t, err, "%+v", err)
23 assert.Equal(t, typ, bin[0])
24 // make sure this is compatible with current (Bytes()) encoding
25 assert.Equal(t, in.Bytes(), bin)
26 // make sure we have the expected length
27 assert.Equal(t, size, len(bin))
29 err = data.FromWire(bin, reader)
30 require.Nil(t, err, "%+v", err)
33 // go to json encoding and back
34 func checkJSON(t *testing.T, in interface{}, reader interface{}, typ string) {
35 // test to and from binary
36 js, err := data.ToJSON(in)
37 require.Nil(t, err, "%+v", err)
38 styp := `"` + typ + `"`
39 assert.True(t, strings.Contains(string(js), styp))
41 err = data.FromJSON(js, reader)
42 require.Nil(t, err, "%+v", err)
44 // also check text format
45 text, err := data.ToText(in)
46 require.Nil(t, err, "%+v", err)
47 parts := strings.Split(text, ":")
48 require.Equal(t, 2, len(parts))
49 // make sure the first part is the typ string
50 assert.Equal(t, typ, parts[0])
51 // and the data is also present in the json
52 assert.True(t, strings.Contains(string(js), parts[1]))
55 // make sure go-wire json can still figure this out...
56 func checkWireJSON(t *testing.T, in interface{}, reader interface{}, typ byte) {
57 // test to and from binary
59 js := wire.JSONBytes(in)
60 btyp := fmt.Sprintf("[%d,", typ)
61 assert.True(t, strings.HasPrefix(string(js), btyp), string(js), btyp)
63 wire.ReadJSON(reader, js, &err)
64 require.Nil(t, err, "%+v", err)
67 func TestKeyEncodings(t *testing.T) {
72 // 1 (type byte) + size of byte array
76 privKey: GenPrivKeyEd25519().Wrap(),
83 privKey: GenPrivKeySecp256k1().Wrap(),
84 keyType: TypeSecp256k1,
85 keyName: NameSecp256k1,
91 for _, tc := range cases {
92 // check (de/en)codings of private key
93 var priv2, priv3, priv4 PrivKey
94 checkWire(t, tc.privKey, &priv2, tc.keyType, tc.privSize)
95 assert.EqualValues(t, tc.privKey, priv2)
96 checkJSON(t, tc.privKey, &priv3, tc.keyName)
97 assert.EqualValues(t, tc.privKey, priv3)
98 checkWireJSON(t, tc.privKey, &priv4, tc.keyType)
99 assert.EqualValues(t, tc.privKey, priv4)
101 // check (de/en)codings of public key
102 pubKey := tc.privKey.PubKey()
103 var pub2, pub3, pub4 PubKey
104 checkWire(t, pubKey, &pub2, tc.keyType, tc.pubSize)
105 assert.EqualValues(t, pubKey, pub2)
106 checkJSON(t, pubKey, &pub3, tc.keyName)
107 assert.EqualValues(t, pubKey, pub3)
108 checkWireJSON(t, pubKey, &pub4, tc.keyType)
109 assert.EqualValues(t, pubKey, pub4)
113 func toFromJSON(t *testing.T, in interface{}, recvr interface{}) {
114 js, err := data.ToJSON(in)
115 require.Nil(t, err, "%+v", err)
116 err = data.FromJSON(js, recvr)
117 require.Nil(t, err, "%+v", err)
120 func TestNilEncodings(t *testing.T) {
121 // make sure sigs are okay with nil
124 assert.EqualValues(t, a, b)
126 // make sure sigs are okay with nil
129 assert.EqualValues(t, c, d)
131 // make sure sigs are okay with nil
134 assert.EqualValues(t, e, f)
138 type SigMessage struct {
143 func (s SigMessage) Bytes() []byte {
144 return wire.BinaryBytes(s)
147 func TestEmbededWireEncodings(t *testing.T) {
148 assert := assert.New(t)
154 size int // pub + sig size
157 privKey: GenPrivKeyEd25519().Wrap(),
158 keyType: TypeEd25519,
159 keyName: NameEd25519,
163 // privKey: GenPrivKeySecp256k1().Wrap(),
164 // keyType: TypeSecp256k1,
165 // keyName: NameSecp256k1,
166 // size: 2 + 33 + 72, // ugh, either 72 or 73 depending....
170 payload := randBytes(20)
171 for i, tc := range cases {
172 pubKey := tc.privKey.PubKey()
173 sig := tc.privKey.Sign(payload)
174 assert.True(pubKey.VerifyBytes(payload, sig), "%d", i)
181 checkWire(t, msg, &msg2, tc.keyType, tc.size)