-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathper_host_test.go
140 lines (130 loc) · 3.01 KB
/
per_host_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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package proxy
import (
"context"
"errors"
"fmt"
"net"
"slices"
"testing"
)
type recordingProxy struct {
addrs []string
}
func (r *recordingProxy) Dial(network, addr string) (net.Conn, error) {
r.addrs = append(r.addrs, addr)
return nil, errors.New("recordingProxy")
}
func TestPerHost(t *testing.T) {
for _, test := range []struct {
config string // passed to PerHost.AddFromString
nomatch []string // addrs using the default dialer
match []string // addrs using the bypass dialer
}{{
config: "localhost,*.zone,127.0.0.1,10.0.0.1/8,1000::/16",
nomatch: []string{
"example.com:123",
"1.2.3.4:123",
"[1001::]:123",
},
match: []string{
"localhost:123",
"zone:123",
"foo.zone:123",
"127.0.0.1:123",
"10.1.2.3:123",
"[1000::]:123",
"[1000::%25.example.com]:123",
},
}, {
config: "localhost",
nomatch: []string{
"127.0.0.1:80",
},
match: []string{
"localhost:80",
},
}, {
config: "*.zone",
nomatch: []string{
"foo.com:80",
},
match: []string{
"foo.zone:80",
"foo.bar.zone:80",
},
}, {
config: "1.2.3.4",
nomatch: []string{
"127.0.0.1:80",
"11.2.3.4:80",
},
match: []string{
"1.2.3.4:80",
},
}, {
config: "10.0.0.0/24",
nomatch: []string{
"10.0.1.1:80",
},
match: []string{
"10.0.0.1:80",
"10.0.0.255:80",
},
}, {
config: "fe80::/10",
nomatch: []string{
"[fec0::1]:80",
"[fec0::1%en0]:80",
},
match: []string{
"[fe80::1]:80",
"[fe80::1%en0]:80",
},
}, {
// We don't allow zone IDs in network prefixes,
// so this config matches nothing.
config: "fe80::%en0/10",
nomatch: []string{
"[fec0::1]:80",
"[fec0::1%en0]:80",
"[fe80::1]:80",
"[fe80::1%en0]:80",
"[fe80::1%en1]:80",
},
}} {
for _, addr := range test.match {
testPerHost(t, test.config, addr, true)
}
for _, addr := range test.nomatch {
testPerHost(t, test.config, addr, false)
}
}
}
func testPerHost(t *testing.T, config, addr string, wantMatch bool) {
name := fmt.Sprintf("config %q, dial %q", config, addr)
var def, bypass recordingProxy
perHost := NewPerHost(&def, &bypass)
perHost.AddFromString(config)
perHost.Dial("tcp", addr)
// Dial and DialContext should have the same results.
var defc, bypassc recordingProxy
perHostc := NewPerHost(&defc, &bypassc)
perHostc.AddFromString(config)
perHostc.DialContext(context.Background(), "tcp", addr)
if !slices.Equal(def.addrs, defc.addrs) {
t.Errorf("%v: Dial default=%v, bypass=%v; DialContext default=%v, bypass=%v", name, def.addrs, bypass.addrs, defc.addrs, bypass.addrs)
return
}
if got, want := slices.Concat(def.addrs, bypass.addrs), []string{addr}; !slices.Equal(got, want) {
t.Errorf("%v: dialed %q, want %q", name, got, want)
return
}
gotMatch := len(bypass.addrs) > 0
if gotMatch != wantMatch {
t.Errorf("%v: matched=%v, want %v", name, gotMatch, wantMatch)
return
}
}