Skip to content

Commit

Permalink
Merge pull request #476 from libp2p/fix/473
Browse files Browse the repository at this point in the history
mocknet: use peer ID in peer address
  • Loading branch information
Stebalien authored Nov 8, 2018
2 parents 3e2dc09 + f422947 commit da772d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
22 changes: 19 additions & 3 deletions p2p/net/mock/mock_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mocknet
import (
"context"
"fmt"
"net"
"sort"
"sync"

Expand All @@ -17,10 +18,13 @@ import (
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
testutil "github.com/libp2p/go-testutil"
ma "github.com/multiformats/go-multiaddr"
)

// IP6 range that gets blackholed (in case our traffic ever makes it out onto
// the internet).
var blackholeIP6 = net.ParseIP("100::")

// mocknet implements mocknet.Mocknet
type mocknet struct {
nets map[peer.ID]*peernet
Expand Down Expand Up @@ -54,8 +58,20 @@ func (mn *mocknet) GenPeer() (host.Host, error) {
if err != nil {
return nil, err
}

a := testutil.RandLocalTCPAddress()
id, err := peer.IDFromPrivateKey(sk)
if err != nil {
return nil, err
}
suffix := id
if len(id) > 8 {
suffix = id[len(id)-8:]
}
ip := append(net.IP{}, blackholeIP6...)
copy(ip[net.IPv6len-len(suffix):], suffix)
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip6/%s/tcp/4242", ip))
if err != nil {
return nil, fmt.Errorf("failed to create test multiaddr: %s", err)
}

h, err := mn.AddPeer(sk, a)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions p2p/net/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,14 @@ func TestLimitedStreams(t *testing.T) {
t.Fatal("Expected 2ish seconds but got ", time.Since(before))
}
}
func TestFuzzManyPeers(t *testing.T) {
for i := 0; i < 50000; i++ {
_, err := FullMeshConnected(context.Background(), 2)
if err != nil {
t.Fatal(err)
}
}
}

func TestStreamsWithLatency(t *testing.T) {
latency := time.Millisecond * 500
Expand Down

0 comments on commit da772d1

Please sign in to comment.