This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86a4170
commit 3e4984c
Showing
5 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package routing | ||
|
||
import ( | ||
"github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
"github.com/libp2p/go-libp2p-core/test" | ||
"testing" | ||
) | ||
|
||
func TestRoutingStateFromAddrInfo(t *testing.T) { | ||
id, _ := test.RandPeerID() | ||
addrs := test.GenerateTestAddrs(10) | ||
info := peer.AddrInfo{ | ||
ID: id, | ||
Addrs: addrs, | ||
} | ||
state := RoutingStateFromAddrInfo(&info) | ||
if state.PeerID != info.ID { | ||
t.Fatalf("expected routing state to have peer id %s, got %s", id.Pretty(), state.PeerID.Pretty()) | ||
} | ||
test.AssertAddressesEqual(t, addrs, state.Multiaddrs()) | ||
} | ||
|
||
func TestRoutingStateFromEnvelope(t *testing.T) { | ||
priv, pub, err := test.RandTestKeyPair(crypto.Ed25519, 256) | ||
test.AssertNilError(t, err) | ||
|
||
id, err := peer.IDFromPublicKey(pub) | ||
test.AssertNilError(t, err) | ||
|
||
addrs := test.GenerateTestAddrs(10) | ||
state := RoutingStateWithMultiaddrs(id, addrs) | ||
|
||
t.Run("can unwrap a RoutingState from a serialized envelope", func(t *testing.T) { | ||
env, err := state.ToSignedEnvelope(priv) | ||
test.AssertNilError(t, err) | ||
|
||
envBytes, err := env.Marshal() | ||
test.AssertNilError(t, err) | ||
|
||
state2, err := RoutingStateFromEnvelope(envBytes) | ||
if !state.Equal(state2) { | ||
t.Error("expected routing state to be unaltered after wrapping in signed envelope") | ||
} | ||
}) | ||
|
||
t.Run("unwrapping from signed envelope fails if peer id does not match signing key", func(t *testing.T) { | ||
priv2, _, err := test.RandTestKeyPair(crypto.Ed25519, 256) | ||
test.AssertNilError(t, err) | ||
env, err := state.ToSignedEnvelope(priv2) | ||
test.AssertNilError(t, err) | ||
envBytes, err := env.Marshal() | ||
test.AssertNilError(t, err) | ||
|
||
_, err = RoutingStateFromEnvelope(envBytes) | ||
test.ExpectError(t, err, "unwrapping RoutingState from envelope should fail if peer id does not match key used to sign envelope") | ||
}) | ||
|
||
t.Run("unwrapping from signed envelope fails if envelope has wrong domain string", func (t *testing.T) { | ||
stateBytes, err := state.Marshal() | ||
test.AssertNilError(t, err) | ||
|
||
env, err := crypto.MakeEnvelope(priv, "wrong-domain", StateEnvelopePayloadType, stateBytes) | ||
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") | ||
}) | ||
|
||
t.Run("unwrapping from signed envelope fails if envelope has wrong payload type", func (t *testing.T) { | ||
stateBytes, err := state.Marshal() | ||
test.AssertNilError(t, err) | ||
payloadType := []byte("wrong-payload-type") | ||
env, err := crypto.MakeEnvelope(priv, StateEnvelopeDomain, payloadType, stateBytes) | ||
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") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func AssertNilError(t *testing.T, err error) { | ||
t.Helper() | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
} | ||
|
||
func ExpectError(t *testing.T, err error, msg string) { | ||
t.Helper() | ||
if err == nil { | ||
t.Error(msg) | ||
} | ||
} | ||
|
||
func GenerateTestAddrs(n int) []ma.Multiaddr { | ||
out := make([]ma.Multiaddr, n) | ||
for i := 0; i < n; i++ { | ||
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/1.2.3.4/tcp/%d", i)) | ||
if err != nil { | ||
continue | ||
} | ||
out[i] = a | ||
} | ||
return out | ||
} | ||
|
||
func AssertAddressesEqual(t *testing.T, exp, act []ma.Multiaddr) { | ||
t.Helper() | ||
if len(exp) != len(act) { | ||
t.Fatalf("lengths not the same. expected %d, got %d\n", len(exp), len(act)) | ||
} | ||
|
||
for _, a := range exp { | ||
found := false | ||
|
||
for _, b := range act { | ||
if a.Equal(b) { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
t.Fatalf("expected address %s not found", a) | ||
} | ||
} | ||
} | ||
|