1package dns 2 3import "testing" 4 5func TestCompareDomainName(t *testing.T) { 6 s1 := "www.miek.nl." 7 s2 := "miek.nl." 8 s3 := "www.bla.nl." 9 s4 := "nl.www.bla." 10 s5 := "nl." 11 s6 := "miek.nl." 12 13 if CompareDomainName(s1, s2) != 2 { 14 t.Errorf("%s with %s should be %d", s1, s2, 2) 15 } 16 if CompareDomainName(s1, s3) != 1 { 17 t.Errorf("%s with %s should be %d", s1, s3, 1) 18 } 19 if CompareDomainName(s3, s4) != 0 { 20 t.Errorf("%s with %s should be %d", s3, s4, 0) 21 } 22 // Non qualified tests 23 if CompareDomainName(s1, s5) != 1 { 24 t.Errorf("%s with %s should be %d", s1, s5, 1) 25 } 26 if CompareDomainName(s1, s6) != 2 { 27 t.Errorf("%s with %s should be %d", s1, s5, 2) 28 } 29 30 if CompareDomainName(s1, ".") != 0 { 31 t.Errorf("%s with %s should be %d", s1, s5, 0) 32 } 33 if CompareDomainName(".", ".") != 0 { 34 t.Errorf("%s with %s should be %d", ".", ".", 0) 35 } 36 if CompareDomainName("test.com.", "TEST.COM.") != 2 { 37 t.Errorf("test.com. and TEST.COM. should be an exact match") 38 } 39} 40 41func TestSplit(t *testing.T) { 42 splitter := map[string]int{ 43 "www.miek.nl.": 3, 44 "www.miek.nl": 3, 45 "www..miek.nl": 4, 46 `www\.miek.nl.`: 2, 47 `www\\.miek.nl.`: 3, 48 ".": 0, 49 "nl.": 1, 50 "nl": 1, 51 "com.": 1, 52 ".com.": 2, 53 } 54 for s, i := range splitter { 55 if x := len(Split(s)); x != i { 56 t.Errorf("labels should be %d, got %d: %s %v", i, x, s, Split(s)) 57 } 58 } 59} 60 61func TestSplit2(t *testing.T) { 62 splitter := map[string][]int{ 63 "www.miek.nl.": {0, 4, 9}, 64 "www.miek.nl": {0, 4, 9}, 65 "nl": {0}, 66 } 67 for s, i := range splitter { 68 x := Split(s) 69 switch len(i) { 70 case 1: 71 if x[0] != i[0] { 72 t.Errorf("labels should be %v, got %v: %s", i, x, s) 73 } 74 default: 75 if x[0] != i[0] || x[1] != i[1] || x[2] != i[2] { 76 t.Errorf("labels should be %v, got %v: %s", i, x, s) 77 } 78 } 79 } 80} 81 82func TestPrevLabel(t *testing.T) { 83 type prev struct { 84 string 85 int 86 } 87 prever := map[prev]int{ 88 {"www.miek.nl.", 0}: 12, 89 {"www.miek.nl.", 1}: 9, 90 {"www.miek.nl.", 2}: 4, 91 92 {"www.miek.nl", 0}: 11, 93 {"www.miek.nl", 1}: 9, 94 {"www.miek.nl", 2}: 4, 95 96 {"www.miek.nl.", 5}: 0, 97 {"www.miek.nl", 5}: 0, 98 99 {"www.miek.nl.", 3}: 0, 100 {"www.miek.nl", 3}: 0, 101 } 102 for s, i := range prever { 103 x, ok := PrevLabel(s.string, s.int) 104 if i != x { 105 t.Errorf("label should be %d, got %d, %t: preving %d, %s", i, x, ok, s.int, s.string) 106 } 107 } 108} 109 110func TestCountLabel(t *testing.T) { 111 splitter := map[string]int{ 112 "www.miek.nl.": 3, 113 "www.miek.nl": 3, 114 "nl": 1, 115 ".": 0, 116 } 117 for s, i := range splitter { 118 x := CountLabel(s) 119 if x != i { 120 t.Errorf("CountLabel should have %d, got %d", i, x) 121 } 122 } 123} 124 125func TestSplitDomainName(t *testing.T) { 126 labels := map[string][]string{ 127 "miek.nl": {"miek", "nl"}, 128 ".": nil, 129 "www.miek.nl.": {"www", "miek", "nl"}, 130 "www.miek.nl": {"www", "miek", "nl"}, 131 "www..miek.nl": {"www", "", "miek", "nl"}, 132 `www\.miek.nl`: {`www\.miek`, "nl"}, 133 `www\\.miek.nl`: {`www\\`, "miek", "nl"}, 134 ".www.miek.nl.": {"", "www", "miek", "nl"}, 135 } 136domainLoop: 137 for domain, splits := range labels { 138 parts := SplitDomainName(domain) 139 if len(parts) != len(splits) { 140 t.Errorf("SplitDomainName returned %v for %s, expected %v", parts, domain, splits) 141 continue domainLoop 142 } 143 for i := range parts { 144 if parts[i] != splits[i] { 145 t.Errorf("SplitDomainName returned %v for %s, expected %v", parts, domain, splits) 146 continue domainLoop 147 } 148 } 149 } 150} 151 152func TestIsDomainName(t *testing.T) { 153 type ret struct { 154 ok bool 155 lab int 156 } 157 names := map[string]*ret{ 158 "..": {false, 1}, 159 "@.": {true, 1}, 160 "www.example.com": {true, 3}, 161 "www.e%ample.com": {true, 3}, 162 "www.example.com.": {true, 3}, 163 "mi\\k.nl.": {true, 2}, 164 "mi\\k.nl": {true, 2}, 165 } 166 for d, ok := range names { 167 l, k := IsDomainName(d) 168 if ok.ok != k || ok.lab != l { 169 t.Errorf(" got %v %d for %s ", k, l, d) 170 t.Errorf("have %v %d for %s ", ok.ok, ok.lab, d) 171 } 172 } 173} 174 175func BenchmarkSplitLabels(b *testing.B) { 176 for i := 0; i < b.N; i++ { 177 Split("www.example.com.") 178 } 179} 180 181func BenchmarkLenLabels(b *testing.B) { 182 for i := 0; i < b.N; i++ { 183 CountLabel("www.example.com.") 184 } 185} 186 187func BenchmarkCompareDomainName(b *testing.B) { 188 b.ReportAllocs() 189 for i := 0; i < b.N; i++ { 190 CompareDomainName("www.example.com.", "aa.example.com.") 191 } 192} 193 194func BenchmarkIsSubDomain(b *testing.B) { 195 b.ReportAllocs() 196 for i := 0; i < b.N; i++ { 197 IsSubDomain("www.example.com.", "aa.example.com.") 198 IsSubDomain("example.com.", "aa.example.com.") 199 IsSubDomain("miek.nl.", "aa.example.com.") 200 } 201} 202