From df897ca2a81f15de7855969d0103da401c2f1663 Mon Sep 17 00:00:00 2001 From: Hugo Landau Date: Wed, 26 Nov 2014 16:12:18 +0000 Subject: [PATCH] refactoring, coveralls --- .travis.yml | 8 ++ backend/backend.go | 176 ++++----------------------------------- ncdomain/convert.go | 2 +- ncdomain/convert_test.go | 90 ++++++++++---------- 4 files changed, 69 insertions(+), 207 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2fd7aed..a3d20eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,3 +2,11 @@ language: go go: - 1.3 - tip +before_install: + - go get github.com/axw/gocov/gocov + - go get github.com/mattn/goveralls + - go get golang.org/x/tools/cmd/cover +script: + - $HOME/gopath/bin/goveralls -repotoken $COVERALLS_TOKEN +# COVERALLS_TOKEN +secure: "K748GqgZz2I4yHcS49Zi78MWx/xWR79zl1YIGoFxHU4TS6JtGJF9gjqDdSv8DBvBEZRXFMq3n5M+/CtYkIf2jQeQawT6I+PKxHJvpFlpqseWxN/aJRHfVsd0NxbWq6Icx7qzUOvgwKDxMSgXqdndh3reHkyTgkou5XIDlU2zf7g=" diff --git a/backend/backend.go b/backend/backend.go index 98f091b..677423a 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -3,15 +3,13 @@ package backend import "github.com/golang/groupcache/lru" import "github.com/miekg/dns" import "github.com/hlandau/degoutils/log" -import "encoding/json" -import "encoding/base64" -import "encoding/hex" import "fmt" import "strings" import "net" import "github.com/hlandau/ncdns/namecoin" import "github.com/hlandau/madns/merr" import "github.com/hlandau/ncdns/util" +import "github.com/hlandau/ncdns/ncdomain" import "sync" // Provides an abstract zone file for the Namecoin .bit TLD. @@ -70,19 +68,7 @@ func New(cfg *Config) (backend *Backend, err error) { // Keep domains in parsed format. type domain struct { - ncv *ncValue -} - -// Root of a domain JSON structure -type ncValue struct { - IP interface{} `json:"ip"` - IP6 interface{} `json:"ip6"` - Service [][]interface{} `json:"service"` - Alias string `json:"alias"` - NS interface{} `json:"ns"` - Map map[string]*ncValue `json:"map"` // may contain "" and "*" - DS [][]interface{} `json:"ds"` - TXT interface{} `json:"txt"` + ncv *ncdomain.Value } func toNamecoinName(basename string) (string, error) { @@ -141,20 +127,17 @@ func (b *Backend) getNamecoinEntryLL(name string) (*domain, error) { return d, nil } -func jsonToDomain(v string) (dd *domain, err error) { +func jsonToDomain(jsonValue string) (*domain, error) { d := &domain{} - ncv := &ncValue{} - err = json.Unmarshal([]byte(v), ncv) + v, err := ncdomain.ParseValue(jsonValue, nil) if err != nil { - //log.Infoe(err, fmt.Sprintf("cannot unmarshal JSON: %+v", v)) - return + return nil, err } - d.ncv = ncv + d.ncv = v - dd = d - return + return d, nil } type btx struct { @@ -324,7 +307,7 @@ func (tx *btx) doUnderDomain(d *domain) (rrs []dns.RR, err error) { return } -func (tx *btx) addAnswersUnderNCValue(rncv *ncValue, subname string) (rrs []dns.RR, err error) { +func (tx *btx) addAnswersUnderNCValue(rncv *ncdomain.Value, subname string) (rrs []dns.RR, err error) { ncv, sn, err := tx.findNCValue(rncv, subname, nil /*hasNS*/) if err != nil { return @@ -334,17 +317,17 @@ func (tx *btx) addAnswersUnderNCValue(rncv *ncValue, subname string) (rrs []dns. return tx.addAnswersUnderNCValueActual(ncv, sn) } -func hasNS(ncv *ncValue) bool { +/*func hasNS(ncv *ncdomain.Value) bool { nss, err := ncv.GetNSs() return err == nil && len(nss) > 0 -} +}*/ -func (tx *btx) findNCValue(ncv *ncValue, subname string, shortCircuitFunc func(curNCV *ncValue) bool) (xncv *ncValue, sn string, err error) { +func (tx *btx) findNCValue(ncv *ncdomain.Value, subname string, shortCircuitFunc func(curNCV *ncdomain.Value) bool) (xncv *ncdomain.Value, sn string, err error) { return tx._findNCValue(ncv, subname, "", 0, shortCircuitFunc) } -func (tx *btx) _findNCValue(ncv *ncValue, isubname, subname string, depth int, - shortCircuitFunc func(curNCV *ncValue) bool) (xncv *ncValue, sn string, err error) { +func (tx *btx) _findNCValue(ncv *ncdomain.Value, isubname, subname string, depth int, + shortCircuitFunc func(curNCV *ncdomain.Value) bool) (xncv *ncdomain.Value, sn string, err error) { if shortCircuitFunc != nil && shortCircuitFunc(ncv) { return ncv, subname, nil @@ -373,137 +356,8 @@ func (tx *btx) _findNCValue(ncv *ncValue, isubname, subname string, depth int, return ncv, subname, nil } -func (tx *btx) addAnswersUnderNCValueActual(ncv *ncValue, sn string) (rrs []dns.RR, err error) { - rrs = convertAt(nil, dns.Fqdn(tx.qname), ncv) - return -} - -func (ncv *ncValue) getArray(a interface{}) (ips []string, err error) { - if a == nil { - return - } - - ipa, ok := a.([]interface{}) - if ok { - for _, v := range ipa { - s, ok := v.(string) - if ok { - ips = append(ips, s) - } - } - } else { - s, ok := a.(string) - if ok { - ips = []string{s} - } else { - err = fmt.Errorf("malformed IP value") - } - } - return -} - -func (ncv *ncValue) GetIPs() (ips []string, err error) { - return ncv.getArray(ncv.IP) -} - -func (ncv *ncValue) GetIP6s() (ips []string, err error) { - return ncv.getArray(ncv.IP6) -} - -func (ncv *ncValue) GetNSs() (nss []string, err error) { - return ncv.getArray(ncv.NS) -} - -func (ncv *ncValue) getArrayTXT(a interface{}) (txts [][]string, err error) { - if a == nil { - return - } - - if txta, ok := a.([]interface{}); ok { - // ["...", "..."] or [["...","..."], ["...","..."]] - for _, v := range txta { - if sa, ok := v.([]string); ok { - // [["...", "..."], ["...","..."]] - txts = append(txts, sa) - } else if s, ok := v.(string); ok { - // ["...", "..."] - txts = append(txts, segmentizeTXT(s)) - } else { - err = fmt.Errorf("malformed TXT value") - return - } - } - } else { - // "..." - if s, ok := a.(string); ok { - txts = append(txts, segmentizeTXT(s)) - } else { - err = fmt.Errorf("malformed TXT value") - } - } - return -} - -func (ncv *ncValue) GetTXTs() (txts [][]string, err error) { - return ncv.getArrayTXT(ncv.TXT) -} - -func segmentizeTXT(txt string) (a []string) { - for len(txt) > 255 { - a = append(a, txt[0:255]) - txt = txt[255:] - } - a = append(a, txt) - return -} - -func (ncv *ncValue) GetDSs() (dss []dns.DS, err error) { - for _, ds := range ncv.DS { - //log.Info(" - DS: ", ds) - if len(ds) != 4 { - log.Info(" DS is bad len") - continue - } - - a1, ok := ds[0].(float64) - if !ok { - log.Info(" DS[0]") - continue - } - a2, ok := ds[1].(float64) - if !ok { - log.Info(" DS[1]") - continue - } - a3, ok := ds[2].(float64) - if !ok { - log.Info(" DS[2]") - continue - } - a4, ok := ds[3].(string) - if !ok { - log.Info(" DS[3]") - continue - } - - a4b, err := base64.StdEncoding.DecodeString(a4) - if err != nil { - log.Info("can't decode: ", err) - err = nil - continue - } - - a4h := hex.EncodeToString(a4b) - - d := dns.DS{ - Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 60}, - KeyTag: uint16(a1), - Algorithm: uint8(a2), - DigestType: uint8(a3), - Digest: a4h, - } - dss = append(dss, d) - } +func (tx *btx) addAnswersUnderNCValueActual(ncv *ncdomain.Value, sn string) (rrs []dns.RR, err error) { + rrs, err = ncv.RRs(nil, dns.Fqdn(tx.qname)) //convertAt(nil, dns.Fqdn(tx.qname), ncv) return } diff --git a/ncdomain/convert.go b/ncdomain/convert.go index 560a1be..7817f89 100644 --- a/ncdomain/convert.go +++ b/ncdomain/convert.go @@ -89,7 +89,7 @@ func (v *Value) appendNSs(out []dns.RR, suffix string) ([]dns.RR, error) { Class: dns.ClassINET, Ttl: 600, }, - Ns: ns, + Ns: dns.Fqdn(ns), }) } diff --git a/ncdomain/convert_test.go b/ncdomain/convert_test.go index 294950d..55265ff 100644 --- a/ncdomain/convert_test.go +++ b/ncdomain/convert_test.go @@ -1,6 +1,6 @@ package ncdomain_test -import "github.com/hlandau/ncdns/convert" +import "github.com/hlandau/ncdns/ncdomain" import "github.com/miekg/dns" import "testing" import "net" @@ -8,48 +8,48 @@ import "fmt" type item struct { jsonValue string - value *convert.Value + value *ncdomain.Value expectedError error merges map[string]string } var suite = []item{ - item{`{}`, &convert.Value{}, nil, nil}, - item{`{"ip":"1.2.3.4"}`, &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}, nil, nil}, - item{`{"ip":["1.2.3.4"]}`, &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}, nil, nil}, - item{`{"ip":["1.2.3.4","200.200.200.200"]}`, &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4"), net.ParseIP("200.200.200.200")}}, nil, nil}, - item{`{"ip6":"dead:b33f::deca:fbad"}`, &convert.Value{IP6: []net.IP{net.ParseIP("dead:b33f::deca:fbad")}}, nil, nil}, - item{`{"ip6":["dead:b33f::deca:fbad"]}`, &convert.Value{IP6: []net.IP{net.ParseIP("dead:b33f::deca:fbad")}}, nil, nil}, - item{`{"ip6":["dead:b33f::deca:fbad","1234:abcd:5678:bcde:9876:fedc:5432:ba98"]}`, &convert.Value{IP6: []net.IP{net.ParseIP("dead:b33f::deca:fbad"), net.ParseIP("1234:abcd:5678:bcde:9876:fedc:5432:ba98")}}, nil, nil}, - item{`{"ns":"alpha.beta.gamma.delta"}`, &convert.Value{NS: []string{"alpha.beta.gamma.delta"}}, nil, nil}, - item{`{"ns":["alpha.beta.gamma.delta"]}`, &convert.Value{NS: []string{"alpha.beta.gamma.delta"}}, nil, nil}, - item{`{"ns":["alpha.beta.gamma.delta","delta.gamma.beta.alpha"]}`, &convert.Value{NS: []string{"alpha.beta.gamma.delta", "delta.gamma.beta.alpha"}}, nil, nil}, - item{`{"mx":[[10,"alpha.beta.gamma.delta"]]}`, &convert.Value{MX: []dns.MX{dns.MX{Hdr: dns.RR_Header{Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: 600}, Preference: 10, Mx: "alpha.beta.gamma.delta"}}}, nil, nil}, - item{`{"mx":[[10,"alpha.beta.gamma.delta"],[20,"epsilon.example"]]}`, &convert.Value{MX: []dns.MX{dns.MX{Hdr: dns.RR_Header{Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: 600}, Preference: 10, Mx: "alpha.beta.gamma.delta"}, dns.MX{Hdr: dns.RR_Header{Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: 600}, Preference: 20, Mx: "epsilon.example"}}}, nil, nil}, - item{`{"alias":"alpha.beta.gamma.delta"}`, &convert.Value{Alias: "alpha.beta.gamma.delta"}, nil, nil}, - item{`{"translate":"alpha.beta.gamma.delta"}`, &convert.Value{Translate: "alpha.beta.gamma.delta"}, nil, nil}, - item{`{"txt":"text record"}`, &convert.Value{TXT: [][]string{[]string{"text record"}}}, nil, nil}, - item{`{"txt":"[text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record]"}`, &convert.Value{TXT: [][]string{[]string{"[text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record]", "[text ... record]"}}}, nil, nil}, - item{`{"txt":["text record"]}`, &convert.Value{TXT: [][]string{[]string{"text record"}}}, nil, nil}, - item{`{"txt":["text record","text record 2"]}`, &convert.Value{TXT: [][]string{[]string{"text record"}, []string{"text record 2"}}}, nil, nil}, - item{`{"txt":[["text", "record"]]}`, &convert.Value{TXT: [][]string{[]string{"text", "record"}}}, nil, nil}, - item{`{"txt":[["text", "record"],["text", "record", "2"]]}`, &convert.Value{TXT: [][]string{[]string{"text", "record"}, []string{"text", "record", "2"}}}, nil, nil}, - item{`{"service":[ ["http","tcp",1,2,80,"alpha.beta.gamma.delta"] ]}`, &convert.Value{Service: []dns.SRV{dns.SRV{Hdr: dns.RR_Header{Name: "_http._tcp", Ttl: 600, Rrtype: dns.TypeSRV, Class: dns.ClassINET}, Priority: 1, Weight: 2, Port: 80, Target: "alpha.beta.gamma.delta"}}}, nil, nil}, - item{`{"service":[ ["http","tcp",1,2,80,"alpha.beta.gamma.delta"], ["https","tcp",1,2,443,"alpha.beta.gamma.delta"] ]}`, &convert.Value{Service: []dns.SRV{dns.SRV{Hdr: dns.RR_Header{Name: "_http._tcp", Ttl: 600, Rrtype: dns.TypeSRV, Class: dns.ClassINET}, Priority: 1, Weight: 2, Port: 80, Target: "alpha.beta.gamma.delta"}, dns.SRV{Hdr: dns.RR_Header{Name: "_https._tcp", Ttl: 600, Rrtype: dns.TypeSRV, Class: dns.ClassINET}, Priority: 1, Weight: 2, Port: 443, Target: "alpha.beta.gamma.delta"}}}, nil, nil}, - item{`{"map":{ "": { } }}`, &convert.Value{}, nil, nil}, - item{`{"map":{ "": { "ip": "1.2.3.4" } }}`, &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}, nil, nil}, - item{`{"map":{ "www": { "ip": "1.2.3.4" } }}`, &convert.Value{Map: map[string]*convert.Value{"www": &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}}}, nil, nil}, - item{`{"map":{ "": "1.2.3.4" }}`, &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}, nil, nil}, - item{`{"map":{ "www": "1.2.3.4" }}`, &convert.Value{Map: map[string]*convert.Value{"www": &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}}}, nil, nil}, - item{`{"ds":[[12345,8,2,"4tPJFvbe6scylOgmj7WIUESoM/xUWViPSpGEz8QaV2Y="]]}`, &convert.Value{DS: []dns.DS{dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 12345, Algorithm: 8, DigestType: 2, Digest: "e2d3c916f6deeac73294e8268fb5885044a833fc5459588f4a9184cfc41a5766"}}}, nil, nil}, - item{`{"ds":[[54321,8,1,"5sFxbPtr3IToTOGrVRDaxpFztbI="],[12345,8,2,"4tPJFvbe6scylOgmj7WIUESoM/xUWViPSpGEz8QaV2Y="]]}`, &convert.Value{DS: []dns.DS{dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 54321, Algorithm: 8, DigestType: 1, Digest: "e6c1716cfb6bdc84e84ce1ab5510dac69173b5b2"}, dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 12345, Algorithm: 8, DigestType: 2, Digest: "e2d3c916f6deeac73294e8268fb5885044a833fc5459588f4a9184cfc41a5766"}}}, nil, nil}, - item{`{"email":"hostmaster@example.com"}`, &convert.Value{Hostmaster: "hostmaster@example.com"}, nil, nil}, - item{`{"ip":["1.2.3.4"],"import":"d/example"}`, &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}, IP6: []net.IP{net.ParseIP("::beef")}}, nil, map[string]string{"d/example": `{"ip6":["::beef"]}`}}, - item{`{"ip":["1.2.3.4"],"import":"d/example"}`, &convert.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}, IP6: []net.IP{net.ParseIP("::beef")}}, nil, map[string]string{"d/example": `{"ip":["2.3.4.5"],"ip6":["::beef"]}`}}, - item{`{"ns":["alpha.beta"],"import":"d/example"}`, &convert.Value{NS: []string{"alpha.beta"}, IP6: []net.IP{net.ParseIP("::beef")}}, nil, map[string]string{"d/example": `{"ns":["gamma.delta"],"ip6":["::beef"]}`}}, - item{`{"ds":[[12345,8,2,"4tPJFvbe6scylOgmj7WIUESoM/xUWViPSpGEz8QaV2Y="]],"import":"d/example"}`, &convert.Value{DS: []dns.DS{dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 12345, Algorithm: 8, DigestType: 2, Digest: "e2d3c916f6deeac73294e8268fb5885044a833fc5459588f4a9184cfc41a5766"}}}, nil, map[string]string{"d/example": `{"ds":[ [54321,8,1,"5sFxbPtr3IToTOGrVRDaxpFztbI="] ]}`}}, - item{`{"import":"d/example"}`, &convert.Value{DS: []dns.DS{dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 54321, Algorithm: 8, DigestType: 1, Digest: "e6c1716cfb6bdc84e84ce1ab5510dac69173b5b2"}}}, nil, map[string]string{"d/example": `{"ds":[ [54321,8,1,"5sFxbPtr3IToTOGrVRDaxpFztbI="] ]}`}}, - item{`{"ip":["1.2.3.4"],"delegate":"d/example"}`, &convert.Value{IP6: []net.IP{net.ParseIP("::beef")}}, nil, map[string]string{"d/example": `{"ip6":["::beef"]}`}}, + item{`{}`, &ncdomain.Value{}, nil, nil}, + item{`{"ip":"1.2.3.4"}`, &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}, nil, nil}, + item{`{"ip":["1.2.3.4"]}`, &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}, nil, nil}, + item{`{"ip":["1.2.3.4","200.200.200.200"]}`, &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4"), net.ParseIP("200.200.200.200")}}, nil, nil}, + item{`{"ip6":"dead:b33f::deca:fbad"}`, &ncdomain.Value{IP6: []net.IP{net.ParseIP("dead:b33f::deca:fbad")}}, nil, nil}, + item{`{"ip6":["dead:b33f::deca:fbad"]}`, &ncdomain.Value{IP6: []net.IP{net.ParseIP("dead:b33f::deca:fbad")}}, nil, nil}, + item{`{"ip6":["dead:b33f::deca:fbad","1234:abcd:5678:bcde:9876:fedc:5432:ba98"]}`, &ncdomain.Value{IP6: []net.IP{net.ParseIP("dead:b33f::deca:fbad"), net.ParseIP("1234:abcd:5678:bcde:9876:fedc:5432:ba98")}}, nil, nil}, + item{`{"ns":"alpha.beta.gamma.delta"}`, &ncdomain.Value{NS: []string{"alpha.beta.gamma.delta"}}, nil, nil}, + item{`{"ns":["alpha.beta.gamma.delta"]}`, &ncdomain.Value{NS: []string{"alpha.beta.gamma.delta"}}, nil, nil}, + item{`{"ns":["alpha.beta.gamma.delta","delta.gamma.beta.alpha"]}`, &ncdomain.Value{NS: []string{"alpha.beta.gamma.delta", "delta.gamma.beta.alpha"}}, nil, nil}, + item{`{"mx":[[10,"alpha.beta.gamma.delta"]]}`, &ncdomain.Value{MX: []*dns.MX{&dns.MX{Hdr: dns.RR_Header{Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: 600}, Preference: 10, Mx: "alpha.beta.gamma.delta"}}}, nil, nil}, + item{`{"mx":[[10,"alpha.beta.gamma.delta"],[20,"epsilon.example"]]}`, &ncdomain.Value{MX: []*dns.MX{&dns.MX{Hdr: dns.RR_Header{Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: 600}, Preference: 10, Mx: "alpha.beta.gamma.delta"}, &dns.MX{Hdr: dns.RR_Header{Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: 600}, Preference: 20, Mx: "epsilon.example"}}}, nil, nil}, + item{`{"alias":"alpha.beta.gamma.delta"}`, &ncdomain.Value{Alias: "alpha.beta.gamma.delta"}, nil, nil}, + item{`{"translate":"alpha.beta.gamma.delta"}`, &ncdomain.Value{Translate: "alpha.beta.gamma.delta"}, nil, nil}, + item{`{"txt":"text record"}`, &ncdomain.Value{TXT: [][]string{[]string{"text record"}}}, nil, nil}, + item{`{"txt":"[text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record]"}`, &ncdomain.Value{TXT: [][]string{[]string{"[text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record][text ... record]", "[text ... record]"}}}, nil, nil}, + item{`{"txt":["text record"]}`, &ncdomain.Value{TXT: [][]string{[]string{"text record"}}}, nil, nil}, + item{`{"txt":["text record","text record 2"]}`, &ncdomain.Value{TXT: [][]string{[]string{"text record"}, []string{"text record 2"}}}, nil, nil}, + item{`{"txt":[["text", "record"]]}`, &ncdomain.Value{TXT: [][]string{[]string{"text", "record"}}}, nil, nil}, + item{`{"txt":[["text", "record"],["text", "record", "2"]]}`, &ncdomain.Value{TXT: [][]string{[]string{"text", "record"}, []string{"text", "record", "2"}}}, nil, nil}, + item{`{"service":[ ["http","tcp",1,2,80,"alpha.beta.gamma.delta"] ]}`, &ncdomain.Value{Service: []*dns.SRV{&dns.SRV{Hdr: dns.RR_Header{Name: "_http._tcp", Ttl: 600, Rrtype: dns.TypeSRV, Class: dns.ClassINET}, Priority: 1, Weight: 2, Port: 80, Target: "alpha.beta.gamma.delta"}}}, nil, nil}, + item{`{"service":[ ["http","tcp",1,2,80,"alpha.beta.gamma.delta"], ["https","tcp",1,2,443,"alpha.beta.gamma.delta"] ]}`, &ncdomain.Value{Service: []*dns.SRV{&dns.SRV{Hdr: dns.RR_Header{Name: "_http._tcp", Ttl: 600, Rrtype: dns.TypeSRV, Class: dns.ClassINET}, Priority: 1, Weight: 2, Port: 80, Target: "alpha.beta.gamma.delta"}, &dns.SRV{Hdr: dns.RR_Header{Name: "_https._tcp", Ttl: 600, Rrtype: dns.TypeSRV, Class: dns.ClassINET}, Priority: 1, Weight: 2, Port: 443, Target: "alpha.beta.gamma.delta"}}}, nil, nil}, + item{`{"map":{ "": { } }}`, &ncdomain.Value{}, nil, nil}, + item{`{"map":{ "": { "ip": "1.2.3.4" } }}`, &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}, nil, nil}, + item{`{"map":{ "www": { "ip": "1.2.3.4" } }}`, &ncdomain.Value{Map: map[string]*ncdomain.Value{"www": &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}}}, nil, nil}, + item{`{"map":{ "": "1.2.3.4" }}`, &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}, nil, nil}, + item{`{"map":{ "www": "1.2.3.4" }}`, &ncdomain.Value{Map: map[string]*ncdomain.Value{"www": &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}}}}, nil, nil}, + item{`{"ds":[[12345,8,2,"4tPJFvbe6scylOgmj7WIUESoM/xUWViPSpGEz8QaV2Y="]]}`, &ncdomain.Value{DS: []*dns.DS{&dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 12345, Algorithm: 8, DigestType: 2, Digest: "e2d3c916f6deeac73294e8268fb5885044a833fc5459588f4a9184cfc41a5766"}}}, nil, nil}, + item{`{"ds":[[54321,8,1,"5sFxbPtr3IToTOGrVRDaxpFztbI="],[12345,8,2,"4tPJFvbe6scylOgmj7WIUESoM/xUWViPSpGEz8QaV2Y="]]}`, &ncdomain.Value{DS: []*dns.DS{&dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 54321, Algorithm: 8, DigestType: 1, Digest: "e6c1716cfb6bdc84e84ce1ab5510dac69173b5b2"}, &dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 12345, Algorithm: 8, DigestType: 2, Digest: "e2d3c916f6deeac73294e8268fb5885044a833fc5459588f4a9184cfc41a5766"}}}, nil, nil}, + item{`{"email":"hostmaster@example.com"}`, &ncdomain.Value{Hostmaster: "hostmaster@example.com"}, nil, nil}, + item{`{"ip":["1.2.3.4"],"import":"d/example"}`, &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}, IP6: []net.IP{net.ParseIP("::beef")}}, nil, map[string]string{"d/example": `{"ip6":["::beef"]}`}}, + item{`{"ip":["1.2.3.4"],"import":"d/example"}`, &ncdomain.Value{IP: []net.IP{net.ParseIP("1.2.3.4")}, IP6: []net.IP{net.ParseIP("::beef")}}, nil, map[string]string{"d/example": `{"ip":["2.3.4.5"],"ip6":["::beef"]}`}}, + item{`{"ns":["alpha.beta"],"import":"d/example"}`, &ncdomain.Value{NS: []string{"alpha.beta"}, IP6: []net.IP{net.ParseIP("::beef")}}, nil, map[string]string{"d/example": `{"ns":["gamma.delta"],"ip6":["::beef"]}`}}, + item{`{"ds":[[12345,8,2,"4tPJFvbe6scylOgmj7WIUESoM/xUWViPSpGEz8QaV2Y="]],"import":"d/example"}`, &ncdomain.Value{DS: []*dns.DS{&dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 12345, Algorithm: 8, DigestType: 2, Digest: "e2d3c916f6deeac73294e8268fb5885044a833fc5459588f4a9184cfc41a5766"}}}, nil, map[string]string{"d/example": `{"ds":[ [54321,8,1,"5sFxbPtr3IToTOGrVRDaxpFztbI="] ]}`}}, + item{`{"import":"d/example"}`, &ncdomain.Value{DS: []*dns.DS{&dns.DS{Hdr: dns.RR_Header{Rrtype: dns.TypeDS, Class: dns.ClassINET, Ttl: 600}, KeyTag: 54321, Algorithm: 8, DigestType: 1, Digest: "e6c1716cfb6bdc84e84ce1ab5510dac69173b5b2"}}}, nil, map[string]string{"d/example": `{"ds":[ [54321,8,1,"5sFxbPtr3IToTOGrVRDaxpFztbI="] ]}`}}, + item{`{"ip":["1.2.3.4"],"delegate":"d/example"}`, &ncdomain.Value{IP6: []net.IP{net.ParseIP("::beef")}}, nil, map[string]string{"d/example": `{"ip6":["::beef"]}`}}, } func TestConversion(t *testing.T) { @@ -65,7 +65,7 @@ func TestConversion(t *testing.T) { return "", fmt.Errorf("not found") } } - v, err := convert.ParseValue(item.jsonValue, resolve) + v, err := ncdomain.ParseValue(item.jsonValue, resolve) if err != item.expectedError { t.Errorf("Item %d did not match expected error: got %+v but expected %+v", i, err, item.expectedError) } @@ -77,7 +77,7 @@ func TestConversion(t *testing.T) { // utility functions for testing equality -func equals(v1 *convert.Value, v2 *convert.Value) bool { +func equals(v1 *ncdomain.Value, v2 *ncdomain.Value) bool { return (v1 != nil) == (v2 != nil) && eqIPArray(v1.IP, v2.IP) && eqIPArray(v1.IP6, v2.IP6) && @@ -113,7 +113,7 @@ func eqStringArray(a []string, b []string) bool { return true } -func eqDSArray(a []dns.DS, b []dns.DS) bool { +func eqDSArray(a []*dns.DS, b []*dns.DS) bool { if len(a) != len(b) { return false } @@ -125,7 +125,7 @@ func eqDSArray(a []dns.DS, b []dns.DS) bool { return true } -func eqDS(a dns.DS, b dns.DS) bool { +func eqDS(a *dns.DS, b *dns.DS) bool { return a.KeyTag == b.KeyTag && a.Algorithm == b.Algorithm && a.DigestType == b.DigestType && a.Digest == b.Digest && eqHdr(a.Hdr, b.Hdr) } @@ -146,7 +146,7 @@ func eqStringArrayArray(a [][]string, b [][]string) bool { return true } -func eqServiceArray(a []dns.SRV, b []dns.SRV) bool { +func eqServiceArray(a []*dns.SRV, b []*dns.SRV) bool { if len(a) != len(b) { return false } @@ -158,12 +158,12 @@ func eqServiceArray(a []dns.SRV, b []dns.SRV) bool { return true } -func eqService(a dns.SRV, b dns.SRV) bool { +func eqService(a *dns.SRV, b *dns.SRV) bool { return a.Priority == b.Priority && a.Weight == b.Weight && a.Port == b.Port && a.Target == b.Target && eqHdr(a.Hdr, b.Hdr) } -func eqValueMap(a *convert.Value, b *convert.Value) bool { +func eqValueMap(a *ncdomain.Value, b *ncdomain.Value) bool { if len(a.Map) != len(b.Map) { return false }