-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestserver.go
More file actions
340 lines (305 loc) · 10 KB
/
testserver.go
File metadata and controls
340 lines (305 loc) · 10 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package tornago
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
)
const (
// testTorSocksAddr is the dedicated SocksPort used for Tornago integration tests.
testTorSocksAddr = "127.0.0.1:19050"
// testTorControlAddr is the dedicated ControlPort used for Tornago integration tests.
testTorControlAddr = "127.0.0.1:19051"
)
// TestServer wraps a TorProcess and Server for integration tests.
type TestServer struct {
// Process points to the TorProcess launched for tests.
Process *TorProcess
// Server exposes the Socks/Control addresses of the launched Tor instance.
Server Server
// t holds the testing context for logging/failures.
t *testing.T
// clientMu protects lazy client creation and shutdown.
clientMu sync.Mutex
// client caches the Client instance connected to this server.
client *Client
// controlAuth stores credentials for ControlPort access.
controlAuth ControlAuth
}
// StartTestServer launches a Tor daemon for tests using a project-local DataDirectory
// and dedicated ports, skipping if tor is unavailable.
func StartTestServer(t *testing.T) *TestServer {
t.Helper()
// Use external Tor if configured via env.
if ctrl := os.Getenv("TORNAGO_TOR_CONTROL"); ctrl != "" {
return startExternalTestServer(t, ctrl)
}
home := os.Getenv("HOME")
if home == "" {
t.Fatalf("tornago: HOME environment variable is not set")
}
baseDir := filepath.Join(home, ".cache", "tornago-test")
if err := os.MkdirAll(baseDir, 0o700); err != nil {
t.Fatalf("tornago: failed to create base tor directory: %v", err)
}
dataDir := filepath.Join(baseDir, fmt.Sprintf("test-%d", time.Now().UnixNano()))
if err := os.MkdirAll(dataDir, 0o700); err != nil {
t.Fatalf("tornago: failed to create tor data directory: %v", err)
}
cookiePath := filepath.Join(dataDir, "control_auth_cookie")
torrcPath := filepath.Join(baseDir, fmt.Sprintf("torrc-%d", time.Now().UnixNano()))
torrc := fmt.Sprintf(`
SocksPort %s
ControlPort %s
DataDirectory %s
CookieAuthentication 1
CookieAuthFile %s
ClientUseIPv6 0
RunAsDaemon 0
Log notice stdout
`, testTorSocksAddr, testTorControlAddr, dataDir, cookiePath)
if err := os.WriteFile(torrcPath, []byte(strings.TrimSpace(torrc)+"\n"), 0o600); err != nil {
t.Fatalf("tornago: failed to write torrc: %v", err)
}
bootstrapped := make(chan struct{}, 1)
launchCfg, err := NewTorLaunchConfig(
WithTorDataDir(dataDir),
WithTorSocksAddr(testTorSocksAddr),
WithTorControlAddr(testTorControlAddr),
WithTorConfigFile(torrcPath),
)
if err != nil {
t.Fatalf("tornago: failed to build launch config: %v", err)
}
process, err := StartTorDaemon(launchCfg)
if err != nil {
var te *TornagoError
if errors.As(err, &te) && te.Kind == ErrTorBinaryNotFound {
t.Skipf("tornago: skipping because tor binary not found: %v", err)
}
t.Fatalf("tornago: failed to start tor daemon: %v", err)
}
// Wait for cookie file (Tor creates it during startup)
if err := waitForCookieFile(cookiePath, 60*time.Second); err != nil {
t.Logf("tornago: skipping integration test because control cookie is unavailable: %v", err)
t.SkipNow()
}
// Obtain control cookie & auth; this function should internally retry
// until PROTOCOLINFO succeeds and cookie can be read.
controlAuth, cookiePath, err := ControlAuthFromTor(process.ControlAddr(), 30*time.Second)
if err != nil {
t.Logf("tornago: skipping integration test because control cookie could not be read: %v", err)
t.SkipNow()
}
t.Logf("tornago: control cookie path %s", cookiePath)
serverCfg, err := NewServerConfig(
WithServerSocksAddr(process.SocksAddr()),
WithServerControlAddr(process.ControlAddr()),
)
if err != nil {
if stopErr := process.Stop(); stopErr != nil {
t.Logf("tornago: failed to stop tor after server config error: %v", stopErr)
}
t.Fatalf("tornago: failed to build server config: %v", err)
}
server, err := NewServer(serverCfg)
if err != nil {
if stopErr := process.Stop(); stopErr != nil {
t.Logf("tornago: failed to stop tor after server init error: %v", stopErr)
}
t.Fatalf("tornago: failed to build server: %v", err)
}
// Explicitly wait until Tor reports bootstrap 100% via control port.
// Use a generous timeout since bootstrap can take several minutes depending on network conditions
if err := waitForTorBootstrap(process.ControlAddr(), controlAuth, 5*time.Minute); err != nil {
t.Logf("tornago: skipping integration test because tor failed to bootstrap: %v", err)
t.SkipNow()
}
// Also wait for the log-based bootstrap signal
select {
case <-bootstrapped:
t.Log("tornago: bootstrap 100% confirmed via logs")
case <-time.After(5 * time.Second):
// This is OK - we already verified via control port
}
return &TestServer{
Process: process,
Server: server,
t: t,
controlAuth: controlAuth,
}
}
// waitForCookieFile polls for the existence of the control cookie file at the given path.
// It returns nil when the file exists and has non-zero size, or an error if the timeout expires.
func waitForCookieFile(path string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
info, err := os.Stat(path)
if err == nil {
if info.Size() > 0 {
return nil
}
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
time.Sleep(100 * time.Millisecond)
}
return fmt.Errorf("timed out waiting for cookie file %s", path)
}
// Client returns a Client configured to use the started Tor instance.
func (ts *TestServer) Client(t *testing.T) *Client {
t.Helper()
ts.clientMu.Lock()
defer ts.clientMu.Unlock()
if ts.client != nil {
return ts.client
}
cfg, err := NewClientConfig(
WithClientSocksAddr(ts.Server.SocksAddr()),
WithClientRequestTimeout(5*time.Minute),
WithClientDialTimeout(5*time.Minute),
)
if err != nil {
t.Fatalf("tornago: failed to build client config: %v", err)
}
client, err := NewClient(cfg)
if err != nil {
t.Fatalf("tornago: failed to create client: %v", err)
}
ts.client = client
return client
}
// Close shuts down the client and Tor process launched for tests.
func (ts *TestServer) Close() {
if ts == nil {
return
}
ts.clientMu.Lock()
client := ts.client
ts.client = nil
ts.clientMu.Unlock()
if client != nil {
_ = client.Close()
// Silently ignore errors during cleanup
}
if ts.Process != nil {
_ = ts.Process.Stop() //nolint:errcheck // Silently ignore errors during cleanup
ts.Process = nil
}
}
// ControlAuth returns ControlPort credentials for this TestServer.
func (ts *TestServer) ControlAuth(t *testing.T) ControlAuth {
t.Helper()
return ts.controlAuth
}
// waitForTorBootstrap polls Tor's control port until bootstrap reaches 100% or timeout.
// It returns nil on successful bootstrap, or an error describing the last failure.
func waitForTorBootstrap(controlAddr string, auth ControlAuth, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
var lastErr error
for time.Now().Before(deadline) {
client, err := NewControlClient(controlAddr, auth, 5*time.Second)
if err != nil {
lastErr = err
time.Sleep(200 * time.Millisecond)
continue
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
info, infoErr := client.GetInfo(ctx, "status/bootstrap-phase")
cancel()
_ = client.Close()
if infoErr == nil {
if progress, ok := parseBootstrapProgress(info); ok {
if progress == 100 {
return nil
}
lastErr = fmt.Errorf("bootstrap progress %d%%", progress)
} else {
lastErr = errors.New("tor not fully bootstrapped")
}
} else {
lastErr = infoErr
}
time.Sleep(200 * time.Millisecond)
}
if lastErr == nil {
lastErr = errors.New("timed out waiting for tor bootstrap")
}
return fmt.Errorf("tor failed to bootstrap: %w", lastErr)
}
// parseBootstrapProgress extracts the PROGRESS value from a Tor bootstrap status string.
// Returns the progress percentage and true if successfully parsed, or 0 and false otherwise.
func parseBootstrapProgress(info string) (int, bool) {
idx := strings.LastIndex(info, "PROGRESS=")
if idx < 0 {
return 0, false
}
start := idx + len("PROGRESS=")
end := start
for end < len(info) && info[end] >= '0' && info[end] <= '9' {
end++
}
if start == end {
return 0, false
}
progress, err := strconv.Atoi(info[start:end])
if err != nil {
return 0, false
}
return progress, true
}
// startExternalTestServer creates a TestServer using an externally running Tor instance.
// It expects TORNAGO_TOR_SOCKS and either TORNAGO_TOR_COOKIE or TORNAGO_TOR_PASSWORD env vars.
func startExternalTestServer(t *testing.T, controlAddr string) *TestServer {
t.Helper()
socksAddr := os.Getenv("TORNAGO_TOR_SOCKS")
if socksAddr == "" {
t.Skip("tornago: TORNAGO_TOR_SOCKS not set")
}
cookiePath := os.Getenv("TORNAGO_TOR_COOKIE")
password := os.Getenv("TORNAGO_TOR_PASSWORD")
if cookiePath != "" && password != "" {
t.Fatalf("tornago: set either TORNAGO_TOR_COOKIE or TORNAGO_TOR_PASSWORD, not both")
}
var controlAuth ControlAuth
switch {
case password != "":
controlAuth = ControlAuthFromPassword(password)
case cookiePath != "":
data, err := os.ReadFile(filepath.Clean(cookiePath))
if err != nil {
t.Fatalf("tornago: failed to read control cookie %s: %v", cookiePath, err)
}
controlAuth = ControlAuthFromCookieBytes(data)
default:
t.Fatalf("tornago: TORNAGO_TOR_COOKIE or TORNAGO_TOR_PASSWORD must be set")
}
serverCfg, err := NewServerConfig(
WithServerSocksAddr(socksAddr),
WithServerControlAddr(controlAddr),
)
if err != nil {
t.Fatalf("tornago: failed to build server config: %v", err)
}
server, err := NewServer(serverCfg)
if err != nil {
t.Fatalf("tornago: failed to build server: %v", err)
}
if err := WaitForControlPort(controlAddr, 30*time.Second); err != nil {
t.Skipf("tornago: skipping integration test (external tor control port unavailable): %v", err)
}
if err := waitForTorBootstrap(controlAddr, controlAuth, 5*time.Minute); err != nil {
t.Skipf("tornago: skipping integration test (external tor failed to bootstrap): %v", err)
}
return &TestServer{
Server: server,
t: t,
controlAuth: controlAuth,
}
}