2019-11-18 23:13:06 +08:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/test"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-11-21 23:09:31 +08:00
|
|
|
func TestSignedRoutingStateFromEnvelope(t *testing.T) {
|
|
|
|
priv, _, err := test.RandTestKeyPair(crypto.Ed25519, 256)
|
2019-11-18 23:13:06 +08:00
|
|
|
test.AssertNilError(t, err)
|
|
|
|
|
|
|
|
addrs := test.GenerateTestAddrs(10)
|
2019-11-21 23:09:31 +08:00
|
|
|
state, err := MakeSignedRoutingState(priv, addrs)
|
|
|
|
test.AssertNilError(t, err)
|
2019-11-18 23:13:06 +08:00
|
|
|
|
2019-11-21 23:09:31 +08:00
|
|
|
t.Run("is unaltered after round-trip serde", func(t *testing.T) {
|
|
|
|
envBytes, err := state.Marshal()
|
2019-11-18 23:13:06 +08:00
|
|
|
test.AssertNilError(t, err)
|
|
|
|
|
2019-11-21 23:09:31 +08:00
|
|
|
state2, err := UnmarshalSignedRoutingState(envBytes)
|
2019-11-18 23:13:06 +08:00
|
|
|
test.AssertNilError(t, err)
|
|
|
|
if !state.Equal(state2) {
|
2019-11-21 23:09:31 +08:00
|
|
|
t.Error("expected routing state to be unaltered after round-trip serde")
|
2019-11-18 23:13:06 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-11-18 23:26:53 +08:00
|
|
|
t.Run("unwrapping from signed envelope fails if envelope has wrong domain string", func(t *testing.T) {
|
2019-11-18 23:13:06 +08:00
|
|
|
stateBytes, err := state.Marshal()
|
|
|
|
test.AssertNilError(t, err)
|
|
|
|
|
|
|
|
env, err := crypto.MakeEnvelope(priv, "wrong-domain", StateEnvelopePayloadType, stateBytes)
|
2019-11-20 01:43:47 +08:00
|
|
|
test.AssertNilError(t, err)
|
2019-11-18 23:13:06 +08:00
|
|
|
envBytes, err := env.Marshal()
|
2019-11-21 23:09:31 +08:00
|
|
|
_, err = UnmarshalSignedRoutingState(envBytes)
|
2019-11-18 23:13:06 +08:00
|
|
|
test.ExpectError(t, err, "unwrapping RoutingState from envelope should fail if envelope was created with wrong domain string")
|
|
|
|
})
|
|
|
|
|
2019-11-18 23:26:53 +08:00
|
|
|
t.Run("unwrapping from signed envelope fails if envelope has wrong payload type", func(t *testing.T) {
|
2019-11-18 23:13:06 +08:00
|
|
|
stateBytes, err := state.Marshal()
|
|
|
|
test.AssertNilError(t, err)
|
|
|
|
payloadType := []byte("wrong-payload-type")
|
|
|
|
env, err := crypto.MakeEnvelope(priv, StateEnvelopeDomain, payloadType, stateBytes)
|
2019-11-20 01:43:47 +08:00
|
|
|
test.AssertNilError(t, err)
|
2019-11-18 23:13:06 +08:00
|
|
|
envBytes, err := env.Marshal()
|
2019-11-21 23:09:31 +08:00
|
|
|
_, err = UnmarshalSignedRoutingState(envBytes)
|
2019-11-18 23:13:06 +08:00
|
|
|
test.ExpectError(t, err, "unwrapping RoutingState from envelope should fail if envelope was created with wrong payload type")
|
|
|
|
})
|
|
|
|
}
|