-
Notifications
You must be signed in to change notification settings - Fork 376
/
testing.go
241 lines (197 loc) · 6.26 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package ipfsutil
import (
"context"
crand "crypto/rand"
"encoding/base64"
"math/rand"
"testing"
"time"
tinder "berty.tech/berty/v2/go/internal/tinder"
ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
ipfs_cfg "github.com/ipfs/go-ipfs-config"
ipfs_core "github.com/ipfs/go-ipfs/core"
ipfs_mock "github.com/ipfs/go-ipfs/core/mock"
ipfs_repo "github.com/ipfs/go-ipfs/repo"
libp2p_ci "github.com/libp2p/go-libp2p-core/crypto"
host "github.com/libp2p/go-libp2p-core/host"
p2pnetwork "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
libp2p_peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-core/routing"
discovery "github.com/libp2p/go-libp2p-discovery"
pubsub "github.com/libp2p/go-libp2p-pubsub"
rendezvous "github.com/libp2p/go-libp2p-rendezvous"
p2p_rpdb "github.com/libp2p/go-libp2p-rendezvous/db/sqlite"
libp2p_mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
// CoreAPIMock implements ipfs.CoreAPI and adds some debugging helpers
type CoreAPIMock interface {
API() ExtendedCoreAPI
PubSub() *pubsub.PubSub
Tinder() tinder.Driver
MockNetwork() libp2p_mocknet.Mocknet
MockNode() *ipfs_core.IpfsNode
Close()
}
func TestingRepo(t testing.TB) ipfs_repo.Repo {
t.Helper()
c := ipfs_cfg.Config{}
priv, pub, err := libp2p_ci.GenerateKeyPairWithReader(libp2p_ci.RSA, 2048, crand.Reader)
if err != nil {
t.Fatalf("failed to generate pair key: %v", err)
}
pid, err := libp2p_peer.IDFromPublicKey(pub)
if err != nil {
t.Fatalf("failed to get pid from pub key: %v", err)
}
privkeyb, err := priv.Bytes()
if err != nil {
t.Fatalf("failed to get raw priv key: %v", err)
}
c.Bootstrap = []string{}
c.Addresses.Swarm = []string{"/ip6/::/tcp/0"}
c.Identity.PeerID = pid.Pretty()
c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb)
dstore := dsync.MutexWrap(ds.NewMapDatastore())
return &ipfs_repo.Mock{
D: dstore,
C: c,
}
}
type TestingAPIOpts struct {
Logger *zap.Logger
Mocknet libp2p_mocknet.Mocknet
RDVPeer peer.AddrInfo
}
// TestingCoreAPIUsingMockNet returns a fully initialized mocked Core API with the given mocknet
func TestingCoreAPIUsingMockNet(ctx context.Context, t testing.TB, opts *TestingAPIOpts) (CoreAPIMock, func()) {
t.Helper()
if opts.Logger == nil {
opts.Logger = zap.NewNop()
}
var ps *pubsub.PubSub
var disc tinder.Driver
repo := TestingRepo(t)
exapi, node, err := NewCoreAPIFromRepo(ctx, repo, &CoreAPIConfig{
DisableCorePubSub: true,
Host: ipfs_mock.MockHostOption(opts.Mocknet),
HostConfig: func(h host.Host, r routing.Routing) error {
var err error
if opts.RDVPeer.ID != "" {
// opts.Mocknet.ConnectPeers(node.Identity, opts.RDVPeer.ID)
h.Peerstore().AddAddrs(opts.RDVPeer.ID, opts.RDVPeer.Addrs, peerstore.PermanentAddrTTL)
// @FIXME(gfanton): use rand as argument
disc = tinder.NewRendezvousDiscovery(opts.Logger, h, opts.RDVPeer.ID, rand.New(rand.NewSource(rand.Int63())))
} else {
disc = tinder.NewDriverRouting(opts.Logger, "dht", r)
}
minBackoff, maxBackoff := time.Second, time.Minute
rng := rand.New(rand.NewSource(rand.Int63()))
disc, err = tinder.NewService(
opts.Logger,
disc,
discovery.NewExponentialBackoff(minBackoff, maxBackoff, discovery.FullJitter, time.Second, 5.0, 0, rng),
)
if err != nil {
return err
}
ps, err = pubsub.NewGossipSub(ctx, h,
pubsub.WithMessageSigning(true),
pubsub.WithFloodPublish(true),
pubsub.WithDiscovery(disc),
pubsub.WithPeerExchange(true),
)
return err
},
})
require.NoError(t, err, "failed to initialize IPFS node mock")
require.NotNil(t, ps)
require.NotNil(t, disc)
_, err = opts.Mocknet.LinkPeers(node.Identity, opts.RDVPeer.ID)
psapi := NewPubSubAPI(ctx, opts.Logger, disc, ps)
exapi = InjectPubSubCoreAPIExtendedAdaptater(exapi, psapi)
EnableConnLogger(ctx, opts.Logger, node.PeerHost)
api := &coreAPIMock{
coreapi: exapi,
mocknet: opts.Mocknet,
pubsub: ps,
node: node,
tinder: disc,
}
return api, func() {
_ = node.Close()
_ = node.PeerHost.Close()
_ = repo.Close()
}
}
// TestingCoreAPI returns a fully initialized mocked Core API.
// If you want to do some tests involving multiple peers you should use
// `TestingCoreAPIUsingMockNet` with the same mocknet instead.
func TestingCoreAPI(ctx context.Context, t testing.TB) (CoreAPIMock, func()) {
t.Helper()
m := libp2p_mocknet.New(ctx)
rdvPeer, err := m.GenPeer()
require.NoError(t, err)
_, cleanrdvp := TestingRDVP(ctx, t, rdvPeer)
api, cleanapi := TestingCoreAPIUsingMockNet(ctx, t, &TestingAPIOpts{
Mocknet: m,
RDVPeer: rdvPeer.Network().Peerstore().PeerInfo(rdvPeer.ID()),
})
cleanup := func() {
cleanapi()
cleanrdvp()
_ = rdvPeer.Close()
}
return api, cleanup
}
func TestingRDVP(ctx context.Context, t testing.TB, h host.Host) (*rendezvous.RendezvousService, func()) {
db, err := p2p_rpdb.OpenDB(ctx, ":memory:")
require.NoError(t, err)
svc := rendezvous.NewRendezvousService(h, db)
cleanup := func() {
_ = db.Close() // dont use me for now as db is open in_memory
}
return svc, cleanup
}
type coreAPIMock struct {
coreapi ExtendedCoreAPI
pubsub *pubsub.PubSub
mocknet libp2p_mocknet.Mocknet
node *ipfs_core.IpfsNode
tinder tinder.Driver
}
func (m *coreAPIMock) ConnMgr() ConnMgr {
return m.node.PeerHost.ConnManager()
}
func (m *coreAPIMock) NewStream(ctx context.Context, p libp2p_peer.ID, pids ...protocol.ID) (p2pnetwork.Stream, error) {
return m.node.PeerHost.NewStream(ctx, p, pids...)
}
func (m *coreAPIMock) SetStreamHandler(pid protocol.ID, handler p2pnetwork.StreamHandler) {
m.node.PeerHost.SetStreamHandler(pid, handler)
}
func (m *coreAPIMock) RemoveStreamHandler(pid protocol.ID) {
m.node.PeerHost.RemoveStreamHandler(pid)
}
func (m *coreAPIMock) API() ExtendedCoreAPI {
return m.coreapi
}
func (m *coreAPIMock) MockNetwork() libp2p_mocknet.Mocknet {
return m.mocknet
}
func (m *coreAPIMock) MockNode() *ipfs_core.IpfsNode {
return m.node
}
func (m *coreAPIMock) PubSub() *pubsub.PubSub {
return m.pubsub
}
func (m *coreAPIMock) Tinder() tinder.Driver {
return m.tinder
}
func (m *coreAPIMock) Close() {
m.node.Close()
}