github.com/robhaswell/grandperspective-scan@v0.1.0/test/go-go1.7.1/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/openbsd-resolv.conf",
    65  		want: &dnsConfig{
    66  			ndots:    1,
    67  			timeout:  5 * time.Second,
    68  			attempts: 2,
    69  			lookup:   []string{"file", "bind"},
    70  			servers:  []string{"169.254.169.254:53", "10.240.0.1:53"},
    71  			search:   []string{"c.symbolic-datum-552.internal."},
    72  		},
    73  	},
    74  }
    75  
    76  func TestDNSReadConfig(t *testing.T) {
    77  	origGetHostname := getHostname
    78  	defer func() { getHostname = origGetHostname }()
    79  	getHostname = func() (string, error) { return "host.domain.local", nil }
    80  
    81  	for _, tt := range dnsReadConfigTests {
    82  		conf := dnsReadConfig(tt.name)
    83  		if conf.err != nil {
    84  			t.Fatal(conf.err)
    85  		}
    86  		conf.mtime = time.Time{}
    87  		if !reflect.DeepEqual(conf, tt.want) {
    88  			t.Errorf("%s:\ngot: %+v\nwant: %+v", tt.name, conf, tt.want)
    89  		}
    90  	}
    91  }
    92  
    93  func TestDNSReadMissingFile(t *testing.T) {
    94  	origGetHostname := getHostname
    95  	defer func() { getHostname = origGetHostname }()
    96  	getHostname = func() (string, error) { return "host.domain.local", nil }
    97  
    98  	conf := dnsReadConfig("a-nonexistent-file")
    99  	if !os.IsNotExist(conf.err) {
   100  		t.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf.err, os.ErrNotExist)
   101  	}
   102  	conf.err = nil
   103  	want := &dnsConfig{
   104  		servers:  defaultNS,
   105  		ndots:    1,
   106  		timeout:  5 * time.Second,
   107  		attempts: 2,
   108  		search:   []string{"domain.local."},
   109  	}
   110  	if !reflect.DeepEqual(conf, want) {
   111  		t.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf, want)
   112  	}
   113  }
   114  
   115  var dnsDefaultSearchTests = []struct {
   116  	name string
   117  	err  error
   118  	want []string
   119  }{
   120  	{
   121  		name: "host.long.domain.local",
   122  		want: []string{"long.domain.local."},
   123  	},
   124  	{
   125  		name: "host.local",
   126  		want: []string{"local."},
   127  	},
   128  	{
   129  		name: "host",
   130  		want: nil,
   131  	},
   132  	{
   133  		name: "host.domain.local",
   134  		err:  errors.New("errored"),
   135  		want: nil,
   136  	},
   137  	{
   138  		// ensures we don't return []string{""}
   139  		// which causes duplicate lookups
   140  		name: "foo.",
   141  		want: nil,
   142  	},
   143  }
   144  
   145  func TestDNSDefaultSearch(t *testing.T) {
   146  	origGetHostname := getHostname
   147  	defer func() { getHostname = origGetHostname }()
   148  
   149  	for _, tt := range dnsDefaultSearchTests {
   150  		getHostname = func() (string, error) { return tt.name, tt.err }
   151  		got := dnsDefaultSearch()
   152  		if !reflect.DeepEqual(got, tt.want) {
   153  			t.Errorf("dnsDefaultSearch with hostname %q and error %+v = %q, wanted %q", tt.name, tt.err, got, tt.want)
   154  		}
   155  	}
   156  }