提交 a3a48ce6 作者: Juan Batiz-Benet

conn: tests pass :)

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