relay_test.go 7.9 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package relay_test

import (
Jeromy's avatar
Jeromy committed
4
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
6
7
	"io"
	"testing"

Jeromy's avatar
Jeromy committed
8
9
10
	bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
	relay "github.com/libp2p/go-libp2p/p2p/protocol/relay"

Jeromy's avatar
Jeromy committed
11
	logging "github.com/ipfs/go-log"
Jeromy's avatar
Jeromy committed
12
	inet "github.com/libp2p/go-libp2p-net"
Jeromy's avatar
Jeromy committed
13
	testutil "github.com/libp2p/go-libp2p-netutil"
Jeromy's avatar
Jeromy committed
14
	protocol "github.com/libp2p/go-libp2p-protocol"
Jeromy's avatar
Jeromy committed
15
	msmux "github.com/whyrusleeping/go-multistream"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
17
)

Jeromy's avatar
Jeromy committed
18
var log = logging.Logger("relay_test")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19
20
21
22
23
24

func TestRelaySimple(t *testing.T) {

	ctx := context.Background()

	// these networks have the relay service wired in already.
Jeromy's avatar
Jeromy committed
25
26
27
	n1 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	n2 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	n3 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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

	n1p := n1.ID()
	n2p := n2.ID()
	n3p := n3.ID()

	n2pi := n2.Peerstore().PeerInfo(n2p)
	if err := n1.Connect(ctx, n2pi); err != nil {
		t.Fatal("Failed to connect:", err)
	}
	if err := n3.Connect(ctx, n2pi); err != nil {
		t.Fatal("Failed to connect:", err)
	}

	// setup handler on n3 to copy everything over to the pipe.
	piper, pipew := io.Pipe()
	n3.SetStreamHandler(protocol.TestingID, func(s inet.Stream) {
		log.Debug("relay stream opened to n3!")
		log.Debug("piping and echoing everything")
		w := io.MultiWriter(s, pipew)
		io.Copy(w, s)
		log.Debug("closing stream")
		s.Close()
	})

	// ok, now we can try to relay n1--->n2--->n3.
	log.Debug("open relay stream")
54
	s, err := n1.NewStream(ctx, n2p, relay.ID)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55
56
57
58
59
60
61
62
63
64
65
66
	if err != nil {
		t.Fatal(err)
	}

	// ok first thing we write the relay header n1->n3
	log.Debug("write relay header")
	if err := relay.WriteHeader(s, n1p, n3p); err != nil {
		t.Fatal(err)
	}

	// ok now the header's there, we can write the next protocol header.
	log.Debug("write testing header")
Jeromy's avatar
Jeromy committed
67
	if err := msmux.SelectProtoOrFail(string(protocol.TestingID), s); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
		t.Fatal(err)
	}

	// okay, now we should be able to write text, and read it out.
	buf1 := []byte("abcdefghij")
	buf2 := make([]byte, 10)
	buf3 := make([]byte, 10)
	log.Debug("write in some text.")
	if _, err := s.Write(buf1); err != nil {
		t.Fatal(err)
	}

	// read it out from the pipe.
	log.Debug("read it out from the pipe.")
	if _, err := io.ReadFull(piper, buf2); err != nil {
		t.Fatal(err)
	}
	if string(buf1) != string(buf2) {
		t.Fatal("should've gotten that text out of the pipe")
	}

	// read it out from the stream (echoed)
	log.Debug("read it out from the stream (echoed).")
	if _, err := io.ReadFull(s, buf3); err != nil {
		t.Fatal(err)
	}
	if string(buf1) != string(buf3) {
		t.Fatal("should've gotten that text out of the stream")
	}

	// sweet. relay works.
	log.Debug("sweet, relay works.")
	s.Close()
}

func TestRelayAcrossFour(t *testing.T) {

	ctx := context.Background()

	// these networks have the relay service wired in already.
Jeromy's avatar
Jeromy committed
108
109
110
111
112
	n1 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	n2 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	n3 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	n4 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	n5 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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

	n1p := n1.ID()
	n2p := n2.ID()
	n3p := n3.ID()
	n4p := n4.ID()
	n5p := n5.ID()

	n2pi := n2.Peerstore().PeerInfo(n2p)
	n4pi := n4.Peerstore().PeerInfo(n4p)

	if err := n1.Connect(ctx, n2pi); err != nil {
		t.Fatalf("Failed to dial:", err)
	}
	if err := n3.Connect(ctx, n2pi); err != nil {
		t.Fatalf("Failed to dial:", err)
	}
	if err := n3.Connect(ctx, n4pi); err != nil {
		t.Fatalf("Failed to dial:", err)
	}
	if err := n5.Connect(ctx, n4pi); err != nil {
		t.Fatalf("Failed to dial:", err)
	}

	// setup handler on n5 to copy everything over to the pipe.
	piper, pipew := io.Pipe()
	n5.SetStreamHandler(protocol.TestingID, func(s inet.Stream) {
		log.Debug("relay stream opened to n5!")
		log.Debug("piping and echoing everything")
		w := io.MultiWriter(s, pipew)
		io.Copy(w, s)
		log.Debug("closing stream")
		s.Close()
	})

	// ok, now we can try to relay n1--->n2--->n3--->n4--->n5
	log.Debug("open relay stream")
149
	s, err := n1.NewStream(ctx, n2p, relay.ID)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
150
151
152
153
154
155
156
157
158
159
	if err != nil {
		t.Fatal(err)
	}

	log.Debugf("write relay header n1->n3 (%s -> %s)", n1p, n3p)
	if err := relay.WriteHeader(s, n1p, n3p); err != nil {
		t.Fatal(err)
	}

	log.Debugf("write relay header n1->n4 (%s -> %s)", n1p, n4p)
Jeromy's avatar
Jeromy committed
160
	if err := msmux.SelectProtoOrFail(string(relay.ID), s); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161
162
163
164
165
166
167
		t.Fatal(err)
	}
	if err := relay.WriteHeader(s, n1p, n4p); err != nil {
		t.Fatal(err)
	}

	log.Debugf("write relay header n1->n5 (%s -> %s)", n1p, n5p)
