-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresolve_test.go
256 lines (212 loc) · 9.24 KB
/
resolve_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
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
package qcon
import (
"context"
"flag"
"net/http"
"testing"
"time"
)
var testID = flag.String("id", "", "QuickConnect ID")
func TestGetInfo(t *testing.T) {
ctx := context.Background()
tr := &mockTransport{
responses: map[string]response{
defaultServURL: {Status: 200, Body: testServResp},
},
}
c := Client{
Client: &http.Client{
Transport: tr,
},
Timeout: 500 * time.Millisecond,
}
info, err := c.GetInfo(ctx, "foo")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
exp := Info{
ServerID: "030344165",
Records: []Record{
{URL: "https://10.20.1.100:5001", Type: httpsLanIPv4},
{URL: "https://[fe80::211:32ff:ef63:bca8]:5001", Type: httpsLanIPv6},
{URL: "https://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:50551", Type: httpsWanIPv6},
{URL: "https://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:5001", Type: httpsWanIPv6},
{URL: "https://[fd5e:fa6f:11df::100]:50551", Type: httpsWanIPv6},
{URL: "https://[fd5e:fa6f:11df::100]:5001", Type: httpsWanIPv6},
{URL: "https://75.66.42.168:50551", Type: httpsWanIPv4},
{URL: "https://75.66.42.168:5001", Type: httpsWanIPv4},
{URL: "http://10.20.1.100:5000", Type: httpLanIPv4},
{URL: "http://[fe80::211:32ff:ef63:bca8]:5000", Type: httpLanIPv6},
{URL: "http://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:50550", Type: httpWanIPv6},
{URL: "http://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:5000", Type: httpWanIPv6},
{URL: "http://[fd5e:fa6f:11df::100]:50550", Type: httpWanIPv6},
{URL: "http://[fd5e:fa6f:11df::100]:5000", Type: httpWanIPv6},
{URL: "http://75.66.42.168:50550", Type: httpWanIPv4},
{URL: "http://75.66.42.168:5000", Type: httpWanIPv4},
},
}
if info.ServerID != exp.ServerID {
t.Errorf("unexpected ServerID:\n exp: %s\n got: %s\n", exp.ServerID, info.ServerID)
}
if len(info.Records) != len(exp.Records) {
t.Fatalf("incorrect number of records returned: expected %d, got %d", len(exp.Records), len(info.Records))
}
for i := range info.Records {
if info.Records[i].URL != exp.Records[i].URL {
t.Errorf("record %d: unexpected URL:\n exp: %s\n got: %s\n", i, exp.Records[i].URL, info.Records[i].URL)
}
if info.Records[i].Type != exp.Records[i].Type {
t.Errorf("record %d: unexpected Type: exp: %d, got: %d", i, exp.Records[i].Type, info.Records[i].Type)
}
}
}
// TestLiveResolve will test against Synology central server
// using the QuickConnectID passed in the -id option to go test.
// If no ID option is present, this test will be skipped.
func TestLiveResolve(t *testing.T) {
if *testID == "" {
t.Skip()
}
ctx := context.Background()
urls, err := Resolve(ctx, *testID)
if err != nil {
t.Fatal(err)
}
t.Logf("%+v\n", urls)
}
// helper function for comparing results of resolve tests.
func runResolveTest(t *testing.T, tr *mockTransport, exp []string) {
t.Helper()
ctx := context.Background()
c := Client{
Client: &http.Client{
Transport: tr,
},
Timeout: 500 * time.Millisecond,
}
urls, err := c.Resolve(ctx, "foo")
if err != nil {
t.Fatal(err)
}
if len(urls) != len(exp) {
t.Fatalf("unexpected number of returned strings. Expected %d, got %d", len(exp), len(urls))
}
for i := range urls {
if urls[i] != exp[i] {
t.Errorf("returned string mismatch:\n exp: '%s'\n got: '%s'", exp[i], urls[i])
}
}
}
func TestResolve01(t *testing.T) {
// Test default response for get_server_info and have only a subset of URLs respond
tr := &mockTransport{
responses: map[string]response{
defaultServURL: {Status: 200, Body: testServResp},
"http://75.66.42.168:5000" + pingPath: {Status: 200, Body: testPingSuccess},
"http://10.20.1.100:5000" + pingPath: {Status: 200, Body: testPingSuccess},
"https://75.66.42.168:5001" + pingPath: {Status: 200, Body: testPingSuccess},
"http://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:5000" + pingPath: {Status: 200, Body: testPingSuccess},
"https://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:50551" + pingPath: {Status: 200, Body: testPingSuccess},
"https://10.20.1.100:5001" + pingPath: {Status: 200, Body: testPingSuccess},
},
}
exp := []string{
"https://10.20.1.100:5001", // httpsLanIPv4
"https://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:50551", // httpsLanIPv6
"https://75.66.42.168:5001", // httpsWanIPv4
"http://10.20.1.100:5000", // httpLanIPv4
"http://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:5000", // httpLanIPv6
"http://75.66.42.168:5000", // httpWanIPv4
}
runResolveTest(t, tr, exp)
}
func TestResolve02(t *testing.T) {
// Test a selection of URLs, some of which return invalid ID hash values
tr := &mockTransport{
responses: map[string]response{
defaultServURL: {Status: 200, Body: testServResp},
"http://75.66.42.168:5000" + pingPath: {Status: 200, Body: testPingInvalid},
"http://10.20.1.100:5000" + pingPath: {Status: 200, Body: testPingSuccess},
"https://75.66.42.168:5001" + pingPath: {Status: 200, Body: testPingInvalid},
"http://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:5000" + pingPath: {Status: 200, Body: testPingInvalid},
"https://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:50551" + pingPath: {Status: 200, Body: testPingInvalid},
"https://10.20.1.100:5001" + pingPath: {Status: 200, Body: testPingSuccess},
},
}
exp := []string{
"https://10.20.1.100:5001", // httpsLanIPv4
"http://10.20.1.100:5000", // httpLanIPv4
}
runResolveTest(t, tr, exp)
}
func TestResolve03(t *testing.T) {
// Test a selection of URLs, some of which take too long to return
tr := &mockTransport{
responses: map[string]response{
defaultServURL: {Status: 200, Body: testServResp},
"http://75.66.42.168:5000" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 2.0},
"http://10.20.1.100:5000" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 0.1},
"https://75.66.42.168:5001" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 2.0},
"http://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:5000" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 2.0},
"https://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:50551" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 2.0},
"https://10.20.1.100:5001" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 0.1},
},
}
exp := []string{
"https://10.20.1.100:5001", // httpsLanIPv4
"http://10.20.1.100:5000", // httpLanIPv4
}
runResolveTest(t, tr, exp)
}
func TestResolve04(t *testing.T) {
// Test a selection of URLs, some of which return unexpected body values and/or status errors
tr := &mockTransport{
responses: map[string]response{
defaultServURL: {Status: 200, Body: testServResp},
"http://75.66.42.168:5000" + pingPath: {Status: 200, Body: "foobar"},
"http://10.20.1.100:5000" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 0.2},
"https://75.66.42.168:5001" + pingPath: {Status: 200, Body: "deadbeef"},
"http://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:5000" + pingPath: {Status: 404, Body: "<html><body>Error</body></html>"},
"https://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:50551" + pingPath: {Status: 200, Body: "hello, world!"},
"https://10.20.1.100:5001" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 0.2},
},
}
exp := []string{
"https://10.20.1.100:5001", // httpsLanIPv4
"http://10.20.1.100:5000", // httpLanIPv4
}
runResolveTest(t, tr, exp)
}
func TestResolve05(t *testing.T) {
// Cancel Resolve before it returns, verify correct error returned.
tr := &mockTransport{
responses: map[string]response{
defaultServURL: {Status: 200, Body: testServResp},
"http://75.66.42.168:5000" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 10},
"http://10.20.1.100:5000" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 10},
"https://75.66.42.168:5001" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 10},
"http://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:5000" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 10},
"https://[fd5e:fa6f:11df:0:211:32ff:ef63:bca8]:50551" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 10},
"https://10.20.1.100:5001" + pingPath: {Status: 200, Body: testPingSuccess, Delay: 10},
},
}
ctx, cancel := context.WithCancel(context.Background())
// cancel context after 0.5 sec
go func() {
time.Sleep(500 * time.Millisecond)
cancel()
}()
c := Client{
Client: &http.Client{
Transport: tr,
},
Timeout: 2 * time.Second,
}
_, err := c.Resolve(ctx, "foo")
if err != ErrCancelled {
if err == nil {
t.Fatal("cancelled function returned no error")
}
t.Fatalf("incorrect error returned")
}
}