Debian Codegraph @

cmd/atomdns/handlers/cookie/cookie_test.go

raw
1 package cookie_test
2
3 import (
4 "context"
5 "encoding/hex"
6 "hash/fnv"
7 "io"
8 "testing"
9
10 "codeberg.org/miekg/dns"
11 "codeberg.org/miekg/dns/cmd/atomdns/atomtest"
12 "codeberg.org/miekg/dns/cmd/atomdns/handlers/cookie"
13 "codeberg.org/miekg/dns/dnstest"
14 )
15
16 func TestCookie(t *testing.T) {
17 c := &cookie.Cookie{Secret: "geheim"}
18
19 f := fnv.New64()
20 io.WriteString(f, "::1")
21 io.WriteString(f, "::1")
22 io.WriteString(f, "ook geheim")
23 cookie := &dns.COOKIE{Cookie: hex.EncodeToString(f.Sum(nil))}
24
25 m := dnstest.NewMsg()
26 m.Pseudo = []dns.RR{cookie}
27 m.Pack()
28
29 tw := dnstest.NewTestRecorder()
30 c.HandlerFunc(atomtest.Echo).ServeDNS(context.TODO(), tw, m)
31
32 tw.Msg.Unpack()
33 if len(tw.Msg.Pseudo) != 1 {
34 t.Fatal("expected pseudo section")
35 }
36 if _, ok := tw.Msg.Pseudo[0].(*dns.COOKIE); !ok {
37 t.Fatal("expected COOKIE RR")
38 }
39 }