From: Mikio Hara Date: Fri, 27 Nov 2015 08:17:18 +0000 (+0900) Subject: net: fix case insensitivity lookup for local database such as /etc/hosts X-Git-Tag: go1.6beta1~276 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=85bfa33fdc02417cadf46b3f185784e1cf7886b3;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/net/hosts.go b/src/net/hosts.go index aba2cea787..8cf73fd5db 100644 --- a/src/net/hosts.go +++ b/src/net/hosts.go @@ -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. diff --git a/src/net/hosts_test.go b/src/net/hosts_test.go index 99569cd016..a3173ff9ef 100644 --- a/src/net/hosts_test.go +++ b/src/net/hosts_test.go @@ -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) { diff --git a/src/net/testdata/case-hosts b/src/net/testdata/case-hosts new file mode 100644 index 0000000000..1f30df1179 --- /dev/null +++ b/src/net/testdata/case-hosts @@ -0,0 +1,2 @@ +127.0.0.1 PreserveMe PreserveMe.local +::1 PreserveMe PreserveMe.local diff --git a/src/net/testdata/hosts b/src/net/testdata/hosts index 4b8abb431c..b601763898 100644 --- a/src/net/testdata/hosts +++ b/src/net/testdata/hosts @@ -1,8 +1,6 @@ 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