| 1 |
package dnsutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"slices" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestJoin(t *testing.T) { |
| 9 |
testcases := []struct { |
| 10 |
in []string |
| 11 |
out string |
| 12 |
}{ |
| 13 |
{[]string{"bla", "bliep", "example", "org"}, "bla.bliep.example.org."}, |
| 14 |
{[]string{"example", "."}, "example."}, |
| 15 |
{[]string{"example", "org."}, "example.org."}, // technically we should not be called like this. |
| 16 |
{[]string{"."}, "."}, |
| 17 |
} |
| 18 |
|
| 19 |
for i, tc := range testcases { |
| 20 |
if x := Join(tc.in...); x != tc.out { |
| 21 |
t.Errorf("test %d, expected %s, got %s", i, tc.out, x) |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func TestSplit(t *testing.T) { |
| 27 |
testcases := []struct { |
| 28 |
in string |
| 29 |
out []string |
| 30 |
}{ |
| 31 |
{"bla.bliep.example.org.", []string{"bla", "bliep", "example", "org"}}, |
| 32 |
{"example.org", []string{"example", "org"}}, |
| 33 |
{".", []string{"."}}, |
| 34 |
{"", []string{""}}, |
| 35 |
} |
| 36 |
|
| 37 |
for i, tc := range testcases { |
| 38 |
if x := Split(tc.in); slices.Compare(x, tc.out) != 0 { |
| 39 |
t.Errorf("test %d, expected %v, got %v", i, tc.out, x) |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestNext(t *testing.T) { |
| 45 |
type next struct { |
| 46 |
string |
| 47 |
int |
| 48 |
} |
| 49 |
testcases := map[next]int{ |
| 50 |
{"", 1}: 0, |
| 51 |
{"www.miek.nl.", 0}: 4, |
| 52 |
{"www.miek.nl.", 4}: 9, |
| 53 |
{"www.miek.nl.", 9}: 12, |
| 54 |
} |
| 55 |
for tc, i := range testcases { |
| 56 |
x, ok := Next(tc.string, tc.int) |
| 57 |
if i != x { |
| 58 |
t.Errorf("label should be %d, got %d, %t: next %d, %s", i, x, ok, tc.int, tc.string) |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestForward(t *testing.T) { |
| 64 |
testcases := []struct { |
| 65 |
in string |
| 66 |
out []string |
| 67 |
}{ |
| 68 |
{".", []string{"."}}, |
| 69 |
{"nl.", []string{"nl"}}, |
| 70 |
{"www.miek.nl.", []string{"www", "miek", "nl"}}, |
| 71 |
} |
| 72 |
for _, tc := range testcases { |
| 73 |
t.Run(tc.in, func(t *testing.T) { |
| 74 |
out := []string{} |
| 75 |
for x := range Forward(tc.in) { |
| 76 |
out = append(out, x) |
| 77 |
} |
| 78 |
if slices.Compare(tc.out, out) != 0 { |
| 79 |
t.Fatalf("labels should be %v, got %v", tc.out, out) |
| 80 |
} |
| 81 |
}) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestPrev(t *testing.T) { |
| 86 |
type prev struct { |
| 87 |
string |
| 88 |
int |
| 89 |
} |
| 90 |
testcases := map[prev]int{ |
| 91 |
{"", 1}: 0, |
| 92 |
{".", 1}: 0, |
| 93 |
{"www.miek.nl.", 12}: 9, |
| 94 |
{"www.miek.nl.", 9}: 4, |
| 95 |
{"www.miek.nl.", 4}: 0, |
| 96 |
} |
| 97 |
for tc, i := range testcases { |
| 98 |
x, ok := Prev(tc.string, tc.int) |
| 99 |
if i != x { |
| 100 |
t.Errorf("label should be %d, got %d, %t: prev2 %d, %s", i, x, ok, tc.int, tc.string) |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestBackward(t *testing.T) { |
| 106 |
testcases := []struct { |
| 107 |
in string |
| 108 |
out []string |
| 109 |
}{ |
| 110 |
{".", []string{"."}}, |
| 111 |
{"nl.", []string{"nl"}}, |
| 112 |
{"www.miek.nl.", []string{"nl", "miek", "www"}}, |
| 113 |
} |
| 114 |
for _, tc := range testcases { |
| 115 |
t.Run(tc.in, func(t *testing.T) { |
| 116 |
out := []string{} |
| 117 |
for x := range Backward(tc.in) { |
| 118 |
out = append(out, x) |
| 119 |
} |
| 120 |
if slices.Compare(tc.out, out) != 0 { |
| 121 |
t.Fatalf("labels should be %v, got %v", tc.out, out) |
| 122 |
} |
| 123 |
}) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func TestLabels(t *testing.T) { |
| 128 |
testcases := map[string]int{ |
| 129 |
"www.miek.nl.": 3, |
| 130 |
"www.miek.nl": 3, |
| 131 |
"nl": 1, |
| 132 |
".": 0, |
| 133 |
} |
| 134 |
for tc, i := range testcases { |
| 135 |
x := Labels(tc) |
| 136 |
if x != i { |
| 137 |
t.Errorf("labels should have %d, got %d", i, x) |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestCanonical(t *testing.T) { |
| 143 |
for s, expect := range map[string]string{ |
| 144 |
"": ".", |
| 145 |
".": ".", |
| 146 |
"tld": "tld.", |
| 147 |
"tld.": "tld.", |
| 148 |
"example.test": "example.test.", |
| 149 |
"Lower.CASE.test.": "lower.case.test.", |
| 150 |
"*.Test": "*.test.", |
| 151 |
"ÉxamplE.com": "Éxample.com.", |
| 152 |
"É.com": "É.com.", |
| 153 |
} { |
| 154 |
if got := Canonical(s); got != expect { |
| 155 |
t.Errorf("Canonical(%q) = %q, expected %q", s, got, expect) |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestIsName(t *testing.T) { |
| 161 |
testcases := map[string]bool{ |
| 162 |
".": true, |
| 163 |
"..": false, |
| 164 |
"double-dot..test": false, |
| 165 |
".leading-dot.test": false, |
| 166 |
"@.": true, |
| 167 |
"www.example.com": true, |
| 168 |
"www.e%ample.com": true, |
| 169 |
"www.example.com.": true, |
| 170 |
"mi\\k.nl.": true, |
| 171 |
"mi\\k.nl": true, |
| 172 |
"": true, |
| 173 |
} |
| 174 |
for tc, ok := range testcases { |
| 175 |
ok1 := IsName(tc) |
| 176 |
if ok != ok1 { |
| 177 |
t.Errorf("have %t for %s ", ok, tc) |
| 178 |
} |
| 179 |
} |
| 180 |
} |