github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/src/net/dnsconfig_unix_test.go (about)

     1  // Copyright 2013 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  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package net
     8  
     9  import (
    10  	"errors"
    11  	"os"
    12  	"reflect"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  var dnsReadConfigTests = []struct {
    18  	name string
    19  	want *dnsConfig
    20  }{
    21  	{
    22  		name: "testdata/resolv.conf",
    23  		want: &dnsConfig{
    24  			servers:    []string{"8.8.8.8:53", "[2001:4860:4860::8888]:53", "[fe80::1%lo0]:53"},
    25  			search:     []string{"localdomain."},
    26  			ndots:      5,
    27  			timeout:    10 * time.Second,
    28  			attempts:   3,
    29  			rotate:     true,
    30  			unknownOpt: true, // the "options attempts 3" line
    31  		},
    32  	},
    33  	{
    34  		name: "testdata/domain-resolv.conf",
    35  		want: &dnsConfig{
    36  			servers:  []string{"8.8.8.8:53"},
    37  			search:   []string{"localdomain."},
    38  			ndots:    1,
    39  			timeout:  5 * time.Second,
    40  			attempts: 2,
    41  		},
    42  	},
    43  	{
    44  		name: "testdata/search-resolv.conf",
    45  		want: &dnsConfig{
    46  			servers:  []string{"8.8.8.8:53"},
    47  			search:   []string{"test.", "invalid."},
    48  			ndots:    1,
    49  			timeout:  5 * time.Second,
    50  			attempts: 2,
    51  		},
    52  	},
    53  	{
    54  		name: "testdata/empty-resolv.conf",
    55  		want: &dnsConfig{
    56  			servers:  defaultNS,
    57  			ndots:    1,
    58  			timeout:  5 * time.Second,
    59  			attempts: 2,
    60  			search:   []string{"domain.local."},
    61  		},
    62  	},
    63  	{
    64  		name: "testdata/invalid-ndots-resolv.conf",
    65  		want: &dnsConfig{
    66  			servers:  defaultNS,
    67  			ndots:    0,
    68  			timeout:  5 * time.Second,
    69  			attempts: 2,
    70  			search:   []string{"domain.local."},
    71  		},
    72  	},
    73  	{
    74  		name: "testdata/large-ndots-resolv.conf",
    75  		want: &dnsConfig{
    76  			servers:  defaultNS,
    77  			ndots:    15,
    78  			timeout:  5 * time.Second,
    79  			attempts: 2,
    80  			search:   []string{"domain.local."},
    81  		},
    82  	},
    83  	{
    84  		name: "testdata/negative-ndots-resolv.conf",
    85  		want: &dnsConfig{
    86  			servers:  defaultNS,
    87  			ndots:    0,
    88  			timeout:  5 * time.Second,
    89  			attempts: 2,
    90  			search:   []string{"domain.local."},
    91  		},
    92  	},
    93  	{
    94  		name: "testdata/openbsd-resolv.conf",
    95  		want: &dnsConfig{
    96  			ndots:    1,
    97  			timeout:  5 * time.Second,
    98  			attempts: 2,
    99  			lookup:   []string{"file", "bind"},
   100  			servers:  []string{"169.254.169.254:53", "10.240.0.1:53"},
   101  			search:   []string{"c.symbolic-datum-552.internal."},
   102  		},
   103  	},
   104  }
   105  
   106  func TestDNSReadConfig(t *testing.T) {
   107  	origGetHostname := getHostname
   108  	defer func() { getHostname = origGetHostname }()
   109  	getHostname = func() (string, error) { return "host.domain.local", nil }
   110  
   111  	for _, tt := range dnsReadConfigTests {
   112  		conf := dnsReadConfig(tt.name)
   113  		if conf.err != nil {
   114  			t.Fatal(conf.err)
   115  		}
   116  		conf.mtime = time.Time{}
   117  		if !reflect.DeepEqual(conf, tt.want) {
   118  			t.Errorf("%s:\ngot: %+v\nwant: %+v", tt.name, conf, tt.want)
   119  		}
   120  	}
   121  }
   122  
   123  func TestDNSReadMissingFile(t *testing.T) {
   124  	origGetHostname := getHostname
   125  	defer func() { getHostname = origGetHostname }()
   126  	getHostname = func() (string, error) { return "host.domain.local", nil }
   127  
   128  	conf := dnsReadConfig("a-nonexistent-file")
   129  	if !os.IsNotExist(conf.err) {
   130  		t.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf.err, os.ErrNotExist)
   131  	}
   132  	conf.err = nil
   133  	want := &dnsConfig{
   134  		servers:  defaultNS,
   135  		ndots:    1,
   136  		timeout:  5 * time.Second,
   137  		attempts: 2,
   138  		search:   []string{"domain.local."},
   139  	}
   140  	if !reflect.DeepEqual(conf, want) {
   141  		t.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf, want)
   142  	}
   143  }
   144  
   145  var dnsDefaultSearchTests = []struct {
   146  	name string
   147  	err  error
   148  	want []string
   149  }{
   150  	{
   151  		name: "host.long.domain.local",
   152  		want: []string{"long.domain.local."},
   153  	},
   154  	{
   155  		name: "host.local",
   156  		want: []string{"local."},
   157  	},
   158  	{
   159  		name: "host",
   160  		want: nil,
   161  	},
   162  	{
   163  		name: "host.domain.local",
   164  		err:  errors.New("errored"),
   165  		want: nil,
   166  	},
   167  	{
   168  		// ensures we don't return []string{""}
   169  		// which causes duplicate lookups
   170  		name: "foo.",
   171  		want: nil,
   172  	},
   173  }
   174  
   175  func TestDNSDefaultSearch(t *testing.T) {
   176  	origGetHostname := getHostname
   177  	defer func() { getHostname = origGetHostname }()
   178  
   179  	for _, tt := range dnsDefaultSearchTests {
   180  		getHostname = func() (string, error) { return tt.name, tt.err }
   181  		got := dnsDefaultSearch()
   182  		if !reflect.DeepEqual(got, tt.want) {
   183  			t.Errorf("dnsDefaultSearch with hostname %q and error %+v = %q, wanted %q", tt.name, tt.err, got, tt.want)
   184  		}
   185  	}
   186  }