mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-04-28 17:10:14 +08:00
use test helpers
This commit is contained in:
parent
08da615203
commit
8f13c6042b
@ -13,16 +13,12 @@ import (
|
||||
// Make an envelope, verify & open it, marshal & unmarshal it
|
||||
func TestEnvelopeHappyPath(t *testing.T) {
|
||||
priv, pub, err := test.RandTestKeyPair(Ed25519, 256)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
payload := []byte("happy hacking")
|
||||
domain := "libp2p-testing"
|
||||
payloadType := []byte("/libp2p/testdata")
|
||||
envelope, err := MakeEnvelope(priv, domain, payloadType, payload)
|
||||
if err != nil {
|
||||
t.Errorf("error constructing envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
|
||||
if !envelope.PublicKey().Equals(pub) {
|
||||
t.Error("envelope has unexpected public key")
|
||||
@ -33,13 +29,9 @@ func TestEnvelopeHappyPath(t *testing.T) {
|
||||
}
|
||||
|
||||
serialized, err := envelope.Marshal()
|
||||
if err != nil {
|
||||
t.Errorf("error serializing envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
deserialized, err := OpenEnvelope(serialized, domain)
|
||||
if err != nil {
|
||||
t.Errorf("error deserializing envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
|
||||
if bytes.Compare(deserialized.Payload(), payload) != 0 {
|
||||
t.Error("payload of envelope does not match input")
|
||||
@ -58,74 +50,54 @@ func TestMakeEnvelopeFailsWithEmptyDomain(t *testing.T) {
|
||||
payload := []byte("happy hacking")
|
||||
payloadType := []byte("/libp2p/testdata")
|
||||
_, err = MakeEnvelope(priv, "", payloadType, payload)
|
||||
if err == nil {
|
||||
t.Errorf("making an envelope with an empty domain should fail")
|
||||
}
|
||||
test.ExpectError(t, err, "making an envelope with an empty domain should fail")
|
||||
}
|
||||
|
||||
func TestEnvelopeValidateFailsForDifferentDomain(t *testing.T) {
|
||||
priv, _, err := test.RandTestKeyPair(Ed25519, 256)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
payload := []byte("happy hacking")
|
||||
domain := "libp2p-testing"
|
||||
payloadType := []byte("/libp2p/testdata")
|
||||
envelope, err := MakeEnvelope(priv, domain, payloadType, payload)
|
||||
if err != nil {
|
||||
t.Errorf("error constructing envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
serialized, err := envelope.Marshal()
|
||||
// try to open our modified envelope
|
||||
_, err = OpenEnvelope(serialized, "wrong-domain")
|
||||
if err == nil {
|
||||
t.Error("should not be able to open envelope with incorrect domain")
|
||||
}
|
||||
test.ExpectError(t, err, "should not be able to open envelope with incorrect domain")
|
||||
}
|
||||
|
||||
func TestEnvelopeValidateFailsIfTypeHintIsAltered(t *testing.T) {
|
||||
priv, _, err := test.RandTestKeyPair(Ed25519, 256)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
payload := []byte("happy hacking")
|
||||
domain := "libp2p-testing"
|
||||
payloadType := []byte("/libp2p/testdata")
|
||||
envelope, err := MakeEnvelope(priv, domain, payloadType, payload)
|
||||
if err != nil {
|
||||
t.Errorf("error constructing envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
serialized := alterMessageAndMarshal(t, envelope, func(msg *pb.SignedEnvelope) {
|
||||
msg.PayloadType = []byte("foo")
|
||||
})
|
||||
// try to open our modified envelope
|
||||
_, err = OpenEnvelope(serialized, domain)
|
||||
if err == nil {
|
||||
t.Error("should not be able to open envelope with modified payloadType")
|
||||
}
|
||||
test.ExpectError(t, err, "should not be able to open envelope with modified payloadType")
|
||||
}
|
||||
|
||||
func TestEnvelopeValidateFailsIfContentsAreAltered(t *testing.T) {
|
||||
priv, _, err := test.RandTestKeyPair(Ed25519, 256)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
payload := []byte("happy hacking")
|
||||
domain := "libp2p-testing"
|
||||
payloadType := []byte("/libp2p/testdata")
|
||||
envelope, err := MakeEnvelope(priv, domain, payloadType, payload)
|
||||
if err != nil {
|
||||
t.Errorf("error constructing envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
|
||||
serialized := alterMessageAndMarshal(t, envelope, func(msg *pb.SignedEnvelope) {
|
||||
msg.Payload = []byte("totally legit, trust me")
|
||||
})
|
||||
// try to open our modified envelope
|
||||
_, err = OpenEnvelope(serialized, domain)
|
||||
if err == nil {
|
||||
t.Error("should not be able to open envelope with modified payload")
|
||||
}
|
||||
test.ExpectError(t, err, "should not be able to open envelope with modified payload")
|
||||
}
|
||||
|
||||
// Since we're outside of the crypto package (to avoid import cycles with test package),
|
||||
@ -134,19 +106,14 @@ func TestEnvelopeValidateFailsIfContentsAreAltered(t *testing.T) {
|
||||
// alter the protobuf message.
|
||||
// Returns the serialized altered protobuf message.
|
||||
func alterMessageAndMarshal(t *testing.T, envelope *SignedEnvelope, alterMsg func(*pb.SignedEnvelope)) []byte {
|
||||
t.Helper()
|
||||
serialized, err := envelope.Marshal()
|
||||
if err != nil {
|
||||
t.Errorf("error marshaling envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
msg := pb.SignedEnvelope{}
|
||||
err = proto.Unmarshal(serialized, &msg)
|
||||
if err != nil {
|
||||
t.Errorf("error unmarshaling envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
alterMsg(&msg)
|
||||
serialized, err = msg.Marshal()
|
||||
if err != nil {
|
||||
t.Errorf("error marshaling envelope: %v", err)
|
||||
}
|
||||
test.AssertNilError(t, err)
|
||||
return serialized
|
||||
}
|
||||
|
@ -61,6 +61,7 @@ func TestRoutingStateFromEnvelope(t *testing.T) {
|
||||
test.AssertNilError(t, err)
|
||||
|
||||
env, err := crypto.MakeEnvelope(priv, "wrong-domain", StateEnvelopePayloadType, stateBytes)
|
||||
test.AssertNilError(t, err)
|
||||
envBytes, err := env.Marshal()
|
||||
_, err = RoutingStateFromEnvelope(envBytes)
|
||||
test.ExpectError(t, err, "unwrapping RoutingState from envelope should fail if envelope was created with wrong domain string")
|
||||
@ -71,6 +72,7 @@ func TestRoutingStateFromEnvelope(t *testing.T) {
|
||||
test.AssertNilError(t, err)
|
||||
payloadType := []byte("wrong-payload-type")
|
||||
env, err := crypto.MakeEnvelope(priv, StateEnvelopeDomain, payloadType, stateBytes)
|
||||
test.AssertNilError(t, err)
|
||||
envBytes, err := env.Marshal()
|
||||
_, err = RoutingStateFromEnvelope(envBytes)
|
||||
test.ExpectError(t, err, "unwrapping RoutingState from envelope should fail if envelope was created with wrong payload type")
|
||||
|
Loading…
Reference in New Issue
Block a user