Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mimuret committed Jul 13, 2021
1 parent d331fa5 commit c6caf3d
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 70 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
all:
go mod tidy
go test -coverprofile=cover.out ./...
go tool cover -html=cover.out -o cover.html
go fmt
16 changes: 6 additions & 10 deletions ddns/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,9 @@ func (d *DDNS) PrerequisiteProessing(z dnsutils.ZoneInterface, msg *dns.Msg) int
if _, ok := z.GetRootNode().GetNameNode(rr.Header().Name); !ok {
return dns.RcodeNXRrset
}
nn, ok := tempNode.GetNameNode(rr.Header().Name)
if !ok {
nn = dnsutils.NewNameNode(rr.Header().Name, z.GetClass())
set := dnsutils.NewRRSet(rr.Header().Name, rr.Header().Ttl, z.GetClass(), rr.Header().Rrtype, nil)
if err := nn.SetRRSet(set); err != nil {
return dns.RcodeServerFailure
}
if err := tempNode.SetNameNode(nn); err != nil {
return dns.RcodeServerFailure
}
nn, err := dnsutils.GetNameNodeOrCreate(tempNode, rr.Header().Name)
if err != nil {
return dns.RcodeServerFailure
}
set := dnsutils.GetRRSetOrCreate(nn, rr.Header().Rrtype, rr.Header().Ttl)
if err := set.AddRR(rr); err != nil {
Expand All @@ -206,6 +199,9 @@ func (d *DDNS) PrerequisiteProessing(z dnsutils.ZoneInterface, msg *dns.Msg) int
if err := nn.SetRRSet(set); err != nil {
return dns.RcodeServerFailure
}
if err := tempNode.SetNameNode(nn); err != nil {
return dns.RcodeServerFailure
}
} else {
return dns.RcodeFormatError
}
Expand Down
42 changes: 28 additions & 14 deletions ddns/ddns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ var _ = Describe("DDNS", func() {
panic(err)
}
})
Context("NewDDNS", func() {
When("UpdateInterface is nil", func() {
It("returns nil", func() {
Expect(ddns.NewDDNS(nil)).To(BeNil())
})
})
})
Context("Test for DDNS.CheckZoneSection", func() {
It("can not request multiple zone section records", func() {
msg.Question = append(msg.Question, dns.Question{Name: "example.jp.", Qtype: dns.TypeSOA, Qclass: dns.ClassINET})
Expand Down Expand Up @@ -280,14 +287,14 @@ var _ = Describe("DDNS", func() {
})
Context("Test for DDNS.UpdatePrescan", func() {
When("request rtype is not supported", func() {
It("return rcode formerror", func() {
It("returns rcode formerror", func() {
msg.Insert([]dns.RR{MustNewRR("example.jp. 3600 IN DNSKEY 256 3 8 AwEAAb6AguVDdiFFs84nDYA6sIXMG3E0Y6QJm98IUH60hfoltGvvkFh9 QMWG2wrqYUhUWvWYXW9gXfeWRCgay/FgrnKjcvAErFmv3dPT81E8jEQc Q7uUlpoIxs/8oVGG1jY1qZJINxwWsF0vm3xx6fnGSwelOCKoRuawo4U4 +TWiO9wf")})
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeNotImplemented))
})
})
When("request name is not zone domain name", func() {
It("return rcode NotZone", func() {
It("returns rcode NotZone", func() {
msg.Ns = []dns.RR{MustNewRR("example2.jp. 0 IN A 172.16.0.1")}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeNotZone))
Expand All @@ -298,7 +305,7 @@ var _ = Describe("DDNS", func() {
})
When("request is Add To An RRset (rfc2136 2.5.1)", func() {
When("request rtype is invalid", func() {
It("return rcode formerror", func() {
It("returns rcode formerror", func() {
msg.Ns = []dns.RR{&dns.ANY{Hdr: dns.RR_Header{Name: "example.jp.", Ttl: 0, Rrtype: dns.TypeAXFR, Class: dns.ClassINET, Rdlength: 0}}}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeFormatError))
Expand All @@ -311,7 +318,7 @@ var _ = Describe("DDNS", func() {
})
})
When("request is normal query", func() {
It("return rcode NoError", func() {
It("returns rcode NoError", func() {
msg.Ns = []dns.RR{MustNewRR("example.jp. 3600 IN A 192.168.0.1")}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeSuccess))
Expand All @@ -320,30 +327,37 @@ var _ = Describe("DDNS", func() {
})
When("request is Delete An RRset (rfc2136 2.5.2)", func() {
When("request ttl is not zero", func() {
It("return rcode formerror", func() {
It("returns rcode formerror", func() {
msg.Ns = []dns.RR{&dns.ANY{Hdr: dns.RR_Header{Name: "example.jp.", Ttl: 30, Rrtype: dns.TypeANY, Class: dns.ClassANY, Rdlength: 0}}}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeFormatError))
})
})
When("request Rdlength is not zero", func() {
It("return rcode formerror", func() {
It("returns rcode formerror", func() {
msg.Ns = []dns.RR{&dns.ANY{Hdr: dns.RR_Header{Name: "example.jp.", Ttl: 0, Rrtype: dns.TypeANY, Class: dns.ClassANY, Rdlength: 1}}}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeFormatError))
})
})
When("request is normal query", func() {
It("return rcode NoError", func() {
When("request is normal query zone apex", func() {
It("returns rcode NoError", func() {
msg.RemoveName([]dns.RR{MustNewRR("example.jp. 3600 IN A 192.168.0.1")})
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeSuccess))
})
})
When("request is normal query", func() {
It("returns rcode NoError", func() {
msg.RemoveName([]dns.RR{MustNewRR("mail.example.jp 3600 IN A 192.168.0.1")})
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeSuccess))
})
})
})
When("request is Delete All RRsets From A Name (rfc2136 2.5.3)", func() {
When("request rtype is invalid", func() {
It("return rcode formerror", func() {
It("returns rcode formerror", func() {
msg.Ns = []dns.RR{&dns.ANY{Hdr: dns.RR_Header{Name: "example.jp.", Ttl: 0, Rrtype: dns.TypeAXFR, Class: dns.ClassANY, Rdlength: 0}}}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeFormatError))
Expand All @@ -356,14 +370,14 @@ var _ = Describe("DDNS", func() {
})
})
When("request Rdlength is not zero", func() {
It("return rcode formerror", func() {
It("returns rcode formerror", func() {
msg.Ns = []dns.RR{&dns.ANY{Hdr: dns.RR_Header{Name: "example.jp.", Ttl: 0, Rrtype: dns.TypeA, Class: dns.ClassANY, Rdlength: 1}}}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeFormatError))
})
})
When("request is normal query", func() {
It("return rcode NoError", func() {
It("returns rcode NoError", func() {
msg.RemoveRRset([]dns.RR{MustNewRR("example.jp. 3600 IN A 192.168.0.1")})
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeSuccess))
Expand All @@ -372,7 +386,7 @@ var _ = Describe("DDNS", func() {
})
When("request is Delete An RR From An RRset (rfc2136 2.5.4)", func() {
When("request rtype is invalid", func() {
It("return rcode formerror", func() {
It("returns rcode formerror", func() {
msg.Ns = []dns.RR{&dns.ANY{Hdr: dns.RR_Header{Name: "example.jp.", Ttl: 0, Rrtype: dns.TypeAXFR, Class: dns.ClassNONE, Rdlength: 0}}}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeFormatError))
Expand All @@ -385,14 +399,14 @@ var _ = Describe("DDNS", func() {
})
})
When("request ttl is not zero", func() {
It("return rcode formerror", func() {
It("returns rcode formerror", func() {
msg.Ns = []dns.RR{&dns.ANY{Hdr: dns.RR_Header{Name: "example.jp.", Ttl: 30, Rrtype: dns.TypeA, Class: dns.ClassNONE, Rdlength: 0}}}
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeFormatError))
})
})
When("request is normal query", func() {
It("return rcode NoError", func() {
It("returns rcode NoError", func() {
msg.Remove([]dns.RR{MustNewRR("example.jp. 3600 IN A 192.168.0.1")})
rc := d.UpdatePrescan(zone, msg)
Expect(rc).To(Equal(dns.RcodeSuccess))
Expand Down
18 changes: 9 additions & 9 deletions name_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,32 @@ var _ = Describe("NameNode", func() {
})
})
Context("Test for GetNameNode", func() {
It("return current node with true (strict match)", func() {
It("returns current node with true (strict match)", func() {
nameNode, ok := root.GetNameNode("example.jp")
Expect(ok).To(BeTrue())
Expect(nameNode).To(Equal(root))
})
It("return child node with true (strict match)", func() {
It("returns child node with true (strict match)", func() {
nameNode, ok := root.GetNameNode("www1.example.jp")
Expect(ok).To(BeTrue())
Expect(nameNode).To(Equal(www1))
})
It("return grand child node with true (strict match)", func() {
It("returns grand child node with true (strict match)", func() {
nameNode, ok := root.GetNameNode("blue.www4.example.jp")
Expect(ok).To(BeTrue())
Expect(nameNode).To(Equal(blue))
})
It("return nearly node with false (loose match)", func() {
It("returns nearly node with false (loose match)", func() {
nameNode, ok := root.GetNameNode("apple.www1.example.jp")
Expect(ok).To(BeFalse())
Expect(nameNode).To(Equal(www1))
})
It("return grand child node with false (loose match)", func() {
It("returns grand child node with false (loose match)", func() {
nameNode, ok := root.GetNameNode("apple.blue.www4.example.jp")
Expect(ok).To(BeFalse())
Expect(nameNode).To(Equal(blue))
})
It("return nil with false (if name is not node subdomain name and equals to node domain name)", func() {
It("returns nil with false (if name is not node subdomain name and equals to node domain name)", func() {
nameNode, ok := root.GetNameNode("example2.jp")
Expect(ok).To(BeFalse())
Expect(nameNode).To(BeNil())
Expand All @@ -95,7 +95,7 @@ var _ = Describe("NameNode", func() {
})
})
Context("Test for GetChildNodes", func() {
It("return child node", func() {
It("returns child node", func() {
Expect(root.CopyChildNodes()).To(Equal(map[string]dnsutils.NameNodeInterface{
"www1.example.jp.": www1,
"www2.example.jp.": www2,
Expand All @@ -105,7 +105,7 @@ var _ = Describe("NameNode", func() {
})
})
Context("Test for GetRRSetMap", func() {
It("return RRSetInterface", func() {
It("returns RRSetInterface", func() {
Expect(root.CopyRRSetMap()).To(Equal(map[uint16]dnsutils.RRSetInterface{
dns.TypeA: aRRSet,
dns.TypeAAAA: aaaaRRSet,
Expand Down Expand Up @@ -263,7 +263,7 @@ var _ = Describe("NameNode", func() {
})
})
Context("Test for RRSetLen", func() {
It("return the number of not empty rrset", func() {
It("returns the number of not empty rrset", func() {
Expect(root.RRSetLen()).To(Equal(2))
set := dnsutils.NewRRSet("example.jp.", 300, dns.ClassINET, dns.TypeTXT, nil)
err := root.SetRRSet(set)
Expand Down
20 changes: 19 additions & 1 deletion rrset.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func NewRRSet(name string, ttl uint32, class dns.Class, rrtype uint16, rrs []dns
}
}
func NewRRSetFromRR(rr dns.RR) *RRSet {
if rr == nil {
return nil
}
return &RRSet{
name: dns.CanonicalName(rr.Header().Name),
ttl: rr.Header().Ttl,
Expand All @@ -43,6 +46,22 @@ func NewRRSetFromRR(rr dns.RR) *RRSet {
rrs: []dns.RR{rr},
}
}
func NewRRSetFromRRs(rrs []dns.RR) *RRSet {
if len(rrs) == 0 {
return nil
}
var set *RRSet
for _, rr := range rrs {
if set == nil {
set = NewRRSetFromRR(rr)
} else {
if err := set.AddRR(rr); err != nil {
return nil
}
}
}
return set
}

func (r *RRSet) GetName() string {
return r.name
Expand Down Expand Up @@ -85,7 +104,6 @@ func (r *RRSet) AddRR(rr dns.RR) error {
return ErrTTL
}
if rr.Header().Class != uint16(r.class) {
fmt.Printf("%d %d\n", rr.Header().Class, r.class)
return ErrClass
}
if len(r.rrs) >= 1 {
Expand Down
Loading

0 comments on commit c6caf3d

Please sign in to comment.