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

import (
	"testing"

	"github.com/coredns/caddy"
)

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

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

		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.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)
				}
			}
		}
	}
}