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
|
package variables
import (
"bytes"
"testing"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
func TestGetValue(t *testing.T) {
// test.ResponseWriter has the following values:
// The remote will always be 10.240.0.1 and port 40212.
// The local address is always 127.0.0.1 and port 53.
tests := []struct {
varName string
expectedValue []byte
shouldErr bool
}{
{
queryName,
[]byte("example.com."),
false,
},
{
queryType,
[]byte{0x00, 0x01},
false,
},
{
clientIP,
[]byte{10, 240, 0, 1},
false,
},
{
clientPort,
[]byte{0x9D, 0x14},
false,
},
{
protocol,
[]byte("udp"),
false,
},
{
serverIP,
[]byte{127, 0, 0, 1},
false,
},
{
serverPort,
[]byte{0, 53},
false,
},
{
"wrong_var",
[]byte{},
true,
},
}
for i, tc := range tests {
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeA)
m.Question[0].Qclass = dns.ClassINET
state := request.Request{W: &test.ResponseWriter{}, Req: m}
value, err := GetValue(state, tc.varName)
if tc.shouldErr && err == nil {
t.Errorf("Test %d: Expected error, but didn't recieve", i)
}
if !tc.shouldErr && err != nil {
t.Errorf("Test %d: Expected no error, but got error: %v", i, err.Error())
}
if !bytes.Equal(tc.expectedValue, value) {
t.Errorf("Test %d: Expected %v but got %v", i, tc.expectedValue, value)
}
}
}
|