-
Notifications
You must be signed in to change notification settings - Fork 1
/
measure_test.go
394 lines (350 loc) · 11.8 KB
/
measure_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
(C) 2022 Robert Kisteleki & RIPE NCC
See LICENSE file for the license.
*/
package goat
import (
"net/netip"
"reflect"
"testing"
)
// Test if the measurement probe spec part works
func TestMeasureProbeArea(t *testing.T) {
var err error
var spec MeasurementSpec
spec = MeasurementSpec{}
err = spec.AddProbesArea("X", 5)
if err == nil {
t.Errorf("Invalid probe area is accepted")
}
for _, area := range Areas {
spec = MeasurementSpec{}
err = spec.AddProbesArea(area, 5)
if err != nil {
t.Errorf("Valid probe area %s is not accepted: %v", area, err)
}
}
b, err := spec.apiSpec.Probes[0].MarshalJSON()
if err != nil {
t.Fatalf("Probe area spec failed to marshal to JSON: %v", err)
}
if string(b) != `{"type":"area","value":"South-East","requested":5}` {
t.Errorf("Probe area spec improperly serialized: %s", string(b))
}
}
func TestMeasureProbeCountry(t *testing.T) {
var err error
var spec MeasurementSpec
spec = MeasurementSpec{}
err = spec.AddProbesCountry("NED", 5)
if err == nil {
t.Errorf("Invalid country is accepted")
}
spec = MeasurementSpec{}
err = spec.AddProbesCountry("NL", 5)
if err != nil {
t.Errorf("Valid country is not accepted: %v", err)
}
b, err := spec.apiSpec.Probes[0].MarshalJSON()
if err != nil {
t.Fatalf("Probe country spec failed to marshal to JSON: %v", err)
}
if string(b) != `{"type":"country","value":"NL","requested":5}` {
t.Errorf("Probe country spec improperly serialized: %s", string(b))
}
}
func TestMeasureProbePrefix(t *testing.T) {
var err error
spec := MeasurementSpec{}
err = spec.AddProbesPrefix(netip.MustParsePrefix("10.0.0.0/24"), 5)
if err != nil {
t.Errorf("Good probe prefix is not accepted: %v", err)
}
b, err := spec.apiSpec.Probes[0].MarshalJSON()
if err != nil {
t.Fatalf("Probe prefix spec failed to marshal to JSON: %v", err)
}
if string(b) != `{"type":"prefix","value":"10.0.0.0/24","requested":5}` {
t.Errorf("Probe prefix spec improperly serialized: %s", string(b))
}
}
func TestMeasureProbeAsn(t *testing.T) {
var err error
spec := MeasurementSpec{}
err = spec.AddProbesAsn(1234, 5)
if err != nil {
t.Errorf("Good probe ASN is not accepted: %v", err)
}
b, err := spec.apiSpec.Probes[0].MarshalJSON()
if err != nil {
t.Fatalf("Probe ASN spec failed to marshal to JSON: %v", err)
}
if string(b) != `{"type":"asn","value":"1234","requested":5}` {
t.Errorf("Probe ASN spec improperly serialized: %s", string(b))
}
}
func TestMeasureProbeList(t *testing.T) {
var err error
var spec MeasurementSpec
spec = MeasurementSpec{}
err = spec.AddProbesList([]uint{})
if err == nil {
t.Errorf("Empty probe list is accepted")
}
spec = MeasurementSpec{}
err = spec.AddProbesList([]uint{1, 2, 3})
if err != nil {
t.Errorf("Good probe list is not accepted: %v", err)
}
b, err := spec.apiSpec.Probes[0].MarshalJSON()
if err != nil {
t.Fatalf("Probe list spec failed to marshal to JSON: %v", err)
}
if string(b) != `{"type":"probes","value":"1,2,3","requested":3}` {
t.Errorf("Probe list spec improperly serialized: %s", string(b))
}
}
func TestMeasureProbeReuse(t *testing.T) {
var err error
var spec MeasurementSpec
spec = MeasurementSpec{}
err = spec.AddProbesReuse(10, 5)
if err == nil {
t.Errorf("Bad msm reuse ID is accepted")
}
spec = MeasurementSpec{}
err = spec.AddProbesReuse(1000001, 5)
if err != nil {
t.Errorf("Good msm reuse ID is not accepted: %v", err)
}
b, err := spec.apiSpec.Probes[0].MarshalJSON()
if err != nil {
t.Fatalf("Probe reuse spec failed to marshal to JSON: %v", err)
}
if string(b) != `{"type":"msm","value":"1000001","requested":5}` {
t.Errorf("Probe reuse spec improperly serialized: %s", string(b))
}
}
func TestMeasureProbeTags(t *testing.T) {
var err error
spec := MeasurementSpec{}
err = spec.AddProbesReuseWithTags(1000001, 5, &[]string{"itag1", "itag2"}, &[]string{"etag1", "etag2"})
if err != nil {
t.Errorf("Good probe tag filter list is not accepted: %v", err)
}
pspec := spec.apiSpec.Probes[0]
if pspec.Tags.Include == nil ||
pspec.Tags.Exclude == nil ||
!reflect.DeepEqual(*pspec.Tags.Include, []string{"itag1", "itag2"}) ||
!reflect.DeepEqual(*pspec.Tags.Exclude, []string{"etag1", "etag2"}) {
t.Errorf("Probe filter tag list is not registered properly")
}
}
// Test if the measurement target spec part works
func TestMeasureTargetGeneral(t *testing.T) {
var err error
var spec MeasurementSpec
spec = MeasurementSpec{}
err = spec.AddPing("description", "www.google.com", 3, nil, nil)
if err == nil {
t.Errorf("Invalid address family is accepted")
}
err = spec.AddPing("description", "www.google.com", 4, nil, nil)
if err != nil {
t.Errorf("Valid address family is not accepted: %v", err)
}
err = spec.AddPing("description", "www.google.com", 6, nil, nil)
if err != nil {
t.Errorf("Valid address family is not accepted: %v", err)
}
spec = MeasurementSpec{}
err = spec.AddPing("", "", 4, nil, nil)
if err == nil {
t.Errorf("Empty description or target is accepted")
}
spec = MeasurementSpec{}
err = spec.AddPing("description1", "www.google.com", 4, nil, nil)
if err != nil {
t.Errorf("Valid ping measurement target spec rejected: %v", err)
}
}
func TestMeasureTargetBase(t *testing.T) {
var err error
var spec MeasurementSpec
err = spec.AddPing("description1", "www.meta.com", 6, &BaseOptions{
ResolveOnProbe: true,
Interval: 999,
Tags: []string{"tag1", "tag2"},
Spread: 50,
SkipDNSCheck: true,
}, nil)
if err != nil {
t.Fatalf("Ping measurement target spec with options failed: %v", err)
}
b2, err := spec.apiSpec.Definitons[0].MarshalJSON()
if err != nil {
t.Fatalf("Ping measurement target spec with options failed to marshal to JSON: %v", err)
}
if string(b2) != `{"description":"description1","target":"www.meta.com","type":"ping","af":6,"interval":999,"resolve_on_probe":true,"tags":["tag1","tag2"],"spread":50,"skip_dns_check":true}` {
t.Errorf("Measurement with base options improperly serialized: %s", string(b2))
}
}
func TestMeasureTargetPing(t *testing.T) {
var err error
var spec MeasurementSpec
err = spec.AddPing("description2", "www.meta.com", 6, nil, &PingOptions{
Packets: 5,
PacketSize: 9,
PacketInterval: 99,
IncludeProbeID: true,
})
if err != nil {
t.Fatalf("Ping measurement target spec with options failed: %v", err)
}
b2, err := spec.apiSpec.Definitons[0].MarshalJSON()
if err != nil {
t.Fatalf("Ping measurement target spec with options failed to marshal to JSON: %v", err)
}
if string(b2) != `{"description":"description2","target":"www.meta.com","type":"ping","af":6,"packets":5,"packet_size":9,"packet_interval":99,"include_probe_id":true}` {
t.Errorf("Measurement (ping) with options improperly serialized: %s", string(b2))
}
}
func TestMeasureTargetTrace(t *testing.T) {
var err error
var spec MeasurementSpec
err = spec.AddTrace("description2", "www.meta.com", 6, nil, &TraceOptions{
Protocol: "ICMP",
ResponseTimeout: 19,
Packets: 5,
PacketSize: 9,
ParisId: 8,
FirstHop: 2,
LastHop: 3,
DestinationEH: 4,
HopByHopEH: 7,
DontFragment: true,
})
if err != nil {
t.Fatalf("Trace measurement target spec with options failed: %v", err)
}
b2, err := spec.apiSpec.Definitons[0].MarshalJSON()
if err != nil {
t.Fatalf("Trace measurement target spec with options failed to marshal to JSON: %v", err)
}
if string(b2) != `{"description":"description2","target":"www.meta.com","type":"traceroute","af":6,"protocol":"ICMP","response_timeout":19,"packets":5,"packet_size":9,"paris":8,"first_hop":2,"max_hops":3,"destination_option_size":4,"hop_by_hop_option_size":7,"dont_fragment":true}` {
t.Errorf("Measurement (trace) with options improperly serialized: %s", string(b2))
}
}
func TestMeasureTargetDns(t *testing.T) {
var err error
var spec MeasurementSpec
err = spec.AddDns("description2", "ns.ripe.net", 6, nil, &DnsOptions{
Protocol: "TCP",
Class: "CHAOS",
Type: "AAAA",
Argument: "ping.ripe.net",
UseMacros: true,
UseResolver: true,
UdpPayloadSize: 1024,
Retries: 3,
IncludeQbuf: true,
IncludeAbuf: true,
PrependProbeID: true,
SetRd: true,
SetDo: true,
SetCd: true,
Timeout: 999,
})
if err != nil {
t.Fatalf("DNS measurement target spec with options failed: %v", err)
}
b2, err := spec.apiSpec.Definitons[0].MarshalJSON()
if err != nil {
t.Fatalf("DNS measurement target spec with options failed to marshal to JSON: %v", err)
}
if string(b2) != `{"description":"description2","target":"ns.ripe.net","type":"dns","af":6,"protocol":"TCP","query_class":"CHAOS","query_type":"AAAA","query_argument":"ping.ripe.net","use_macros":true,"use_probe_resolver":true,"udp_payload_size":1024,"retry":3,"include_qbuf":true,"include_abuf":true,"prepend_probe_id":true,"set_rd_bit":true,"set_do_bit":true,"set_cd_bit":true,"timeout":999}` {
t.Errorf("Measurement (DNS) with options improperly serialized: %s", string(b2))
}
}
func TestMeasureTargetTls(t *testing.T) {
var err error
var spec MeasurementSpec
err = spec.AddTls("description2", "www.meta.com", 6, nil, &TlsOptions{
Port: 333,
Sni: "www.meta.com",
})
if err != nil {
t.Fatalf("Tls measurement target spec with options failed: %v", err)
}
b2, err := spec.apiSpec.Definitons[0].MarshalJSON()
if err != nil {
t.Fatalf("Tls measurement target spec with options failed to marshal to JSON: %v", err)
}
if string(b2) != `{"description":"description2","target":"www.meta.com","type":"sslcert","af":6,"port":333,"hostname":"www.meta.com"}` {
t.Errorf("Measurement (TLS) with options improperly serialized: %s", string(b2))
}
}
func TestMeasureTargetNtp(t *testing.T) {
var err error
var spec MeasurementSpec
err = spec.AddNtp("description2", "www.meta.com", 6, nil, &NtpOptions{
Packets: 5,
Timeout: 99,
})
if err != nil {
t.Fatalf("NTP measurement target spec with options failed: %v", err)
}
b2, err := spec.apiSpec.Definitons[0].MarshalJSON()
if err != nil {
t.Fatalf("NTP measurement target spec with options failed to marshal to JSON: %v", err)
}
if string(b2) != `{"description":"description2","target":"www.meta.com","type":"ntp","af":6,"packets":5,"timeout":99}` {
t.Errorf("Measurement (NTP) with options improperly serialized: %s", string(b2))
}
}
func TestMeasureTargetHttp(t *testing.T) {
var err error
var spec MeasurementSpec
err = spec.AddHttp("description2", "www.meta.com", 6, nil, &HttpOptions{
Method: "GET",
Path: "/favicon.ico",
Query: "a=b",
Port: 8080,
HeaderBytes: 1024,
Version: "1.1",
ExtendedTiming: true,
MoreExtendedTiming: true,
})
if err != nil {
t.Fatalf("HTTP measurement target spec with options failed: %v", err)
}
b2, err := spec.apiSpec.Definitons[0].MarshalJSON()
if err != nil {
t.Fatalf("HTTP measurement target spec with options failed to marshal to JSON: %v", err)
}
if string(b2) != `{"description":"description2","target":"www.meta.com","type":"http","af":6,"method":"GET","path":"/favicon.ico","query_string":"a=b","port":8080,"header_bytes":1024,"version":"1.1","extended_timing":true,"more_extended_timing":true}` {
t.Errorf("Measurement (HTTP) with options improperly serialized: %s", string(b2))
}
}
// Test if the measurement spec generator works
func TestMeasureSpec(t *testing.T) {
var err error
var spec MeasurementSpec
spec = MeasurementSpec{}
_, err = spec.Schedule()
if err == nil {
t.Errorf("Measurement spec without probes or targets is accepted")
}
spec = MeasurementSpec{}
spec.AddProbesArea("WW", 3)
_, err = spec.Schedule()
if err == nil {
t.Errorf("Measurement spec without targets is accepted")
}
spec = MeasurementSpec{}
spec.AddPing("ping", "google.com", 4, nil, nil)
_, err = spec.Schedule()
if err == nil {
t.Errorf("Measurement spec without probes is accepted")
}
}