2
0
mirror of https://github.com/namecoin/ncdns synced 2024-11-16 00:13:01 +00:00
ncdns/util/util_test.go

84 lines
3.0 KiB
Go
Raw Normal View History

2014-11-27 23:59:18 +00:00
package util_test
import "testing"
import "github.com/namecoin/ncdns/util"
2015-09-02 07:49:40 +00:00
import "gopkg.in/hlandau/madns.v1/merr"
2014-11-27 23:59:18 +00:00
type item struct {
2014-12-01 22:57:11 +00:00
input string
expectedHead string
expectedRest string
expectedTail string
expectedTailRest string
2014-11-27 23:59:18 +00:00
}
var items = []item{
2017-11-04 08:01:56 +00:00
{"", "", "", "", ""},
{"a", "a", "", "a", ""},
{"alpha", "alpha", "", "alpha", ""},
{"alpha.beta", "beta", "alpha", "alpha", "beta"},
{"alpha.beta.gamma", "gamma", "alpha.beta", "alpha", "beta.gamma"},
{"alpha.beta.gamma.delta", "delta", "alpha.beta.gamma", "alpha", "beta.gamma.delta"},
{"alpha.beta.gamma.delta.", "delta", "alpha.beta.gamma", "alpha", "beta.gamma.delta."},
2014-11-27 23:59:18 +00:00
}
func TestSplitDomainHead(t *testing.T) {
for i := range items {
2014-12-01 00:47:46 +00:00
head, rest := util.SplitDomainHead(items[i].input)
2014-12-01 22:57:11 +00:00
tail, trest := util.SplitDomainTail(items[i].input)
2014-11-27 23:59:18 +00:00
if head != items[i].expectedHead {
t.Errorf("Input \"%s\": head \"%s\" does not equal expected value \"%s\"", items[i].input, head, items[i].expectedHead)
}
if rest != items[i].expectedRest {
t.Errorf("Input \"%s\": rest \"%s\" does not equal expected value \"%s\"", items[i].input, rest, items[i].expectedRest)
}
2014-12-01 22:57:11 +00:00
if tail != items[i].expectedTail {
t.Errorf("Input \"%s\": tail \"%s\" does not equal expected value \"%s\"", items[i].input, tail, items[i].expectedTail)
}
if trest != items[i].expectedTailRest {
t.Errorf("Input \"%s\": tail rest \"%s\" does not equal expected value \"%s\"", items[i].input, trest, items[i].expectedTailRest)
}
2014-11-27 23:59:18 +00:00
}
}
2014-12-04 12:04:14 +00:00
type aitem struct {
input string
anchor string
expectedSubname string
expectedBasename string
expectedRootname string
expectedError error
}
var aitems = []aitem{
2017-11-04 08:01:56 +00:00
{"", "bit", "", "", "", merr.ErrNotInZone},
{".", "bit", "", "", "", merr.ErrNotInZone},
{"d.", "bit", "", "", "", merr.ErrNotInZone},
{"a.b.c.d.", "bit", "", "", "", merr.ErrNotInZone},
{"a.b.c.d.bit.", "bit", "a.b.c", "d", "bit", nil},
{"d.bit.", "bit", "", "d", "bit", nil},
{"bit.", "bit", "", "", "bit", nil},
{"bit.x.y.z.", "bit", "", "", "bit.x.y.z", nil},
{"d.bit.x.y.z.", "bit", "", "d", "bit.x.y.z", nil},
{"c.d.bit.x.y.z.", "bit", "c", "d", "bit.x.y.z", nil},
{"a.b.c.d.bit.x.y.z.", "bit", "a.b.c", "d", "bit.x.y.z", nil},
2014-12-04 12:04:14 +00:00
}
func TestSplitDomainByFloatingAnchor(t *testing.T) {
for i, it := range aitems {
subname, basename, rootname, err := util.SplitDomainByFloatingAnchor(it.input, it.anchor)
if subname != it.expectedSubname {
t.Errorf("Item %d: subname \"%s\" does not equal expected value \"%s\"", i, subname, it.expectedSubname)
}
if basename != it.expectedBasename {
t.Errorf("Item %d: basename \"%s\" does not equal expected value \"%s\"", i, basename, it.expectedBasename)
}
if rootname != it.expectedRootname {
t.Errorf("Item %d: rootname \"%s\" does not equal expected value \"%s\"", i, basename, it.expectedRootname)
}
if err != it.expectedError {
t.Errorf("Item %d: error \"%s\" does not equal expected error \"%s\"", i, err, it.expectedError)
}
}
}