dial_test.go 6.08 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package conn

import (
4
	"fmt"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
6
	"io"
	"net"
7
	"strings"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
9
10
	"testing"
	"time"

Jeromy's avatar
Jeromy committed
11
12
13
	ic "github.com/ipfs/go-libp2p/p2p/crypto"
	transport "github.com/ipfs/go-libp2p/p2p/net/transport"
	peer "github.com/ipfs/go-libp2p/p2p/peer"
Jeromy's avatar
Jeromy committed
14
	tu "github.com/ipfs/go-libp2p/testutil"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15

Jeromy's avatar
Jeromy committed
16
	context "QmacZi9WygGK7Me8mH53pypyscHzU386aUZXpr28GZgUct/context"
Jeromy's avatar
Jeromy committed
17
	ma "QmbWxL1aXQhBjc1XGjGF1f2KGBMCBYSuT2ThA8YXnXJK83/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
)

func echoListen(ctx context.Context, listener Listener) {
	for {
		c, err := listener.Accept()
		if err != nil {

			select {
			case <-ctx.Done():
				return
			default:
			}

			if ne, ok := err.(net.Error); ok && ne.Temporary() {
				<-time.After(time.Microsecond * 10)
				continue
			}

			log.Debugf("echoListen: listener appears to be closing")
			return
		}

		go echo(c.(Conn))
	}
}

func echo(c Conn) {
	io.Copy(c, c)
}

func setupSecureConn(t *testing.T, ctx context.Context) (a, b Conn, p1, p2 tu.PeerNetParams) {
	return setupConn(t, ctx, true)
}

func setupSingleConn(t *testing.T, ctx context.Context) (a, b Conn, p1, p2 tu.PeerNetParams) {
	return setupConn(t, ctx, false)
}

Jeromy's avatar
Jeromy committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
func Listen(ctx context.Context, addr ma.Multiaddr, local peer.ID, sk ic.PrivKey) (Listener, error) {
	list, err := transport.NewTCPTransport().Listen(addr)
	if err != nil {
		return nil, err
	}

	return WrapTransportListener(ctx, list, local, sk)
}

