Commit 85bfa33f authored by Mikio Hara's avatar Mikio Hara

net: fix case insensitivity lookup for local database such as /etc/hosts

The previous change for #12806 modified internal lookup tables and made
LookupAddr return forcibly lowercased host names by accident.

This change fixes the issue again without any behavioral change for
LookupAddr and adds missing test cases for lookupStaticHost and
lookupStaticAddr.

Updates #12806.
Fixes #13359.

Change-Id: Ifff4741cd79eb8b320b1b0f8c5e02b3a167c9fa8
Reviewed-on: https://go-review.googlesource.com/17217
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 98abf293
......@@ -27,11 +27,21 @@ func parseLiteralIP(addr string) string {
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
}
......@@ -60,11 +70,12 @@ func readHosts() {
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.
......
......@@ -6,6 +6,7 @@ package net
import (
"reflect"
"strings"
"testing"
)
......@@ -48,6 +49,13 @@ var lookupStaticHostTests = []struct {
{"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) {
......@@ -56,9 +64,12 @@ 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)
}
}
}
}
......@@ -74,7 +85,6 @@ var lookupStaticAddrTests = []struct {
{"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"}},
......@@ -104,6 +114,13 @@ var lookupStaticAddrTests = []struct {
{"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) {
......
127.0.0.1 PreserveMe PreserveMe.local
::1 PreserveMe PreserveMe.local
255.255.255.255 broadcasthost
127.0.0.2 odin
127.0.0.3 odin # inline comment
# case insensitivity
127.0.0.4 Bor
::2 odin
127.1.1.1 thor
# aliases
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment