]> Cypherpunks repositories - gostls13.git/commitdiff
net: fix case insensitivity lookup for local database such as /etc/hosts
authorMikio Hara <mikioh.mikioh@gmail.com>
Fri, 27 Nov 2015 08:17:18 +0000 (17:17 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Fri, 27 Nov 2015 22:52:14 +0000 (22:52 +0000)
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: Brad Fitzpatrick <bradfitz@golang.org>
src/net/hosts.go
src/net/hosts_test.go
src/net/testdata/case-hosts [new file with mode: 0644]
src/net/testdata/hosts

index aba2cea787454a6f66b596da054a094666e19621..8cf73fd5db287486addb535d686e888e616ae179 100644 (file)
@@ -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.
index 99569cd01629a480011600c4126652852c87d667..a3173ff9ef30ab031e21b8893de0d6961ed0b24d 100644 (file)
@@ -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 (file)
index 0000000..1f30df1
--- /dev/null
@@ -0,0 +1,2 @@
+127.0.0.1      PreserveMe      PreserveMe.local
+::1            PreserveMe      PreserveMe.local
index 4b8abb431cbcd04117eb231fe82b501cc6c9bf10..b601763898b7e27ded621cc207effa4a403cecc0 100644 (file)
@@ -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