github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/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  	"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  			{"127.0.0.4", []string{"bor"}},
    78  			{"::2", []string{"odin"}},
    79  			{"127.1.1.1", []string{"thor"}},
    80  			{"127.1.1.2", []string{"ullr", "ullrhost"}},
    81  			{"fe80::1%lo0", []string{"localhost"}},
    82  		},
    83  	},
    84  	{
    85  		"testdata/singleline-hosts", // see golang.org/issue/6646
    86  		[]staticHostEntry{
    87  			{"127.0.0.2", []string{"odin"}},
    88  		},
    89  	},
    90  	{
    91  		"testdata/ipv4-hosts", // see golang.org/issue/8996
    92  		[]staticHostEntry{
    93  			{"127.0.0.1", []string{"localhost"}},
    94  			{"127.0.0.2", []string{"localhost"}},
    95  			{"127.0.0.3", []string{"localhost", "localhost.localdomain"}},
    96  		},
    97  	},
    98  	{
    99  		"testdata/ipv6-hosts", // see golang.org/issue/8996
   100  		[]staticHostEntry{
   101  			{"::1", []string{"localhost"}},
   102  			{"fe80::1", []string{"localhost"}},
   103  			{"fe80::2%lo0", []string{"localhost"}},
   104  			{"fe80::3%lo0", []string{"localhost", "localhost.localdomain"}},
   105  		},
   106  	},
   107  }
   108  
   109  func TestLookupStaticAddr(t *testing.T) {
   110  	defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
   111  
   112  	for _, tt := range lookupStaticAddrTests {
   113  		testHookHostsPath = tt.name
   114  		for _, ent := range tt.ents {
   115  			hosts := lookupStaticAddr(ent.in)
   116  			if !reflect.DeepEqual(hosts, ent.out) {
   117  				t.Errorf("%s, lookupStaticAddr(%s) = %v; want %v", tt.name, ent.in, hosts, ent.out)
   118  			}
   119  		}
   120  	}
   121  }