File: selection_test.go

package info (click to toggle)
mirrorbits 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 984 kB
  • sloc: sh: 675; makefile: 93
file content (466 lines) | stat: -rw-r--r-- 12,133 bytes parent folder | download | duplicates (2)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright (c) 2024 Arnaud Rebillout
// Licensed under the MIT license

package http

import (
	"fmt"
	"os"
	"testing"
	"time"

	. "github.com/etix/mirrorbits/config"
	"github.com/etix/mirrorbits/filesystem"
	"github.com/etix/mirrorbits/network"
	"github.com/etix/mirrorbits/mirrors"
)

var noFileInfo *filesystem.FileInfo
var noClientInfo network.GeoIPRecord

func TestMain(m *testing.M) {
	noFileInfo = nil
	noClientInfo = network.GeoIPRecord{}
	SetConfiguration(&Configuration{
		FixTimezoneOffsets: false,
	})
	os.Exit(m.Run())
}

// Helper to check the results of the Filter function on a single mirror
func checkResultsSingle(t *testing.T, a mirrors.Mirrors, x mirrors.Mirrors, reason string, url string) {
	t.Helper()
	var m mirrors.Mirror

	// If reason was set, we expect the mirror to have been excluded, while
	// no reason means that the mirror should have been accepted.
	if reason == "" {
		if len(a) != 1 || len(x) != 0 {
			t.Fatalf("There should be 1 mirror accepted and 0 mirror excluded")
		}
		m = a[0]
	} else {
		if len(a) != 0 || len(x) != 1 {
			t.Fatalf("There should be 0 mirror accepted and 1 mirror excluded")
		}
		m = x[0]
	}

	// Test that the field ExcludeReason was set as expected, or is unset
	// in case the mirror was accepted.
	if m.ExcludeReason != reason {
		t.Fatalf("Invalid ExcludeReason, expected '%s', got '%s'", reason, m.ExcludeReason)
	}

	// The field AbsoluteURL is expected to be set, except in the case when
	// the mirror is disabled.
	if m.ExcludeReason != "Disabled" && m.AbsoluteURL != url {
		t.Fatalf("Invalid AbsoluteURL, expected '%s', got '%s'", url, m.AbsoluteURL)
	}
}

// Helper to test the Filter function on a single mirror
func testFilterSingle(t *testing.T, m mirrors.Mirror, secureOption SecureOption, fileInfo *filesystem.FileInfo, clientInfo network.GeoIPRecord, reason string) {
	t.Helper()
	mlist := mirrors.Mirrors{m}
	a, x, _, _ := Filter(mlist, secureOption, fileInfo, clientInfo)
	checkResultsSingle(t, a, x, reason, m.HttpURL)
}

// Helper to test the Filter function on a single mirror, with a specific AbsoluteURL
func testFilterSingleAbsoluteURL(t *testing.T, m mirrors.Mirror, secureOption SecureOption, fileInfo *filesystem.FileInfo, clientInfo network.GeoIPRecord, reason string, url string) {
	t.Helper()
	mlist := mirrors.Mirrors{m}
	a, x, _, _ := Filter(mlist, secureOption, fileInfo, clientInfo)
	checkResultsSingle(t, a, x, reason, url)
}

