提交 a3a48ce6 作者: Juan Batiz-Benet

conn: tests pass :)

上级 0061f0c1
...@@ -42,22 +42,17 @@ func TestClose(t *testing.T) { ...@@ -42,22 +42,17 @@ func TestClose(t *testing.T) {
// t.Skip("Skipping in favor of another test") // t.Skip("Skipping in favor of another test")
ctx := context.Background() ctx := context.Background()
c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/5534", "/ip4/127.0.0.1/tcp/5545") c1, c2 := setupSingleConn(t, ctx, "/ip4/127.0.0.1/tcp/5534", "/ip4/127.0.0.1/tcp/5545")
testOneSendRecv(t, c1, c2) testOneSendRecv(t, c1, c2)
testOneSendRecv(t, c2, c1) testOneSendRecv(t, c2, c1)
c1.Close() c1.Close()
time.After(200 * time.Millisecond)
testNotOneSendRecv(t, c1, c2) testNotOneSendRecv(t, c1, c2)
testNotOneSendRecv(t, c2, c1)
c2.Close() c2.Close()
time.After(20000 * time.Millisecond)
testNotOneSendRecv(t, c1, c2)
testNotOneSendRecv(t, c2, c1) testNotOneSendRecv(t, c2, c1)
testNotOneSendRecv(t, c1, c2)
} }
func TestCloseLeak(t *testing.T) { func TestCloseLeak(t *testing.T) {
...@@ -75,7 +70,7 @@ func TestCloseLeak(t *testing.T) { ...@@ -75,7 +70,7 @@ func TestCloseLeak(t *testing.T) {
a1 := strconv.Itoa(p1) a1 := strconv.Itoa(p1)
a2 := strconv.Itoa(p2) a2 := strconv.Itoa(p2)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2) c1, c2 := setupSingleConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2)
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
b1 := []byte(fmt.Sprintf("beep%d", i)) b1 := []byte(fmt.Sprintf("beep%d", i))
......
...@@ -78,6 +78,10 @@ func (d *Dialer) DialAddr(ctx context.Context, raddr ma.Multiaddr, remote peer.P ...@@ -78,6 +78,10 @@ func (d *Dialer) DialAddr(ctx context.Context, raddr ma.Multiaddr, remote peer.P
return nil, err return nil, err
} }
if d.WithoutSecureTransport {
return c, nil
}
select { select {
case <-ctx.Done(): case <-ctx.Done():
c.Close() c.Close()
......
...@@ -49,7 +49,15 @@ func echo(c Conn) { ...@@ -49,7 +49,15 @@ func echo(c Conn) {
io.Copy(c, c) io.Copy(c, c)
} }
func setupConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) { func setupSecureConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
return setupConn(t, ctx, a1, a2, true)
}
func setupSingleConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
return setupConn(t, ctx, a1, a2, false)
}
func setupConn(t *testing.T, ctx context.Context, a1, a2 string, secure bool) (a, b Conn) {
p1, err := setupPeer(a1) p1, err := setupPeer(a1)
if err != nil { if err != nil {
...@@ -72,6 +80,7 @@ func setupConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) { ...@@ -72,6 +80,7 @@ func setupConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
ps2.Add(p2) ps2.Add(p2)
l1, err := Listen(ctx, laddr, p1, ps1) l1, err := Listen(ctx, laddr, p1, ps1)
l1.SetWithoutSecureTransport(!secure)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -79,6 +88,7 @@ func setupConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) { ...@@ -79,6 +88,7 @@ func setupConn(t *testing.T, ctx context.Context, a1, a2 string) (a, b Conn) {
d2 := &Dialer{ d2 := &Dialer{
Peerstore: ps2, Peerstore: ps2,
LocalPeer: p2, LocalPeer: p2,
WithoutSecureTransport: !secure,
} }
var c2 Conn var c2 Conn
......
...@@ -57,6 +57,10 @@ type Dialer struct { ...@@ -57,6 +57,10 @@ type Dialer struct {
// because when an incoming connection is identified, we should reuse the // because when an incoming connection is identified, we should reuse the
// same peer objects (otherwise things get inconsistent). // same peer objects (otherwise things get inconsistent).
Peerstore peer.Peerstore Peerstore peer.Peerstore
// WithoutSecureTransport determines whether to initialize an insecure connection.
// Phrased negatively so default is Secure, and verbosely to be very clear.
WithoutSecureTransport bool
} }
// Listener is an object that can accept connections. It matches net.Listener // Listener is an object that can accept connections. It matches net.Listener
...@@ -65,6 +69,11 @@ type Listener interface { ...@@ -65,6 +69,11 @@ type Listener interface {
// Accept waits for and returns the next connection to the listener. // Accept waits for and returns the next connection to the listener.
Accept() (net.Conn, error) Accept() (net.Conn, error)
// {Set}WithoutSecureTransport decides whether to start insecure connections.
// Phrased negatively so default is Secure, and verbosely to be very clear.
WithoutSecureTransport() bool
SetWithoutSecureTransport(bool)
// Addr is the local address // Addr is the local address
Addr() net.Addr Addr() net.Addr
......
...@@ -13,8 +13,7 @@ import ( ...@@ -13,8 +13,7 @@ import (
// listener is an object that can accept connections. It implements Listener // listener is an object that can accept connections. It implements Listener
type listener struct { type listener struct {
notSecure bool withoutSecureTransport bool
notSecureIMeanIt bool
manet.Listener manet.Listener
...@@ -52,19 +51,22 @@ func (l *listener) Accept() (net.Conn, error) { ...@@ -52,19 +51,22 @@ func (l *listener) Accept() (net.Conn, error) {
return nil, fmt.Errorf("Error accepting connection: %v", err) return nil, fmt.Errorf("Error accepting connection: %v", err)
} }
if l.Secure() { if l.withoutSecureTransport {
return c, nil
}
sc, err := newSecureConn(ctx, c, l.peers) sc, err := newSecureConn(ctx, c, l.peers)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error securing connection: %v", err) return nil, fmt.Errorf("Error securing connection: %v", err)
} }
return sc, nil return sc, nil
} }
return c, nil func (l *listener) WithoutSecureTransport() bool {
return l.withoutSecureTransport
} }
func (l *listener) Secure() bool { func (l *listener) SetWithoutSecureTransport(b bool) {
return !(l.notSecure && l.notSecureIMeanIt) l.withoutSecureTransport = b
} }
func (l *listener) Addr() net.Addr { func (l *listener) Addr() net.Addr {
...@@ -93,7 +95,7 @@ func (l *listener) Loggable() map[string]interface{} { ...@@ -93,7 +95,7 @@ func (l *listener) Loggable() map[string]interface{} {
"listener": map[string]interface{}{ "listener": map[string]interface{}{
"peer": l.LocalPeer(), "peer": l.LocalPeer(),
"address": l.Multiaddr(), "address": l.Multiaddr(),
"secure": l.Secure(), "withoutSecureTransport": l.withoutSecureTransport,
}, },
} }
} }
...@@ -111,8 +113,7 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local peer.Peer, peers peer. ...@@ -111,8 +113,7 @@ func Listen(ctx context.Context, addr ma.Multiaddr, local peer.Peer, peers peer.
maddr: addr, maddr: addr,
peers: peers, peers: peers,
local: local, local: local,
notSecure: false, withoutSecureTransport: false,
notSecureIMeanIt: false,
} }
log.Infof("swarm listening on %s\n", l.Multiaddr()) log.Infof("swarm listening on %s\n", l.Multiaddr())
......
...@@ -15,9 +15,8 @@ import ( ...@@ -15,9 +15,8 @@ import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
) )
func setupSecureConn(t *testing.T, ctx context.Context, c Conn) (Conn, error) { func upgradeToSecureConn(t *testing.T, ctx context.Context, c Conn) (Conn, error) {
c, ok := c.(*secureConn) if c, ok := c.(*secureConn); ok {
if ok {
return c, nil return c, nil
} }
...@@ -29,26 +28,33 @@ func setupSecureConn(t *testing.T, ctx context.Context, c Conn) (Conn, error) { ...@@ -29,26 +28,33 @@ func setupSecureConn(t *testing.T, ctx context.Context, c Conn) (Conn, error) {
return s, nil return s, nil
} }
func secureHandshake(t *testing.T, ctx context.Context, c Conn, done chan error) {
_, err := upgradeToSecureConn(t, ctx, c)
done <- err
}
func TestSecureClose(t *testing.T) { func TestSecureClose(t *testing.T) {
// t.Skip("Skipping in favor of another test") // t.Skip("Skipping in favor of another test")
ctx := context.Background() ctx := context.Background()
c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/6634", "/ip4/127.0.0.1/tcp/6645") c1, c2 := setupSingleConn(t, ctx, "/ip4/127.0.0.1/tcp/6634", "/ip4/127.0.0.1/tcp/6645")
done := make(chan error)
go secureHandshake(t, ctx, c1, done)
go secureHandshake(t, ctx, c2, done)
c1, err1 := setupSecureConn(t, ctx, c1) for i := 0; i < 2; i++ {
c2, err2 := setupSecureConn(t, ctx, c2) if err := <-done; err != nil {
if err1 != nil { t.Error(err)
t.Fatal(err1)
} }
if err2 != nil {
t.Fatal(err2)
} }
testOneSendRecv(t, c1, c2) testOneSendRecv(t, c1, c2)
testOneSendRecv(t, c2, c1)
c1.Close() c1.Close()
testNotOneSendRecv(t, c1, c2)
c2.Close()
testNotOneSendRecv(t, c1, c2) testNotOneSendRecv(t, c1, c2)
testNotOneSendRecv(t, c2, c1) testNotOneSendRecv(t, c2, c1)
...@@ -57,23 +63,20 @@ func TestSecureClose(t *testing.T) { ...@@ -57,23 +63,20 @@ func TestSecureClose(t *testing.T) {
func TestSecureCancelHandshake(t *testing.T) { func TestSecureCancelHandshake(t *testing.T) {
// t.Skip("Skipping in favor of another test") // t.Skip("Skipping in favor of another test")
ctx := context.Background() ctx, cancel := context.WithCancel(context.Background())
c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/6634", "/ip4/127.0.0.1/tcp/6645") c1, c2 := setupSingleConn(t, ctx, "/ip4/127.0.0.1/tcp/6634", "/ip4/127.0.0.1/tcp/6645")
done := make(chan error)
go secureHandshake(t, ctx, c1, done)
<-time.After(50 * time.Millisecond)
cancel() // cancel ctx
go secureHandshake(t, ctx, c2, done)
done := make(chan struct{}) for i := 0; i < 2; i++ {
go func() { if err := <-done; err == nil {
_, err1 := setupSecureConn(t, ctx, c1) t.Error("cancel should've errored out")
_, err2 := setupSecureConn(t, ctx, c2)
if err1 == nil {
t.Fatal(err1)
} }
if err2 == nil {
t.Fatal(err2)
} }
done <- struct{}{}
}()
<-done
} }
func TestSecureCloseLeak(t *testing.T) { func TestSecureCloseLeak(t *testing.T) {
...@@ -92,16 +95,7 @@ func TestSecureCloseLeak(t *testing.T) { ...@@ -92,16 +95,7 @@ func TestSecureCloseLeak(t *testing.T) {
a1 := strconv.Itoa(p1) a1 := strconv.Itoa(p1)
a2 := strconv.Itoa(p2) a2 := strconv.Itoa(p2)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2) c1, c2 := setupSecureConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2)
c1, err1 := setupSecureConn(t, ctx, c1)
c2, err2 := setupSecureConn(t, ctx, c2)
if err1 != nil {
t.Fatal(err1)
}
if err2 != nil {
t.Fatal(err2)
}
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
b1 := []byte("beep") b1 := []byte("beep")
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论