aboutsummaryrefslogtreecommitdiff
path: root/test/cache_test.go
blob: 831c39fa5ef37affe5837cfd1c9c284cfcef7a72 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package test

import (
	"testing"

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

	"github.com/miekg/dns"
)

func TestLookupCache(t *testing.T) {
	// Start auth. CoreDNS holding the auth zone.
	name, rm, err := test.TempFile(".", exampleOrg)
	if err != nil {
		t.Fatalf("Failed to create zone: %s", err)
	}
	defer rm()

	corefile := `example.org:0 {
		file ` + name + `
	}`

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

	// Start caching forward CoreDNS that we want to test.
	corefile = `example.org:0 {
		forward . ` + udp + `
		cache 10
	}`

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

	t.Run("Long TTL", func(t *testing.T) {
		testCase(t, "example.org.", udp, 2, 10)
	})

	t.Run("Short TTL", func(t *testing.T) {
		testCase(t, "short.example.org.", udp, 1, 5)
	})

	t.Run("DNSSEC OPT", func(t *testing.T) {
		testCaseDNSSEC(t, "example.org.", udp, 4096)
	})

	t.Run("DNSSEC OPT", func(t *testing.T) {
		testCaseDNSSEC(t, "example.org.", udp, 0)
	})
}

func testCase(t *testing.T, name, addr string, expectAnsLen int, expectTTL uint32) {
	m := new(dns.Msg)
	m.SetQuestion(name, dns.TypeA)
	resp, err := dns.Exchange(m, addr)
	if err != nil {
		t.Fatalf("Expected to receive reply, but didn't: %s", err)
	}

	if len(resp.Answer) != expectAnsLen {
		t.Fatalf("Expected %v RR in the answer section, got %v.", expectAnsLen, len(resp.Answer))
	}

	ttl := resp.Answer[0].Header().Ttl
	if ttl != expectTTL {
		t.Errorf("Expected TTL to be %d, got %d", expectTTL, ttl)
	}
}

func testCaseDNSSEC(t *testing.T, name, addr string, bufsize int) {
	m := new(dns.Msg)
	m.SetQuestion(name, dns.TypeA)

	if bufsize > 0 {
		o := &dns.OPT{Hdr: dns.RR_Header{Name: ".", Rrtype: dns.TypeOPT}}
		o.SetDo()
		o.SetUDPSize(uint16(bufsize))
		m.Extra = append(m.Extra, o)
	}
	resp, err := dns.Exchange(m, addr)
	if err != nil {
		t.Fatalf("Expected to receive reply, but didn't: %s", err)
	}

	if len(resp.Extra) == 0 && bufsize == 0 {
		// no OPT, this is OK
		return
	}

	opt := resp.Extra[len(resp.Extra)-1]
	if x, ok := opt.(*dns.OPT); !ok && bufsize > 0 {
		t.Fatalf("Expected OPT RR, got %T", x)
	}
	if bufsize > 0 {
		if !opt.(*dns.OPT).Do() {
			t.Errorf("Expected DO bit to be set, got false")
		}
		if x := opt.(*dns.OPT).UDPSize(); int(x) != bufsize {
			t.Errorf("Expected %d bufsize, got %d", bufsize, x)
		}
	} else {
		if opt.Header().Rrtype == dns.TypeOPT {
			t.Errorf("Expected no OPT RR, but got one: %s", opt)
		}
	}
}

func TestLookupCacheWithoutEdns(t *testing.T) {
	name, rm, err := test.TempFile(".", exampleOrg)
	if err != nil {
		t.Fatalf("Failed to create zone: %s", err)
	}
	defer rm()

	corefile := `example.org:0 {
		file ` + name + `
	}`

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

	// Start caching forward CoreDNS that we want to test.
	corefile = `example.org:0 {
		forward . ` + udp + `
		cache 10
	}`

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

	m := new(dns.Msg)
	m.SetQuestion("example.org.", dns.TypeA)
	resp, err := dns.Exchange(m, udp)
	if err != nil {
		t.Fatalf("Expected to receive reply, but didn't: %s", err)
	}
	if len(resp.Extra) == 0 {
		return
	}

	if resp.Extra[0].Header().Rrtype == dns.TypeOPT {
		t.Fatalf("Expected no OPT RR, but got: %s", resp.Extra[0])
	}
	t.Fatalf("Expected empty additional section, got %v", resp.Extra)
}
.noreply.github.com> 2022-06-30Integration Docs Next Steps (#3677)Gravatar Dan Jutan 11-314/+666 * sitemap readme skeleton + first sections * Revert "sitemap readme skeleton + first sections" This reverts commit cc55b312b6dc95522645002806d63f32c33d1956. * sitemap readme skeleton + first sections * remove canonicalURL option from sitemap * add customPages option to readme * sitemap examples * partytown * deno run command * reference deno example * node readme * netlify & vercel readmes * note that telemetry is installed * telemetry is *enabled*, not installed * Update packages/integrations/vercel/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update packages/integrations/vercel/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * readme -> README * Update packages/integrations/deno/readme.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Update packages/integrations/deno/readme.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * qualify they * Update packages/integrations/sitemap/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * Uppercase README names * Update packages/integrations/partytown/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * imports -> import typo * update changeset Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> 2022-06-30[ci] formatGravatar tony-sull 1-2/+2 2022-06-30refactor to provide better cli error handling (#3768)Gravatar Fred K. Schott 2-43/+37 2022-06-30[ci] release (#3772)@astrojs/preact@0.3.1Gravatar Fred K. Bot 12-22/+23 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-06-30Added Cloudflare adapter to README.md (#3773)Gravatar Isaac McFadyen 1-0/+1 2022-06-30[ci] formatGravatar hippotastic 1-5/+4 2022-06-30Fix "Invalid hook call" warning (#3769)Gravatar hippotastic 2-9/+79 * Fix "Invalid hook call" warning * Fix eslint warnings * Apply code review suggestions 2022-06-29[ci] release (#3759)astro@1.0.0-beta.59@astrojs/telemetry@0.2.2@astrojs/preact@0.3.0Gravatar Fred K. Bot 42-121/+117 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-06-29[ci] formatGravatar FredKSchott 8-35/+36 2022-06-29manual lockfile update (#3751)Gravatar Fred K. Schott 3-2659/+2871 * lockfile update * update lockfile gen script * Update index.ts 2022-06-29add error event to telemetry (#3750)Gravatar Fred K. Schott 16-85/+270