aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/parse/parse_test.go
blob: e0f817f4976a788c2916c5672a7aed9946a2b2df (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
package parse

import (
	"testing"

	"github.com/coredns/caddy"
)

func TestTransfer(t *testing.T) {
	tests := []struct {
		inputFileRules string
		shouldErr      bool
		secondary      bool
		expectedTo     []string
		expectedFrom   []string
	}{
		// OK transfer to
		{
			`to 127.0.0.1`,
			false, false, []string{"127.0.0.1:53"}, []string{},
		},
		// OK transfer tos
		{
			`to 127.0.0.1 127.0.0.2`,
			false, false, []string{"127.0.0.1:53", "127.0.0.2:53"}, []string{},
		},
		// OK transfer from
		{
			`from 127.0.0.1`,
			false, true, []string{}, []string{"127.0.0.1:53"},
		},
		// OK transfer froms
		{
			`from 127.0.0.1 127.0.0.2`,
			false, true, []string{}, []string{"127.0.0.1:53", "127.0.0.2:53"},
		},
		// OK transfer tos/froms
		{
			`to 127.0.0.1 127.0.0.2
			from 127.0.0.1 127.0.0.2`,
			false, true, []string{"127.0.0.1:53", "127.0.0.2:53"}, []string{"127.0.0.1:53", "127.0.0.2:53"},
		},
		// Bad transfer from, secondary false
		{
			`from 127.0.0.1`,
			true, false, []string{}, []string{},
		},
		// Bad transfer from garbage
		{
			`from !@#$%^&*()`,
			true, true, []string{}, []string{},
		},
		// Bad transfer from no args
		{
			`from`,
			true, false, []string{}, []string{},
		},
		// Bad transfer from *
		{
			`from *`,
			true, true, []string{}, []string{},
		},
	}

	for i, test := range tests {
		c := caddy.NewTestController("dns", test.inputFileRules)
		tos, froms, err := Transfer(c, test.secondary)

		if err == nil && test.shouldErr {
			t.Fatalf("Test %d expected errors, but got no error %+v %+v", i, err, test)
		} else if err != nil && !test.shouldErr {
			t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
		}

		if test.expectedTo != nil {
			for j, got := range tos {
				if got != test.expectedTo[j] {
					t.Fatalf("Test %d expected %v, got %v", i, test.expectedTo[j], got)
				}
			}
		}
		if test.expectedFrom != nil {
			for j, got := range froms {
				if got != test.expectedFrom[j] {
					t.Fatalf("Test %d expected %v, got %v", i, test.expectedFrom[j], got)
				}
			}
		}

	}

}