github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/net/hosts_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package net 6 7 import ( 8 "reflect" 9 "strings" 10 "testing" 11 ) 12 13 type staticHostEntry struct { 14 in string 15 out []string 16 } 17 18 var lookupStaticHostTests = []struct { 19 name string 20 ents []staticHostEntry 21 }{ 22 { 23 "testdata/hosts", 24 []staticHostEntry{ 25 {"odin", []string{"127.0.0.2", "127.0.0.3", "::2"}}, 26 {"thor", []string{"127.1.1.1"}}, 27 {"ullr", []string{"127.1.1.2"}}, 28 {"ullrhost", []string{"127.1.1.2"}}, 29 {"localhost", []string{"fe80::1%lo0"}}, 30 }, 31 }, 32 { 33 "testdata/singleline-hosts", // see golang.org/issue/6646 34 []staticHostEntry{ 35 {"odin", []string{"127.0.0.2"}}, 36 }, 37 }, 38 { 39 "testdata/ipv4-hosts", // see golang.org/issue/8996 40 []staticHostEntry{ 41 {"localhost", []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}}, 42 {"localhost.localdomain", []string{"127.0.0.3"}}, 43 }, 44 }, 45 { 46 "testdata/ipv6-hosts", // see golang.org/issue/8996 47 []staticHostEntry{ 48 {"localhost", []string{"::1", "fe80::1", "fe80::2%lo0", "fe80::3%lo0"}}, 49 {"localhost.localdomain", []string{"fe80::3%lo0"}}, 50 }, 51 }, 52 { 53 "testdata/case-hosts", // see golang.org/issue/12806 54 []staticHostEntry{ 55 {"PreserveMe", []string{"127.0.0.1", "::1"}}, 56 {"PreserveMe.local", []string{"127.0.0.1", "::1"}}, 57 }, 58 }, 59 } 60 61 func TestLookupStaticHost(t *testing.T) { 62 defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath) 63 64 for _, tt := range lookupStaticHostTests { 65 testHookHostsPath = tt.name 66 for _, ent := range tt.ents { 67 ins := []string{ent.in, absDomainName([]byte(ent.in)), strings.ToLower(ent.in), strings.ToUpper(ent.in)} 68 for _, in := range ins { 69 addrs := lookupStaticHost(in) 70 if !reflect.DeepEqual(addrs, ent.out) { 71 t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", tt.name, in, addrs, ent.out) 72 } 73 } 74 } 75 } 76 } 77 78 var lookupStaticAddrTests = []struct { 79 name string 80 ents []staticHostEntry 81 }{ 82 { 83 "testdata/hosts", 84 []staticHostEntry{ 85 {"255.255.255.255", []string{"broadcasthost"}}, 86 {"127.0.0.2", []string{"odin"}}, 87 {"127.0.0.3", []string{"odin"}}, 88 {"::2", []string{"odin"}}, 89 {"127.1.1.1", []string{"thor"}}, 90 {"127.1.1.2", []string{"ullr", "ullrhost"}}, 91 {"fe80::1%lo0", []string{"localhost"}}, 92 }, 93 }, 94 { 95 "testdata/singleline-hosts", // see golang.org/issue/6646 96 []staticHostEntry{ 97 {"127.0.0.2", []string{"odin"}}, 98 }, 99 }, 100 { 101 "testdata/ipv4-hosts", // see golang.org/issue/8996 102 []staticHostEntry{ 103 {"127.0.0.1", []string{"localhost"}}, 104 {"127.0.0.2", []string{"localhost"}}, 105 {"127.0.0.3", []string{"localhost", "localhost.localdomain"}}, 106 }, 107 }, 108 { 109 "testdata/ipv6-hosts", // see golang.org/issue/8996 110 []staticHostEntry{ 111 {"::1", []string{"localhost"}}, 112 {"fe80::1", []string{"localhost"}}, 113 {"fe80::2%lo0", []string{"localhost"}}, 114 {"fe80::3%lo0", []string{"localhost", "localhost.localdomain"}}, 115 }, 116 }, 117 { 118 "testdata/case-hosts", // see golang.org/issue/12806 119 []staticHostEntry{ 120 {"127.0.0.1", []string{"PreserveMe", "PreserveMe.local"}}, 121 {"::1", []string{"PreserveMe", "PreserveMe.local"}}, 122 }, 123 }, 124 } 125 126 func TestLookupStaticAddr(t *testing.T) { 127 defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath) 128 129 for _, tt := range lookupStaticAddrTests { 130 testHookHostsPath = tt.name 131 for _, ent := range tt.ents { 132 hosts := lookupStaticAddr(ent.in) 133 for i := range ent.out { 134 ent.out[i] = absDomainName([]byte(ent.out[i])) 135 } 136 if !reflect.DeepEqual(hosts, ent.out) { 137 t.Errorf("%s, lookupStaticAddr(%s) = %v; want %v", tt.name, ent.in, hosts, ent.out) 138 } 139 } 140 } 141 }