Jeromy's avatar
Jeromy committed
168
	if err := msmux.SelectProtoOrFail(string(relay.ID), s); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
169
170
171
172
173
174
175
176
		t.Fatal(err)
	}
	if err := relay.WriteHeader(s, n1p, n5p); err != nil {
		t.Fatal(err)
	}

	// ok now the header's there, we can write the next protocol header.
	log.Debug("write testing header")
Jeromy's avatar
Jeromy committed
177
	if err := msmux.SelectProtoOrFail(string(protocol.TestingID), s); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
		t.Fatal(err)
	}

	// okay, now we should be able to write text, and read it out.
	buf1 := []byte("abcdefghij")
	buf2 := make([]byte, 10)
	buf3 := make([]byte, 10)
	log.Debug("write in some text.")
	if _, err := s.Write(buf1); err != nil {
		t.Fatal(err)
	}

	// read it out from the pipe.
	log.Debug("read it out from the pipe.")
	if _, err := io.ReadFull(piper, buf2); err != nil {
		t.Fatal(err)
	}
	if string(buf1) != string(buf2) {
		t.Fatal("should've gotten that text out of the pipe")
	}

	// read it out from the stream (echoed)
	log.Debug("read it out from the stream (echoed).")
	if _, err := io.ReadFull(s, buf3); err != nil {
		t.Fatal(err)
	}
	if string(buf1) != string(buf3) {
		t.Fatal("should've gotten that text out of the stream")
	}

	// sweet. relay works.
	log.Debug("sweet, relaying across 4 works.")
	s.Close()
}

func TestRelayStress(t *testing.T) {
	buflen := 1 << 18
	iterations := 10

	ctx := context.Background()

	// these networks have the relay service wired in already.
Jeromy's avatar
Jeromy committed
220
221
222
	n1 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	n2 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
	n3 := bhost.New(testutil.GenSwarmNetwork(t, ctx))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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

	n1p := n1.ID()
	n2p := n2.ID()
	n3p := n3.ID()

	n2pi := n2.Peerstore().PeerInfo(n2p)
	if err := n1.Connect(ctx, n2pi); err != nil {
		t.Fatalf("Failed to dial:", err)
	}
	if err := n3.Connect(ctx, n2pi); err != nil {
		t.Fatalf("Failed to dial:", err)
	}

	// setup handler on n3 to copy everything over to the pipe.
	piper, pipew := io.Pipe()
	n3.SetStreamHandler(protocol.TestingID, func(s inet.Stream) {
		log.Debug("relay stream opened to n3!")
		log.Debug("piping and echoing everything")
		w := io.MultiWriter(s, pipew)
		io.Copy(w, s)
		log.Debug("closing stream")
		s.Close()
	})

	// ok, now we can try to relay n1--->n2--->n3.
	log.Debug("open relay stream")
249
	s, err := n1.NewStream(ctx, n2p, relay.ID)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
250
251
252
253
254
255
256
257
258
259
260
261
	if err != nil {
		t.Fatal(err)
	}

	// ok first thing we write the relay header n1->n3
	log.Debug("write relay header")
	if err := relay.WriteHeader(s, n1p, n3p); err != nil {
		t.Fatal(err)
	}

	// ok now the header's there, we can write the next protocol header.
	log.Debug("write testing header")
Jeromy's avatar
Jeromy committed
262
	if err := msmux.SelectProtoOrFail(string(protocol.TestingID), s); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
		t.Fatal(err)
	}

	// okay, now write lots of text and read it back out from both
	// the pipe and the stream.
	buf1 := make([]byte, buflen)
	buf2 := make([]byte, len(buf1))
	buf3 := make([]byte, len(buf1))

	fillbuf := func(buf []byte, b byte) {
		for i := range buf {
			buf[i] = b
		}
	}

	for i := 0; i < iterations; i++ {
		fillbuf(buf1, byte(int('a')+i))
		log.Debugf("writing %d bytes (%d/%d)", len(buf1), i, iterations)
		if _, err := s.Write(buf1); err != nil {
			t.Fatal(err)
		}

		log.Debug("read it out from the pipe.")
		if _, err := io.ReadFull(piper, buf2); err != nil {
			t.Fatal(err)
		}
		if string(buf1) != string(buf2) {
			t.Fatal("should've gotten that text out of the pipe")
		}

		// read it out from the stream (echoed)
		log.Debug("read it out from the stream (echoed).")
		if _, err := io.ReadFull(s, buf3); err != nil {
			t.Fatal(err)
		}
		if string(buf1) != string(buf3) {
			t.Fatal("should've gotten that text out of the stream")
		}
	}

	log.Debug("sweet, relay works under stress.")
	s.Close()
}