2014-12-09 18:49:04 -08:00
|
|
|
package account
|
2014-10-03 17:59:54 -07:00
|
|
|
|
|
|
|
import (
|
2015-01-03 20:24:02 -08:00
|
|
|
"bytes"
|
2014-10-03 17:59:54 -07:00
|
|
|
"testing"
|
2015-01-03 20:24:02 -08:00
|
|
|
|
2015-06-09 23:17:19 -04:00
|
|
|
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519"
|
2015-07-25 15:45:45 -07:00
|
|
|
"github.com/tendermint/tendermint/wire"
|
2015-04-01 17:30:16 -07:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2014-10-03 17:59:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSignAndValidate(t *testing.T) {
|
|
|
|
|
|
|
|
privAccount := GenPrivAccount()
|
2014-12-17 01:37:13 -08:00
|
|
|
pubKey := privAccount.PubKey
|
|
|
|
privKey := privAccount.PrivKey
|
2014-10-03 17:59:54 -07:00
|
|
|
|
2014-10-04 19:16:49 -07:00
|
|
|
msg := CRandBytes(128)
|
2014-12-17 01:37:13 -08:00
|
|
|
sig := privKey.Sign(msg)
|
2014-10-03 17:59:54 -07:00
|
|
|
t.Logf("msg: %X, sig: %X", msg, sig)
|
|
|
|
|
|
|
|
// Test the signature
|
2014-12-17 01:37:13 -08:00
|
|
|
if !pubKey.VerifyBytes(msg, sig) {
|
2014-10-03 17:59:54 -07:00
|
|
|
t.Errorf("Account message signature verification failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutate the signature, just one bit.
|
2015-07-17 17:19:16 -04:00
|
|
|
sigEd := sig.(SignatureEd25519)
|
|
|
|
sigEd[0] ^= byte(0x01)
|
|
|
|
sig = Signature(sigEd)
|
2014-10-03 17:59:54 -07:00
|
|
|
|
2014-12-17 01:37:13 -08:00
|
|
|
if pubKey.VerifyBytes(msg, sig) {
|
2014-10-03 17:59:54 -07:00
|
|
|
t.Errorf("Account message signature verification should have failed but passed instead")
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 20:24:02 -08:00
|
|
|
|
|
|
|
func TestBinaryDecode(t *testing.T) {
|
|
|
|
|
|
|
|
privAccount := GenPrivAccount()
|
|
|
|
pubKey := privAccount.PubKey
|
|
|
|
privKey := privAccount.PrivKey
|
|
|
|
|
|
|
|
msg := CRandBytes(128)
|
|
|
|
sig := privKey.Sign(msg)
|
|
|
|
t.Logf("msg: %X, sig: %X", msg, sig)
|
|
|
|
|
|
|
|
buf, n, err := new(bytes.Buffer), new(int64), new(error)
|
2015-07-25 15:45:45 -07:00
|
|
|
wire.WriteBinary(sig, buf, n, err)
|
2015-01-03 20:24:02 -08:00
|
|
|
if *err != nil {
|
|
|
|
t.Fatalf("Failed to write Signature: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-07-17 17:19:16 -04:00
|
|
|
if len(buf.Bytes()) != ed25519.SignatureSize+1 {
|
|
|
|
// 1 byte TypeByte, 64 bytes signature bytes
|
2015-01-03 20:24:02 -08:00
|
|
|
t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
|
|
|
|
}
|
|
|
|
if buf.Bytes()[0] != SignatureTypeEd25519 {
|
|
|
|
t.Fatalf("Unexpected signature type byte")
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:45:45 -07:00
|
|
|
sig2, ok := wire.ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
|
2015-01-03 20:24:02 -08:00
|
|
|
if !ok || *err != nil {
|
|
|
|
t.Fatalf("Failed to read Signature: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the signature
|
|
|
|
if !pubKey.VerifyBytes(msg, sig2) {
|
|
|
|
t.Errorf("Account message signature verification failed")
|
|
|
|
}
|
|
|
|
}
|