func TestFilter(t *testing.T) {
	// Test that a mirror that is disabled is rejected

	m1 := mirrors.Mirror{
		HttpURL: "http://m1.mirror",
	}
	t.Run("disabled", func(t *testing.T) {
		testFilterSingle(t, m1, UNDEFINED, noFileInfo, noClientInfo, "Disabled")
	})

	// Given that a mirror is enabled, test that it's rejected when the
	// requested protocol is not available (either it's not supported by
	// the mirror, or it's down).

	tests1 := map[string]struct {
		secureOption SecureOption
		mirrorURL string
		excludeReason string
		absoluteURL string
	} {
		"want_https_but_http_only": {
			secureOption: WITHTLS,
			mirrorURL: "http://m1.mirror",
			excludeReason: "Not HTTPS",
			absoluteURL: "http://m1.mirror",
		},
		"want_https_has_https_but_down": {
			secureOption: WITHTLS,
			mirrorURL: "https://m1.mirror",
			excludeReason: "Down",
			absoluteURL: "https://m1.mirror",
		},
		"want_https_has_any_but_down": {
			secureOption: WITHTLS,
			mirrorURL: "m1.mirror",
			excludeReason: "Down",
			absoluteURL: "https://m1.mirror",
		},
		"want_http_but_https_only": {
			secureOption: WITHOUTTLS,
			mirrorURL: "https://m1.mirror",
			excludeReason: "Not HTTP",
			absoluteURL: "https://m1.mirror",
		},
		"want_http_has_http_but_down": {
			secureOption: WITHOUTTLS,
			mirrorURL: "http://m1.mirror",
			excludeReason: "Down",
			absoluteURL: "http://m1.mirror",
		},
		"want_http_has_any_but_down": {
			secureOption: WITHOUTTLS,
			mirrorURL: "m1.mirror",
			excludeReason: "Down",
			absoluteURL: "http://m1.mirror",
		},
		"want_any_has_http_only_but_down": {
			secureOption: UNDEFINED,
			mirrorURL: "http://m1.mirror",
			excludeReason: "Down / Not HTTPS",
			absoluteURL: "http://m1.mirror",
		},
		"want_any_has_https_only_but_down": {
			secureOption: UNDEFINED,
			mirrorURL: "https://m1.mirror",
			excludeReason: "Not HTTP / Down",
			absoluteURL: "https://m1.mirror",
		},
		"want_any_has_any_but_down": {
			secureOption: UNDEFINED,
			mirrorURL: "m1.mirror",
			excludeReason: "Down",
			absoluteURL: "http://m1.mirror",
		},
	}

	for name, test := range tests1 {
		m1 := mirrors.Mirror{
			HttpURL: test.mirrorURL,
			Enabled: true,
		}
		t.Run(name, func(t *testing.T) {
			testFilterSingleAbsoluteURL(t, m1, test.secureOption, noFileInfo, noClientInfo,
				test.excludeReason, test.absoluteURL)
		})
	}

	// Given that a mirror is enabled and the protocol requested is available,
	// test that a mirror is rejected when the requested file is not valid
	// (wrong size or mod time).

	testfile := &filesystem.FileInfo{
		Path: "/test/file.tgz",
		Size: 43000,
		ModTime: time.Now(),
	}

	tests2 := map[string]struct {
		fileSize int64
		fileModTime time.Time
		excludeReason string
	} {
		"wrong_size": {
			fileSize: 12345,
			fileModTime: testfile.ModTime,
			excludeReason: "File size mismatch",
		},
		"wrong_mod_time_newer_on_mirror": {
			fileSize: testfile.Size,
			fileModTime: testfile.ModTime.Add(time.Second * 10),
			excludeReason: "Mod time mismatch (diff: -10s)",
		},
		"wrong_mod_time_older_on_mirror": {
			fileSize: testfile.Size,
			fileModTime: testfile.ModTime.Add(time.Second * -10),
			excludeReason: "Mod time mismatch (diff: 10s)",
		},
	}

	for name, test := range tests2 {
		m1 := mirrors.Mirror{
			HttpURL: "https://m1.mirror",
			Enabled: true,
			HttpsUp: true,
			FileInfo: &filesystem.FileInfo{
				Path: "/test/file.tgz",
				Size: test.fileSize,
				ModTime: test.fileModTime,
			},
		}
		t.Run(name, func(t *testing.T) {
			testFilterSingle(t, m1, WITHTLS, testfile, noClientInfo, test.excludeReason)
		})
	}

	// Given that a mirror is enabled, the protocol requested is available
	// and the file on the mirror is valid, test that a mirror is rejected
	// when the client doesn't meet the geolocation requirements.

	clientInfo := network.GeoIPRecord{
		ContinentCode: "EU",
		CountryCode: "FR",
		ASNum: 4444,
	}

	tests3 := map[string]struct {
		continentOnly bool
		continentCode string
		countryOnly bool
		countryCodes string
		asOnly bool
		asNum uint
		excludedCountryCodes string
		excludeReason string
	} {
		"wrong_continent": {
			continentOnly: true,
			continentCode: "NA",
			excludeReason: "Continent only",
		},
		"wrong_country": {
			countryOnly: true,
			countryCodes: "UK",
			excludeReason: "Country only",
		},
		"wrong_countries": {
			countryOnly: true,
			countryCodes: "FI NO SE",
			excludeReason: "Country only",
		},
		"wrong_as": {
			asOnly: true,
			asNum: 5555,
			excludeReason: "AS only",
		},
		"excluded_country": {
			excludedCountryCodes: "FR",
			excludeReason: "User's country restriction",
		},
		"excluded_countries": {
			excludedCountryCodes: "ES FR IT PT",
			excludeReason: "User's country restriction",
		},
	}

	for name, test := range tests3 {
		m1 := mirrors.Mirror{
			HttpURL: "https://m1.mirror",
			Enabled: true,
			HttpsUp: true,
			FileInfo: testfile,
			ContinentOnly: test.continentOnly,
			ContinentCode: test.continentCode,
			CountryOnly: test.countryOnly,
			CountryCodes: test.countryCodes,
			ASOnly: test.asOnly,
			Asnum: test.asNum,
			ExcludedCountryCodes: test.excludedCountryCodes,
		}
		m1.Prepare()
		t.Run(name, func(t *testing.T) {
			testFilterSingle(t, m1, WITHTLS, testfile, clientInfo, test.excludeReason)
		})
	}

	// Given valid mirrors, test that the distances returned are correct.

	tests4 := map[string]struct {
		distances []float32
		extrema []float32
	} {
		"no_mirror": {
			distances: []float32{},
			extrema: []float32{0, 0},
		},
		"one_mirror": {
			distances: []float32{10},
			extrema: []float32{10, 10},
		},
		"some_mirrors": {
			distances: []float32{30, 20, 10},
			extrema: []float32{10, 30},
		},
	}

	for name, test := range tests4 {
		mlist := make([]mirrors.Mirror, 0, 5)
		for i, d := range test.distances {
			m := mirrors.Mirror{
				HttpURL: fmt.Sprintf("https://m%d.mirror", i),
				Enabled: true,
				HttpsUp: true,
				FileInfo: testfile,
				Distance: d,
			}
			mlist = append(mlist, m)
		}
		t.Run(name, func(t *testing.T) {
			a, x, closest, farthest := Filter(mlist, WITHTLS, testfile, clientInfo)
			if len(a) != len(mlist) || len(x) != 0 {
				t.Fatalf("There should be %d mirror(s) accepted and 0 mirror excluded",
					len(mlist))
			}
			if closest != test.extrema[0] || farthest != test.extrema[1] {
				t.Fatalf("Wrong results for [closest farthest], expected %v, got %v",
					test.extrema, []float32{closest, farthest})
			}
		})
	}
}

