-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedded_lifecycle_test.go
More file actions
456 lines (427 loc) · 12.6 KB
/
Copy pathembedded_lifecycle_test.go
File metadata and controls
456 lines (427 loc) · 12.6 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package ivnp_test
import (
"context"
"crypto/ed25519"
cryptorand "crypto/rand"
"encoding/binary"
"errors"
"io"
"net"
"os"
"path/filepath"
"sync"
"testing"
"time"
"gosuda.org/ivnp"
"gosuda.org/ivnp/foundation"
"gosuda.org/ivnp/networking"
)
var errMemoryTransportUnavailable = errors.New("memory transport: target unavailable")
const embeddedTestTimeout = 20 * time.Second
type embeddedMemoryNetwork struct {
mu sync.RWMutex
endpoints map[foundation.Hash]*embeddedMemoryTransport
flood foundation.Hash
floodDB *networking.NetworkDatabase
nextID uint32
}
type embeddedMemoryTransport struct {
network *embeddedMemoryNetwork
local foundation.Hash
bindings networking.RouterTransportBindings
done chan struct{}
once sync.Once
running bool
}
func newEmbeddedMemoryNetwork(flood networking.NetworkDatabaseRouterInfo) *embeddedMemoryNetwork {
return &embeddedMemoryNetwork{
endpoints: make(map[foundation.Hash]*embeddedMemoryTransport),
flood: flood.Hash(),
floodDB: networking.NetworkDatabaseNewDatabase(flood.Hash(), 16),
}
}
func (n *embeddedMemoryNetwork) transport() *embeddedMemoryTransport {
return &embeddedMemoryTransport{network: n, done: make(chan struct{})}
}
func (n *embeddedMemoryNetwork) messageID() uint32 {
n.mu.Lock()
defer n.mu.Unlock()
n.nextID++
if n.nextID == 0 {
n.nextID++
}
return n.nextID
}
func (t *embeddedMemoryTransport) Start(ctx context.Context, bindings networking.RouterTransportBindings) error {
bindings.LocalInfo.SetReachability(networking.RouterReachabilityReachable)
if err := bindings.LocalInfo.Publish(ctx); err != nil {
return err
}
t.network.mu.Lock()
t.local = bindings.LocalInfo.Hash()
t.bindings = bindings
t.running = true
t.network.endpoints[t.local] = t
t.network.mu.Unlock()
return nil
}
func (t *embeddedMemoryTransport) Send(ctx context.Context, target foundation.Hash, message networking.I2NPMessage) error {
if err := ctx.Err(); err != nil {
return err
}
return t.network.route(t.localHash(), target, message)
}
func (t *embeddedMemoryTransport) Close() error {
t.once.Do(func() {
t.network.mu.Lock()
delete(t.network.endpoints, t.local)
t.running = false
t.network.mu.Unlock()
close(t.done)
})
return nil
}
func (t *embeddedMemoryTransport) Wait() error {
<-t.done
return nil
}
func (t *embeddedMemoryTransport) Status() networking.RouterTransportStatus {
t.network.mu.RLock()
status := networking.RouterTransportStatus{Running: t.running}
t.network.mu.RUnlock()
return status
}
func (t *embeddedMemoryTransport) localHash() foundation.Hash {
t.network.mu.RLock()
local := t.local
t.network.mu.RUnlock()
return local
}
func (t *embeddedMemoryTransport) routerInfo() networking.NetworkDatabaseRouterInfo {
t.network.mu.RLock()
localInfo := t.bindings.LocalInfo
t.network.mu.RUnlock()
if localInfo == nil {
return networking.NetworkDatabaseRouterInfo{}
}
return localInfo.Snapshot()
}
func (n *embeddedMemoryNetwork) route(from, target foundation.Hash, message networking.I2NPMessage) error {
if target == n.flood {
return n.handleFlood(from, message)
}
if message.Header.Type == networking.I2NPShortTunnelBuild {
if _, err := networking.I2NPParseBuildRecords(networking.I2NPShortTunnelBuild, message.Payload); err != nil {
return err
}
}
n.mu.RLock()
endpoint := n.endpoints[target]
n.mu.RUnlock()
if endpoint == nil {
return errMemoryTransportUnavailable
}
return endpoint.bindings.HandleI2NPFrom(from, message, uint64(time.Now().UnixMilli()), false)
}
func (n *embeddedMemoryNetwork) handleFlood(from foundation.Hash, message networking.I2NPMessage) error {
now := uint64(time.Now().UnixMilli())
switch message.Header.Type {
case networking.I2NPDatabaseStore:
store, err := networking.I2NPParseDatabaseStore(message.Payload)
if err != nil {
return err
}
if err = n.floodDB.HandleDatabaseStore(store, false, now); err != nil {
return err
}
if store.ReplyToken == 0 {
return nil
}
var payload [12]byte
binary.BigEndian.PutUint32(payload[:4], store.ReplyToken)
binary.BigEndian.PutUint64(payload[4:], now)
status := networking.I2NPMessage{
Header: networking.I2NPHeader{
Type: networking.I2NPDeliveryStatus,
ID: n.messageID(),
Expiration: now + 60_000,
},
Payload: payload[:],
}
return n.reply(n.flood, store.ReplyGateway, store.ReplyTunnelID, status)
case networking.I2NPDatabaseLookup:
lookup, err := networking.I2NPParseDatabaseLookup(message.Payload)
if err != nil {
return err
}
typeID, data, found := n.floodDB.StoredLeaseSet(lookup.Key)
if !found {
return networking.NetworkDatabaseErrNoFloodfill
}
payload := marshalEmbeddedDatabaseStore(lookup.Key, typeID, data)
reply := networking.I2NPMessage{
Header: networking.I2NPHeader{
Type: networking.I2NPDatabaseStore,
ID: n.messageID(),
Expiration: now + 60_000,
},
Payload: payload,
}
return n.reply(n.flood, lookup.From, lookup.ReplyTunnelID, reply)
default:
return n.route(from, from, message)
}
}
func marshalEmbeddedDatabaseStore(key foundation.Hash, typeID networking.I2NPStoreType, data []byte) []byte {
payload := make([]byte, 37+len(data))
copy(payload[:foundation.HashLength], key[:])
payload[foundation.HashLength] = byte(typeID)
copy(payload[37:], data)
return payload
}
func (n *embeddedMemoryNetwork) reply(from, gateway foundation.Hash, tunnelID uint32, message networking.I2NPMessage) error {
if tunnelID == 0 {
return n.route(from, gateway, message)
}
frame := make([]byte, message.EncodedLen())
if _, err := message.MarshalTo(frame); err != nil {
return err
}
payload := make([]byte, networking.I2NPTunnelGatewayHeaderLen+len(frame))
binary.BigEndian.PutUint32(payload[:4], tunnelID)
binary.BigEndian.PutUint16(payload[4:6], uint16(len(frame)))
copy(payload[6:], frame)
gatewayMessage := networking.I2NPMessage{
Header: networking.I2NPHeader{
Type: networking.I2NPTunnelGateway,
ID: n.messageID(),
Expiration: message.Header.Expiration,
},
Payload: payload,
}
return n.route(from, gateway, gatewayMessage)
}
func embeddedTestFloodfill(t *testing.T) networking.NetworkDatabaseRouterInfo {
t.Helper()
public, private, err := ed25519.GenerateKey(cryptorand.Reader)
if err != nil {
t.Fatal(err)
}
identity := make([]byte, foundation.IdentityBaseLength+7)
copy(identity[352:384], public)
identity[384] = byte(foundation.CertificateKey)
identity[385], identity[386] = 0, 4
identity[387], identity[388] = 0, byte(foundation.SigningEdDSASHA512Ed25519)
identity[389], identity[390] = 0, byte(foundation.CryptoElGamal)
options := make([]byte, 16)
optionLen, err := foundation.MarshalMappingTo(options, []foundation.MappingEntry{{Key: []byte("caps"), Value: []byte("f")}})
if err != nil {
t.Fatal(err)
}
now := uint64(time.Now().UnixMilli())
unsigned := append(identity, make([]byte, 10)...)
binary.BigEndian.PutUint64(unsigned[len(identity):len(identity)+8], now)
unsigned = append(unsigned, options[:optionLen]...)
info, err := networking.NetworkDatabaseParseRouterInfo(append(unsigned, ed25519.Sign(private, unsigned)...))
if err != nil {
t.Fatal(err)
}
return info
}
func embeddedTestConfig(t *testing.T) ivnp.Config {
t.Helper()
base := t.TempDir()
if err := os.Chmod(base, 0o700); err != nil {
t.Fatal(err)
}
var cfg ivnp.Config
cfg.DataDir = base
cfg.StateDir = base
cfg.StatePath = filepath.Join(base, "router.state")
cfg.KeyPath = filepath.Join(base, "router.keys")
cfg.Network.ID = 2
cfg.Network.IPv4 = true
cfg.Router.Version = "0.9.70"
cfg.State.MaxBytes = 1 << 20
cfg.State.MaxDestinations = 16
cfg.State.MaxNameBytes = 64
cfg.Tunnel.ExploratoryInboundTarget = 1
cfg.Tunnel.ExploratoryOutboundTarget = 1
cfg.Tunnel.ExploratoryPoolCapacity = 2
cfg.NetDB.BucketCapacity = 16
cfg.NetDB.LookupCapacity = 32
cfg.Tunnel.Enabled = true
cfg.Tunnel.Hops = 1
cfg.Tunnel.ClientInboundTarget = 1
cfg.Tunnel.ClientOutboundTarget = 1
cfg.Tunnel.ClientPoolCapacity = 2
cfg.Tunnel.BuildPendingCapacity = 4
cfg.Tunnel.Lifetime = 10 * time.Minute
cfg.Tunnel.RenewBefore = 10 * time.Second
cfg.Tunnel.MaintenanceInterval = 100 * time.Millisecond
cfg.Tunnel.BandwidthRateBytesPerSecond = 64 * 1024
return cfg
}
func captureEmbeddedRouterInfo(t *testing.T, cfg ivnp.Config, network *embeddedMemoryNetwork) []byte {
t.Helper()
transport := network.transport()
router, err := ivnp.New(cfg, ivnp.Options{Transport: transport})
if err != nil {
t.Fatal(err)
}
if err = router.DestroyDestination(context.Background(), "default"); err != nil {
t.Fatal(err)
}
if err = router.Start(context.Background()); err != nil {
t.Fatal(err)
}
info := transport.routerInfo()
encoded := append([]byte(nil), info.Bytes()...)
if len(encoded) == 0 {
t.Fatal("started node did not expose its local RouterInfo to the transport")
}
if err = router.Close(); err != nil {
t.Fatal(err)
}
if err = router.Wait(); err != nil {
t.Fatal(err)
}
return encoded
}
func TestEmbeddedDestinationLifecycle(t *testing.T) {
flood := embeddedTestFloodfill(t)
network := newEmbeddedMemoryNetwork(flood)
configs := []ivnp.Config{embeddedTestConfig(t), embeddedTestConfig(t), embeddedTestConfig(t)}
routerInfos := make([][]byte, len(configs))
for index := range configs {
routerInfos[index] = captureEmbeddedRouterInfo(t, configs[index], network)
}
bootstrapDir := t.TempDir()
bootstrapPaths := make([]string, 0, len(routerInfos)+1)
for index, info := range routerInfos {
path := filepath.Join(bootstrapDir, "router-"+string(rune('a'+index))+".dat")
if err := os.WriteFile(path, info, 0o600); err != nil {
t.Fatal(err)
}
bootstrapPaths = append(bootstrapPaths, path)
}
floodPath := filepath.Join(bootstrapDir, "floodfill.dat")
if err := os.WriteFile(floodPath, flood.Bytes(), 0o600); err != nil {
t.Fatal(err)
}
for index := range configs {
paths := make([]string, 0, len(configs))
for peerIndex, path := range bootstrapPaths {
if peerIndex != index {
paths = append(paths, path)
}
}
configs[index].NetDB.BootstrapRouterInfoPaths = append(paths, floodPath)
}
routers := make([]*ivnp.Node, 0, len(configs))
for _, cfg := range configs {
router, err := ivnp.New(cfg, ivnp.Options{Transport: network.transport()})
if err != nil {
t.Fatal(err)
}
if err = router.DestroyDestination(context.Background(), "default"); err != nil {
t.Fatal(err)
}
routers = append(routers, router)
}
t.Cleanup(func() {
for _, router := range routers {
_ = router.Close()
_ = router.Wait()
}
})
for _, router := range routers {
if err := router.Start(context.Background()); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithTimeout(context.Background(), embeddedTestTimeout)
defer cancel()
source, err := routers[0].DestinationController().CreateDestination(ctx, ivnp.DestinationSpec{})
if err != nil {
t.Fatal(err)
}
defer source.Close()
target, err := routers[1].DestinationController().CreateDestination(ctx, ivnp.DestinationSpec{})
if err != nil {
t.Fatal(err)
}
defer target.Close()
for _, endpoint := range []ivnp.DestinationEndpoint{source, target} {
ready, ok := endpoint.(ivnp.ReadyDestinationEndpoint)
if !ok {
t.Fatalf("endpoint %T does not implement ivnp.ReadyDestinationEndpoint", endpoint)
}
if err = ready.WaitReady(ctx); err != nil {
t.Fatal(err)
}
}
listener, err := target.ListenI2P(ctx, ":8080")
if err != nil {
t.Fatal(err)
}
defer listener.Close()
accepted := make(chan net.Conn, 1)
acceptErrors := make(chan error, 1)
go func() {
connection, acceptErr := listener.Accept()
if acceptErr != nil {
acceptErrors <- acceptErr
return
}
accepted <- connection
}()
outbound, err := source.DialI2P(ctx, target.B32()+":8080")
if err != nil {
t.Fatal(err)
}
defer outbound.Close()
var inbound net.Conn
select {
case inbound = <-accepted:
case err = <-acceptErrors:
t.Fatal(err)
case <-ctx.Done():
t.Fatal(ctx.Err())
}
defer inbound.Close()
deadline := time.Now().Add(5 * time.Second)
if err = outbound.SetDeadline(deadline); err != nil {
t.Fatal(err)
}
if err = inbound.SetDeadline(deadline); err != nil {
t.Fatal(err)
}
payload := []byte("embedded-router-round-trip")
if _, err = outbound.Write(payload); err != nil {
t.Fatal(err)
}
got := make([]byte, len(payload))
if _, err = io.ReadFull(inbound, got); err != nil {
t.Fatal(err)
}
if string(got) != string(payload) {
t.Fatalf("received %q, want %q", got, payload)
}
if err = source.Close(); err != nil {
t.Fatal(err)
}
if err = target.Close(); err != nil {
t.Fatal(err)
}
for _, router := range routers {
if err = router.Close(); err != nil {
t.Fatal(err)
}
}
for _, router := range routers {
if err = router.Wait(); err != nil {
t.Fatal(err)
}
}
}