func dialer(t *testing.T, a ma.Multiaddr) transport.Dialer {
	tpt := transport.NewTCPTransport()
	tptd, err := tpt.Dialer(a)
	if err != nil {
		t.Fatal(err)
	}

	return tptd
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75
76
77
78
79
80
81
82
83
84
85
func setupConn(t *testing.T, ctx context.Context, secure bool) (a, b Conn, p1, p2 tu.PeerNetParams) {

	p1 = tu.RandPeerNetParamsOrFatal(t)
	p2 = tu.RandPeerNetParamsOrFatal(t)

	key1 := p1.PrivKey
	key2 := p2.PrivKey
	if !secure {
		key1 = nil
		key2 = nil
	}
86
	l1, err := Listen(ctx, p1.Addr, p1.ID, key1)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
88
89
	if err != nil {
		t.Fatal(err)
	}
90
	p1.Addr = l1.Multiaddr() // Addr has been determined by kernel.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
92
93
94
95
96

	d2 := &Dialer{
		LocalPeer:  p2.ID,
		PrivateKey: key2,
	}

Jeromy's avatar
Jeromy committed
97
98
	d2.AddDialer(dialer(t, p2.Addr))

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99
100
101
102
	var c2 Conn

	done := make(chan error)
	go func() {
103
104
		defer close(done)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105
106
107
108
		var err error
		c2, err = d2.Dial(ctx, p1.Addr, p1.ID)
		if err != nil {
			done <- err
109
110
111
112
113
114
115
116
			return
		}

		// if secure, need to read + write, as that's what triggers the handshake.
		if secure {
			if err := sayHello(c2); err != nil {
				done <- err
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117
118
119
120
121
122
123
		}
	}()

	c1, err := l1.Accept()
	if err != nil {
		t.Fatal("failed to accept", err)
	}
124
125
126
127
128
129
130
131

	// if secure, need to read + write, as that's what triggers the handshake.
	if secure {
		if err := sayHello(c1); err != nil {
			done <- err
		}
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132
133
134
135
136
137
138
	if err := <-done; err != nil {
		t.Fatal(err)
	}

	return c1.(Conn), c2, p1, p2
}

139
140
141
142
143
144
145
146
147
148
149
150
151
152
func sayHello(c net.Conn) error {
	h := []byte("hello")
	if _, err := c.Write(h); err != nil {
		return err
	}
	if _, err := c.Read(h); err != nil {
		return err
	}
	if string(h) != "hello" {
		return fmt.Errorf("did not get hello")
	}
	return nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153
154
155
156
157
158
159
160
161
162
163
func testDialer(t *testing.T, secure bool) {
	// t.Skip("Skipping in favor of another test")

	p1 := tu.RandPeerNetParamsOrFatal(t)
	p2 := tu.RandPeerNetParamsOrFatal(t)

	key1 := p1.PrivKey
	key2 := p2.PrivKey
	if !secure {
		key1 = nil
		key2 = nil
164
165
166
		t.Log("testing insecurely")
	} else {
		t.Log("testing securely")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
167
168
169
170
171
172
173
	}

	ctx, cancel := context.WithCancel(context.Background())
	l1, err := Listen(ctx, p1.Addr, p1.ID, key1)
	if err != nil {
		t.Fatal(err)
	}
174
	p1.Addr = l1.Multiaddr() // Addr has been determined by kernel.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
175
176
177
178
179

	d2 := &Dialer{
		LocalPeer:  p2.ID,
		PrivateKey: key2,
	}
Jeromy's avatar
Jeromy committed
180
	d2.AddDialer(dialer(t, p2.Addr))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181
182
183
184
185
186
187
188
189

	go echoListen(ctx, l1)

	c, err := d2.Dial(ctx, p1.Addr, p1.ID)
	if err != nil {
		t.Fatal("error dialing peer", err)
	}

	// fmt.Println("sending")
Jeromy's avatar
Jeromy committed
190
191
192
193
	mc := msgioWrap(c)
	mc.WriteMsg([]byte("beep"))
	mc.WriteMsg([]byte("boop"))
	out, err := mc.ReadMsg()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194
195
196
197
198
199
200
201
202
203
	if err != nil {
		t.Fatal(err)
	}

	// fmt.Println("recving", string(out))
	data := string(out)
	if data != "beep" {
		t.Error("unexpected conn output", data)
	}

Jeromy's avatar
Jeromy committed
204
	out, err = mc.ReadMsg()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
	if err != nil {
		t.Fatal(err)
	}

	data = string(out)
	if string(out) != "boop" {
		t.Error("unexpected conn output", data)
	}

	// fmt.Println("closing")
	c.Close()
	l1.Close()
	cancel()
}

func TestDialerInsecure(t *testing.T) {
	// t.Skip("Skipping in favor of another test")
	testDialer(t, false)
}

func TestDialerSecure(t *testing.T) {
	// t.Skip("Skipping in favor of another test")
	testDialer(t, true)
}
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

func testDialerCloseEarly(t *testing.T, secure bool) {
	// t.Skip("Skipping in favor of another test")

	p1 := tu.RandPeerNetParamsOrFatal(t)
	p2 := tu.RandPeerNetParamsOrFatal(t)

	key1 := p1.PrivKey
	if !secure {
		key1 = nil
		t.Log("testing insecurely")
	} else {
		t.Log("testing securely")
	}

	ctx, cancel := context.WithCancel(context.Background())
	l1, err := Listen(ctx, p1.Addr, p1.ID, key1)
	if err != nil {
		t.Fatal(err)
	}
	p1.Addr = l1.Multiaddr() // Addr has been determined by kernel.

	// lol nesting
	d2 := &Dialer{
		LocalPeer: p2.ID,
		// PrivateKey: key2, -- dont give it key. we'll just close the conn.
	}
Jeromy's avatar
Jeromy committed
256
	d2.AddDialer(dialer(t, p2.Addr))
257
258
259
260
261
262
263

	errs := make(chan error, 100)
	done := make(chan struct{}, 1)
	gotclosed := make(chan struct{}, 1)
	go func() {
		defer func() { done <- struct{}{} }()

264
		c, err := l1.Accept()
265
266
267
268
269
270
271
		if err != nil {
			if strings.Contains(err.Error(), "closed") {
				gotclosed <- struct{}{}
				return
			}
			errs <- err
		}
272
273
274
275
276
277
278

		if _, err := c.Write([]byte("hello")); err != nil {
			gotclosed <- struct{}{}
			return
		}

		errs <- fmt.Errorf("wrote to conn")
279
280
281
282
	}()

	c, err := d2.Dial(ctx, p1.Addr, p1.ID)
	if err != nil {
Jeromy's avatar
Jeromy committed
283
		t.Fatal(err)
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
	}
	c.Close() // close it early.

	readerrs := func() {
		for {
			select {
			case e := <-errs:
				t.Error(e)
			default:
				return
			}
		}
	}
	readerrs()

	l1.Close()
	<-done
	cancel()
	readerrs()
	close(errs)

	select {
	case <-gotclosed:
	default:
		t.Error("did not get closed")
	}
}

// we dont do a handshake with singleConn, so cant "close early."
// func TestDialerCloseEarlyInsecure(t *testing.T) {
// 	// t.Skip("Skipping in favor of another test")
// 	testDialerCloseEarly(t, false)
// }

func TestDialerCloseEarlySecure(t *testing.T) {
	// t.Skip("Skipping in favor of another test")
	testDialerCloseEarly(t, true)
}