secure_conn_test.go 2.96 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package conn

import (
	"bytes"
	"fmt"
	"runtime"
	"strconv"
	"sync"
	"testing"
	"time"

	peer "github.com/jbenet/go-ipfs/peer"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)

func setupSecureConn(t *testing.T, c Conn) Conn {
	c, ok := c.(*secureConn)
	if ok {
		return c
	}

	// shouldn't happen, because dial + listen already return secure conns.
	s, err := newSecureConn(c.Context(), c, peer.NewPeerstore())
	if err != nil {
		t.Fatal(err)
	}
	return s
}

func TestSecureClose(t *testing.T) {
32
	// t.Skip("Skipping in favor of another test")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34 35 36 37 38 39 40

	ctx, cancel := context.WithCancel(context.Background())
	c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/1234", "/ip4/127.0.0.1/tcp/2345")

	c1 = setupSecureConn(t, c1)
	c2 = setupSecureConn(t, c2)

	select {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	case <-c1.Closed():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
		t.Fatal("done before close")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43
	case <-c2.Closed():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46 47 48 49 50
		t.Fatal("done before close")
	default:
	}

	c1.Close()

	select {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
	case <-c1.Closed():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
	default:
53
		t.Fatal("not done after close")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55 56 57 58
	}

	c2.Close()

	select {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
	case <-c2.Closed():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
	default:
61
		t.Fatal("not done after close")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63 64 65 66 67
	}

	cancel() // close the listener :P
}

func TestSecureCancel(t *testing.T) {
68
	// t.Skip("Skipping in favor of another test")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70 71 72 73 74 75 76

	ctx, cancel := context.WithCancel(context.Background())
	c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/1234", "/ip4/127.0.0.1/tcp/2345")

	c1 = setupSecureConn(t, c1)
	c2 = setupSecureConn(t, c2)

	select {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
	case <-c1.Closed():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78
		t.Fatal("done before close")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
	case <-c2.Closed():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82 83 84 85 86 87 88 89 90
		t.Fatal("done before close")
	default:
	}

	cancel()

	// wait to ensure other goroutines run and close things.
	<-time.After(time.Microsecond * 10)
	// test that cancel called Close.

	select {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
	case <-c1.Closed():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93 94 95 96
	default:
		t.Fatal("not done after cancel")
	}

	select {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97
	case <-c2.Closed():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98 99 100 101 102 103 104
	default:
		t.Fatal("not done after cancel")
	}

}

func TestSecureCloseLeak(t *testing.T) {
105
	// t.Skip("Skipping in favor of another test")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106 107 108 109 110 111 112 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 149 150 151 152 153 154 155 156 157

	var wg sync.WaitGroup

	runPair := func(p1, p2, num int) {
		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 = setupSecureConn(t, c1)
		c2 = setupSecureConn(t, c2)

		for i := 0; i < num; i++ {
			b1 := []byte("beep")
			c1.Out() <- b1
			b2 := <-c2.In()
			if !bytes.Equal(b1, b2) {
				panic("bytes not equal")
			}

			b2 = []byte("boop")
			c2.Out() <- b2
			b1 = <-c1.In()
			if !bytes.Equal(b1, b2) {
				panic("bytes not equal")
			}

			<-time.After(time.Microsecond * 5)
		}

		cancel() // close the listener
		wg.Done()
	}

	var cons = 20
	var msgs = 100
	fmt.Printf("Running %d connections * %d msgs.\n", cons, msgs)
	for i := 0; i < cons; i++ {
		wg.Add(1)
		go runPair(2000+i, 2001+i, msgs)
	}

	fmt.Printf("Waiting...\n")
	wg.Wait()
	// done!

	<-time.After(time.Microsecond * 100)
	if runtime.NumGoroutine() > 10 {
		// panic("uncomment me to debug")
		t.Fatal("leaking goroutines:", runtime.NumGoroutine())
	}
}