| 1 |
package dnsutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"codeberg.org/miekg/dns" |
| 8 |
) |
| 9 |
|
| 10 |
func TestToString(t *testing.T) { |
| 11 |
if x := RcodeToString(5); x != "REFUSED" { |
| 12 |
t.Errorf("expected %s, got %s", "REFUSED", x) |
| 13 |
} |
| 14 |
if x := RcodeToString(55); x != "RCODE55" { |
| 15 |
t.Errorf("expected %s, got %s", "RCODE55", x) |
| 16 |
} |
| 17 |
if x := OpcodeToString(0); x != "QUERY" { |
| 18 |
t.Errorf("expected %s, got %s", "QUERY", x) |
| 19 |
} |
| 20 |
if x := OpcodeToString(12); x != "OPCODE12" { |
| 21 |
t.Errorf("expected %s, got %s", "OPCODE12", x) |
| 22 |
} |
| 23 |
if x := TypeToString(1); x != "A" { |
| 24 |
t.Errorf("expected %s, got %s", "A", x) |
| 25 |
} |
| 26 |
if x := ClassToString(1); x != "IN" { |
| 27 |
t.Errorf("expected %s, got %s", "IN", x) |
| 28 |
} |
| 29 |
if x := CodeToString(10); x != "COOKIE" { |
| 30 |
t.Errorf("expected %s, got %s", "COOKIE", x) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestStringToType(t *testing.T) { |
| 35 |
testcases := map[string]struct { |
| 36 |
Number uint16 |
| 37 |
Fn func(s string) (uint16, error) |
| 38 |
Err bool |
| 39 |
}{ |
| 40 |
// Type |
| 41 |
"A": {dns.TypeA, StringToType, true}, |
| 42 |
"AAAA": {dns.TypeAAAA, StringToType, true}, |
| 43 |
"a": {dns.TypeA, StringToType, true}, |
| 44 |
"banana": {0, StringToType, false}, |
| 45 |
"type1": {dns.TypeA, StringToType, true}, |
| 46 |
"typex": {0, StringToType, false}, |
| 47 |
"type100000": {0, StringToType, false}, |
| 48 |
TypeToString(dns.TypeMX): {dns.TypeMX, StringToType, true}, |
| 49 |
TypeToString(60000): {60000, StringToType, true}, |
| 50 |
// Code |
| 51 |
"REFUSED": {dns.RcodeRefused, StringToRcode, true}, |
| 52 |
"refused": {dns.RcodeRefused, StringToRcode, true}, |
| 53 |
"orange": {0, StringToRcode, false}, |
| 54 |
"rcode5": {dns.RcodeRefused, StringToRcode, true}, |
| 55 |
"RCODE55": {55, StringToRcode, true}, |
| 56 |
"rcodex": {0, StringToRcode, false}, |
| 57 |
"rcode100000": {0, StringToRcode, false}, |
| 58 |
RcodeToString(dns.RcodeBadCookie): {dns.RcodeBadCookie, StringToRcode, true}, |
| 59 |
RcodeToString(55): {55, StringToRcode, true}, |
| 60 |
// Class |
| 61 |
"IN": {dns.ClassINET, StringToClass, true}, |
| 62 |
"in": {dns.ClassINET, StringToClass, true}, |
| 63 |
"appel": {0, StringToClass, false}, |
| 64 |
"CLass1": {dns.ClassINET, StringToClass, true}, |
| 65 |
"classx": {0, StringToClass, false}, |
| 66 |
"class100000": {0, StringToClass, false}, |
| 67 |
ClassToString(dns.ClassCHAOS): {dns.ClassCHAOS, StringToClass, true}, |
| 68 |
ClassToString(200): {200, StringToClass, true}, |
| 69 |
// Code |
| 70 |
"COOKIE": {dns.CodeCOOKIE, StringToCode, true}, |
| 71 |
"cookie": {dns.CodeCOOKIE, StringToCode, true}, |
| 72 |
"pear": {0, StringToCode, false}, |
| 73 |
"code10": {dns.CodeCOOKIE, StringToCode, true}, |
| 74 |
"codex": {0, StringToCode, false}, |
| 75 |
"code100000": {0, StringToCode, false}, |
| 76 |
CodeToString(dns.CodeLOCALEND): {dns.CodeLOCALEND, StringToCode, true}, |
| 77 |
CodeToString(200): {200, StringToCode, true}, |
| 78 |
} |
| 79 |
for name, tc := range testcases { |
| 80 |
t.Run(name, func(t *testing.T) { |
| 81 |
got, err := tc.Fn(name) |
| 82 |
if !tc.Err && err == nil { |
| 83 |
t.Errorf("expected error, got nil") |
| 84 |
} |
| 85 |
if tc.Err { |
| 86 |
if err != nil { |
| 87 |
t.Fatalf("expected no error, got %v", err) |
| 88 |
} |
| 89 |
if got != tc.Number { |
| 90 |
t.Fatalf("expected %v, got %v", tc.Number, got) |
| 91 |
} |
| 92 |
} |
| 93 |
}) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestStringToOpcode(t *testing.T) { |
| 98 |
testcases := map[string]struct { |
| 99 |
Opcode uint8 |
| 100 |
Err bool |
| 101 |
}{ |
| 102 |
"QUERY": {dns.OpcodeQuery, true}, |
| 103 |
"status": {dns.OpcodeStatus, true}, |
| 104 |
"banana": {0, false}, |
| 105 |
"opcode0": {dns.OpcodeQuery, true}, |
| 106 |
"opcodex": {0, false}, |
| 107 |
"opcode300": {0, false}, |
| 108 |
OpcodeToString(dns.OpcodeStateful): {dns.OpcodeStateful, true}, |
| 109 |
OpcodeToString(20): {20, true}, |
| 110 |
} |
| 111 |
for name, tc := range testcases { |
| 112 |
t.Run(name, func(t *testing.T) { |
| 113 |
got, err := StringToOpcode(name) |
| 114 |
if !tc.Err && err == nil { |
| 115 |
t.Errorf("expected error, got nil") |
| 116 |
} |
| 117 |
if tc.Err { |
| 118 |
if err != nil { |
| 119 |
t.Fatalf("expected no error, got %v", err) |
| 120 |
} |
| 121 |
if got != tc.Opcode { |
| 122 |
t.Errorf("expected %v, got %v", tc.Opcode, got) |
| 123 |
} |
| 124 |
} |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func ExampleTypeToString() { |
| 130 |
rr := &dns.MX{Hdr: dns.Header{Name: "miek.nl.", Class: dns.ClassINET, TTL: 3600}, Preference: 10, Mx: "mx.miek.nl."} |
| 131 |
fmt.Println(TypeToString(dns.RRToType(rr))) |
| 132 |
// Output: MX |
| 133 |
} |