func TestFilterAllowOutdatedFiles(t *testing.T) {
	// Given a file that is outdated on a mirror, test that the mirror is
	// rejected, unless the configuration setting AllowOutdatedFiles is set
	// correctly in order to accept this file.

	testfile := &filesystem.FileInfo{
		Path: "/test/file.tgz",
		Size: 43000,
		ModTime: time.Now(),
	}

	configValues := [][]OutdatedFilesConfig{
		[]OutdatedFilesConfig{},
		[]OutdatedFilesConfig{{
			Prefix: "/test/",
			Minutes: 1,
		}},
		[]OutdatedFilesConfig{{
			Prefix: "/wrong/",
			Minutes: 2,
		}},
		[]OutdatedFilesConfig{{
			Prefix: "/test/",
			Minutes: 2,
		}},
	}

	tests := map[string]struct {
		fileSize int64
		fileModTime time.Time
		excludeReason []string
	} {
		"outdated_same_size": {
			fileSize: testfile.Size,
			fileModTime: testfile.ModTime.Add(-100 * time.Second),
			excludeReason: []string{
				"Mod time mismatch (diff: 1m40s)",
				"Mod time mismatch (diff: 1m40s)",
				"Mod time mismatch (diff: 1m40s)",
				"",
			},
		},
		"outdated_different_size": {
			fileSize: 12345,
			fileModTime: testfile.ModTime.Add(-100 * time.Second),
			excludeReason: []string{
				"File size mismatch",
				"Mod time mismatch (diff: 1m40s)",
				"File size mismatch",
				"",
			},
		},
	}

	for idx, configValue := range configValues {
		SetConfiguration(&Configuration{
			AllowOutdatedFiles: configValue,
		})
		for name, test := range tests {
			m1 := mirrors.Mirror{
				HttpURL: fmt.Sprintf("https://m%d.mirror", 1),
				Enabled: true,
				HttpsUp: true,
				FileInfo: &filesystem.FileInfo{
					Path: testfile.Path,
					Size: test.fileSize,
					ModTime: test.fileModTime,
				},
			}
			t.Run(name, func(t *testing.T) {
				testFilterSingle(t, m1, WITHTLS, testfile, noClientInfo, test.excludeReason[idx])
			})
		}
	}
}

func TestFilterFixTimezoneOffsets(t *testing.T) {
	// Given a mirror with a 1-hour timezone offset, test that the mirror
	// is rejected unless 1) the TZOffset of the mirror is set correctly,
	// and 2) the configuration setting FixTimezoneOffsets is enabled.

	var offset int64 = 3600
	modTime := time.Now()
	outdatedModTime := modTime.Add(time.Duration(-offset) * time.Second)

	fileRequested := &filesystem.FileInfo{
		Path: "/test/file.tgz",
		Size: 43000,
		ModTime: modTime,
	}

	fileOnMirror := &filesystem.FileInfo{
		Path: "/test/file.tgz",
		Size: 43000,
		ModTime: outdatedModTime,
	}

	configValues := []bool{false, true}

	tests := map[string]struct {
		tzoffset int64
		excludeReason []string
		
	} {
		"tzoffset_unset": {
			tzoffset: 0,
			excludeReason: []string{
				"Mod time mismatch (diff: 1h0m0s)",
				"Mod time mismatch (diff: 1h0m0s)",
			},
		},
		"tzoffset_set": {
			tzoffset: offset * 1000,
			excludeReason: []string{
				"Mod time mismatch (diff: 1h0m0s)",
				"",
			},
		},
	}

	for idx, configValue := range configValues {
		SetConfiguration(&Configuration{
			FixTimezoneOffsets: configValue,
		})
		for name, test := range tests {
			m1 := mirrors.Mirror{
				HttpURL: fmt.Sprintf("https://m%d.mirror", 1),
				Enabled: true,
				HttpsUp: true,
				FileInfo: fileOnMirror,
				TZOffset: test.tzoffset,
			}
			t.Run(name, func(t *testing.T) {
				testFilterSingle(t, m1, WITHTLS, fileRequested, noClientInfo, test.excludeReason[idx])
			})
		}
	}
}