Debian Codegraph @

tests/tor_isolate_socks_auth_test.go

raw symbols SCIP
1 package tests
2
3 import (
4 "context"
5 "strconv"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/cretz/bine/tor"
11 "golang.org/x/net/proxy"
12 )
13
14 // TestIsolateSocksAuth simply confirms the functionality of IsolateSOCKSAuth,
15 // namely that it uses a different circuit for different SOCKS credentials.
16 func TestIsolateSocksAuth(t *testing.T) {
17 // Create context w/ no isolate
18 ctx := NewTestContext(t, &tor.StartConf{
19 NoAutoSocksPort: true,
20 ExtraArgs: []string{"--SocksPort", "auto NoIsolateSOCKSAuth"},
21 })
22 // Make sure it reused the circuit (i.e. only has one) for both separate-auth calls
23 uniqueCircuitIDs := doSeparateAuthHttpCalls(ctx)
24 ctx.Debugf("Unique IDs without isolate: %v", uniqueCircuitIDs)
25 ctx.Require.Len(uniqueCircuitIDs, 1)
26 // Create context w/ isolate
27 ctx = NewTestContext(t, nil)
28 // Make sure it made a new circuit (i.e. has two) for each separate-auth call
29 uniqueCircuitIDs = doSeparateAuthHttpCalls(ctx)
30 ctx.Debugf("Unique IDs with isolate: %v", uniqueCircuitIDs)
31 ctx.Require.Len(uniqueCircuitIDs, 2)
32 }
33
34 // Returns the map keyed with unique circuit IDs after second separate-auth HTTP call
35 func doSeparateAuthHttpCalls(ctx *TestContext) map[int]struct{} {
36 defer ctx.Close()
37 enableCtx, enableCancel := context.WithTimeout(ctx, 100*time.Second)
38 defer enableCancel()
39 ctx.Require.NoError(ctx.EnableNetwork(enableCtx, true))
40 // Make HTTP call w/ an auth
41 client := httpClient(ctx, &tor.DialConf{ProxyAuth: &proxy.Auth{"foo", "bar"}})
42 byts := httpGet(ctx, client, "https://check.torproject.org/api/ip")
43 ctx.Debugf("Read bytes: %v", string(byts))
44 // Confirm just size 1
45 ids := uniqueStreamCircuitIDs(ctx)
46 ctx.Require.Len(ids, 1)
47 // Now make call with another auth and just return circuit IDs
48 client = httpClient(ctx, &tor.DialConf{ProxyAuth: &proxy.Auth{"baz", "qux"}})
49 byts = httpGet(ctx, client, "https://check.torproject.org/api/ip")
50 ctx.Debugf("Read bytes: %v", string(byts))
51 return uniqueStreamCircuitIDs(ctx)
52 }
53
54 // Return each stream circuit as a key of an empty-val map
55 func uniqueStreamCircuitIDs(ctx *TestContext) map[int]struct{} {
56 ret := map[int]struct{}{}
57 vals, err := ctx.Control.GetInfo("stream-status")
58 ctx.Require.NoError(err)
59 for _, val := range vals {
60 ctx.Require.Equal("stream-status", val.Key)
61 for _, line := range strings.Split(val.Val, "\n") {
62 pieces := strings.Split(strings.TrimSpace(line), " ")
63 if len(pieces) < 3 {
64 continue
65 }
66 i, err := strconv.Atoi(pieces[2])
67 ctx.Require.NoError(err)
68 ret[i] = struct{}{}
69 }
70 }
71 return ret
72 }