-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage_extra_test.go
More file actions
186 lines (168 loc) · 4.73 KB
/
Copy pathcoverage_extra_test.go
File metadata and controls
186 lines (168 loc) · 4.73 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
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
package singproxy
import (
"net/http"
"net/url"
"strings"
"testing"
"github.com/sagernet/sing-box/option"
)
// --- naive parser ---
// The naive outbound requires the with_naive_outbound build tag; the
// parser itself is always available, so without the tag the failure
// must come from outbound creation, never from parsing.
func TestParseNaiveParserReachable(t *testing.T) {
_, err := FromURL(Config{}, "naive+https://user:pass@example.com:443")
if err == nil {
return // build includes naive
}
const stub = "naive outbound is not included in this build"
if !strings.Contains(err.Error(), stub) {
t.Fatalf("expected outbound-creation error, got: %v", err)
}
}
func TestParseNaiveNoCredsRejected(t *testing.T) {
u := mustParseURLForTest("naive+https://example.com:443")
out := newNaiveOptionsForTest()
if err := parseNaive(out, u); err == nil {
t.Error("expected error for naive without credentials")
}
}
func TestParseNaiveSecurityNoneDisablesTLS(t *testing.T) {
u := mustParseURLForTest("naive+https://user:pass@example.com:443?security=none")
out := newNaiveOptionsForTest()
if err := parseNaive(out, u); err != nil {
t.Fatal(err)
}
if out.TLS != nil {
t.Error("security=none must disable TLS")
}
}
func mustParseURLForTest(raw string) *url.URL {
u, err := url.Parse(raw)
if err != nil {
panic(err)
}
return u
}
func newNaiveOptionsForTest() *option.NaiveOutboundOptions {
return &option.NaiveOutboundOptions{}
}
// --- browser masquerade variants ---
func TestApplyMasqueradedHeadersAllBrowsers(t *testing.T) {
for _, ua := range []string{"chrome", "firefox", "safari", "edge", "curl", "golang"} {
c := &xhttpConfig{}
h := http.Header{}
h.Set("User-Agent", ua)
c.applyMasqueradedHeaders(h, "fetch")
switch ua {
case "golang":
if h.Get("User-Agent") != "" {
t.Errorf("golang variant must strip User-Agent, got %q", h.Get("User-Agent"))
}
case "curl":
if h.Get("User-Agent") == "" {
t.Error("curl variant must set a UA")
}
default:
if h.Get("User-Agent") == "" {
t.Errorf("%s variant must set a UA", ua)
}
if h.Get("Sec-Fetch-Mode") == "" {
t.Errorf("%s fetch variant missing Sec-Fetch-Mode", ua)
}
}
}
}
func TestApplyMasqueradedHeadersVariants(t *testing.T) {
for _, variant := range []string{"nav", "ws", "fetch"} {
c := &xhttpConfig{}
h := http.Header{}
c.applyMasqueradedHeaders(h, variant)
if variant == "nav" && h.Get("Sec-Fetch-Mode") != "navigate" {
t.Errorf("nav variant Sec-Fetch-Mode = %q", h.Get("Sec-Fetch-Mode"))
}
if variant == "ws" && h.Get("Sec-Fetch-Mode") != "websocket" {
t.Errorf("ws variant Sec-Fetch-Mode = %q", h.Get("Sec-Fetch-Mode"))
}
}
}
// --- reuse manager ---
func TestReuseManagerPickAndClose(t *testing.T) {
made := 0
m := &xhttpReuseManager{
maxConnections: 2,
maker: func() http.RoundTripper {
made++
return &staticResponseTripper{status: http.StatusOK}
},
}
rt1 := m.GetTransport()
rt2 := m.GetTransport()
rt3 := m.GetTransport()
if rt1 == nil || rt2 == nil || rt3 == nil {
t.Fatal("GetTransport must not return nil")
}
closer1, ok := rt1.(interface{ Close() error })
if !ok {
t.Fatal("GetTransport result must implement Close")
}
closer2, _ := rt2.(interface{ Close() error })
closer3, _ := rt3.(interface{ Close() error })
_ = closer1.Close()
_ = closer2.Close()
_ = closer3.Close()
if err := m.Close(); err != nil {
t.Fatal(err)
}
// Double close must be safe.
if err := m.Close(); err != nil {
t.Fatal(err)
}
}
func TestXHTTPReuseTransportRoundTrip(t *testing.T) {
tr := &staticResponseTripper{status: http.StatusOK}
entry := &xhttpReuseEntry{}
entry.leftRequests.Store(10)
rt := &xhttpReuseTransport{entry: entry}
entry.transport = tr
req, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
resp, err := rt.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("status = %d", resp.StatusCode)
}
_ = rt.Close()
_ = rt.Close() // double close safe
}
// --- greased CH order ---
func TestXHTTPGetGreasedChOrder(t *testing.T) {
for _, n := range []int{1, 2, 3, 4} {
for seed := 0; seed < 4; seed++ {
order := xhttpGetGreasedChOrder(n, seed)
if len(order) != n {
t.Errorf("n=%d seed=%d: got %d entries, want %d", n, seed, len(order), n)
}
seen := make(map[int]bool, n)
for _, v := range order {
if v < 0 || v >= n {
t.Errorf("n=%d seed=%d: out of range %d", n, seed, v)
}
if seen[v] {
t.Errorf("n=%d seed=%d: duplicate %d", n, seed, v)
}
seen[v] = true
}
}
}
}
// --- direct proxy ---
func TestDirectProxyInterface(t *testing.T) {
if Direct.String() != "direct" {
t.Errorf("String() = %q", Direct.String())
}
if err := Direct.Close(); err != nil {
t.Errorf("Close() = %v", err)
}
}