Home | History | Annotate | Download | only in net
      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 	"testing"
     10 )
     11 
     12 type staticHostEntry struct {
     13 	in  string
     14 	out []string
     15 }
     16 
     17 var lookupStaticHostTests = []struct {
     18 	name string
     19 	ents []staticHostEntry
     20 }{
     21 	{
     22 		"testdata/hosts",
     23 		[]staticHostEntry{
     24 			{"odin", []string{"127.0.0.2", "127.0.0.3", "::2"}},
     25 			{"thor", []string{"127.1.1.1"}},
     26 			{"ullr", []string{"127.1.1.2"}},
     27 			{"ullrhost", []string{"127.1.1.2"}},
     28 			{"localhost", []string{"fe80::1%lo0"}},
     29 		},
     30 	},
     31 	{
     32 		"testdata/singleline-hosts", // see golang.org/issue/6646
     33 		[]staticHostEntry{
     34 			{"odin", []string{"127.0.0.2"}},
     35 		},
     36 	},
     37 	{
     38 		"testdata/ipv4-hosts", // see golang.org/issue/8996
     39 		[]staticHostEntry{
     40 			{"localhost", []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}},
     41 			{"localhost.localdomain", []string{"127.0.0.3"}},
     42 		},
     43 	},
     44 	{
     45 		"testdata/ipv6-hosts", // see golang.org/issue/8996
     46 		[]staticHostEntry{
     47 			{"localhost", []string{"::1", "fe80::1", "fe80::2%lo0", "fe80::3%lo0"}},
     48 			{"localhost.localdomain", []string{"fe80::3%lo0"}},
     49 		},
     50 	},
     51 }
     52 
     53 func TestLookupStaticHost(t *testing.T) {
     54 	defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
     55 
     56 	for _, tt := range lookupStaticHostTests {
     57 		testHookHostsPath = tt.name
     58 		for _, ent := range tt.ents {
     59 			addrs := lookupStaticHost(ent.in)
     60 			if !reflect.DeepEqual(addrs, ent.out) {
     61 				t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", tt.name, ent.in, addrs, ent.out)
     62 			}
     63 		}
     64 	}
     65 }
     66 
     67 var lookupStaticAddrTests = []struct {
     68 	name string
     69 	ents []staticHostEntry
     70 }{
     71 	{
     72 		"testdata/hosts",
     73 		[]staticHostEntry{
     74 			{"255.255.255.255", []string{"broadcasthost"}},
     75 			{"127.0.0.2", []string{"odin"}},
     76 			{"127.0.0.3", []string{"odin"}},
     77 			{"::2", []string{"odin"}},
     78 			{"127.1.1.1", []string{"thor"}},
     79 			{"127.1.1.2", []string{"ullr", "ullrhost"}},
     80 			{"fe80::1%lo0", []string{"localhost"}},
     81 		},
     82 	},
     83 	{
     84 		"testdata/singleline-hosts", // see golang.org/issue/6646
     85 		[]staticHostEntry{
     86 			{"127.0.0.2", []string{"odin"}},
     87 		},
     88 	},
     89 	{
     90 		"testdata/ipv4-hosts", // see golang.org/issue/8996
     91 		[]staticHostEntry{
     92 			{"127.0.0.1", []string{"localhost"}},
     93 			{"127.0.0.2", []string{"localhost"}},
     94 			{"127.0.0.3", []string{"localhost", "localhost.localdomain"}},
     95 		},
     96 	},
     97 	{
     98 		"testdata/ipv6-hosts", // see golang.org/issue/8996
     99 		[]staticHostEntry{
    100 			{"::1", []string{"localhost"}},
    101 			{"fe80::1", []string{"localhost"}},
    102 			{"fe80::2%lo0", []string{"localhost"}},
    103 			{"fe80::3%lo0", []string{"localhost", "localhost.localdomain"}},
    104 		},
    105 	},
    106 }
    107 
    108 func TestLookupStaticAddr(t *testing.T) {
    109 	defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
    110 
    111 	for _, tt := range lookupStaticAddrTests {
    112 		testHookHostsPath = tt.name
    113 		for _, ent := range tt.ents {
    114 			hosts := lookupStaticAddr(ent.in)
    115 			if !reflect.DeepEqual(hosts, ent.out) {
    116 				t.Errorf("%s, lookupStaticAddr(%s) = %v; want %v", tt.name, ent.in, hosts, ent.out)
    117 			}
    118 		}
    119 	}
    120 }
    121