Home | History | Annotate | Download | only in net
      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 	"os"
     11 	"reflect"
     12 	"testing"
     13 )
     14 
     15 var dnsReadConfigTests = []struct {
     16 	name string
     17 	want *dnsConfig
     18 }{
     19 	{
     20 		name: "testdata/resolv.conf",
     21 		want: &dnsConfig{
     22 			servers:    []string{"8.8.8.8", "2001:4860:4860::8888", "fe80::1%lo0"},
     23 			search:     []string{"localdomain"},
     24 			ndots:      5,
     25 			timeout:    10,
     26 			attempts:   3,
     27 			rotate:     true,
     28 			unknownOpt: true, // the "options attempts 3" line
     29 		},
     30 	},
     31 	{
     32 		name: "testdata/domain-resolv.conf",
     33 		want: &dnsConfig{
     34 			servers:  []string{"8.8.8.8"},
     35 			search:   []string{"localdomain"},
     36 			ndots:    1,
     37 			timeout:  5,
     38 			attempts: 2,
     39 		},
     40 	},
     41 	{
     42 		name: "testdata/search-resolv.conf",
     43 		want: &dnsConfig{
     44 			servers:  []string{"8.8.8.8"},
     45 			search:   []string{"test", "invalid"},
     46 			ndots:    1,
     47 			timeout:  5,
     48 			attempts: 2,
     49 		},
     50 	},
     51 	{
     52 		name: "testdata/empty-resolv.conf",
     53 		want: &dnsConfig{
     54 			servers:  defaultNS,
     55 			ndots:    1,
     56 			timeout:  5,
     57 			attempts: 2,
     58 		},
     59 	},
     60 	{
     61 		name: "testdata/openbsd-resolv.conf",
     62 		want: &dnsConfig{
     63 			ndots:    1,
     64 			timeout:  5,
     65 			attempts: 2,
     66 			lookup:   []string{"file", "bind"},
     67 			servers:  []string{"169.254.169.254", "10.240.0.1"},
     68 			search:   []string{"c.symbolic-datum-552.internal."},
     69 		},
     70 	},
     71 }
     72 
     73 func TestDNSReadConfig(t *testing.T) {
     74 	for _, tt := range dnsReadConfigTests {
     75 		conf := dnsReadConfig(tt.name)
     76 		if conf.err != nil {
     77 			t.Fatal(conf.err)
     78 		}
     79 		if !reflect.DeepEqual(conf, tt.want) {
     80 			t.Errorf("%s:\ngot: %+v\nwant: %+v", tt.name, conf, tt.want)
     81 		}
     82 	}
     83 }
     84 
     85 func TestDNSReadMissingFile(t *testing.T) {
     86 	conf := dnsReadConfig("a-nonexistent-file")
     87 	if !os.IsNotExist(conf.err) {
     88 		t.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf.err, os.ErrNotExist)
     89 	}
     90 	conf.err = nil
     91 	want := &dnsConfig{
     92 		servers:  defaultNS,
     93 		ndots:    1,
     94 		timeout:  5,
     95 		attempts: 2,
     96 	}
     97 	if !reflect.DeepEqual(conf, want) {
     98 		t.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf, want)
     99 	}
    100 }
    101