aboutsummaryrefslogtreecommitdiff
path: root/test/rewrite_test.go
blob: 34795413de33af64bfa16486ef2f32236b7b70fe (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
package test

import (
	"bytes"
	"testing"

	"github.com/miekg/dns"
)

func TestRewrite(t *testing.T) {
	t.Parallel()
	corefile := `.:0 {
       rewrite type MX a
       rewrite edns0 local set 0xffee hello-world
       erratic . {
	drop 0
	}
}`

	i, udp, _, err := CoreDNSServerAndPorts(corefile)
	if err != nil {
		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
	}

	defer i.Stop()

	testMX(t, udp)
	testEdns0(t, udp)
}

func testMX(t *testing.T, server string) {
	m := new(dns.Msg)
	m.SetQuestion("example.com.", dns.TypeMX)

	r, err := dns.Exchange(m, server)
	if err != nil {
		t.Fatalf("Expected to receive reply, but didn't: %s", err)
	}

	// expect answer section with A record in it
	if len(r.Answer) == 0 {
		t.Error("Expected to at least one RR in the answer section, got none")
	}
	if r.Answer[0].Header().Rrtype != dns.TypeA {
		t.Errorf("Expected RR to A, got: %d", r.Answer[0].Header().Rrtype)
	}
	if r.Answer[0].(*dns.A).A.String() != "192.0.2.53" {
		t.Errorf("Expected 192.0.2.53, got: %s", r.Answer[0].(*dns.A).A.String())
	}
}

func testEdns0(t *testing.T, server string) {
	m := new(dns.Msg)
	m.SetQuestion("example.com.", dns.TypeA)

	r, err := dns.Exchange(m, server)
	if err != nil {
		t.Fatalf("Expected to receive reply, but didn't: %s", err)
	}

	// expect answer section with A record in it
	if len(r.Answer) == 0 {
		t.Error("Expected to at least one RR in the answer section, got none")
	}
	if r.Answer[0].Header().Rrtype != dns.TypeA {
		t.Errorf("Expected RR to A, got: %d", r.Answer[0].Header().Rrtype)
	}
	if r.Answer[0].(*dns.A).A.String() != "192.0.2.53" {
		t.Errorf("Expected 192.0.2.53, got: %s", r.Answer[0].(*dns.A).A.String())
	}
	o := r.IsEdns0()
	if o == nil || len(o.Option) == 0 {
		t.Error("Expected EDNS0 options but got none")
	} else {
		if e, ok := o.Option[0].(*dns.EDNS0_LOCAL); ok {
			if e.Code != 0xffee {
				t.Errorf("Expected EDNS_LOCAL code 0xffee but got %x", e.Code)
			}
			if !bytes.Equal(e.Data, []byte("hello-world")) {
				t.Errorf("Expected EDNS_LOCAL data 'hello-world' but got %q", e.Data)
			}
		} else {
			t.Errorf("Expected EDNS0_LOCAL but got %v", o.Option[0])
		}
	}
}