File: tor_isolate_socks_auth_test.go

package info (click to toggle)
golang-github-cretz-bine 0.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, sid, trixie
  • size: 652 kB
  • sloc: makefile: 3
file content (72 lines) | stat: -rw-r--r-- 2,520 bytes parent folder | download
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
package tests

import (
	"context"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/cretz/bine/tor"
	"golang.org/x/net/proxy"
)

// TestIsolateSocksAuth simply confirms the functionality of IsolateSOCKSAuth,
// namely that it uses a different circuit for different SOCKS credentials.
func TestIsolateSocksAuth(t *testing.T) {
	// Create context w/ no isolate
	ctx := NewTestContext(t, &tor.StartConf{
		NoAutoSocksPort: true,
		ExtraArgs:       []string{"--SocksPort", "auto NoIsolateSOCKSAuth"},
	})
	// Make sure it reused the circuit (i.e. only has one) for both separate-auth calls
	uniqueCircuitIDs := doSeparateAuthHttpCalls(ctx)
	ctx.Debugf("Unique IDs without isolate: %v", uniqueCircuitIDs)
	ctx.Require.Len(uniqueCircuitIDs, 1)
	// Create context w/ isolate
	ctx = NewTestContext(t, nil)
	// Make sure it made a new circuit (i.e. has two) for each separate-auth call
	uniqueCircuitIDs = doSeparateAuthHttpCalls(ctx)
	ctx.Debugf("Unique IDs with isolate: %v", uniqueCircuitIDs)
	ctx.Require.Len(uniqueCircuitIDs, 2)
}

// Returns the map keyed with unique circuit IDs after second separate-auth HTTP call
func doSeparateAuthHttpCalls(ctx *TestContext) map[int]struct{} {
	defer ctx.Close()
	enableCtx, enableCancel := context.WithTimeout(ctx, 100*time.Second)
	defer enableCancel()
	ctx.Require.NoError(ctx.EnableNetwork(enableCtx, true))
	// Make HTTP call w/ an auth
	client := httpClient(ctx, &tor.DialConf{ProxyAuth: &proxy.Auth{"foo", "bar"}})
	byts := httpGet(ctx, client, "https://check.torproject.org/api/ip")
	ctx.Debugf("Read bytes: %v", string(byts))
	// Confirm just size 1
	ids := uniqueStreamCircuitIDs(ctx)
	ctx.Require.Len(ids, 1)
	// Now make call with another auth and just return circuit IDs
	client = httpClient(ctx, &tor.DialConf{ProxyAuth: &proxy.Auth{"baz", "qux"}})
	byts = httpGet(ctx, client, "https://check.torproject.org/api/ip")
	ctx.Debugf("Read bytes: %v", string(byts))
	return uniqueStreamCircuitIDs(ctx)
}

// Return each stream circuit as a key of an empty-val map
func uniqueStreamCircuitIDs(ctx *TestContext) map[int]struct{} {
	ret := map[int]struct{}{}
	vals, err := ctx.Control.GetInfo("stream-status")
	ctx.Require.NoError(err)
	for _, val := range vals {
		ctx.Require.Equal("stream-status", val.Key)
		for _, line := range strings.Split(val.Val, "\n") {
			pieces := strings.Split(strings.TrimSpace(line), " ")
			if len(pieces) < 3 {
				continue
			}
			i, err := strconv.Atoi(pieces[2])
			ctx.Require.NoError(err)
			ret[i] = struct{}{}
		}
	}
	return ret
}