aboutsummaryrefslogtreecommitdiff
path: root/plugin/hosts/setup_test.go
blob: 58351cc523f4240d4a436eb33c7156cd524cf90c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package hosts

import (
	"testing"

	"github.com/coredns/coredns/plugin/pkg/fall"

	"github.com/mholt/caddy"
)

func TestHostsParse(t *testing.T) {
	tests := []struct {
		inputFileRules      string
		shouldErr           bool
		expectedPath        string
		expectedOrigins     []string
		expectedFallthrough *fall.F
	}{
		{
			`hosts
`,
			false, "/etc/hosts", nil, nil,
		},
		{
			`hosts /tmp`,
			false, "/tmp", nil, nil,
		},
		{
			`hosts /etc/hosts miek.nl.`,
			false, "/etc/hosts", []string{"miek.nl."}, nil,
		},
		{
			`hosts /etc/hosts miek.nl. pun.gent.`,
			false, "/etc/hosts", []string{"miek.nl.", "pun.gent."}, nil,
		},
		{
			`hosts {
				fallthrough
			}`,
			false, "/etc/hosts", nil, fall.Zero(),
		},
		{
			`hosts /tmp {
				fallthrough
			}`,
			false, "/tmp", nil, fall.Zero(),
		},
		{
			`hosts /etc/hosts miek.nl. {
				fallthrough
			}`,
			false, "/etc/hosts", []string{"miek.nl."}, fall.Zero(),
		},
		{
			`hosts /etc/hosts miek.nl 10.0.0.9/8 {
				fallthrough
			}`,
			false, "/etc/hosts", []string{"miek.nl.", "10.in-addr.arpa."}, fall.Zero(),
		},
	}

	for i, test := range tests {
		c := caddy.NewTestController("dns", test.inputFileRules)
		h, err := hostsParse(c)

		if err == nil && test.shouldErr {
			t.Fatalf("Test %d expected errors, but got no error", i)
		} else if err != nil && !test.shouldErr {
			t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
		} else if !test.shouldErr {
			if h.path != test.expectedPath {
				t.Fatalf("Test %d expected %v, got %v", i, test.expectedPath, h.path)
			}
		} else {
			if !h.Fall.Equal(test.expectedFallthrough) {
				t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fall)
			}
			if len(h.Origins) != len(test.expectedOrigins) {
				t.Fatalf("Test %d expected %v, got %v", i, test.expectedOrigins, h.Origins)
			}
			for j, name := range test.expectedOrigins {
				if h.Origins[j] != name {
					t.Fatalf("Test %d expected %v for %d th zone, got %v", i, name, j, h.Origins[j])
				}
			}
		}
	}
}

func TestHostsInlineParse(t *testing.T) {
	tests := []struct {
		inputFileRules      string
		shouldErr           bool
		expectedbyAddr      map[string][]string
		expectedFallthrough *fall.F
	}{
		{
			`hosts highly_unlikely_to_exist_hosts_file example.org {
                                10.0.0.1 example.org
                                fallthrough
                        }`,
			false,
			map[string][]string{
				`10.0.0.1`: {
					`example.org.`,
				},
			},
			fall.Zero(),
		},
		{
			`hosts highly_unlikely_to_exist_hosts_file example.org {
			                                10.0.0.1 example.org
			                        }`,
			false,
			map[string][]string{
				`10.0.0.1`: {
					`example.org.`,
				},
			},
			nil,
		},
		{
			`hosts highly_unlikely_to_exist_hosts_file example.org {
			                                fallthrough
			                                10.0.0.1 example.org
			                        }`,
			true,
			map[string][]string{},
			fall.Zero(),
		},
	}

	for i, test := range tests {
		c := caddy.NewTestController("dns", test.inputFileRules)
		h, err := hostsParse(c)

		if err == nil && test.shouldErr {
			t.Fatalf("Test %d expected errors, but got no error", i)
		} else if err != nil && !test.shouldErr {
			t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
		} else if !test.shouldErr {
			if !h.Fall.Equal(test.expectedFallthrough) {
				t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fall)
			}
			for k, expectedVal := range test.expectedbyAddr {
				if val, ok := h.hmap.byAddr[k]; !ok {
					t.Fatalf("Test %d expected %v, got no entry", i, k)
				} else {
					if len(expectedVal) != len(val) {
						t.Fatalf("Test %d expected %v records for %v, got %v", i, len(expectedVal), k, len(val))
					}
					for j := range expectedVal {
						if expectedVal[j] != val[j] {
							t.Fatalf("Test %d expected %v for %v, got %v", i, expectedVal[j], j, val[j])
						}
					}
				}
			}
		}
	}

}