Repository navigation
Expand file tree
/
Copy pathProxy_test.go
More file actions
178 lines (131 loc) · 4.61 KB
/
Copy pathProxy_test.go
File metadata and controls
178 lines (131 loc) · 4.61 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
package icmptunnel
import "tholian-warps/protocols/dns"
import "tholian-warps/protocols/http"
import "tholian-warps/protocols/test"
import net_url "net/url"
import "testing"
import "time"
func TestProxy(t *testing.T) {
t.Run("Proxy with ProxyCache and HTTP Payload", func(t *testing.T) {
url, _ := net_url.Parse("http://example.com/index.html")
expected := http.NewPacket()
expected.SetURL(*url)
expected.SetStatus(http.StatusOK)
expected.SetHeader("Content-Type", "text/html")
expected.SetHeader("X-Proxy", "SpyProxyCache")
expected.SetPayload([]byte("Hello, world!"))
cache := test.NewSpyProxyCache(true, &expected, false)
proxy := NewProxy("localhost", 13337, &cache)
tunnel := NewTunnel("127.0.0.1", 13337)
socket_a, socket_b := test.NewLoopbackSocket()
proxy.SetSocket(socket_b)
tunnel.SetSocket(socket_a)
request := http.NewPacket()
request.SetMethod(http.MethodGet)
request.SetURL(*url)
go func() {
err1 := proxy.Listen()
if err1 != nil {
t.Errorf("Unexpected error '%s'", err1.Error())
}
}()
go func() {
time.Sleep(2 * time.Second)
proxy.Destroy()
}()
time.Sleep(100 * time.Millisecond)
response := tunnel.RequestPacket(request)
response.Decode()
if expected.Status != response.Status {
t.Errorf("Expected HTTP response status '%s' but got '%s'", http.Status(expected.Status).String(), http.Status(response.Status).String())
}
if string(expected.Payload) != string(response.Payload) {
t.Errorf("Expected HTTP response payload '%s' but got '%s'", string(expected.Payload), string(response.Payload))
}
time.Sleep(2 * time.Second)
})
t.Run("Proxy with ResolverCache and DNS Payload", func(t *testing.T) {
record := dns.NewRecord("example.com", dns.TypeA)
record.SetIPv4("1.3.3.7")
expected := dns.NewPacket()
expected.SetType("response")
expected.SetResponseCode(dns.ResponseCodeNoError)
expected.AddQuestion(dns.NewQuestion("example.com", dns.TypeA))
expected.AddAnswer(record)
cache := test.NewSpyResolverCache(true, &expected, false)
resolver := NewResolver("localhost", 13337, &cache)
proxy := NewProxy("localhost", 13337, nil)
proxy.SetResolver(&resolver)
tunnel := NewTunnel("127.0.0.1", 13337)
socket_a, socket_b := test.NewLoopbackSocket()
proxy.SetSocket(socket_b)
tunnel.SetSocket(socket_a)
query := dns.NewPacket()
query.SetType("query")
query.AddQuestion(dns.NewQuestion("example.com", dns.TypeA))
go func() {
err1 := proxy.Listen()
if err1 != nil {
t.Errorf("Unexpected error '%s'", err1.Error())
}
}()
go func() {
time.Sleep(2 * time.Second)
proxy.Destroy()
}()
time.Sleep(100 * time.Millisecond)
response := tunnel.ResolvePacket(query)
if len(response.Questions) > 0 && len(response.Answers) > 0 {
if response.Answers[0].Type != dns.TypeA {
t.Errorf("Expected DNS response record type to be '%s' but got '%s'", dns.TypeA.String(), dns.Type(response.Answers[0].Type).String())
}
} else {
t.Errorf("Expected DNS response to have questions and answers")
}
time.Sleep(2 * time.Second)
})
t.Run("Proxy Chain with ProxyCache and HTTP Payload", func(t *testing.T) {
url, _ := net_url.Parse("http://example.com/index.html")
expected := http.NewPacket()
expected.SetURL(*url)
expected.SetStatus(http.StatusOK)
expected.SetHeader("X-Proxy", "SpyProxyCache")
expected.SetPayload([]byte("Hello, world!"))
cache := test.NewSpyProxyCache(true, &expected, false)
gateway := NewProxy("localhost", 13337, &cache)
forward := NewProxy("localhost", 13337, nil)
client := NewTunnel("127.0.0.1", 13337)
hop := NewTunnel("127.0.0.1", 13337)
socket_client, socket_forward := test.NewLoopbackSocket()
socket_hop, socket_gateway := test.NewLoopbackSocket()
client.SetSocket(socket_client)
forward.SetSocket(socket_forward)
hop.SetSocket(socket_hop)
gateway.SetSocket(socket_gateway)
forward.SetTunnel(&hop)
request := http.NewPacket()
request.SetMethod(http.MethodGet)
request.SetURL(*url)
go func() {
forward.Listen()
}()
go func() {
gateway.Listen()
}()
go func() {
time.Sleep(2 * time.Second)
forward.Destroy()
gateway.Destroy()
}()
time.Sleep(100 * time.Millisecond)
response := client.RequestPacket(request)
response.Decode()
if expected.Status != response.Status {
t.Errorf("Expected HTTP response status '%s' but got '%s'", http.Status(expected.Status).String(), http.Status(response.Status).String())
}
if string(expected.Payload) != string(response.Payload) {
t.Errorf("Expected HTTP response payload '%s' but got '%s'", string(expected.Payload), string(response.Payload))
}
time.Sleep(2 * time.Second)
})
}