File: tcpproxy_test.go

package info (click to toggle)
golang-github-juju-testing 0.0~git20170608.2fe0e88-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 464 kB
  • sloc: makefile: 6
file content (191 lines) | stat: -rw-r--r-- 4,530 bytes parent folder | download
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package testing_test

import (
	"fmt"
	"io"
	"net"
	"sync"
	"time"

	"github.com/juju/testing"
	gc "gopkg.in/check.v1"
)

var _ = gc.Suite(&tcpProxySuite{})

type tcpProxySuite struct{}

func (*tcpProxySuite) TestTCPProxy(c *gc.C) {
	var wg sync.WaitGroup

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	c.Assert(err, gc.IsNil)
	defer listener.Close()
	wg.Add(1)
	go tcpEcho(&wg, listener)

	p := testing.NewTCPProxy(c, listener.Addr().String())
	c.Assert(p.Addr(), gc.Not(gc.Equals), listener.Addr().String())

	// Dial the proxy and check that we see the text echoed correctly.
	conn, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn.Close()

	assertEcho(c, conn)

	// Close the connection and check that we see
	// the connection closed for read.
	conn.(*net.TCPConn).CloseWrite()
	assertEOF(c, conn)

	// Make another connection and close the proxy,
	// which should close down the proxy and cause us
	// to get an error.
	conn, err = net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn.Close()

	p.Close()
	assertEOF(c, conn)

	// Make sure that we cannot dial the proxy address either.
	conn, err = net.Dial("tcp", p.Addr())
	c.Assert(err, gc.ErrorMatches, ".*connection refused")

	listener.Close()
	// Make sure that all our connections have gone away too.
	wg.Wait()
}

func (*tcpProxySuite) TestCloseConns(c *gc.C) {
	var wg sync.WaitGroup

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	c.Assert(err, gc.IsNil)
	defer listener.Close()
	wg.Add(1)
	go tcpEcho(&wg, listener)

	p := testing.NewTCPProxy(c, listener.Addr().String())
	c.Assert(p.Addr(), gc.Not(gc.Equals), listener.Addr().String())

	// Make a couple of connections through the proxy
	// and test that they work.
	conn1, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn1.Close()
	assertEcho(c, conn1)

	conn2, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn1.Close()
	assertEcho(c, conn1)

	p.CloseConns()

	// Assert that both the connections have been broken.
	assertEOF(c, conn1)
	assertEOF(c, conn2)

	// Check that we can still make a connection.
	conn3, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn3.Close()
	assertEcho(c, conn3)

	// Close the proxy and check that the last connection goes.
	p.Close()
	assertEOF(c, conn3)

	listener.Close()
	// Make sure that all our connections have gone away too.
	wg.Wait()
}

func (*tcpProxySuite) TestPauseConns(c *gc.C) {
	var wg sync.WaitGroup

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	c.Assert(err, gc.IsNil)
	defer listener.Close()
	wg.Add(1)
	go tcpEcho(&wg, listener)

	p := testing.NewTCPProxy(c, listener.Addr().String())
	c.Assert(p.Addr(), gc.Not(gc.Equals), listener.Addr().String())

	// Make a connection through the proxy
	// and test that it works.
	conn, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn.Close()
	assertEcho(c, conn)

	p.PauseConns()

	msg := "hello, world\n"
	n, err := fmt.Fprint(conn, msg)
	c.Assert(err, gc.IsNil)
	c.Assert(n, gc.Equals, len(msg))
	assertReadTimeout(c, conn)

	p.ResumeConns()

	buf := make([]byte, n)
	n, err = conn.Read(buf)
	c.Assert(err, gc.IsNil)
	c.Assert(n, gc.Equals, len(msg))
	c.Assert(string(buf), gc.Equals, msg)
}

// tcpEcho listens on the given listener for TCP connections,
// writes all traffic received back to the sender, and calls
// wg.Done when all its goroutines have completed.
func tcpEcho(wg *sync.WaitGroup, listener net.Listener) {
	defer wg.Done()
	for {
		conn, err := listener.Accept()
		if err != nil {
			return
		}
		wg.Add(1)
		go func() {
			defer wg.Done()
			defer conn.Close()
			// Echo anything that was written.
			io.Copy(conn, conn)
		}()
	}
}

func assertEcho(c *gc.C, conn net.Conn) {
	txt := "hello, world\n"
	fmt.Fprint(conn, txt)

	buf := make([]byte, len(txt))
	n, err := io.ReadFull(conn, buf)
	c.Assert(err, gc.IsNil)
	c.Assert(string(buf[0:n]), gc.Equals, txt)
}

func assertEOF(c *gc.C, r io.Reader) {
	n, err := r.Read(make([]byte, 1))
	c.Assert(err, gc.Equals, io.EOF)
	c.Assert(n, gc.Equals, 0)
}

func assertReadTimeout(c *gc.C, conn net.Conn) {
	err := conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
	c.Assert(err, gc.IsNil)
	defer conn.SetReadDeadline(time.Time{})
	buf := make([]byte, 1)
	n, err := conn.Read(buf)
	c.Assert(n, gc.Equals, 0)
	nerr, ok := err.(net.Error)
	c.Assert(ok, gc.Equals, true)
	c.Assert(nerr.Timeout(), gc.Equals, true)
}