2018-08-28 03:01:08 +00:00
|
|
|
package pubsub
|
2018-08-27 07:53:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-08-28 03:01:08 +00:00
|
|
|
pb "github.com/libp2p/go-libp2p-pubsub/pb"
|
2018-08-27 07:53:52 +00:00
|
|
|
|
2019-05-26 16:19:03 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2018-08-27 07:53:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"),
|
|
|
|
}
|
2018-10-13 23:16:41 +00:00
|
|
|
signMessage(id, privk, &m)
|
2018-08-27 07:53:52 +00:00
|
|
|
err = verifyMessageSignature(&m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|