aboutsummaryrefslogtreecommitdiff
path: root/plugin/bufsize/bufsize_test.go
blob: 3d714d2f194c2285fa3a082ebff5f99542fa896f (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
package bufsize

import (
	"context"
	"testing"

	"github.com/coredns/coredns/plugin"
	"github.com/coredns/coredns/plugin/test"
	"github.com/coredns/coredns/plugin/whoami"

	"github.com/miekg/dns"
)

func TestBufsize(t *testing.T) {
	em := Bufsize{
		Size: 512,
	}

	tests := []struct {
		next            plugin.Handler
		qname           string
		inputBufsize    uint16
		outgoingBufsize uint16
		expectedErr     error
	}{
		// This plugin is responsible for limiting outgoing query's bufize
		{
			next:            whoami.Whoami{},
			qname:           ".",
			inputBufsize:    1200,
			outgoingBufsize: 512,
			expectedErr:     nil,
		},
		// If EDNS is not enabled, this plugin adds it
		{
			next:            whoami.Whoami{},
			qname:           ".",
			outgoingBufsize: 512,
			expectedErr:     nil,
		},
	}

	for i, tc := range tests {
		req := new(dns.Msg)
		req.SetQuestion(dns.Fqdn(tc.qname), dns.TypeA)
		req.Question[0].Qclass = dns.ClassINET
		em.Next = tc.next

		if tc.inputBufsize != 0 {
			req.SetEdns0(tc.inputBufsize, false)
		}

		_, err := em.ServeDNS(context.Background(), &test.ResponseWriter{}, req)

		if err != tc.expectedErr {
			t.Errorf("Test %d: Expected error is %v, but got %v", i, tc.expectedErr, err)
		}

		if tc.outgoingBufsize != 0 {
			for _, extra := range req.Extra {
				if option, ok := extra.(*dns.OPT); ok {
					b := option.UDPSize()
					if b != tc.outgoingBufsize {
						t.Errorf("Test %d: Expected outgoing bufsize is %d, but got %d", i, tc.outgoingBufsize, b)
					}
				} else {
					t.Errorf("Test %d: Not found OPT RR.", i)
				}
			}
		}
	}
}