Debian Codegraph @

rdata_test.go

raw
1 package dns_test
2
3 import (
4 "fmt"
5 "maps"
6 "testing"
7
8 "codeberg.org/miekg/dns"
9 "codeberg.org/miekg/dns/dnsutil"
10 "codeberg.org/miekg/dns/internal/dnsstring"
11 "codeberg.org/miekg/dns/rdata"
12 )
13
14 // Example on how get the text presentation of a [dns.RR].
15 func ExampleRDATA_string() {
16 rr := &dns.MX{Hdr: dns.Header{Name: "miek.nl.", Class: dns.ClassINET, TTL: 3600}, Preference: 10, Mx: "mx.miek.nl."}
17 s := rr.Header().String() + " " + dnsutil.TypeToString(dns.RRToType(rr)) + "\t" + rr.Data().String()
18 fmt.Println(s)
19 // Output: miek.nl. 3600 IN MX 10 mx.miek.nl.
20 }
21
22 // Example on how to set the rdata of an RR.
23 func ExampleRDATA_newData() {
24 rd, _ := dns.NewData(dns.TypeMX, "10 mx.miek.nl.")
25 rr := dns.TypeToRR[dns.TypeMX]()
26 rr.Header().Name = "miek.nl."
27 rr.Header().Class = dns.ClassINET
28 fn := dns.TypeToRDATA[dns.TypeMX]
29 // Set the rdata in the rr.
30 fn(rr, rd)
31 fmt.Println(rr)
32 // Output: miek.nl. 0 IN MX 10 mx.miek.nl.
33 }
34
35 func TestTypeToRDATA(t *testing.T) {
36 testcases := []struct {
37 name string
38 t uint16
39 in string
40 fn func(rr dns.RR) error
41 }{
42 {
43 "mx",
44 dns.TypeMX,
45 "10 mx.miek.nl.",
46 func(rr dns.RR) error {
47 mx, ok := rr.(*dns.MX)
48 if !ok {
49 return fmt.Errorf("expected MX, got %T", rr)
50 }
51 if mx.Preference != 10 {
52 return fmt.Errorf("expected 10, got %d", mx.Preference)
53 }
54 if mx.Mx != "mx.miek.nl." {
55 return fmt.Errorf("expected mx.miek.nl., got %s", mx.Mx)
56 }
57 return nil
58 },
59 },
60 }
61 for _, tc := range testcases {
62 t.Run(tc.name, func(t *testing.T) {
63 rd, _ := dns.NewData(tc.t, tc.in, ".")
64 rr := dns.TypeToRR[tc.t]()
65 fn := dns.TypeToRDATA[tc.t]
66 fn(rr, rd)
67
68 })
69 }
70 }
71
72 func TestNewData(t *testing.T) {
73 testcases := []struct {
74 name string
75 t uint16
76 in string
77 fn func(rd dns.RDATA) error
78 }{
79 {
80 "mx-origin-ok",
81 dns.TypeMX,
82 "10 mx.miek.nl",
83 func(rd dns.RDATA) error {
84 if rd == nil {
85 return fmt.Errorf("expected rd, got none")
86 }
87 mx := rd.(rdata.MX)
88 if mx.Preference != 10 {
89 return fmt.Errorf("expected 10, got %d", mx.Preference)
90 }
91 if mx.Mx != "mx.miek.nl." {
92 return fmt.Errorf("expected mx.miek.nl., got %s", mx.Mx)
93 }
94 return nil
95 },
96 },
97 {
98 "mx-ok",
99 dns.TypeMX,
100 "10 mx.miek.nl.",
101 func(rd dns.RDATA) error {
102 if rd == nil {
103 return fmt.Errorf("expected rd, got none")
104 }
105 mx := rd.(rdata.MX)
106 if mx.Preference != 10 {
107 return fmt.Errorf("expected 10, got %d", mx.Preference)
108 }
109 if mx.Mx != "mx.miek.nl." {
110 return fmt.Errorf("expected mx.miek.nl., got %s", mx.Mx)
111 }
112 return nil
113 },
114 },
115 {
116 "mx-space-fail",
117 dns.TypeMX,
118 " 10 mx.miek.nl.",
119 func(rd dns.RDATA) error {
120 if rd.(rdata.MX).Preference == 0 {
121 return nil
122 }
123 return fmt.Errorf("expected nil rd: %v", rd)
124 },
125 },
126 }
127 for _, tc := range testcases {
128 t.Run(tc.name, func(t *testing.T) {
129 rd, _ := dns.NewData(tc.t, tc.in, ".")
130 if err := tc.fn(rd); err != nil {
131 t.Fatal(err)
132 }
133 })
134 }
135 }
136
137 func TestDataDrift(t *testing.T) {
138 // The rdata package renders algorithms via its own copy of the map, which must not drift.
139 if !maps.Equal(dns.AlgorithmToString, dnsstring.AlgorithmToString) {
140 t.Errorf("expected %v, got %v", dns.AlgorithmToString, dnsstring.AlgorithmToString)
141 }
142 }