util.go 1.09 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
4
5
package testutil

import (
	"testing"

6
7
8
9
10
	bhost "github.com/ipfs/go-ipfs/p2p/host/basic"
	inet "github.com/ipfs/go-ipfs/p2p/net"
	swarm "github.com/ipfs/go-ipfs/p2p/net/swarm"
	peer "github.com/ipfs/go-ipfs/p2p/peer"
	tu "github.com/ipfs/go-ipfs/util/testutil"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11

12
13
	ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
15
16
17
18
19
20
)

func GenSwarmNetwork(t *testing.T, ctx context.Context) *swarm.Network {
	p := tu.RandPeerNetParamsOrFatal(t)
	ps := peer.NewPeerstore()
	ps.AddPubKey(p.ID, p.PubKey)
	ps.AddPrivKey(p.ID, p.PrivKey)
21
	n, err := swarm.NewNetwork(ctx, []ma.Multiaddr{p.Addr}, p.ID, ps)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22
23
24
	if err != nil {
		t.Fatal(err)
	}
25
	ps.AddAddrs(p.ID, n.ListenAddresses(), peer.PermanentAddrTTL)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26
27
28
29
30
	return n
}

func DivulgeAddresses(a, b inet.Network) {
	id := a.LocalPeer()
31
32
	addrs := a.Peerstore().Addrs(id)
	b.Peerstore().AddAddrs(id, addrs, peer.PermanentAddrTTL)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
34
35
36
37
38
}

func GenHostSwarm(t *testing.T, ctx context.Context) *bhost.BasicHost {
	n := GenSwarmNetwork(t, ctx)
	return bhost.New(n)
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39
40

var RandPeerID = tu.RandPeerID