test message signing

This commit is contained in:
vyzo 2018-08-27 10:53:52 +03:00
parent 60a06ce961
commit 1ef82b2cee
1 changed files with 42 additions and 0 deletions

42
sign_test.go Normal file
View File

@ -0,0 +1,42 @@
package floodsub
import (
"testing"
pb "github.com/libp2p/go-floodsub/pb"
crypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
)
func TestSigning(t *testing.T) {
privk, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
t.Fatal(err)
}
testSignVerify(t, privk)
privk, _, err = crypto.GenerateKeyPair(crypto.Ed25519, 0)
if err != nil {
t.Fatal(err)
}
testSignVerify(t, privk)
}
func testSignVerify(t *testing.T, privk crypto.PrivKey) {
id, err := peer.IDFromPublicKey(privk.GetPublic())
if err != nil {
t.Fatal(err)
}
m := pb.Message{
Data: []byte("abc"),
TopicIDs: []string{"foo"},
From: []byte(id),
Seqno: []byte("123"),
}
signMessage(privk, &m)
err = verifyMessageSignature(&m)
if err != nil {
t.Fatal(err)
}
}