Add canonical sorting
Add a low-allocation canonical sorting function that can be used in middleware. This implements the tests from rfc4034 correctly.
This commit is contained in:
parent
1a455f7bc4
commit
162dca6eeb
2 changed files with 129 additions and 0 deletions
72
middleware/canonical_test.go
Normal file
72
middleware/canonical_test.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type set []string
|
||||
|
||||
func (p set) Len() int { return len(p) }
|
||||
func (p set) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p set) Less(i, j int) bool { d := Less(p[i], p[j]); return d <= 0 }
|
||||
|
||||
func TestLess(t *testing.T) {
|
||||
tests := []struct {
|
||||
in []string
|
||||
out []string
|
||||
}{
|
||||
{
|
||||
[]string{"aaa.powerdns.de", "bbb.powerdns.net.", "xxx.powerdns.com."},
|
||||
[]string{"xxx.powerdns.com.", "aaa.powerdns.de", "bbb.powerdns.net."},
|
||||
},
|
||||
{
|
||||
[]string{"aaa.POWERDNS.de", "bbb.PoweRdnS.net.", "xxx.powerdns.com."},
|
||||
[]string{"xxx.powerdns.com.", "aaa.POWERDNS.de", "bbb.PoweRdnS.net."},
|
||||
},
|
||||
{
|
||||
[]string{"aaa.aaaa.aa.", "aa.aaa.a.", "bbb.bbbb.bb."},
|
||||
[]string{"aa.aaa.a.", "aaa.aaaa.aa.", "bbb.bbbb.bb."},
|
||||
},
|
||||
{
|
||||
[]string{"aaaaa.", "aaa.", "bbb."},
|
||||
[]string{"aaa.", "aaaaa.", "bbb."},
|
||||
},
|
||||
{
|
||||
[]string{"a.a.a.a.", "a.a.", "a.a.a."},
|
||||
[]string{"a.a.", "a.a.a.", "a.a.a.a."},
|
||||
},
|
||||
{
|
||||
[]string{"example.", "z.example.", "a.example."},
|
||||
[]string{"example.", "a.example.", "z.example."},
|
||||
},
|
||||
{
|
||||
[]string{"a.example.", "Z.a.example.", "z.example.", "yljkjljk.a.example.", "\\001.z.example.", "example.", "*.z.example.", "\\200.z.example.", "zABC.a.EXAMPLE."},
|
||||
[]string{"example.", "a.example.", "yljkjljk.a.example.", "Z.a.example.", "zABC.a.EXAMPLE.", "z.example.", "\\001.z.example.", "*.z.example.", "\\200.z.example."},
|
||||
},
|
||||
{
|
||||
// RFC3034 example.
|
||||
[]string{"a.example.", "Z.a.example.", "z.example.", "yljkjljk.a.example.", "example.", "*.z.example.", "zABC.a.EXAMPLE."},
|
||||
[]string{"example.", "a.example.", "yljkjljk.a.example.", "Z.a.example.", "zABC.a.EXAMPLE.", "z.example.", "*.z.example."},
|
||||
},
|
||||
}
|
||||
|
||||
Tests:
|
||||
for j, test := range tests {
|
||||
sort.Sort(set(test.in))
|
||||
for i := 0; i < len(test.in); i++ {
|
||||
if test.in[i] != test.out[i] {
|
||||
t.Errorf("test %d: expected %s, got %s\n", j, test.out[i], test.in[i])
|
||||
n := ""
|
||||
for k, in := range test.in {
|
||||
if k+1 == len(test.in) {
|
||||
n = "\n"
|
||||
}
|
||||
t.Logf("%s <-> %s\n%s", in, test.out[k], n)
|
||||
}
|
||||
continue Tests
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue