dnsconfig_unix_test.go 2.11 KB
Newer Older
1 2 3 4
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

5
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
6 7 8

package net

9
import (
10
	"os"
11 12 13
	"reflect"
	"testing"
)
14

15 16
var dnsReadConfigTests = []struct {
	name string
17
	want *dnsConfig
18 19 20
}{
	{
		name: "testdata/resolv.conf",
21 22 23 24 25 26 27 28
		want: &dnsConfig{
			servers:    []string{"8.8.8.8", "2001:4860:4860::8888", "fe80::1%lo0"},
			search:     []string{"localdomain"},
			ndots:      5,
			timeout:    10,
			attempts:   3,
			rotate:     true,
			unknownOpt: true, // the "options attempts 3" line
29 30 31 32
		},
	},
	{
		name: "testdata/domain-resolv.conf",
33
		want: &dnsConfig{
34 35 36 37 38 39 40 41 42
			servers:  []string{"8.8.8.8"},
			search:   []string{"localdomain"},
			ndots:    1,
			timeout:  5,
			attempts: 2,
		},
	},
	{
		name: "testdata/search-resolv.conf",
43
		want: &dnsConfig{
44 45 46 47 48 49 50 51 52
			servers:  []string{"8.8.8.8"},
			search:   []string{"test", "invalid"},
			ndots:    1,
			timeout:  5,
			attempts: 2,
		},
	},
	{
		name: "testdata/empty-resolv.conf",
53 54 55 56 57 58 59 60 61 62
		want: &dnsConfig{
			servers:  defaultNS,
			ndots:    1,
			timeout:  5,
			attempts: 2,
		},
	},
	{
		name: "testdata/openbsd-resolv.conf",
		want: &dnsConfig{
63 64 65
			ndots:    1,
			timeout:  5,
			attempts: 2,
66 67 68
			lookup:   []string{"file", "bind"},
			servers:  []string{"169.254.169.254", "10.240.0.1"},
			search:   []string{"c.symbolic-datum-552.internal."},
69 70 71
		},
	},
}
72

73 74
func TestDNSReadConfig(t *testing.T) {
	for _, tt := range dnsReadConfigTests {
75 76 77
		conf := dnsReadConfig(tt.name)
		if conf.err != nil {
			t.Fatal(conf.err)
78
		}
79 80
		if !reflect.DeepEqual(conf, tt.want) {
			t.Errorf("%s:\ngot: %+v\nwant: %+v", tt.name, conf, tt.want)
81
		}
82 83
	}
}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

func TestDNSReadMissingFile(t *testing.T) {
	conf := dnsReadConfig("a-nonexistent-file")
	if !os.IsNotExist(conf.err) {
		t.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf.err, os.ErrNotExist)
	}
	conf.err = nil
	want := &dnsConfig{
		servers:  defaultNS,
		ndots:    1,
		timeout:  5,
		attempts: 2,
	}
	if !reflect.DeepEqual(conf, want) {
		t.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf, want)
	}
}