-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathproperty_test.go
More file actions
487 lines (408 loc) · 13.4 KB
/
property_test.go
File metadata and controls
487 lines (408 loc) · 13.4 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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package txeh
import (
"slices"
"strings"
"testing"
"testing/quick"
"pgregory.net/rapid"
)
// =============================================================================
// Property-based tests using testing/quick (stdlib)
// These verify invariants that must hold for ALL valid inputs.
// =============================================================================
// TestProperty_ParseRenderRoundTrip_AlwaysParseable verifies that parsing any
// hosts content and rendering it always produces parseable output.
func TestProperty_ParseRenderRoundTrip_AlwaysParseable(t *testing.T) {
t.Parallel()
f := func(input string) bool {
hosts, err := NewHosts(&HostsConfig{RawText: &input})
if err != nil {
return true // invalid input is fine to reject
}
rendered := hosts.RenderHostsFile()
_, err = ParseHostsFromString(rendered)
return err == nil
}
if err := quick.Check(f, &quick.Config{MaxCount: 1000}); err != nil {
t.Error(err)
}
}
// TestProperty_AddHost_ThenLookup_FindsIt verifies that adding a valid host
// to a valid address always makes it findable via lookup.
func TestProperty_AddHost_ThenLookup_FindsIt(t *testing.T) {
t.Parallel()
// Use a fixed set of valid IPs to test the property
validIPs := []string{"10.0.0.1", "192.168.1.1", "172.16.0.1"}
validHosts := []string{"host-a", "host-b", "host-c", "host-d", "host-e"}
f := func(ipIdx, hostIdx uint) bool {
ip := validIPs[ipIdx%uint(len(validIPs))]
hostname := validHosts[hostIdx%uint(len(validHosts))]
base := ""
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
return false
}
hosts.AddHost(ip, hostname)
found := hosts.ListHostsByIP(ip)
return slices.Contains(found, hostname)
}
if err := quick.Check(f, &quick.Config{MaxCount: 500}); err != nil {
t.Error(err)
}
}
// TestProperty_RemoveHost_ThenLookup_NeverFindsIt verifies that removing a host
// guarantees it is no longer associated with any address.
func TestProperty_RemoveHost_ThenLookup_NeverFindsIt(t *testing.T) {
t.Parallel()
hostnames := []string{"alpha", "bravo", "charlie", "delta"}
f := func(idx uint) bool {
hostname := hostnames[idx%uint(len(hostnames))]
base := "10.0.0.1 alpha bravo\n10.0.0.2 charlie delta\n"
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
return false
}
hosts.RemoveHost(hostname)
// Must not appear at any address
lines := hosts.GetHostFileLines()
for _, line := range lines {
if slices.Contains(line.Hostnames, hostname) {
return false
}
}
return true
}
if err := quick.Check(f, &quick.Config{MaxCount: 500}); err != nil {
t.Error(err)
}
}
// TestProperty_RemoveAddress_RemovesAllHostnames verifies that removing an
// address removes every hostname that was on that address's lines.
func TestProperty_RemoveAddress_RemovesAllHostnames(t *testing.T) {
t.Parallel()
base := "10.0.0.1 host1 host2 host3\n10.0.0.2 host4\n"
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
t.Fatal(err)
}
hosts.RemoveAddress("10.0.0.1")
result := hosts.ListHostsByIP("10.0.0.1")
if len(result) != 0 {
t.Errorf("after RemoveAddress, expected 0 hosts at 10.0.0.1, got %d: %v", len(result), result)
}
// Other addresses must be unaffected
other := hosts.ListHostsByIP("10.0.0.2")
if len(other) != 1 || other[0] != "host4" {
t.Errorf("RemoveAddress affected unrelated address, got: %v", other)
}
}
// TestProperty_AddHost_InvalidIP_IsNoOp verifies that adding a host with an
// invalid IP address never modifies the hosts file.
func TestProperty_AddHost_InvalidIP_IsNoOp(t *testing.T) {
t.Parallel()
f := func(hostname string) bool {
base := "127.0.0.1 existing\n"
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
return true
}
before := hosts.RenderHostsFile()
hosts.AddHost("not-a-valid-ip", hostname)
after := hosts.RenderHostsFile()
return before == after
}
if err := quick.Check(f, &quick.Config{MaxCount: 500}); err != nil {
t.Error(err)
}
}
// TestProperty_ParsedLineCount_MatchesInputLines verifies that parsing always
// produces the same number of lines as the input (minus trailing empty line).
func TestProperty_ParsedLineCount_MatchesInputLines(t *testing.T) {
t.Parallel()
f := func(input string) bool {
lines, err := ParseHostsFromString(input)
if err != nil {
return true
}
normalized := strings.ReplaceAll(input, "\r\n", "\n")
inputLines := strings.Split(normalized, "\n")
// Parser trims trailing empty line if present
if len(inputLines) > 0 && inputLines[len(inputLines)-1] == "" {
inputLines = inputLines[:len(inputLines)-1]
}
return len(lines) == len(inputLines)
}
if err := quick.Check(f, &quick.Config{MaxCount: 1000}); err != nil {
t.Error(err)
}
}
// =============================================================================
// Property-based tests using rapid
// These use controlled generators for more targeted invariant testing.
// =============================================================================
// validIPGen generates valid IPv4 addresses in private ranges.
func validIPGen() *rapid.Generator[string] {
return rapid.Custom(func(t *rapid.T) string {
octet3 := rapid.IntRange(0, 255).Draw(t, "octet3")
octet4 := rapid.IntRange(1, 254).Draw(t, "octet4")
prefix := rapid.SampledFrom([]string{"10.0", "192.168", "172.16"}).Draw(t, "prefix")
return prefix + "." + itoa(octet3) + "." + itoa(octet4)
})
}
// hostnameGen generates valid-looking hostnames.
func hostnameGen() *rapid.Generator[string] {
return rapid.Custom(func(t *rapid.T) string {
prefix := rapid.SampledFrom([]string{
"app", "web", "api", "db", "cache", "svc", "node", "host",
}).Draw(t, "prefix")
suffix := rapid.SampledFrom([]string{
".local", ".internal", ".test", ".example.com", "",
}).Draw(t, "suffix")
num := rapid.IntRange(0, 99).Draw(t, "num")
return prefix + "-" + itoa(num) + suffix
})
}
// itoa converts an int to a string without importing strconv.
func itoa(n int) string {
if n == 0 {
return "0"
}
s := ""
neg := n < 0
if neg {
n = -n
}
for n > 0 {
s = string(rune('0'+n%10)) + s
n /= 10
}
if neg {
s = "-" + s
}
return s
}
// TestRapid_AddRemove_Symmetry verifies that adding then removing a host
// returns the hosts file to a state where that host is not present.
func TestRapid_AddRemove_Symmetry(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
ip := validIPGen().Draw(t, "ip")
hostname := hostnameGen().Draw(t, "hostname")
base := testHostsLocalhost
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
t.Fatal(err)
}
hosts.AddHost(ip, hostname)
// Verify it was added
found := hosts.ListHostsByIP(ip)
hasHost := slices.Contains(found, hostname)
if !hasHost {
t.Fatalf("AddHost(%q, %q) did not add the host", ip, hostname)
}
// Remove and verify it's gone
hosts.RemoveHost(hostname)
foundAfter := hosts.ListHostsByIP(ip)
for _, h := range foundAfter {
if h == hostname {
t.Fatalf("RemoveHost(%q) did not remove the host from %q", hostname, ip)
}
}
})
}
// TestRapid_AddHost_Idempotent verifies that adding the same host twice
// to the same address doesn't create duplicates.
func TestRapid_AddHost_Idempotent(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
ip := validIPGen().Draw(t, "ip")
hostname := hostnameGen().Draw(t, "hostname")
base := ""
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
t.Fatal(err)
}
hosts.AddHost(ip, hostname)
hosts.AddHost(ip, hostname)
found := hosts.ListHostsByIP(ip)
count := 0
for _, h := range found {
if h == hostname {
count++
}
}
if count != 1 {
t.Fatalf("AddHost called twice produced %d copies of %q (want 1)", count, hostname)
}
})
}
// TestRapid_RemoveHost_Idempotent verifies that removing a host that doesn't
// exist is a safe no-op that doesn't corrupt state.
func TestRapid_RemoveHost_Idempotent(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
hostname := hostnameGen().Draw(t, "hostname")
base := "127.0.0.1 localhost\n10.0.0.1 existing\n"
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
t.Fatal(err)
}
before := hosts.RenderHostsFile()
hosts.RemoveHost(hostname)
hosts.RemoveHost(hostname) // second remove should be safe no-op
after := hosts.RenderHostsFile()
// If host wasn't in original, content should be unchanged
if !strings.Contains(base, hostname) && before != after {
t.Fatalf("removing non-existent host %q modified the file", hostname)
}
})
}
// TestRapid_BulkAddRemove_StateConsistency verifies that after a sequence of
// adds and removes, the rendered hosts file is always valid and consistent.
func TestRapid_BulkAddRemove_StateConsistency(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
numOps := rapid.IntRange(5, 30).Draw(t, "numOps")
base := testHostsLocalhost
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
t.Fatal(err)
}
for range numOps {
ip := validIPGen().Draw(t, "ip")
hostname := hostnameGen().Draw(t, "hostname")
if rapid.Bool().Draw(t, "isAdd") {
hosts.AddHost(ip, hostname)
} else {
hosts.RemoveHost(hostname)
}
}
// After all operations, state must be consistent
rendered := hosts.RenderHostsFile()
// Must be parseable
reparsed, err := ParseHostsFromString(rendered)
if err != nil {
t.Fatalf("rendered output not parseable after %d ops: %v", numOps, err)
}
// All ADDRESS lines must have non-empty address and hostnames
validateParsedLines(t, reparsed, numOps)
})
}
// validateParsedLines checks that all ADDRESS lines have non-empty address and hostnames.
func validateParsedLines(t *rapid.T, lines []HostFileLine, _ int) {
t.Helper()
for _, line := range lines {
if line.LineType == ADDRESS {
if line.Address == "" {
t.Fatal("ADDRESS line has empty address after bulk operations")
}
if len(line.Hostnames) == 0 {
t.Fatal("ADDRESS line has no hostnames after bulk operations")
}
}
}
}
// TestRapid_CommentOperations_Consistent verifies that adding hosts with comments
// and removing by comment produces consistent state.
func TestRapid_CommentOperations_Consistent(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
ip := validIPGen().Draw(t, "ip")
hostname := hostnameGen().Draw(t, "hostname")
comment := rapid.SampledFrom([]string{
"kubefwd", "added by myapp", "test entry", "k8s-svc",
}).Draw(t, "comment")
base := testHostsLocalhost
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
t.Fatal(err)
}
// Add with comment
hosts.AddHostWithComment(ip, hostname, comment)
// Must be findable by comment
byComment := hosts.ListHostsByComment(comment)
found := slices.Contains(byComment, hostname)
if !found {
t.Fatalf("AddHostWithComment(%q, %q, %q): host not found via ListHostsByComment", ip, hostname, comment)
}
// Remove by comment
hosts.RemoveByComment(comment)
// Must no longer be findable by comment
byCommentAfter := hosts.ListHostsByComment(comment)
if len(byCommentAfter) != 0 {
t.Fatalf("RemoveByComment(%q) did not remove hosts: %v", comment, byCommentAfter)
}
})
}
// TestRapid_HostnameMoveBetweenAddresses verifies that adding a hostname to a
// new (non-localhost) address removes it from the old address.
func TestRapid_HostnameMoveBetweenAddresses(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
hostname := hostnameGen().Draw(t, "hostname")
// Use non-localhost IPs so the move behavior applies
ip1 := "10." + itoa(rapid.IntRange(0, 255).Draw(t, "o1")) + ".0.1"
ip2 := "10." + itoa(rapid.IntRange(0, 255).Draw(t, "o2")) + ".0.2"
base := ""
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
t.Fatal(err)
}
// Add to first address
hosts.AddHost(ip1, hostname)
// Move to second address
hosts.AddHost(ip2, hostname)
// If IPs differ, hostname should only be at ip2
if ip1 != ip2 {
atIP1 := hosts.ListHostsByIP(ip1)
for _, h := range atIP1 {
if h == hostname {
t.Fatalf("hostname %q should have moved from %s to %s, but still at old address", hostname, ip1, ip2)
}
}
}
atIP2 := hosts.ListHostsByIP(ip2)
found := slices.Contains(atIP2, hostname)
if !found {
t.Fatalf("hostname %q not found at new address %s", hostname, ip2)
}
})
}
// TestRapid_RenderParse_PreservesAddressEntries verifies that rendering and
// re-parsing preserves all address-hostname mappings.
func TestRapid_RenderParse_PreservesAddressEntries(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
numEntries := rapid.IntRange(1, 10).Draw(t, "numEntries")
base := ""
hosts, err := NewHosts(&HostsConfig{RawText: &base})
if err != nil {
t.Fatal(err)
}
type entry struct {
ip string
hostname string
}
entries := make([]entry, 0, numEntries)
for range numEntries {
ip := validIPGen().Draw(t, "ip")
hostname := hostnameGen().Draw(t, "hostname")
hosts.AddHost(ip, hostname)
entries = append(entries, entry{ip, hostname})
}
// Render and re-parse
rendered := hosts.RenderHostsFile()
hosts2, err := NewHosts(&HostsConfig{RawText: &rendered})
if err != nil {
t.Fatalf("re-parse failed: %v", err)
}
// Every entry that was added should still be findable
// (some may have been moved if the same hostname was added to multiple IPs)
for _, e := range entries {
results := hosts2.ListAddressesByHost(e.hostname, true)
if len(results) == 0 {
t.Fatalf("hostname %q lost after render/re-parse", e.hostname)
}
}
})
}