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 { ...@@ -27,11 +27,21 @@ func parseLiteralIP(addr string) string {
return ip.String() + "%" + zone return ip.String() + "%" + zone
} }
// Simple cache. // hosts contains known host entries.
var hosts struct { var hosts struct {
sync.Mutex 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 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 byAddr map[string][]string
expire time.Time expire time.Time
path string path string
} }
...@@ -60,11 +70,12 @@ func readHosts() { ...@@ -60,11 +70,12 @@ func readHosts() {
continue continue
} }
for i := 1; i < len(f); i++ { for i := 1; i < len(f); i++ {
name := f[i]
h := []byte(f[i]) h := []byte(f[i])
lowerASCIIBytes(h) lowerASCIIBytes(h)
lh := string(h) key := string(h)
hs[lh] = append(hs[lh], addr) hs[key] = append(hs[key], addr)
is[addr] = append(is[addr], lh) is[addr] = append(is[addr], name)
} }
} }
// Update the data cache. // Update the data cache.
......
...@@ -6,6 +6,7 @@ package net ...@@ -6,6 +6,7 @@ package net
import ( import (
"reflect" "reflect"
"strings"
"testing" "testing"
) )
...@@ -48,6 +49,13 @@ var lookupStaticHostTests = []struct { ...@@ -48,6 +49,13 @@ var lookupStaticHostTests = []struct {
{"localhost.localdomain", []string{"fe80::3%lo0"}}, {"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) { func TestLookupStaticHost(t *testing.T) {
...@@ -56,9 +64,12 @@ func TestLookupStaticHost(t *testing.T) { ...@@ -56,9 +64,12 @@ func TestLookupStaticHost(t *testing.T) {
for _, tt := range lookupStaticHostTests { for _, tt := range lookupStaticHostTests {
testHookHostsPath = tt.name testHookHostsPath = tt.name
for _, ent := range tt.ents { for _, ent := range tt.ents {
addrs := lookupStaticHost(ent.in) ins := []string{ent.in, strings.ToLower(ent.in), strings.ToUpper(ent.in)}
if !reflect.DeepEqual(addrs, ent.out) { for _, in := range ins {
t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", tt.name, ent.in, addrs, ent.out) 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 { ...@@ -74,7 +85,6 @@ var lookupStaticAddrTests = []struct {
{"255.255.255.255", []string{"broadcasthost"}}, {"255.255.255.255", []string{"broadcasthost"}},
{"127.0.0.2", []string{"odin"}}, {"127.0.0.2", []string{"odin"}},
{"127.0.0.3", []string{"odin"}}, {"127.0.0.3", []string{"odin"}},
{"127.0.0.4", []string{"bor"}},
{"::2", []string{"odin"}}, {"::2", []string{"odin"}},
{"127.1.1.1", []string{"thor"}}, {"127.1.1.1", []string{"thor"}},
{"127.1.1.2", []string{"ullr", "ullrhost"}}, {"127.1.1.2", []string{"ullr", "ullrhost"}},
...@@ -104,6 +114,13 @@ var lookupStaticAddrTests = []struct { ...@@ -104,6 +114,13 @@ var lookupStaticAddrTests = []struct {
{"fe80::3%lo0", []string{"localhost", "localhost.localdomain"}}, {"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) { func TestLookupStaticAddr(t *testing.T) {
......
127.0.0.1 PreserveMe PreserveMe.local
::1 PreserveMe PreserveMe.local
255.255.255.255 broadcasthost 255.255.255.255 broadcasthost
127.0.0.2 odin 127.0.0.2 odin
127.0.0.3 odin # inline comment 127.0.0.3 odin # inline comment
# case insensitivity
127.0.0.4 Bor
::2 odin ::2 odin
127.1.1.1 thor 127.1.1.1 thor
# aliases # 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