| 1 |
package dns_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strconv" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns" |
| 9 |
"codeberg.org/miekg/dns/dnsutil" |
| 10 |
"golang.org/x/crypto/cryptobyte" |
| 11 |
) |
| 12 |
|
| 13 |
// YoData implements the [RDATA] interface. |
| 14 |
type YoData struct { |
| 15 |
Priority uint8 |
| 16 |
Yo string `dns:"txt"` |
| 17 |
} |
| 18 |
|
| 19 |
func (rd YoData) Len() int { return len(rd.Yo) + 1 } |
| 20 |
func (rd YoData) String() string { |
| 21 |
return strconv.FormatUint(uint64(rd.Priority), 10) + " " + rd.Yo |
| 22 |
} |
| 23 |
|
| 24 |
// YO is a private RR: www.example.org. IN YO 10 Yo! It implements the [RR] interface. |
| 25 |
type YO struct { |
| 26 |
Hdr dns.Header |
| 27 |
Yo YoData |
| 28 |
} |
| 29 |
|
| 30 |
const codepoint = 65281 |
| 31 |
|
| 32 |
// Typer interface. |
| 33 |
func (rr *YO) Type() uint16 { return codepoint } |
| 34 |
|
| 35 |
// RR interface. |
| 36 |
func (rr *YO) Header() *dns.Header { return &rr.Hdr } |
| 37 |
func (rr *YO) Len() int { return rr.Hdr.Len() + rr.Yo.Len() } |
| 38 |
func (rr *YO) Data() dns.RDATA { return rr.Yo } |
| 39 |
func (rr *YO) Clone() dns.RR { return &YO{rr.Hdr, rr.Yo} } |
| 40 |
func (rr *YO) String() string { |
| 41 |
return rr.Header().Name + "\t" + |
| 42 |
strconv.FormatInt(int64(rr.Header().TTL), 10) + "\t" + |
| 43 |
dnsutil.ClassToString(rr.Header().Class) + "\tYO\t" + |
| 44 |
rr.Yo.String() |
| 45 |
} |
| 46 |
|
| 47 |
// Packer interface. |
| 48 |
func (rr *YO) Pack(msg []byte, off int) (int, error) { |
| 49 |
if off+rr.Yo.Len()+1 > len(msg) { |
| 50 |
return len(msg), fmt.Errorf("overflow packing YO") |
| 51 |
} |
| 52 |
msg[off] = rr.Yo.Priority |
| 53 |
off++ |
| 54 |
copy(msg[off:off+len(rr.Yo.Yo)], rr.Yo.Yo) |
| 55 |
off += len(rr.Yo.Yo) |
| 56 |
return off, nil |
| 57 |
} |
| 58 |
|
| 59 |
func (rr *YO) Unpack(data []byte) error { |
| 60 |
s := cryptobyte.String(data) |
| 61 |
if !s.ReadUint8(&rr.Yo.Priority) { |
| 62 |
return fmt.Errorf("overflow unpacking YO") |
| 63 |
} |
| 64 |
var b []byte |
| 65 |
if !s.ReadBytes(&b, len(s)) { |
| 66 |
return fmt.Errorf("overflow unpacking YO") |
| 67 |
} |
| 68 |
rr.Yo.Yo = string(b) |
| 69 |
if !s.Empty() { |
| 70 |
return fmt.Errorf("trailing record data: %s", "YO") |
| 71 |
} |
| 72 |
return nil |
| 73 |
} |
| 74 |
|
| 75 |
// Parser interface. |
| 76 |
func (rr *YO) Parse(tokens []string, _ string) error { |
| 77 |
if len(tokens) < 2 { // no rdata |
| 78 |
return nil |
| 79 |
} |
| 80 |
i, err := strconv.ParseUint(tokens[0], 10, 32) |
| 81 |
if err != nil || i > 255 { |
| 82 |
return fmt.Errorf("bad YO Priority") |
| 83 |
} |
| 84 |
rr.Yo.Priority = uint8(i) |
| 85 |
rr.Yo.Yo = tokens[1] |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
func TestExternalRR(t *testing.T) { |
| 90 |
dns.TypeToRR[codepoint] = func() dns.RR { return new(YO) } |
| 91 |
dns.TypeToString[codepoint] = "YO" |
| 92 |
dns.StringToType["YO"] = codepoint |
| 93 |
|
| 94 |
y := &YO{Hdr: dns.Header{Name: "example.org.", Class: dns.ClassINET}, Yo: YoData{Priority: 10, Yo: "Yo!"}} |
| 95 |
rry, err := dns.New(y.String()) |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
if rry.String() != y.String() { |
| 100 |
t.Fatal("YO string presentations should be identical") |
| 101 |
} |
| 102 |
|
| 103 |
m := dns.NewMsg("yo.example.org.", codepoint) |
| 104 |
m.Answer = []dns.RR{y} |
| 105 |
m.Pack() |
| 106 |
r := new(dns.Msg) |
| 107 |
r.Data = m.Data |
| 108 |
r.Unpack() |
| 109 |
|
| 110 |
if m.String() != r.String() { |
| 111 |
t.Fatal("YO presentation should survive Pack/Unpack") |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func ExampleRR_private() { |
| 116 |
dns.TypeToRR[codepoint] = func() dns.RR { return new(YO) } |
| 117 |
dns.TypeToString[codepoint] = "YO" |
| 118 |
dns.StringToType["YO"] = codepoint |
| 119 |
|
| 120 |
y := &YO{Hdr: dns.Header{Name: "example.org.", Class: dns.ClassINET}, Yo: YoData{Priority: 10, Yo: "Yo!"}} |
| 121 |
|
| 122 |
m := dns.NewMsg("yo.example.org.", codepoint) |
| 123 |
m.Answer = []dns.RR{y} |
| 124 |
m.Pack() |
| 125 |
// do something with m |
| 126 |
} |
| 127 |
|
| 128 |
// YOOPT is a custom EDNS0 option for testing external EDNS0 support. |
| 129 |
type YOOPT struct { |
| 130 |
Yo string |
| 131 |
} |
| 132 |
|
| 133 |
const optcodepoint = 65001 |
| 134 |
|
| 135 |
// Typer interface. |
| 136 |
func (o *YOOPT) Type() uint16 { return optcodepoint } |
| 137 |
|
| 138 |
// RR interface. |
| 139 |
func (o *YOOPT) Header() *dns.Header { return &dns.Header{Name: "."} } |
| 140 |
func (o *YOOPT) Data() dns.RDATA { return o } |
| 141 |
func (o *YOOPT) Len() int { return 4 + len(o.Yo) } // 4 = TLV overhead (code + length) |
| 142 |
func (o *YOOPT) Clone() dns.RR { return &YOOPT{Yo: o.Yo} } |
| 143 |
func (o *YOOPT) String() string { return "YOOPT " + o.Yo } |
| 144 |
|
| 145 |
// EDNS0 interface. |
| 146 |
func (o *YOOPT) Pseudo() bool { return true } |
| 147 |
|
| 148 |
// Packer interface. |
| 149 |
func (o *YOOPT) Pack(msg []byte, off int) (int, error) { |
| 150 |
if off+len(o.Yo) > len(msg) { |
| 151 |
return len(msg), fmt.Errorf("overflow packing YOOPT") |
| 152 |
} |
| 153 |
copy(msg[off:], o.Yo) |
| 154 |
return off + len(o.Yo), nil |
| 155 |
} |
| 156 |
|
| 157 |
func (o *YOOPT) Unpack(yo []byte) error { |
| 158 |
o.Yo = string(yo) |
| 159 |
return nil |
| 160 |
} |
| 161 |
|
| 162 |
func TestExternalEDNS0(t *testing.T) { |
| 163 |
dns.CodeToRR[optcodepoint] = func() dns.EDNS0 { return new(YOOPT) } |
| 164 |
dns.CodeToString[optcodepoint] = "YOOPT" |
| 165 |
|
| 166 |
m := dns.NewMsg("yo.example.org.", dns.TypeA) |
| 167 |
m.Pseudo = []dns.RR{&YOOPT{Yo: "Yo!"}} |
| 168 |
|
| 169 |
if err := m.Pack(); err != nil { |
| 170 |
t.Fatalf("YOOPT failed to Pack: %v", err) |
| 171 |
} |
| 172 |
|
| 173 |
if err := m.Unpack(); err != nil { |
| 174 |
t.Fatalf("YOPT failed to Unpack %v", err) |
| 175 |
} |
| 176 |
|
| 177 |
if len(m.Pseudo) != 1 { |
| 178 |
t.Fatalf("expected 1 pseudo record, got %d", len(m.Pseudo)) |
| 179 |
} |
| 180 |
|
| 181 |
y, ok := m.Pseudo[0].(*YOOPT) |
| 182 |
if !ok { |
| 183 |
t.Fatalf("pseudo record is not YOOPT, got %T", m.Pseudo[0]) |
| 184 |
} |
| 185 |
if y.Yo != "Yo!" { |
| 186 |
t.Fatalf("expected Data 'Yo!', got '%s'", y.Yo) |
| 187 |
} |
| 188 |
if x := y.Type(); x != optcodepoint { |
| 189 |
t.Fatalf("expected type %d, got %d", optcodepoint, x) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func ExampleEDNS0_private() { |
| 194 |
dns.CodeToRR[optcodepoint] = func() dns.EDNS0 { return new(YOOPT) } |
| 195 |
dns.CodeToString[optcodepoint] = "YOOPT" |
| 196 |
|
| 197 |
m := dns.NewMsg("yo.example.org.", dns.TypeA) |
| 198 |
m.Pseudo = []dns.RR{&YOOPT{Yo: "Yo!"}} |
| 199 |
m.Pack() |
| 200 |
// do something with m |
| 201 |
} |