@@ -3,8 +3,6 @@ package hosts
33import (
44 "fmt"
55 "net"
6- "runtime"
7- "strings"
86 "testing"
97)
108
@@ -27,6 +25,7 @@ const ipv4Fail = `
2725const domain = "localhost"
2826const ip = "127.0.0.1"
2927const ipv6 = false
28+ const wildcard = false
3029
3130func Diff (expected , actual string ) string {
3231 return fmt .Sprintf (`
@@ -47,6 +46,38 @@ func (h *hostlist) Contains(b *hostname) bool {
4746 return false
4847}
4948
49+ func TestEquality (t * testing.T ) {
50+ var host1 * hostname
51+ var host2 * hostname
52+
53+ host1 = newHostname ("hello" , net .ParseIP ("255.255.255.255" ), false , false );
54+ host2 = newHostname ("hello" , net .ParseIP ("255.255.255.255" ), false , false );
55+ if ! host1 .Equal (host2 ) {
56+ t .Error ("Hosts are expected equal, got: " , host1 , host2 );
57+ }
58+
59+ host2 = newHostname ("hello2" , net .ParseIP ("255.255.255.255" ), false , false );
60+ if host1 .Equal (host2 ) {
61+ t .Error ("Hosts are expected different, got: " , host1 , host2 );
62+ }
63+
64+ host2 = newHostname ("hello1" , net .ParseIP ("255.255.255.254" ), false , false );
65+ if host1 .Equal (host2 ) {
66+ t .Error ("Hosts are expected different, got: " , host1 , host2 );
67+ }
68+
69+ host2 = newHostname ("hello1" , net .ParseIP ("255.255.255.255" ), true , false );
70+ if host1 .Equal (host2 ) {
71+ t .Error ("Hosts are expected different, got: " , host1 , host2 );
72+ }
73+
74+ host2 = newHostname ("hello1" , net .ParseIP ("255.255.255.255" ), false , true );
75+ if host1 .Equal (host2 ) {
76+ t .Error ("Hosts are expected different, got: " , host1 , host2 );
77+ }
78+
79+ }
80+
5081func TestParseLine (t * testing.T ) {
5182 var hosts hostlist
5283
@@ -73,39 +104,100 @@ func TestParseLine(t *testing.T) {
73104 t .Error ("Expected to find zero hostnames when line is commented out" )
74105 }
75106
107+ var err error ;
108+ err = hosts .add (newHostname ("aaa" , net .ParseIP ("192.168.0.1" ), false , false ));
109+ if err != nil {
110+ t .Error ("Did not expect error on first hostname" );
111+ }
112+ err = hosts .add (newHostname ("aaa" , net .ParseIP ("192.168.0.1" ), false , false ));
113+ if err == nil {
114+ t .Error ("Expected error on duplicate host" );
115+ }
116+
76117 // Not Commented stuff
77- hosts = parseLine ("255.255.255.255 broadcasthost test.domain.com domain.com" )
78- if ! hosts .Contains (newHostname ("broadcasthost" , net .ParseIP ("255.255.255.255" ) , false )) ||
79- ! hosts .Contains (newHostname ("test.domain.com" , net .ParseIP ("255.255.255.255" ) , false )) ||
80- ! hosts .Contains (newHostname ("domain.com" , net .ParseIP ("255.255.255.255" ) , false )) ||
118+ hosts = parseLine ("192.168.0.1 broadcasthost test.domain.com domain.com" )
119+ if ! hosts .Contains (newHostname ("broadcasthost" , net .ParseIP ("192.168.0.1" ), false , false )) ||
120+ ! hosts .Contains (newHostname ("test.domain.com" , net .ParseIP ("192.168.0.1" ), false , false )) ||
121+ ! hosts .Contains (newHostname ("domain.com" , net .ParseIP ("192.168.0.1" ), false , false )) ||
81122 len (hosts ) != 3 {
82123 t .Error ("Expected to find broadcasthost, domain.com, and test.domain.com" )
83124 }
84125
126+ // Wildcard stuff
127+ hosts = parseLine ("192.168.0.1 *.domain.com mail.domain.com serenity" )
128+ if ! hosts .Contains (newHostname ("domain.com" , net .ParseIP ("192.168.0.1" ), false , true )) ||
129+ ! hosts .Contains (newHostname ("mail.domain.com" , net .ParseIP ("192.168.0.1" ), false , false )) ||
130+ ! hosts .Contains (newHostname ("serenity" , net .ParseIP ("192.168.0.1" ), false , false )) ||
131+ len (hosts ) != 3 {
132+ t .Error ("Expected to find *.domain.com, mail.domain.com and serenity." )
133+ }
134+
135+ var ip net.IP ;
136+
137+ ip = hosts .FindHost ("api.domain.com" );
138+ if ! net .ParseIP ("192.168.0.1" ).Equal (ip ) {
139+ t .Error ("Can't match wildcard host api.domain.com" );
140+ }
141+
142+ ip = hosts .FindHost ("google.com" )
143+ if ip != nil {
144+ t .Error ("We shouldn't resolve google.com" );
145+ }
146+
147+ hosts = * newHostlistString (`192.168.0.1 *.domain.com mail.domain.com serenity
148+ 192.168.0.2 api.domain.com` );
149+
150+ if (! net .ParseIP ("192.168.0.2" ).Equal (hosts .FindHost ("api.domain.com" ))) {
151+ t .Error ("Failed matching api.domain.com explicitly" );
152+ }
153+ if (! net .ParseIP ("192.168.0.1" ).Equal (hosts .FindHost ("mail.domain.com" ))) {
154+ t .Error ("Failed matching api.domain.com explicitly" );
155+ }
156+ if (! net .ParseIP ("192.168.0.1" ).Equal (hosts .FindHost ("wildcard.domain.com" ))) {
157+ t .Error ("Failed matching wildcard.domain.com explicitly" );
158+ }
159+ if (net .ParseIP ("192.168.0.1" ).Equal (hosts .FindHost ("sub.wildcard.domain.com" ))) {
160+ t .Error ("Failed not matching sub.wildcard.domain.com explicitly" );
161+ }
162+
163+ // IPv6 (not link-local)
164+ hosts = parseLine ("2a02:7a8:1:250::80:1 rtvslo.si img.rtvslo.si" )
165+ if ! hosts .Contains (newHostname ("img.rtvslo.si" , net .ParseIP ("2a02:7a8:1:250::80:1" ), true , false )) ||
166+ len (hosts ) != 2 {
167+ t .Error ("Expected to find rtvslo.si ipv6, two hosts" )
168+ }
169+
170+ /* the following all fails since the addressses are link-local */
171+
172+ /*
85173 // Ipv6 stuff
86- hosts = hostess . parseLine ("::1 localhost" )
87- if ! hosts .Contains (newHostname ("localhost" , net .ParseIP ("::1" ), true )) ||
174+ hosts = parseLine("::1 localhost")
175+ if !hosts.Contains(newHostname("localhost", net.ParseIP("::1"), true, false )) ||
88176 len(hosts) != 1 {
89- t .Error ("Expected to find localhost ipv6 (enabled) " )
177+ t.Error("Expected to find localhost ipv6")
90178 }
91179
92- hosts = hostess . parseLine ("ff02::1 ip6-allnodes" )
93- if ! hosts .Contains (newHostname ("ip6-allnodes" , net .ParseIP ("ff02::1" ), true )) ||
180+ hosts = parseLine("ff02::1 ip6-allnodes")
181+ if !hosts.Contains(newHostname("ip6-allnodes", net.ParseIP("ff02::1"), true, false )) ||
94182 len(hosts) != 1 {
95- t .Error ("Expected to find ip6-allnodes ipv6 (enabled) " )
183+ t.Error("Expected to find ip6-allnodes ipv6")
96184 }
185+ */
97186}
98187
99188func TestHostname (t * testing.T ) {
100- h := newHostname (domain , net .ParseIP (ip ), ipv6 )
189+ h := newHostname (domain , net .ParseIP (ip ), ipv6 , wildcard )
101190
102191 if h .domain != domain {
103192 t .Errorf ("Domain should be %s" , domain )
104193 }
105194 if ! h .ip .Equal (net .ParseIP (ip )) {
106195 t .Errorf ("IP should be %s" , ip )
107196 }
108- if h .ipv6 != enabled {
109- t .Errorf ("Enabled should be %t" , enabled )
197+ if h .ipv6 != ipv6 {
198+ t .Errorf ("IPv6 should be %t" , ipv6 )
199+ }
200+ if h .wildcard != wildcard {
201+ t .Errorf ("Wildcard should be %t" , wildcard )
110202 }
111203}
0 commit comments