Debian Codegraph @

http/selection_test.go

raw
1 // Copyright (c) 2024 Arnaud Rebillout
2 // Licensed under the MIT license
3
4 package http
5
6 import (
7 "fmt"
8 "testing"
9 "time"
10
11 . "github.com/etix/mirrorbits/config"
12 "github.com/etix/mirrorbits/filesystem"
13 "github.com/etix/mirrorbits/network"
14 "github.com/etix/mirrorbits/mirrors"
15 )
16
17 var noFileInfo *filesystem.FileInfo
18 var noClientInfo network.GeoIPRecord
19
20 func TestMain(m *testing.M) {
21 noFileInfo = nil
22 noClientInfo = network.GeoIPRecord{}
23 SetConfiguration(&Configuration{
24 FixTimezoneOffsets: false,
25 })
26 m.Run()
27 }
28
29 // Helper to check the results of the Filter function on a single mirror
30 func checkResultsSingle(t *testing.T, a mirrors.Mirrors, x mirrors.Mirrors, reason string, url string) {
31 t.Helper()
32 var m mirrors.Mirror
33
34 // If reason was set, we expect the mirror to have been excluded, while
35 // no reason means that the mirror should have been accepted.
36 if reason == "" {
37 if len(a) != 1 || len(x) != 0 {
38 t.Fatalf("There should be 1 mirror accepted and 0 mirror excluded")
39 }
40 m = a[0]
41 } else {
42 if len(a) != 0 || len(x) != 1 {
43 t.Fatalf("There should be 0 mirror accepted and 1 mirror excluded")
44 }
45 m = x[0]
46 }
47
48 // Test that the field ExcludeReason was set as expected, or is unset
49 // in case the mirror was accepted.
50 if m.ExcludeReason != reason {
51 t.Fatalf("Invalid ExcludeReason, expected '%s', got '%s'", reason, m.ExcludeReason)
52 }
53
54 // The field AbsoluteURL is expected to be set, except in the case when
55 // the mirror is disabled.
56 if m.ExcludeReason != "Disabled" && m.AbsoluteURL != url {
57 t.Fatalf("Invalid AbsoluteURL, expected '%s', got '%s'", url, m.AbsoluteURL)
58 }
59 }
60
61 // Helper to test the Filter function on a single mirror
62 func testFilterSingle(t *testing.T, m mirrors.Mirror, secureOption SecureOption, fileInfo *filesystem.FileInfo, clientInfo network.GeoIPRecord, reason string) {
63 t.Helper()
64 mlist := mirrors.Mirrors{m}
65 a, x, _, _ := Filter(mlist, secureOption, fileInfo, clientInfo)
66 checkResultsSingle(t, a, x, reason, m.HttpURL)
67 }
68
69 // Helper to test the Filter function on a single mirror, with a specific AbsoluteURL
70 func testFilterSingleAbsoluteURL(t *testing.T, m mirrors.Mirror, secureOption SecureOption, fileInfo *filesystem.FileInfo, clientInfo network.GeoIPRecord, reason string, url string) {
71 t.Helper()
72 mlist := mirrors.Mirrors{m}
73 a, x, _, _ := Filter(mlist, secureOption, fileInfo, clientInfo)
74 checkResultsSingle(t, a, x, reason, url)
75 }
76
77 func TestFilter(t *testing.T) {
78 // Test that a mirror that is disabled is rejected
79
80 m1 := mirrors.Mirror{
81 HttpURL: "http://m1.mirror",
82 }
83 t.Run("disabled", func(t *testing.T) {
84 testFilterSingle(t, m1, UNDEFINED, noFileInfo, noClientInfo, "Disabled")
85 })
86
87 // Given that a mirror is enabled, test that it's rejected when the
88 // requested protocol is not available (either it's not supported by
89 // the mirror, or it's down).
90
91 tests1 := map[string]struct {
92 secureOption SecureOption
93 mirrorURL string
94 excludeReason string
95 absoluteURL string
96 } {
97 "want_https_but_http_only": {
98 secureOption: WITHTLS,
99 mirrorURL: "http://m1.mirror",
100 excludeReason: "Not HTTPS",
101 absoluteURL: "http://m1.mirror",
102 },
103 "want_https_has_https_but_down": {
104 secureOption: WITHTLS,
105 mirrorURL: "https://m1.mirror",
106 excludeReason: "Down",
107 absoluteURL: "https://m1.mirror",
108 },
109 "want_https_has_any_but_down": {
110 secureOption: WITHTLS,
111 mirrorURL: "m1.mirror",
112 excludeReason: "Down",
113 absoluteURL: "https://m1.mirror",
114 },
115 "want_http_but_https_only": {
116 secureOption: WITHOUTTLS,
117 mirrorURL: "https://m1.mirror",
118 excludeReason: "Not HTTP",
119 absoluteURL: "https://m1.mirror",
120 },
121 "want_http_has_http_but_down": {
122 secureOption: WITHOUTTLS,
123 mirrorURL: "http://m1.mirror",
124 excludeReason: "Down",
125 absoluteURL: "http://m1.mirror",
126 },
127 "want_http_has_any_but_down": {
128 secureOption: WITHOUTTLS,
129 mirrorURL: "m1.mirror",
130 excludeReason: "Down",
131 absoluteURL: "http://m1.mirror",
132 },
133 "want_any_has_http_only_but_down": {
134 secureOption: UNDEFINED,
135 mirrorURL: "http://m1.mirror",
136 excludeReason: "Down / Not HTTPS",
137 absoluteURL: "http://m1.mirror",
138 },
139 "want_any_has_https_only_but_down": {
140 secureOption: UNDEFINED,
141 mirrorURL: "https://m1.mirror",
142 excludeReason: "Not HTTP / Down",
143 absoluteURL: "https://m1.mirror",
144 },
145 "want_any_has_any_but_down": {
146 secureOption: UNDEFINED,
147 mirrorURL: "m1.mirror",
148 excludeReason: "Down",
149 absoluteURL: "http://m1.mirror",
150 },
151 }
152
153 for name, test := range tests1 {
154 m1 := mirrors.Mirror{
155 HttpURL: test.mirrorURL,
156 Enabled: true,
157 }
158 t.Run(name, func(t *testing.T) {
159 testFilterSingleAbsoluteURL(t, m1, test.secureOption, noFileInfo, noClientInfo,
160 test.excludeReason, test.absoluteURL)
161 })
162 }
163
164 // Given that a mirror is enabled and the protocol requested is available,
165 // test that a mirror is rejected when the requested file is not valid
166 // (wrong size or mod time).
167
168 testfile := &filesystem.FileInfo{
169 Path: "/test/file.tgz",
170 Size: 43000,
171 ModTime: time.Now(),
172 }
173
174 tests2 := map[string]struct {
175 fileSize int64
176 fileModTime time.Time
177 excludeReason string
178 } {
179 "wrong_size": {
180 fileSize: 12345,
181 fileModTime: testfile.ModTime,
182 excludeReason: "File size mismatch",
183 },
184 "wrong_mod_time_newer_on_mirror": {
185 fileSize: testfile.Size,
186 fileModTime: testfile.ModTime.Add(time.Second * 10),
187 excludeReason: "Mod time mismatch (diff: -10s)",
188 },
189 "wrong_mod_time_older_on_mirror": {
190 fileSize: testfile.Size,
191 fileModTime: testfile.ModTime.Add(time.Second * -10),
192 excludeReason: "Mod time mismatch (diff: 10s)",
193 },
194 }
195
196 for name, test := range tests2 {
197 m1 := mirrors.Mirror{
198 HttpURL: "https://m1.mirror",
199 Enabled: true,
200 HttpsUp: true,
201 FileInfo: &filesystem.FileInfo{
202 Path: "/test/file.tgz",
203 Size: test.fileSize,
204 ModTime: test.fileModTime,
205 },
206 }
207 t.Run(name, func(t *testing.T) {
208 testFilterSingle(t, m1, WITHTLS, testfile, noClientInfo, test.excludeReason)
209 })
210 }
211
212 // Given that a mirror is enabled, the protocol requested is available
213 // and the file on the mirror is valid, test that a mirror is rejected
214 // when the client doesn't meet the geolocation requirements.
215
216 clientInfo := network.GeoIPRecord{
217 ContinentCode: "EU",
218 CountryCode: "FR",
219 ASNum: 4444,
220 }
221
222 tests3 := map[string]struct {
223 continentOnly bool
224 continentCode string
225 countryOnly bool
226 countryCodes string
227 asOnly bool
228 asNum uint
229 excludedCountryCodes string
230 excludeReason string
231 } {
232 "wrong_continent": {
233 continentOnly: true,
234 continentCode: "NA",
235 excludeReason: "Continent only",
236 },
237 "wrong_country": {
238 countryOnly: true,
239 countryCodes: "UK",
240 excludeReason: "Country only",
241 },
242 "wrong_countries": {
243 countryOnly: true,
244 countryCodes: "FI NO SE",
245 excludeReason: "Country only",
246 },
247 "wrong_as": {
248 asOnly: true,
249 asNum: 5555,
250 excludeReason: "AS only",
251 },
252 "excluded_country": {
253 excludedCountryCodes: "FR",
254 excludeReason: "User's country restriction",
255 },
256 "excluded_countries": {
257 excludedCountryCodes: "ES FR IT PT",
258 excludeReason: "User's country restriction",
259 },
260 }
261
262 for name, test := range tests3 {
263 m1 := mirrors.Mirror{
264 HttpURL: "https://m1.mirror",
265 Enabled: true,
266 HttpsUp: true,
267 FileInfo: testfile,
268 ContinentOnly: test.continentOnly,
269 ContinentCode: test.continentCode,
270 CountryOnly: test.countryOnly,
271 CountryCodes: test.countryCodes,
272 ASOnly: test.asOnly,
273 Asnum: test.asNum,
274 ExcludedCountryCodes: test.excludedCountryCodes,
275 }
276 m1.Prepare()
277 t.Run(name, func(t *testing.T) {
278 testFilterSingle(t, m1, WITHTLS, testfile, clientInfo, test.excludeReason)
279 })
280 }
281
282 // Given valid mirrors, test that the distances returned are correct.
283
284 tests4 := map[string]struct {
285 distances []float32
286 extrema []float32
287 } {
288 "no_mirror": {
289 distances: []float32{},
290 extrema: []float32{0, 0},
291 },
292 "one_mirror": {
293 distances: []float32{10},
294 extrema: []float32{10, 10},
295 },
296 "some_mirrors": {
297 distances: []float32{30, 20, 10},
298 extrema: []float32{10, 30},
299 },
300 }
301
302 for name, test := range tests4 {
303 mlist := make([]mirrors.Mirror, 0, 5)
304 for i, d := range test.distances {
305 m := mirrors.Mirror{
306 HttpURL: fmt.Sprintf("https://m%d.mirror", i),
307 Enabled: true,
308 HttpsUp: true,
309 FileInfo: testfile,
310 Distance: d,
311 }
312 mlist = append(mlist, m)
313 }
314 t.Run(name, func(t *testing.T) {
315 a, x, closest, farthest := Filter(mlist, WITHTLS, testfile, clientInfo)
316 if len(a) != len(mlist) || len(x) != 0 {
317 t.Fatalf("There should be %d mirror(s) accepted and 0 mirror excluded",
318 len(mlist))
319 }
320 if closest != test.extrema[0] || farthest != test.extrema[1] {
321 t.Fatalf("Wrong results for [closest farthest], expected %v, got %v",
322 test.extrema, []float32{closest, farthest})
323 }
324 })
325 }
326 }
327
328 func TestFilterAllowOutdatedFiles(t *testing.T) {
329 // Given a file that is outdated on a mirror, test that the mirror is
330 // rejected, unless the configuration setting AllowOutdatedFiles is set
331 // correctly in order to accept this file.
332
333 testfile := &filesystem.FileInfo{
334 Path: "/test/file.tgz",
335 Size: 43000,
336 ModTime: time.Now(),
337 }
338
339 configValues := [][]OutdatedFilesConfig{
340 []OutdatedFilesConfig{},
341 []OutdatedFilesConfig{{
342 Prefix: "/test/",
343 Minutes: 1,
344 }},
345 []OutdatedFilesConfig{{
346 Prefix: "/wrong/",
347 Minutes: 2,
348 }},
349 []OutdatedFilesConfig{{
350 Prefix: "/test/",
351 Minutes: 2,
352 }},
353 }
354
355 tests := map[string]struct {
356 fileSize int64
357 fileModTime time.Time
358 excludeReason []string
359 } {
360 "outdated_same_size": {
361 fileSize: testfile.Size,
362 fileModTime: testfile.ModTime.Add(-100 * time.Second),
363 excludeReason: []string{
364 "Mod time mismatch (diff: 1m40s)",
365 "Mod time mismatch (diff: 1m40s)",
366 "Mod time mismatch (diff: 1m40s)",
367 "",
368 },
369 },
370 "outdated_different_size": {
371 fileSize: 12345,
372 fileModTime: testfile.ModTime.Add(-100 * time.Second),
373 excludeReason: []string{
374 "File size mismatch",
375 "Mod time mismatch (diff: 1m40s)",
376 "File size mismatch",
377 "",
378 },
379 },
380 }
381
382 for idx, configValue := range configValues {
383 SetConfiguration(&Configuration{
384 AllowOutdatedFiles: configValue,
385 })
386 for name, test := range tests {
387 m1 := mirrors.Mirror{
388 HttpURL: fmt.Sprintf("https://m%d.mirror", 1),
389 Enabled: true,
390 HttpsUp: true,
391 FileInfo: &filesystem.FileInfo{
392 Path: testfile.Path,
393 Size: test.fileSize,
394 ModTime: test.fileModTime,
395 },
396 }
397 t.Run(name, func(t *testing.T) {
398 testFilterSingle(t, m1, WITHTLS, testfile, noClientInfo, test.excludeReason[idx])
399 })
400 }
401 }
402 }
403
404 func TestFilterFixTimezoneOffsets(t *testing.T) {
405 // Given a mirror with a 1-hour timezone offset, test that the mirror
406 // is rejected unless 1) the TZOffset of the mirror is set correctly,
407 // and 2) the configuration setting FixTimezoneOffsets is enabled.
408
409 var offset int64 = 3600
410 modTime := time.Now()
411 outdatedModTime := modTime.Add(time.Duration(-offset) * time.Second)
412
413 fileRequested := &filesystem.FileInfo{
414 Path: "/test/file.tgz",
415 Size: 43000,
416 ModTime: modTime,
417 }
418
419 fileOnMirror := &filesystem.FileInfo{
420 Path: "/test/file.tgz",
421 Size: 43000,
422 ModTime: outdatedModTime,
423 }
424
425 configValues := []bool{false, true}
426
427 tests := map[string]struct {
428 tzoffset int64
429 excludeReason []string
430
431 } {
432 "tzoffset_unset": {
433 tzoffset: 0,
434 excludeReason: []string{
435 "Mod time mismatch (diff: 1h0m0s)",
436 "Mod time mismatch (diff: 1h0m0s)",
437 },
438 },
439 "tzoffset_set": {
440 tzoffset: offset * 1000,
441 excludeReason: []string{
442 "Mod time mismatch (diff: 1h0m0s)",
443 "",
444 },
445 },
446 }
447
448 for idx, configValue := range configValues {
449 SetConfiguration(&Configuration{
450 FixTimezoneOffsets: configValue,
451 })
452 for name, test := range tests {
453 m1 := mirrors.Mirror{
454 HttpURL: fmt.Sprintf("https://m%d.mirror", 1),
455 Enabled: true,
456 HttpsUp: true,
457 FileInfo: fileOnMirror,
458 TZOffset: test.tzoffset,
459 }
460 t.Run(name, func(t *testing.T) {
461 testFilterSingle(t, m1, WITHTLS, fileRequested, noClientInfo, test.excludeReason[idx])
462 })
463 }
464 }
465 }