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

import (
	"context"
	"testing"

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

	"github.com/miekg/dns"
)

func TestBufsize(t *testing.T) {
	const maxBufSize = 1024

	setUpWithRequestBufsz := func(bufferSize uint16) (Bufsize, *dns.Msg) {
		p := Bufsize{
			Size: maxBufSize,
			Next: whoami.Whoami{},
		}
		r := new(dns.Msg)
		r.SetQuestion(dns.Fqdn("."), dns.TypeA)
		r.Question[0].Qclass = dns.ClassINET
		if bufferSize > 0 {
			r.SetEdns0(bufferSize, false)
		}
		return p, r
	}

	t.Run("Limit response buffer size", func(t *testing.T) {
		// GIVEN
		//		plugin initialized with maximum buffer size
		//		request has larger buffer size than allowed
		p, r := setUpWithRequestBufsz(maxBufSize + 128)

		// WHEN
		//		request is processed
		_, err := p.ServeDNS(context.Background(), &test.ResponseWriter{}, r)

		// THEN
		//		no error
		//		OPT RR present
		//		request buffer size is limited
		if err != nil {
			t.Errorf("unexpected error %s", err)
		}
		option := r.IsEdns0()
		if option == nil {
			t.Errorf("OPT RR not present")
		}
		if option.UDPSize() != maxBufSize {
			t.Errorf("buffer size not limited")
		}
	})

	t.Run("Do not increase response buffer size", func(t *testing.T) {
		// GIVEN
		//		plugin initialized with maximum buffer size
		//		request has smaller buffer size than allowed
		const smallerBufferSize = maxBufSize - 128
		p, r := setUpWithRequestBufsz(smallerBufferSize)

		// WHEN
		//		request is processed
		_, err := p.ServeDNS(context.Background(), &test.ResponseWriter{}, r)

		// THEN
		//		no error
		//		request buffer size is not expanded
		if err != nil {
			t.Errorf("unexpected error %s", err)
		}
		option := r.IsEdns0()
		if option == nil {
			t.Errorf("OPT RR not present")
		}
		if option.UDPSize() != smallerBufferSize {
			t.Errorf("buffer size should not be increased")
		}
	})

	t.Run("Buffer size should not be set", func(t *testing.T) {
		// GIVEN
		//		plugin initialized with maximum buffer size
		//		request has no EDNS0 option set
		p, r := setUpWithRequestBufsz(0)

		// WHEN
		//		request is processed
		_, err := p.ServeDNS(context.Background(), &test.ResponseWriter{}, r)

		// THEN
		//		no error
		//		OPT RR is not appended
		if err != nil {
			t.Errorf("unexpected error %s", err)
		}
		if r.IsEdns0() != nil {
			t.Errorf("EDNS0 enabled for incoming request")
		}
	})
}