blob: c8eed618904c98028debdd9feb7401b24f2244be [file] [log] [blame]
Anfernee Yongkun Guib3f38b42013-12-18 08:26:36 -08001// 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
Mikio Haraace8bc32014-04-30 23:26:07 +09005// +build darwin dragonfly freebsd linux netbsd openbsd solaris
Anfernee Yongkun Guib3f38b42013-12-18 08:26:36 -08006
7package net
8
Mikio Haraced0ba52014-09-04 10:00:30 +09009import (
Alex A Skinnerf3901352015-04-25 20:50:21 -040010 "os"
Mikio Haraced0ba52014-09-04 10:00:30 +090011 "reflect"
12 "testing"
13)
14
15var dnsReadConfigTests = []struct {
16 name string
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070017 want *dnsConfig
Mikio Haraced0ba52014-09-04 10:00:30 +090018}{
19 {
20 name: "testdata/resolv.conf",
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070021 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
Mikio Haraced0ba52014-09-04 10:00:30 +090029 },
30 },
31 {
32 name: "testdata/domain-resolv.conf",
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070033 want: &dnsConfig{
Mikio Haraced0ba52014-09-04 10:00:30 +090034 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",
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070043 want: &dnsConfig{
Mikio Haraced0ba52014-09-04 10:00:30 +090044 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",
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070053 want: &dnsConfig{
Alex A Skinnerf3901352015-04-25 20:50:21 -040054 servers: defaultNS,
Mikio Haraced0ba52014-09-04 10:00:30 +090055 ndots: 1,
56 timeout: 5,
57 attempts: 2,
58 },
59 },
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070060 {
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 },
Mikio Haraced0ba52014-09-04 10:00:30 +090071}
Anfernee Yongkun Guib3f38b42013-12-18 08:26:36 -080072
Mikio Haraefd1d052013-12-19 13:02:06 +090073func TestDNSReadConfig(t *testing.T) {
Mikio Haraced0ba52014-09-04 10:00:30 +090074 for _, tt := range dnsReadConfigTests {
Alex A Skinnerf3901352015-04-25 20:50:21 -040075 conf := dnsReadConfig(tt.name)
76 if conf.err != nil {
77 t.Fatal(conf.err)
Mikio Haraced0ba52014-09-04 10:00:30 +090078 }
Brad Fitzpatrick4a0ba7a2015-04-16 14:33:25 -070079 if !reflect.DeepEqual(conf, tt.want) {
Mikio Haraf77e10f2015-05-01 12:38:42 +090080 t.Errorf("%s:\ngot: %+v\nwant: %+v", tt.name, conf, tt.want)
Mikio Haraced0ba52014-09-04 10:00:30 +090081 }
Anfernee Yongkun Guib3f38b42013-12-18 08:26:36 -080082 }
83}
Alex A Skinnerf3901352015-04-25 20:50:21 -040084
85func TestDNSReadMissingFile(t *testing.T) {
86 conf := dnsReadConfig("a-nonexistent-file")
87 if !os.IsNotExist(conf.err) {
Mikio Haraf77e10f2015-05-01 12:38:42 +090088 t.Errorf("missing resolv.conf:\ngot: %v\nwant: %v", conf.err, os.ErrNotExist)
Alex A Skinnerf3901352015-04-25 20:50:21 -040089 }
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) {
Mikio Haraf77e10f2015-05-01 12:38:42 +090098 t.Errorf("missing resolv.conf:\ngot: %+v\nwant: %+v", conf, want)
Alex A Skinnerf3901352015-04-25 20:50:21 -040099 }
100}