Add extra test for encoding with sig,for p2p test fail
This commit is contained in:
parent
aecc32d363
commit
c410fc5e24
|
@ -16,13 +16,15 @@ type byter interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// go to wire encoding and back
|
// go to wire encoding and back
|
||||||
func checkWire(t *testing.T, in byter, reader interface{}, typ byte) {
|
func checkWire(t *testing.T, in byter, reader interface{}, typ byte, size int) {
|
||||||
// test to and from binary
|
// test to and from binary
|
||||||
bin, err := data.ToWire(in)
|
bin, err := data.ToWire(in)
|
||||||
require.Nil(t, err, "%+v", err)
|
require.Nil(t, err, "%+v", err)
|
||||||
assert.Equal(t, typ, bin[0])
|
assert.Equal(t, typ, bin[0])
|
||||||
// make sure this is compatible with current (Bytes()) encoding
|
// make sure this is compatible with current (Bytes()) encoding
|
||||||
assert.Equal(t, in.Bytes(), bin)
|
assert.Equal(t, in.Bytes(), bin)
|
||||||
|
// make sure we have the expected length
|
||||||
|
assert.Equal(t, size, len(bin))
|
||||||
|
|
||||||
err = data.FromWire(bin, reader)
|
err = data.FromWire(bin, reader)
|
||||||
require.Nil(t, err, "%+v", err)
|
require.Nil(t, err, "%+v", err)
|
||||||
|
@ -67,23 +69,29 @@ func TestKeyEncodings(t *testing.T) {
|
||||||
privKey PrivKey
|
privKey PrivKey
|
||||||
keyType byte
|
keyType byte
|
||||||
keyName string
|
keyName string
|
||||||
|
// 1 (type byte) + size of byte array
|
||||||
|
privSize, pubSize int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
privKey: GenPrivKeyEd25519().Wrap(),
|
privKey: GenPrivKeyEd25519().Wrap(),
|
||||||
keyType: TypeEd25519,
|
keyType: TypeEd25519,
|
||||||
keyName: NameEd25519,
|
keyName: NameEd25519,
|
||||||
|
privSize: 65,
|
||||||
|
pubSize: 33,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
privKey: GenPrivKeySecp256k1().Wrap(),
|
privKey: GenPrivKeySecp256k1().Wrap(),
|
||||||
keyType: TypeSecp256k1,
|
keyType: TypeSecp256k1,
|
||||||
keyName: NameSecp256k1,
|
keyName: NameSecp256k1,
|
||||||
|
privSize: 33,
|
||||||
|
pubSize: 34,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
// check (de/en)codings of private key
|
// check (de/en)codings of private key
|
||||||
var priv2, priv3, priv4 PrivKey
|
var priv2, priv3, priv4 PrivKey
|
||||||
checkWire(t, tc.privKey, &priv2, tc.keyType)
|
checkWire(t, tc.privKey, &priv2, tc.keyType, tc.privSize)
|
||||||
assert.EqualValues(t, tc.privKey, priv2)
|
assert.EqualValues(t, tc.privKey, priv2)
|
||||||
checkJSON(t, tc.privKey, &priv3, tc.keyName)
|
checkJSON(t, tc.privKey, &priv3, tc.keyName)
|
||||||
assert.EqualValues(t, tc.privKey, priv3)
|
assert.EqualValues(t, tc.privKey, priv3)
|
||||||
|
@ -93,7 +101,7 @@ func TestKeyEncodings(t *testing.T) {
|
||||||
// check (de/en)codings of public key
|
// check (de/en)codings of public key
|
||||||
pubKey := tc.privKey.PubKey()
|
pubKey := tc.privKey.PubKey()
|
||||||
var pub2, pub3, pub4 PubKey
|
var pub2, pub3, pub4 PubKey
|
||||||
checkWire(t, pubKey, &pub2, tc.keyType)
|
checkWire(t, pubKey, &pub2, tc.keyType, tc.pubSize)
|
||||||
assert.EqualValues(t, pubKey, pub2)
|
assert.EqualValues(t, pubKey, pub2)
|
||||||
checkJSON(t, pubKey, &pub3, tc.keyName)
|
checkJSON(t, pubKey, &pub3, tc.keyName)
|
||||||
assert.EqualValues(t, pubKey, pub3)
|
assert.EqualValues(t, pubKey, pub3)
|
||||||
|
@ -126,3 +134,50 @@ func TestNilEncodings(t *testing.T) {
|
||||||
assert.EqualValues(t, e, f)
|
assert.EqualValues(t, e, f)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SigMessage struct {
|
||||||
|
Key PubKey
|
||||||
|
Sig Signature
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s SigMessage) Bytes() []byte {
|
||||||
|
return wire.BinaryBytes(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmbededWireEncodings(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
privKey PrivKey
|
||||||
|
keyType byte
|
||||||
|
keyName string
|
||||||
|
size int // pub + sig size
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
privKey: GenPrivKeyEd25519().Wrap(),
|
||||||
|
keyType: TypeEd25519,
|
||||||
|
keyName: NameEd25519,
|
||||||
|
size: 2 + 32 + 64,
|
||||||
|
},
|
||||||
|
// {
|
||||||
|
// privKey: GenPrivKeySecp256k1().Wrap(),
|
||||||
|
// keyType: TypeSecp256k1,
|
||||||
|
// keyName: NameSecp256k1,
|
||||||
|
// size: 2 + 33 + 72, // ugh, either 72 or 73 depending....
|
||||||
|
// },
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := randBytes(20)
|
||||||
|
for i, tc := range cases {
|
||||||
|
pubKey := tc.privKey.PubKey()
|
||||||
|
sig := tc.privKey.Sign(payload)
|
||||||
|
assert.True(pubKey.VerifyBytes(payload, sig), "%d", i)
|
||||||
|
|
||||||
|
msg := SigMessage{
|
||||||
|
Key: pubKey,
|
||||||
|
Sig: sig,
|
||||||
|
}
|
||||||
|
var msg2 SigMessage
|
||||||
|
checkWire(t, msg, &msg2, tc.keyType, tc.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue