return ip.String() + "%" + zone
}
-// Simple cache.
+// hosts contains known host entries.
var hosts struct {
sync.Mutex
+
+ // Key for the list of literal IP addresses must be a host
+ // name. It would be part of DNS labels, a FQDN or an absolute
+ // FQDN.
+ // For now the key is converted to lower case for convenience.
byName map[string][]string
+
+ // Key for the list of host names must be a literal IP address
+ // including IPv6 address with zone identifier.
+ // We don't support old-classful IP address notation.
byAddr map[string][]string
+
expire time.Time
path string
}
continue
}
for i := 1; i < len(f); i++ {
+ name := f[i]
h := []byte(f[i])
lowerASCIIBytes(h)
- lh := string(h)
- hs[lh] = append(hs[lh], addr)
- is[addr] = append(is[addr], lh)
+ key := string(h)
+ hs[key] = append(hs[key], addr)
+ is[addr] = append(is[addr], name)
}
}
// Update the data cache.
import (
"reflect"
+ "strings"
"testing"
)
{"localhost.localdomain", []string{"fe80::3%lo0"}},
},
},
+ {
+ "testdata/case-hosts", // see golang.org/issue/12806
+ []staticHostEntry{
+ {"PreserveMe", []string{"127.0.0.1", "::1"}},
+ {"PreserveMe.local", []string{"127.0.0.1", "::1"}},
+ },
+ },
}
func TestLookupStaticHost(t *testing.T) {
for _, tt := range lookupStaticHostTests {
testHookHostsPath = tt.name
for _, ent := range tt.ents {
- addrs := lookupStaticHost(ent.in)
- if !reflect.DeepEqual(addrs, ent.out) {
- t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", tt.name, ent.in, addrs, ent.out)
+ ins := []string{ent.in, strings.ToLower(ent.in), strings.ToUpper(ent.in)}
+ for _, in := range ins {
+ addrs := lookupStaticHost(in)
+ if !reflect.DeepEqual(addrs, ent.out) {
+ t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", tt.name, in, addrs, ent.out)
+ }
}
}
}
{"255.255.255.255", []string{"broadcasthost"}},
{"127.0.0.2", []string{"odin"}},
{"127.0.0.3", []string{"odin"}},
- {"127.0.0.4", []string{"bor"}},
{"::2", []string{"odin"}},
{"127.1.1.1", []string{"thor"}},
{"127.1.1.2", []string{"ullr", "ullrhost"}},
{"fe80::3%lo0", []string{"localhost", "localhost.localdomain"}},
},
},
+ {
+ "testdata/case-hosts", // see golang.org/issue/12806
+ []staticHostEntry{
+ {"127.0.0.1", []string{"PreserveMe", "PreserveMe.local"}},
+ {"::1", []string{"PreserveMe", "PreserveMe.local"}},
+ },
+ },
}
func TestLookupStaticAddr(t *testing.T) {