This repository was archived by the owner on Jan 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
162 lines (135 loc) · 3.59 KB
/
client_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package torch
import (
"bytes"
"crypto/rand"
"fmt"
"github.com/andres-erbsen/torch/directory"
"io/ioutil"
"strings"
"testing"
"time"
"golang.org/x/net/context"
"golang.org/x/net/proxy"
)
func testSlashdotTorchN(t *testing.T, n int) {
ctx, _ := context.WithTimeout(context.Background(), (30 * time.Second))
torch, err := New(ctx, proxy.FromEnvironment())
if err != nil {
t.Fatal(err)
}
defer torch.Stop()
tc, circ, err := torch.UnguardedExitCircuit(ctx, n)
if err != nil {
t.Fatal(err)
}
defer tc.Close()
conn, err := circ.DialTCP(ctx, "tcp", "slashdot.org:80")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
conn.Write([]byte("GET /\r\n\r\n"))
reply, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(reply), "HTTP/") && strings.Contains(string(reply), "slashdot.org") {
t.Errorf("expected %q, got %q", "HTTP/", reply)
}
}
func TestSlashdotTorch2(t *testing.T) { testSlashdotTorchN(t, 2) }
//func TestSlashdotTorch3(t *testing.T) { testSlashdotTorchN(t, 3) }
func TestRendezvous(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), (30 * time.Second))
torch, err := New(ctx, proxy.FromEnvironment())
if err != nil {
t.Fatal(err)
}
defer torch.Stop()
// FIXME: implement threadsafe Pick in Torch
dst := torch.Pick(weighRelayWith, nil)
cookie := make([]byte, 20)
payload := make([]byte, 148)
message1 := make([]byte, PAYLOAD_LEN)
message2 := make([]byte, PAYLOAD_LEN)
rand.Read(cookie)
rand.Read(payload)
rand.Read(message1)
rand.Read(message2)
tc1, c1, err := torch.UnguardedCircuitTo(ctx, 1, dst)
if err != nil {
t.Fatal(err)
}
defer tc1.Close()
accept, err := c1.ListenRendezvousRaw(cookie)
if err != nil {
t.Fatal(err)
}
go func() {
tc2, c2, err := torch.UnguardedCircuitTo(ctx, 1, dst)
if err != nil {
t.Fatal(err)
}
defer tc2.Close()
if err := c2.DialRendezvousRaw(cookie, payload); err != nil {
t.Fatal(err)
}
recvMessage, err := c2.ReadRaw()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(recvMessage, message2) {
fmt.Printf("%x\n!=\n%x\n", recvMessage, message2)
t.Fail()
}
if err := c2.WriteRaw([]byte(string(message1))); err != nil {
t.Fatal(err)
}
}()
payload2, err := accept()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(payload2, payload) {
fmt.Printf("%x\n!=\n%x\n", payload2, payload)
t.Fail()
}
if err := c1.WriteRaw([]byte(string(message2))); err != nil {
t.Fatal(err)
}
recvMessage, err := c1.ReadRaw()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(recvMessage, message1) {
fmt.Printf("%x\n!=\n%x\n", recvMessage, message1)
t.Fail()
}
}
func TestHandshakeFailure(t *testing.T) {
handshakeFailuerIP := [4]byte{193, 23, 244, 244}
handshakeFailuerPort := uint16(443)
ctx, _ := context.WithTimeout(context.Background(), (30 * time.Second))
torch, err := New(ctx, proxy.FromEnvironment())
if err != nil {
t.Fatal(err)
}
defer torch.Stop()
n := torch.Pick(func(w *directory.BandwidthWeights, n *directory.NodeInfo) int64 {
if n.IP == handshakeFailuerIP && n.Port == handshakeFailuerPort {
return 1
}
return 0
}, nil)
tc1, _, err := torch.UnguardedCircuitTo(ctx, 1, n)
if err != nil {
/*
sent: 160301007b010000770303c56160e1eaeaa7e102a9b86276e15527f0253d8802e0f3759565145acb894e1b000018c02fc02bc030c02cc013c009c014c00a002f0035c012000a01000036000500050100000000000a00080006001700180019000b00020100000d000e000c040104030501050302010203ff0100010000120000
received: 15030100020228
*/
// t.Fatal(err)
t.Skipf("%s (this is a known failing test, cause to be diagnosed)", err)
}
defer tc1.Close()
// success.
}