-
Notifications
You must be signed in to change notification settings - Fork 376
/
testing.go
295 lines (241 loc) · 8.07 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package ipfsutil
import (
"context"
crand "crypto/rand"
"encoding/base64"
"fmt"
"math/rand"
"testing"
"time"
ipfs_mobile "github.com/ipfs-shipyard/gomobile-ipfs/go/pkg/ipfsmobile"
ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
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"
p2p_ci "github.com/libp2p/go-libp2p-core/crypto"
host "github.com/libp2p/go-libp2p-core/host"
p2pnetwork "github.com/libp2p/go-libp2p-core/network"
p2p_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/sqlcipher"
p2p_mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"berty.tech/berty/v2/go/internal/tinder"
)
// CoreAPIMock implements ipfs.CoreAPI and adds some debugging helpers
type CoreAPIMock interface {
API() ExtendedCoreAPI
PubSub() *pubsub.PubSub
Tinder() tinder.Service
MockNetwork() p2p_mocknet.Mocknet
MockNode() *ipfs_core.IpfsNode
Close()
}
func getOrCreatePrivateKeyFromDatastore(t testing.TB, ctx context.Context, datastore ds.Datastore) p2p_ci.PrivKey {
const datastoreKeyForPrivateKey = "p2p_private_key"
privkeyb, err := datastore.Get(ctx, ds.NewKey("private_key"))
if err == ds.ErrNotFound {
priv, _, err := p2p_ci.GenerateKeyPairWithReader(p2p_ci.RSA, 2048, crand.Reader)
if err != nil {
t.Fatalf("failed to generate pair key: %v", err)
}
privkeyb, err := p2p_ci.MarshalPrivateKey(priv)
if err != nil {
t.Fatalf("failed to get raw priv key: %v", err)
}
if err := datastore.Put(ctx, ds.NewKey(datastoreKeyForPrivateKey), privkeyb); err != nil {
t.Fatalf("failed to save priv key: %v", err)
}
return priv
} else if err != nil {
t.Fatalf("failed to get value from datastore: %v", err)
}
priv, err := p2p_ci.UnmarshalPrivateKey(privkeyb)
if err != nil {
t.Fatalf("failed to unmarshal priv key: %v", err)
}
return priv
}
func TestingRepo(t testing.TB, ctx context.Context, datastore ds.Datastore) ipfs_repo.Repo {
t.Helper()
c := ipfs_cfg.Config{}
priv := getOrCreatePrivateKeyFromDatastore(t, ctx, datastore)
pid, err := p2p_peer.IDFromPublicKey(priv.GetPublic())
if err != nil {
t.Fatalf("failed to get pid from pub key: %v", err)
}
privkeyb, err := p2p_ci.MarshalPrivateKey(priv)
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)
if datastore == nil {
datastore = ds.NewMapDatastore()
}
dstore := dsync.MutexWrap(datastore)
return &ipfs_repo.Mock{
D: dstore,
C: c,
}
}
type TestingAPIOpts struct {
Logger *zap.Logger
Mocknet p2p_mocknet.Mocknet
RDVPeer p2p_peer.AddrInfo
Datastore ds.Batching
}
// TestingCoreAPIUsingMockNet returns a fully initialized mocked Core API with the given mocknet
func TestingCoreAPIUsingMockNet(ctx context.Context, t testing.TB, opts *TestingAPIOpts) (CoreAPIMock, func()) {
if opts.Logger == nil {
opts.Logger = zap.NewNop()
}
datastore := opts.Datastore
if datastore == nil {
datastore = dsync.MutexWrap(ds.NewMapDatastore())
}
repo := TestingRepo(t, ctx, datastore)
var ps *pubsub.PubSub
var disc tinder.Service
configureRouting := func(h host.Host, r routing.Routing) error {
var err error
drivers := []*tinder.Driver{}
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
rdvp := tinder.NewRendezvousDiscovery(opts.Logger, h, opts.RDVPeer.ID, rand.New(rand.NewSource(rand.Int63())))
if _, err = opts.Mocknet.LinkPeers(h.ID(), opts.RDVPeer.ID); err != nil {
return err
}
driver := tinder.NewDriverFromUnregisterDiscovery("rdpv", rdvp, nil)
drivers = append(drivers, driver)
}
if r != nil {
driver := tinder.NewDriverFromRouting("dht", r, nil)
drivers = append(drivers, driver)
}
// minBackoff, maxBackoff := time.Second, time.Minute
// rng := rand.New(rand.NewSource(rand.Int63()))
tinderOpts := &tinder.Opts{
Logger: opts.Logger,
AdvertiseResetInterval: time.Minute,
AdvertiseGracePeriod: time.Minute,
BackoffStrategy: &tinder.BackoffOpts{
StratFactory: discovery.NewFixedBackoff(time.Second),
},
// BackoffStratFactory: discovery.NewExponentialBackoff(minBackoff, maxBackoff, discovery.FullJitter, time.Second, 5.0, 0, rng),
}
disc, err = tinder.NewService(tinderOpts, h, drivers...)
if err != nil {
return fmt.Errorf("unable to monitor discovery driver: %w", err)
}
ps, err = pubsub.NewGossipSub(ctx, h, pubsub.WithDiscovery(disc))
return err
}
mrepo := ipfs_mobile.NewRepoMobile("", repo)
mnode, err := NewIPFSMobile(ctx, mrepo, &MobileOptions{
HostOption: ipfs_mock.MockHostOption(opts.Mocknet),
RoutingConfigFunc: configureRouting,
ExtraOpts: map[string]bool{
"pubsub": false,
},
})
require.NoError(t, err, "failed to initialize IPFS node mock")
require.NotNil(t, ps, "pubsub should not be nil")
require.NotNil(t, disc, "discovery should not be nil")
exapi, err := NewExtendedCoreAPIFromNode(mnode.IpfsNode)
require.NoError(t, err, "unable to extend core api from node")
psapi := NewPubSubAPI(ctx, opts.Logger, disc, ps)
exapi = InjectPubSubCoreAPIExtendedAdapter(exapi, psapi)
EnableConnLogger(ctx, opts.Logger, mnode.PeerHost())
api := &coreAPIMock{
coreapi: exapi,
mocknet: opts.Mocknet,
pubsub: ps,
node: mnode.IpfsNode,
tinder: disc,
}
return api, func() {
_ = mnode.Close()
_ = mnode.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 := p2p_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 p2p_mocknet.Mocknet
node *ipfs_core.IpfsNode
tinder tinder.Service
}
func (m *coreAPIMock) ConnMgr() ConnMgr {
return m.node.PeerHost.ConnManager()
}
func (m *coreAPIMock) NewStream(ctx context.Context, p p2p_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() p2p_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.Service {
return m.tinder
}
func (m *coreAPIMock) Close() {
m.node.Close()
m.tinder.Close()
}