summaryrefslogtreecommitdiff
path: root/internal/server/server_test.go
blob: eaf0190bcd367861e2e482129df48b7f3a3082c8 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package server

import (
	"testing"

	"github.com/miekg/dns"
)

func TestBuildResponse(t *testing.T) {
	tests := []struct {
		name        string
		req         *dns.Msg
		wantRcode   int
		wantAnswers int
		wantEdns0   bool
	}{
		{
			name: "example.com A returns 127.0.0.1",
			req: func() *dns.Msg {
				m := new(dns.Msg)
				m.SetQuestion("example.com.", dns.TypeA)
				return m
			}(),
			wantRcode:   dns.RcodeSuccess,
			wantAnswers: 1,
			wantEdns0:   false,
		},
		{
			name: "google.com A returns NXDOMAIN",
			req: func() *dns.Msg {
				m := new(dns.Msg)
				m.SetQuestion("google.com.", dns.TypeA)
				return m
			}(),
			wantRcode:   dns.RcodeNameError,
			wantAnswers: 0,
			wantEdns0:   false,
		},
		{
			name: "other.com A returns NXDOMAIN",
			req: func() *dns.Msg {
				m := new(dns.Msg)
				m.SetQuestion("other.com.", dns.TypeA)
				return m
			}(),
			wantRcode:   dns.RcodeNameError,
			wantAnswers: 0,
			wantEdns0:   false,
		},
		{
			name: "no questions returns FORMERR",
			req: func() *dns.Msg {
				return new(dns.Msg)
			}(),
			wantRcode:   dns.RcodeFormatError,
			wantAnswers: 0,
			wantEdns0:   false,
		},
		{
			name: "EDNS0 query preserved with 4096 buffer",
			req: func() *dns.Msg {
				m := new(dns.Msg)
				m.SetQuestion("example.com.", dns.TypeA)
				m.SetEdns0(1232, true)
				return m
			}(),
			wantRcode:   dns.RcodeSuccess,
			wantAnswers: 1,
			wantEdns0:   true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			resp := buildResponse(tt.req)
			if resp.Rcode != tt.wantRcode {
				t.Errorf("rcode: got %d, want %d", resp.Rcode, tt.wantRcode)
			}
			if len(resp.Answer) != tt.wantAnswers {
				t.Errorf("answers: got %d, want %d", len(resp.Answer), tt.wantAnswers)
			}
			if tt.wantEdns0 {
				if opt := resp.IsEdns0(); opt == nil {
					t.Error("expected EDNS0 in response, got none")
				} else if opt.UDPSize() != 4096 {
					t.Errorf("edns0 udp size: got %d, want 4096", opt.UDPSize())
				}
			}
		})
	}
}

func FuzzBuildResponse(f *testing.F) {
	seed := []byte{
		0x00, 0x00, // ID
		0x01, 0x00, // flags: RD
		0x00, 0x01, // QDCOUNT: 1
		0x00, 0x00, // ANCOUNT
		0x00, 0x00, // NSCOUNT
		0x00, 0x00, // ARCOUNT
		// Question: example.com A
		0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
		0x03, 0x63, 0x6f, 0x6d,
		0x00,
		0x00, 0x01, // QTYPE: A
		0x00, 0x01, // QCLASS: IN
	}
	f.Add(seed)
	f.Fuzz(func(t *testing.T, data []byte) {
		msg := new(dns.Msg)
		if err := msg.Unpack(data); err != nil {
			return
		}
		resp := buildResponse(msg)
		if resp == nil {
			t.Fatal("buildResponse returned nil")
		}
		if _, err := resp.Pack(); err != nil {
			t.Errorf("pack failed: %v", err)
		}
	})
}