-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport_test.go
199 lines (172 loc) · 4.64 KB
/
transport_test.go
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
192
193
194
195
196
197
198
199
package netjail_test
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/netip"
"net/url"
"reflect"
"sync"
"sync/atomic"
"testing"
"github.com/stealthrocket/netjail"
)
func TestTransport(t *testing.T) {
const (
url1 = "http://server1.local"
url2 = "http://server2.local"
addr1 = "127.0.0.1"
addr2 = "10.0.1.2"
cidr1 = "127.0.0.0/8"
cidr2 = "10.0.0.0/16"
cidr3 = "0.0.0.0/0"
)
server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer server1.Close()
server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer server2.Close()
error1 := &url.Error{
Op: "Get",
URL: url1,
Err: &net.OpError{Op: "dial", Net: "tcp", Addr: &net.IPAddr{IP: net.IP{127, 0, 0, 1}}, Err: netjail.ErrDenied},
}
error2 := &url.Error{
Op: "Get",
URL: url2,
Err: &net.OpError{Op: "dial", Net: "tcp", Addr: &net.IPAddr{IP: net.IP{10, 0, 1, 2}}, Err: netjail.ErrDenied},
}
tests := []struct {
scenario string
rules netjail.Rules
error1 error
error2 error
}{
{
scenario: "the default rules denies all addresses",
rules: netjail.Rules{},
error1: error1,
error2: error2,
},
{
scenario: "requests to server 1 are allowed when the rules allow it",
rules: netjail.Rules{
Allow: []netip.Prefix{netip.MustParsePrefix(cidr1)},
},
error1: nil,
error2: error2,
},
{
scenario: "requests to server 2 are allowed when the rules allow it",
rules: netjail.Rules{
Allow: []netip.Prefix{netip.MustParsePrefix(cidr2)},
},
error1: error1,
error2: nil,
},
{
scenario: "requests to both servers are allowed when the rules allow it",
rules: netjail.Rules{
Allow: []netip.Prefix{
netip.MustParsePrefix(cidr1),
netip.MustParsePrefix(cidr2),
},
},
error1: nil,
error2: nil,
},
{
scenario: "requests to server 1 are denied when the rules block it",
rules: netjail.Rules{
Allow: []netip.Prefix{netip.MustParsePrefix(cidr3)},
Block: []netip.Prefix{netip.MustParsePrefix(cidr1)},
},
error1: error1,
error2: nil,
},
{
scenario: "requests to server 2 are denied when the rules block it",
rules: netjail.Rules{
Allow: []netip.Prefix{netip.MustParsePrefix(cidr3)},
Block: []netip.Prefix{netip.MustParsePrefix(cidr2)},
},
error1: nil,
error2: error2,
},
}
dial := new(net.Dialer).DialContext
mutex := sync.Mutex{}
conns := []*connTrackingCloseCall{}
transport := &netjail.Transport{
New: func() *http.Transport {
return &http.Transport{
DialContext: func(ctx context.Context, network, address string) (c net.Conn, err error) {
addr, _, _ := net.SplitHostPort(address)
switch addr {
case addr1:
c, err = dial(ctx, network, server1.URL[7:])
case addr2:
c, err = dial(ctx, network, server2.URL[7:])
default:
return nil, &net.OpError{Op: "dial", Net: network, Err: fmt.Errorf("unknown address: %s", address)}
}
if err != nil {
return nil, err
}
conn := &connTrackingCloseCall{Conn: c}
mutex.Lock()
conns = append(conns, conn)
mutex.Unlock()
return conn, nil
},
}
},
Resolver: resolverFunc(func(ctx context.Context, network, host string) ([]netip.Addr, error) {
switch host {
case url1[7:]:
return []netip.Addr{netip.MustParseAddr(addr1)}, nil
case url2[7:]:
return []netip.Addr{netip.MustParseAddr(addr2)}, nil
default:
return nil, &net.DNSError{Err: "no such host", Name: host, IsNotFound: true}
}
}),
}
defer func() {
transport.CloseIdleConnections()
mutex.Lock()
defer mutex.Unlock()
for _, conn := range conns {
if !conn.closed.Load() {
t.Errorf("expected connection to be closed but %s was not", conn.RemoteAddr())
}
}
}()
for _, test := range tests {
t.Run(test.scenario, func(t *testing.T) {
client := &http.Client{Transport: transport}
ctx := netjail.ContextWithRules(context.Background(), &test.rules)
req1, _ := http.NewRequestWithContext(ctx, http.MethodGet, url1, nil)
req2, _ := http.NewRequestWithContext(ctx, http.MethodGet, url2, nil)
if r, err := client.Do(req1); !reflect.DeepEqual(err, test.error1) {
t.Errorf("expected error %v, got %v", test.error1, err)
} else if r != nil {
r.Body.Close()
}
if r, err := client.Do(req2); !reflect.DeepEqual(err, test.error2) {
t.Errorf("expected error %v, got %v", test.error2, err)
} else if r != nil {
r.Body.Close()
}
})
}
}
type connTrackingCloseCall struct {
net.Conn
closed atomic.Bool
}
func (c *connTrackingCloseCall) Close() error {
c.closed.Store(true)
return c.Conn.Close()
}