-
Notifications
You must be signed in to change notification settings - Fork 12
/
hosts_test.go
806 lines (689 loc) · 23.3 KB
/
hosts_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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
package hostsfile
import (
"errors"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/icrowley/fake"
"github.com/stretchr/testify/assert"
)
func randomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
func newHosts() *Hosts {
return &Hosts{
ips: newLookup(),
hosts: newLookup(),
}
}
func newMacOSXDefault() *Hosts {
h := newHosts()
if err := h.loadString(`##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost`); err != nil {
return newHosts()
}
return h
}
func newWindowsDefault() *Hosts {
h := newHosts()
if err := h.loadString(`# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# localhost name resolution is handled within DNS itself.
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# 127.0.0.1 localhost
# ::1 localhost`); err != nil {
return newHosts()
}
return h
}
func newDockerDesktopWindowsDefault() *Hosts {
h := newHosts()
if err := h.loadString(`# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
# Added by Docker Desktop
192.168.8.11 host.docker.internal
192.168.8.11 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section`); err != nil {
return newHosts()
}
return h
}
func newProxmoxDefault() *Hosts {
h := newHosts()
if err := h.loadString(`[::1 ip6-localhost ip6-loopback]
fe00::0
ff00::0
ff02::1
ff02::2
ff02::3`); err != nil {
return newHosts()
}
return h
}
func newMAMPDefault() *Hosts {
h := newHosts()
if err := h.loadString(`127.0.0.1 scratch.test # MAMP PRO - Do NOT remove this entry!
::1 scratch.test # MAMP PRO - Do NOT remove this entry!
127.0.0.1 clean.test # MAMP PRO - Do NOT remove this entry!
::1 clean.test # MAMP PRO - Do NOT remove this entry!
127.0.0.1 cnmd.test # MAMP PRO - Do NOT remove this entry!
::1 cnmd.test # MAMP PRO - Do NOT remove this entry!
127.0.0.1 boilerplate.test # MAMP PRO - Do NOT remove this entry!
::1 boilerplate.test # MAMP PRO - Do NOT remove this entry!
127.0.0.1 macster.local # MAMP PRO - Do NOT remove this entry!
::1 macster.local # MAMP PRO - Do NOT remove this entry!`); err != nil {
return newHosts()
}
return h
}
func Test_DefaultHosts(t *testing.T) {
mac := newMacOSXDefault()
assert.Len(t, mac.Lines, 9)
win := newWindowsDefault()
assert.Len(t, win.Lines, 20)
pve := newProxmoxDefault()
assert.Len(t, pve.Lines, 6)
mamp := newMAMPDefault()
assert.Len(t, mamp.Lines, 10)
winDockerDesktop := newDockerDesktopWindowsDefault()
assert.Len(t, winDockerDesktop.Lines, 27)
}
func Test_NewHosts(t *testing.T) {
hosts, err := NewHosts()
assert.NoError(t, err)
assert.NotEqual(t, "", hosts.Path)
// test env var
expected := os.ExpandEnv(filepath.FromSlash("./test"))
f, err := os.Create(expected)
assert.Nil(t, err)
defer func() {
if err := f.Close(); err != nil {
log.Panic(err)
}
if err := os.Remove(expected); err != nil {
log.Panic(err)
}
}()
assert.Nil(t, os.Setenv("HOSTS_PATH", expected))
hosts, err = NewHosts()
assert.NoError(t, err)
assert.Equal(t, expected, hosts.Path)
// test is writeable
assert.True(t, hosts.IsWritable())
hosts.Path = "./noexist"
assert.False(t, hosts.IsWritable())
// test bad load
assert.Error(t, hosts.Load())
}
func Test_NewCustomHosts(t *testing.T) {
// bad file
_, err := NewCustomHosts("./noexist")
assert.Error(t, err)
}
func TestHostsLine_IsComment(t *testing.T) {
comment := " # This is a comment "
line := NewHostsLine(comment)
assert.True(t, line.IsComment())
}
func TestNewHostsLine(t *testing.T) {
var hlTests = []struct {
input string
output HostsLine
asserts func(t *testing.T, hl HostsLine)
}{
{
input: "",
output: HostsLine{
Raw: "",
Comment: "",
Err: nil,
},
asserts: func(t *testing.T, hl HostsLine) {
assert.False(t, hl.IsComment())
assert.False(t, hl.IsValid())
},
}, {
input: " # This is a comment ",
output: HostsLine{
Raw: " # This is a comment ",
Comment: " This is a comment ",
Err: nil,
},
asserts: func(t *testing.T, hl HostsLine) {
assert.True(t, hl.HasComment())
assert.False(t, hl.IsValid())
},
}, {
input: "127.0.0.1 test1 test2 # This is a comment ",
output: HostsLine{
Raw: "127.0.0.1 test1 test2 # This is a comment ",
IP: "127.0.0.1",
Hosts: []string{
"test1", "test2",
},
Comment: " This is a comment ",
Err: nil,
},
asserts: func(t *testing.T, hl HostsLine) {
assert.True(t, hl.HasComment())
assert.False(t, hl.IsMalformed())
},
}, {
// bad ip parse
input: "127.x.x.1 test1 test2 # This is a comment ",
output: HostsLine{
Raw: "127.x.x.1 test1 test2 # This is a comment ",
IP: "127.x.x.1",
Hosts: []string{
"test1", "test2",
},
Comment: " This is a comment ",
Err: errors.New("bad hosts line: \"127.x.x.1 test1 test2 \""),
},
asserts: func(t *testing.T, hl HostsLine) {
assert.True(t, hl.IsValid()) // technically valid, just had an ip parse error... ?
assert.True(t, hl.IsMalformed())
},
},
}
for _, tt := range hlTests {
hl := NewHostsLine(tt.input)
assert.Equal(t, tt.output, hl)
if nil != tt.asserts {
tt.asserts(t, hl)
}
}
}
func TestHosts_Has(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.AddRaw("127.0.0.1 yadda", "10.0.0.7 nada"))
assert.True(t, hosts.Has("10.0.0.7", "nada"))
assert.False(t, hosts.Has("10.0.0.7", "shuda"))
}
func TestHosts_Remove(t *testing.T) {
// when last host ip combo
expectedLines := []HostsLine{NewHostsLine("127.0.0.1 yadda")}
hosts := newHosts()
assert.Nil(t, hosts.AddRaw("127.0.0.1 yadda", "10.0.0.7 nada"))
assert.Nil(t, hosts.Remove("10.0.0.7", "nada"))
assert.Equal(t, expectedLines, hosts.Lines)
// when ip has other hosts
expectedLines = []HostsLine{NewHostsLine("127.0.0.1 yadda"), NewHostsLine("10.0.0.7 brada")}
hosts = newHosts()
assert.Nil(t, hosts.AddRaw("127.0.0.1 yadda", "10.0.0.7 nada brada"))
assert.Nil(t, hosts.Remove("10.0.0.7", "nada"))
assert.Equal(t, expectedLines, hosts.Lines)
// remove multiple entries
hosts = newHosts()
assert.Nil(t, hosts.AddRaw("127.0.0.1 yadda nadda prada"))
assert.Nil(t, hosts.Remove("127.0.0.1", "yadda", "prada"))
assert.Equal(t, hosts.Lines[0].Raw, "127.0.0.1 nadda")
// nothing to remove
assert.Nil(t, hosts.Remove("127.0.0.1"))
// remove bad ip
assert.Error(t, hosts.Remove("not an ip"))
}
func TestHosts_HasHostname(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.Nil(t, hosts.Add("10.0.0.7", "nada"))
assert.True(t, hosts.HasHostname("nada"))
assert.False(t, hosts.HasHostname("shuda"))
}
func TestHosts_RemoveByIp(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.Nil(t, hosts.Add("10.0.0.7", "nada"))
assert.Nil(t, hosts.Add("42.42.42.42", "foo"))
assert.Nil(t, hosts.Add("10.0.0.255", "bar"))
// remove nothing
assert.Nil(t, hosts.RemoveByIp("192.168.1.1"))
assert.Len(t, hosts.Lines, 4)
assert.Len(t, hosts.ips.l, 4)
assert.Len(t, hosts.hosts.l, 4)
// remove 1
assert.Nil(t, hosts.RemoveByIp("10.0.0.255"))
assert.Len(t, hosts.Lines, 3)
assert.Len(t, hosts.ips.l, 3)
assert.Len(t, hosts.hosts.l, 3)
// remove 1
assert.Nil(t, hosts.RemoveByIp("10.0.0.7"))
assert.Len(t, hosts.Lines, 2)
assert.Len(t, hosts.ips.l, 2)
assert.Len(t, hosts.hosts.l, 2)
// remove 1
assert.Nil(t, hosts.RemoveByIp("127.0.0.1"))
assert.Len(t, hosts.Lines, 1)
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 1)
// remove 0
assert.Nil(t, hosts.RemoveByIp("10.0.0.7"))
assert.Len(t, hosts.Lines, 1)
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 1)
}
func TestHosts_RemoveByHostname(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.Nil(t, hosts.Add("168.1.1.1", "yadda"))
assert.Nil(t, hosts.RemoveByHostname("yadda"))
assert.False(t, hosts.HasHostname("yadda"))
// remove if hostname doesn't exist
hosts = newHosts()
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.False(t, hosts.HasHostname("prada"))
assert.Nil(t, hosts.RemoveByHostname("prada"))
// remove if exists
assert.True(t, hosts.HasHostname("yadda"))
assert.Nil(t, hosts.RemoveByHostname("yadda"))
assert.False(t, hosts.HasHostname("yadda"))
// remove hostname and clean up the IP address if
// it was the only name/alias on the line
hosts = newHosts()
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.Nil(t, hosts.Add("168.1.1.1", "prada"))
assert.Nil(t, hosts.Add("1.2.3.4", "foo", "bar"))
assert.Nil(t, hosts.RemoveByHostname("yadda"))
assert.Len(t, hosts.Lines, 2)
assert.True(t, hosts.HasHostname("prada"))
assert.True(t, hosts.HasHostname("foo"))
assert.True(t, hosts.HasHostname("bar"))
assert.Equal(t, hosts.hosts.l["prada"], []int{0})
assert.Equal(t, hosts.hosts.l["foo"], []int{1})
assert.Equal(t, hosts.hosts.l["bar"], []int{1})
assert.Nil(t, hosts.RemoveByHostname("foo"))
assert.Len(t, hosts.Lines, 2)
assert.True(t, hosts.HasHostname("prada"))
assert.True(t, hosts.HasHostname("bar"))
assert.Nil(t, hosts.RemoveByHostname("bar"))
assert.Len(t, hosts.Lines, 1)
assert.True(t, hosts.HasHostname("prada"))
assert.Equal(t, hosts.hosts.l["prada"], []int{0})
}
func TestHosts_HasIp(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.Nil(t, hosts.Add("168.1.1.1", "yadda"))
// add should have removed yadda from 127.0.0.1
assert.False(t, hosts.HasIP("127.0.0.1"))
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 1)
assert.True(t, hosts.HasIP("168.1.1.1"))
}
func TestHosts_LineWithTrailingComment(t *testing.T) {
tests := []struct {
given string
addIp string
addHost string
expected string
}{
{
given: "127.0.0.1 prada #comment",
addIp: "127.0.0.1",
addHost: "yadda",
expected: "127.0.0.1 prada yadda #comment",
},
{
given: "127.0.0.1 prada # comment",
addIp: "127.0.0.1",
addHost: "yadda",
expected: "127.0.0.1 prada yadda # comment",
},
}
for _, test := range tests {
hosts := newHosts()
assert.Nil(t, hosts.AddRaw(test.given))
assert.Nil(t, hosts.Add(test.addIp, test.addHost))
assert.Equal(t, hosts.Lines[0].Raw, test.expected)
}
}
func TestHosts_LineWithComments(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.AddRaw("#This is the first comment",
"127.0.0.1 prada",
"#This is the second comment",
"127.0.0.2 tada #HostLine with trailing comment",
"#This is third comment"))
for _, hostLine := range hosts.Lines {
assert.Equal(t, hostLine.ToRaw(), hostLine.Raw)
}
}
func TestHosts_Clean(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.AddRaw("127.0.0.2 prada yadda #comment1", "127.0.0.2 tada abba #comment2"))
hosts.Clean()
assert.Equal(t, len(hosts.Lines), 1)
assert.Equal(t, hosts.Lines[0].Comment, "comment1 comment2")
assert.Equal(t, hosts.Lines[0].ToRaw(), "127.0.0.2 abba prada tada yadda #comment1 comment2")
}
func TestHosts_Add(t *testing.T) {
hosts := newHosts()
assert.Error(t, hosts.Add("badip", "hosts1"))
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10")) // valid use with variatic args
assert.Len(t, hosts.Lines, 1)
assert.Error(t, hosts.Add("127.0.0.2", "host11 host12 host13 host14 host15 host16 host17 host18 hosts19 hosts20")) // invalid use
assert.Len(t, hosts.Lines, 1)
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.Len(t, hosts.Lines, 1)
// add the same hosts twice (should be noop with nothing new)
assert.Nil(t, hosts.Add("127.0.0.3", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.Error(t, assert.AnError, hosts.Add("127.0.0.3", "invalid hostname"))
assert.Error(t, assert.AnError, hosts.Add("127.0.0.3", ".invalid*hostname"))
// don't add if the combo ip/host exists somewhere in the file
hosts = newHosts()
assert.Nil(t, hosts.AddRaw("127.0.0.1 tom.test", "127.0.0.1 tom.test example.test"))
assert.Nil(t, hosts.Add("127.0.0.1", "example.test"))
assert.Equal(t, hosts.Lines[0].Raw, "127.0.0.1 tom.test")
// call with no hosts
hosts = newHosts()
assert.Nil(t, hosts.AddRaw("127.0.0.1 yadda", "10.0.0.7 nada"))
hosts.Lines[1] = HostsLine{
IP: "not an ip",
Hosts: []string{"nada"},
}
assert.Error(t, hosts.Add("192.168.1.1", "nada"))
// reset and try adding the same hosts multiple times to two ips
hosts = newHosts()
// add 10 hosts
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.Len(t, hosts.Lines, 1)
assert.Len(t, hosts.hosts.l, 10)
assert.Len(t, hosts.ips.l, 1)
// add the same thing twice, should be no additions.
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.Len(t, hosts.Lines, 1)
assert.Len(t, hosts.hosts.l, 10)
assert.Len(t, hosts.ips.l, 1)
// add a new ip with 10 hosts, should remove first ip
assert.Nil(t, hosts.Add("127.0.0.3", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.False(t, hosts.HasIP("127.0.0.2"))
assert.False(t, hosts.HasIp("127.0.0.2"))
assert.Len(t, hosts.Lines, 1)
assert.Len(t, hosts.hosts.l, 10)
assert.Len(t, hosts.ips.l, 1)
// make sure adding a duplicate host removes it form the previous ip
expectedLines := []HostsLine{NewHostsLine("10.0.0.7 nada yadda brada")}
hosts = newHosts()
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.Nil(t, hosts.Add("10.0.0.7", "nada", "yadda"))
assert.Nil(t, hosts.Add("10.0.0.7", "brada", "yadda"))
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 3)
assert.Equal(t, expectedLines, hosts.Lines)
}
func TestHosts_AddRaw(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.AddRaw("127.0.0.1 yadda"))
assert.Len(t, hosts.Lines, 1)
assert.Nil(t, hosts.AddRaw("127.0.0.2 nada"))
assert.Len(t, hosts.Lines, 2)
assert.Nil(t, hosts.AddRaw("127.0.0.3 host1", "127.0.0.4 host2"))
assert.Len(t, hosts.Lines, 4)
assert.Error(t, hosts.AddRaw("badip host1")) // fail ip parse
assert.Error(t, hosts.AddRaw("127.0.0.1 host1%")) // fail host DNS validation
}
func TestHosts_HostsPerLine(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.Nil(t, hosts.Add("127.0.0.2", "host11", "host12", "host13", "host14", "host15", "host16", "host17", "host18", "host19", "hosts20"))
hosts.HostsPerLine(1) // split into 20 lines
assert.Len(t, hosts.Lines, 20)
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 20)
hosts.Clear()
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.Nil(t, hosts.Add("127.0.0.2", "host11", "host12", "host13", "host14", "host15", "host16", "host17", "host18", "host19", "hosts20"))
hosts.HostsPerLine(2)
assert.Len(t, hosts.Lines, 10)
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 20)
hosts.Clear()
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.Nil(t, hosts.Add("127.0.0.2", "host11", "host12", "host13", "host14", "host15", "host16", "host17", "host18", "host19", "hosts20"))
hosts.HostsPerLine(9) // windows
assert.Len(t, hosts.Lines, 3)
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 20)
hosts.Clear()
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
assert.Nil(t, hosts.Add("127.0.0.2", "host11", "host12", "host13", "host14", "host15", "host16", "host17", "host18", "host19", "hosts20"))
hosts.HostsPerLine(50) // all in one
assert.Len(t, hosts.Lines, 1)
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 20)
hosts.Clear()
assert.Nil(t, hosts.Add("127.0.0.2", "host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8", "host9", "hosts10"))
hosts.HostsPerLine(8)
assert.Len(t, hosts.Lines, 2)
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 10)
hosts.HostsPerLine(0) // noop
assert.Len(t, hosts.Lines, 2)
assert.Len(t, hosts.ips.l, 1)
assert.Len(t, hosts.hosts.l, 10)
// test if multiple calls to HostsPerLine doesn't create havoc and has identical output as calling it once
hosts = newHosts()
for i := 1; i <= 50; i++ {
assert.Nil(t, hosts.Add("127.0.0.1", fmt.Sprintf("a%d.ddev.site", i)))
hosts.HostsPerLine(8) // 8
}
hosts2 := newHosts()
for i := 1; i <= 50; i++ {
assert.Nil(t, hosts2.Add("127.0.0.1", fmt.Sprintf("a%d.ddev.site", i)))
}
hosts2.HostsPerLine(8) // 8
assert.Equal(t, hosts.String(), hosts2.String())
}
func BenchmarkHosts_Add(b *testing.B) {
for _, c := range []int{10000, 25000, 50000, 100000, 250000, 500000} {
b.Run(fmt.Sprintf("%d", c), func(b *testing.B) {
benchmarkHosts_Add(c, b)
// mem()
})
}
}
func BenchmarkHosts_Add10k(b *testing.B) {
benchmarkHosts_Add(10000, b)
}
func BenchmarkHosts_Add25k(b *testing.B) {
benchmarkHosts_Add(25000, b)
}
func BenchmarkHosts_Add50k(b *testing.B) {
benchmarkHosts_Add(50000, b)
}
func BenchmarkHosts_Add250k(b *testing.B) {
benchmarkHosts_Add(250000, b)
}
func benchmarkHosts_Add(c int, b *testing.B) {
fp := "hostsfile"
f, err := os.Create(fp)
assert.Nil(b, err)
defer func() {
assert.Nil(b, f.Close())
assert.Nil(b, os.Remove(fp))
}()
hosts, err := NewCustomHosts(fp)
assert.Nil(b, err)
for i := 0; i < c; i++ {
assert.Nil(b, hosts.Add(fake.IPv4(), randomString(63)))
}
}
func BenchmarkHosts_Flush50k(b *testing.B) {
benchmarkHosts_Flush(5, b)
}
func BenchmarkHosts_Flush100k(b *testing.B) {
benchmarkHosts_Flush(10, b)
}
func BenchmarkHosts_Flush250k(b *testing.B) {
benchmarkHosts_Flush(25, b)
}
func BenchmarkHosts_Flush500k(b *testing.B) {
benchmarkHosts_Flush(50, b)
}
// benchmarks flushing a hostsfile and confirms the hashmap lookup for ips/hosts is thread safe via mutex + locking
func benchmarkHosts_Flush(c int, b *testing.B) {
_, err := os.Create("hostsfile")
assert.Nil(b, err)
defer func() {
assert.Nil(b, os.Remove("hostsfile"))
}()
hosts, err := NewCustomHosts("hostsfile")
assert.Nil(b, err)
wg := sync.WaitGroup{}
wg.Add(c)
for i := 0; i < c; i++ {
go func() {
for i := 0; i < 10000; i++ {
assert.Nil(b, hosts.Add(fake.IPv4(), randomString(63)))
}
wg.Done()
}()
}
wg.Wait()
assert.Nil(b, hosts.Flush())
}
func TestHosts_Flush(t *testing.T) {
f, err := os.Create("hostsfile")
defer func() {
assert.Nil(t, f.Close())
assert.Nil(t, os.Remove("hostsfile"))
}()
assert.Nil(t, err)
hosts, err := NewCustomHosts("./hostsfile")
assert.Nil(t, err)
assert.Nil(t, hosts.Add("127.0.0.2", "host1"))
assert.Equal(t, 1, len(hosts.Lines))
assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw)
assert.Nil(t, hosts.Flush())
assert.Equal(t, 1, len(hosts.Lines))
assert.Equal(t, "127.0.0.2 host1", hosts.Lines[0].Raw)
// bad path can't write
hosts.Path = ""
assert.Error(t, hosts.Flush())
}
func TestHosts_Clear(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.True(t, hosts.HasIP("127.0.0.1"))
assert.Len(t, hosts.Lines, 1)
hosts.Clear()
assert.Len(t, hosts.Lines, 0)
assert.Nil(t, hosts.Add("127.0.0.1", "yadda"))
assert.True(t, hosts.HasIP("127.0.0.1"))
assert.Len(t, hosts.Lines, 1)
}
func TestHosts_RemoveDuplicateHosts(t *testing.T) {
h := newHosts()
assert.Nil(t, h.loadString(`127.0.0.1 test1 test1 test2 test2`+eol+`# comment`))
assert.Len(t, h.Lines, 2)
assert.Len(t, h.ips.l, 1)
assert.Len(t, h.hosts.l, 2)
h.RemoveDuplicateHosts()
assert.Len(t, h.Lines, 2)
assert.Len(t, h.ips.l, 1)
assert.Len(t, h.hosts.l, 2)
assert.Equal(t, `127.0.0.1 test1 test2`+eol+`# comment`+eol, h.String())
h = newHosts()
assert.Nil(t, h.loadString(`127.0.0.1 test1 test1 test2 test2`+eol+`127.0.0.2 test1 test1 test2 test2`+eol))
assert.Len(t, h.Lines, 2)
assert.Len(t, h.ips.l, 2)
assert.Len(t, h.hosts.l, 2)
assert.Len(t, h.hosts.l["test1"], 4)
assert.Len(t, h.hosts.l["test2"], 4)
h.RemoveDuplicateHosts()
assert.Len(t, h.Lines, 2)
assert.Len(t, h.ips.l, 2)
assert.Len(t, h.hosts.l, 2)
assert.Len(t, h.hosts.l["test1"], 2)
assert.Len(t, h.hosts.l["test2"], 2)
assert.Equal(t, "127.0.0.1 test1 test2"+eol+"127.0.0.2 test1 test2"+eol, h.String())
}
func TestHosts_CombineDuplicateIPs(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.loadString(`# comment`+eol+`127.0.0.1 test1 test1 test2 test2`+eol+`127.0.0.1 test1 test1 test2 test2`+eol))
hosts.CombineDuplicateIPs()
assert.Len(t, hosts.Lines, 2)
assert.Equal(t, "# comment"+eol+"127.0.0.1 test1 test1 test1 test1 test2 test2 test2 test2"+eol, hosts.String())
// deprecated
hosts = newHosts()
assert.Nil(t, hosts.loadString(`# comment`+eol+`127.0.0.1 test1 test1 test2 test2`+eol+`127.0.0.1 test1 test1 test2 test2`+eol))
hosts.RemoveDuplicateIps()
assert.Len(t, hosts.Lines, 2)
assert.Equal(t, "# comment"+eol+"127.0.0.1 test1 test1 test1 test1 test2 test2 test2 test2"+eol, hosts.String())
}
func TestHosts_SortIPs(t *testing.T) {
hosts := newHosts()
assert.Nil(t, hosts.loadString(`# comment `+eol+`127.0.0.3 host3`+eol+`127.0.0.2 host2`+eol+`127.0.0.1 host1`+eol))
hosts.SortIPs()
assert.Len(t, hosts.Lines, 4)
assert.Equal(t, strings.Join([]string{
"# comment ",
"127.0.0.1 host1",
"127.0.0.2 host2",
"127.0.0.3 host3",
"",
}, eol), hosts.String())
// deprecated
hosts = newHosts()
assert.Nil(t, hosts.loadString(`# comment `+eol+`127.0.0.3 host3`+eol+`127.0.0.2 host2`+eol+`127.0.0.1 host1`+eol))
hosts.SortByIp()
assert.Len(t, hosts.Lines, 4)
assert.Equal(t, strings.Join([]string{
"# comment ",
"127.0.0.1 host1",
"127.0.0.2 host2",
"127.0.0.3 host3",
"",
}, eol), hosts.String())
}