Repository navigation
Expand file tree
/
Copy pathproxy.go
More file actions
71 lines (61 loc) · 1.59 KB
/
Copy pathproxy.go
File metadata and controls
71 lines (61 loc) · 1.59 KB
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
package proxy
import (
"io"
"net"
"testing"
"github.com/getlantern/testify/assert"
)
var (
ping = []byte("ping")
pong = []byte("pong")
)
// Dialer is an interface for types that allowing connecting to some destination
// via an intervening proxy (or proxies).
type Dialer interface {
// Dial: function that dials a given destination using the proxy.
Dial(network, addr string) (net.Conn, error)
// Close: closes the dialer and any underlying resources
Close() error
}
// Test tests a Dialer.
func Test(t *testing.T, dialer Dialer) {
// Set up listener for server endpoint
sl, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Unable to listen: %s", err)
}
// Server that responds to ping
go func() {
conn, err := sl.Accept()
if err != nil {
t.Fatalf("Unable to accept connection: %s", err)
return
}
defer conn.Close()
b := make([]byte, 4)
_, err = io.ReadFull(conn, b)
if err != nil {
t.Fatalf("Unable to read from client: %s", err)
}
assert.Equal(t, ping, b, "Didn't receive correct ping message")
_, err = conn.Write(pong)
if err != nil {
t.Fatalf("Unable to write to client: %s", err)
}
}()
conn, err := dialer.Dial(sl.Addr().Network(), sl.Addr().String())
if err != nil {
t.Fatalf("Unable to dial via proxy: %s", err)
}
defer conn.Close()
_, err = conn.Write(ping)
if err != nil {
t.Fatalf("Unable to write to server via proxy: %s", err)
}
b := make([]byte, 4)
_, err = io.ReadFull(conn, b)
if err != nil {
t.Fatalf("Unable to read from server: %s", err)
}
assert.Equal(t, pong, b, "Didn't receive correct pong message")
}