conn: tests pass :)
Juan Batiz-Benet committed
Dec 14, 2014 at 20:53 UTC
a3a48ce67bc3f61cd26a5f81958f75d00b630158
6 files changed
+82
-69
net/conn/conn_test.go
+3
-8
@@ -42,22 +42,17 @@ func TestClose(t *testing.T) {
42
// t.Skip("Skipping in favor of another test")
43
44
ctx := context.Background()
45
- c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/5534", "/ip4/127.0.0.1/tcp/5545")
45
+ c1, c2 := setupSingleConn(t, ctx, "/ip4/127.0.0.1/tcp/5534", "/ip4/127.0.0.1/tcp/5545")
46
47
testOneSendRecv(t, c1, c2)
48
testOneSendRecv(t, c2, c1)
49
50
c1.Close()
51
-
52
- time.After(200 * time.Millisecond)
51
testNotOneSendRecv(t, c1, c2)
54
- testNotOneSendRecv(t, c2, c1)
52
53
c2.Close()
57
-
58
- time.After(20000 * time.Millisecond)
59
- testNotOneSendRecv(t, c1, c2)
54
testNotOneSendRecv(t, c2, c1)
55
+ testNotOneSendRecv(t, c1, c2)
56
}
57
58
func TestCloseLeak(t *testing.T) {
@@ -75,7 +70,7 @@ func TestCloseLeak(t *testing.T) {
70
a1 := strconv.Itoa(p1)
71
a2 := strconv.Itoa(p2)
72
ctx, cancel := context.WithCancel(context.Background())
78
- c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2)
73
+ c1, c2 := setupSingleConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2)
74
75
for i := 0; i < num; i++ {
76
b1 := []byte(fmt.Sprintf("beep%d", i))
net/conn/dial.go
+4
@@ -78,6 +78,10 @@ func (d *Dialer) DialAddr(ctx context.Context, raddr ma.Multiaddr, remote peer.P
78
return nil, err
79
}
80
81
+ if d.WithoutSecureTransport {
82
+ return c, nil
83
+ }
84
+
85
select {
86
case <-ctx.Done():
87
c.Close()
net/conn/dial_test.go
+13
-3
@@ -49,7 +49,15 @@ func echo(c Conn) {
49
io.Copy(c, c)
50
}
51
52
-func setupConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
52
+func setupSecureConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
53
+ return setupConn(t, ctx, a1, a2, true)
54
+}
55
+
56
+func setupSingleConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
57
+ return setupConn(t, ctx, a1, a2, false)
58
+}
59
+
60
+func setupConn(t *testing.T, ctx context.Context, a1, a2 string, secure bool) (a, b Conn) {
61
62
p1, err := setupPeer(a1)
63
if err != nil {
@@ -72,13 +80,15 @@ func setupConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
80
ps2.Add(p2)
81
82
l1, err := Listen(ctx, laddr, p1, ps1)
83
+ l1.SetWithoutSecureTransport(!secure)
84
if err != nil {
85
t.Fatal(err)
86
}
87
88
d2 := &Dialer{
80
- Peerstore: ps2,
81
- LocalPeer: p2,
89
+ Peerstore: ps2,
90
+ LocalPeer: p2,
91
+ WithoutSecureTransport: !secure,
92
}
93
94
var c2 Conn
net/conn/interface.go
+9
@@ -57,6 +57,10 @@ type Dialer struct {
57
// because when an incoming connection is identified, we should reuse the
58
// same peer objects (otherwise things get inconsistent).
59
Peerstore peer.Peerstore
60
+
61
+ // WithoutSecureTransport determines whether to initialize an insecure connection.
62
+ // Phrased negatively so default is Secure, and verbosely to be very clear.
63
+ WithoutSecureTransport bool
64
}
65
66
// Listener is an object that can accept connections. It matches net.Listener
@@ -65,6 +69,11 @@ type Listener interface {
69
// Accept waits for and returns the next connection to the listener.
70
Accept() (net.Conn, error)
71
72
+ // {Set}WithoutSecureTransport decides whether to start insecure connections.
73
+ // Phrased negatively so default is Secure, and verbosely to be very clear.
74
+ WithoutSecureTransport() bool
75
+ SetWithoutSecureTransport(bool)
76
+
77
// Addr is the local address
78
Addr() net.Addr
79
net/conn/listen.go
+21
-20
@@ -13,8 +13,7 @@ import (
13
14
// listener is an object that can accept connections. It implements Listener
15
type listener struct {
16
- notSecure bool
17
- notSecureIMeanIt bool
16
+ withoutSecureTransport bool
17
18
manet.Listener
19
@@ -52,19 +51,22 @@ func (l *listener) Accept() (net.Conn, error) {
51
return nil, fmt.Errorf("Error accepting connection: %v", err)
52
}
53
55
- if l.Secure() {
56
- sc, err := newSecureConn(ctx, c, l.peers)
57
- if err != nil {
58
- return nil, fmt.Errorf("Error securing connection: %v", err)
59
- }
60
- return sc, nil
54
+ if l.withoutSecureTransport {
55
+ return c, nil
56
}
57
+ sc, err := newSecureConn(ctx, c, l.peers)
58
+ if err != nil {
59
+ return nil, fmt.Errorf("Error securing connection: %v", err)
60
+ }
61
+ return sc, nil
62
+}
63
63
- return c, nil
64
+func (l *listener) WithoutSecureTransport() bool {
65
+ return l.withoutSecureTransport
66
}
67
66
-func (l *listener) Secure() bool {
67
- return !(l.notSecure && l.notSecureIMeanIt)
68
+func (l *listener) SetWithoutSecureTransport(b bool) {
69
+ l.withoutSecureTransport = b
70
}
71
72
func (l *listener) Addr() net.Addr {
@@ -91,9 +93,9 @@ func (l *listener) Peerstore() peer.Peerstore {
93
func (l *listener) Loggable() map[string]interface{} {
94
return map[string]interface{}{
95
"listener": map[string]interface{}{
94
- "peer": l.LocalPeer(),
95
- "address": l.Multiaddr(),
96
- "secure": l.Secure(),
96
+ "peer": l.LocalPeer(),
97
+ "address": l.Multiaddr(),
98
+ "withoutSecureTransport": l.withoutSecureTransport,
99
},
100
}
101
}
@@ -107,12 +109,11 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local peer.Peer, peers peer.
109
}
110
111
l := &listener{
110
- Listener: ml,
111
- maddr: addr,
112
- peers: peers,
113
- local: local,
114
- notSecure: false,
115
- notSecureIMeanIt: false,
112
+ Listener: ml,
113
+ maddr: addr,
114
+ peers: peers,
115
+ local: local,
116
+ withoutSecureTransport: false,
117
}
118
119
log.Infof("swarm listening on %s\n", l.Multiaddr())
net/conn/secure_conn_test.go
+32
-38
@@ -15,9 +15,8 @@ import (
15
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
16
)
17
18
-func setupSecureConn(t *testing.T, ctx context.Context, c Conn) (Conn, error) {
19
- c, ok := c.(*secureConn)
20
- if ok {
18
+func upgradeToSecureConn(t *testing.T, ctx context.Context, c Conn) (Conn, error) {
19
+ if c, ok := c.(*secureConn); ok {
20
return c, nil
21
}
22
@@ -29,26 +28,33 @@ func setupSecureConn(t *testing.T, ctx context.Context, c Conn) (Conn, error) {
28
return s, nil
29
}
30
31
+func secureHandshake(t *testing.T, ctx context.Context, c Conn, done chan error) {
32
+ _, err := upgradeToSecureConn(t, ctx, c)
33
+ done <- err
34
+}
35
+
36
func TestSecureClose(t *testing.T) {
37
// t.Skip("Skipping in favor of another test")
38
39
ctx := context.Background()
36
- c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/6634", "/ip4/127.0.0.1/tcp/6645")
40
+ c1, c2 := setupSingleConn(t, ctx, "/ip4/127.0.0.1/tcp/6634", "/ip4/127.0.0.1/tcp/6645")
41
38
- c1, err1 := setupSecureConn(t, ctx, c1)
39
- c2, err2 := setupSecureConn(t, ctx, c2)
40
- if err1 != nil {
41
- t.Fatal(err1)
42
- }
43
- if err2 != nil {
44
- t.Fatal(err2)
42
+ done := make(chan error)
43
+ go secureHandshake(t, ctx, c1, done)
44
+ go secureHandshake(t, ctx, c2, done)
45
+
46
+ for i := 0; i < 2; i++ {
47
+ if err := <-done; err != nil {
48
+ t.Error(err)
49
+ }
50
}
51
52
testOneSendRecv(t, c1, c2)
48
- testOneSendRecv(t, c2, c1)
53
54
c1.Close()
55
+ testNotOneSendRecv(t, c1, c2)
56
57
+ c2.Close()
58
testNotOneSendRecv(t, c1, c2)
59
testNotOneSendRecv(t, c2, c1)
60
@@ -57,23 +63,20 @@ func TestSecureClose(t *testing.T) {
63
func TestSecureCancelHandshake(t *testing.T) {
64
// t.Skip("Skipping in favor of another test")
65
60
- ctx := context.Background()
61
- c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/6634", "/ip4/127.0.0.1/tcp/6645")
62
-
63
- done := make(chan struct{})
64
- go func() {
65
- _, err1 := setupSecureConn(t, ctx, c1)
66
- _, err2 := setupSecureConn(t, ctx, c2)
67
- if err1 == nil {
68
- t.Fatal(err1)
69
- }
70
- if err2 == nil {
71
- t.Fatal(err2)
72
- }
73
- done <- struct{}{}
74
- }()
66
+ ctx, cancel := context.WithCancel(context.Background())
67
+ c1, c2 := setupSingleConn(t, ctx, "/ip4/127.0.0.1/tcp/6634", "/ip4/127.0.0.1/tcp/6645")
68
+
69
+ done := make(chan error)
70
+ go secureHandshake(t, ctx, c1, done)
71
+ <-time.After(50 * time.Millisecond)
72
+ cancel() // cancel ctx
73
+ go secureHandshake(t, ctx, c2, done)
74
76
- <-done
75
+ for i := 0; i < 2; i++ {
76
+ if err := <-done; err == nil {
77
+ t.Error("cancel should've errored out")
78
+ }
79
+ }
80
}
81
82
func TestSecureCloseLeak(t *testing.T) {
@@ -92,16 +95,7 @@ func TestSecureCloseLeak(t *testing.T) {
95
a1 := strconv.Itoa(p1)
96
a2 := strconv.Itoa(p2)
97
ctx, cancel := context.WithCancel(context.Background())
95
- c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2)
96
-
97
- c1, err1 := setupSecureConn(t, ctx, c1)
98
- c2, err2 := setupSecureConn(t, ctx, c2)
99
- if err1 != nil {
100
- t.Fatal(err1)
101
- }
102
- if err2 != nil {
103
- t.Fatal(err2)
104
- }
98
+ c1, c2 := setupSecureConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2)
99
100
for i := 0; i < num; i++ {
101
b1 := []byte("beep")