Skip to content

Commit

Permalink
Merge pull request #4 from jimmystewpot/log-cache-entries-drop
Browse files Browse the repository at this point in the history
more tests and benchmarks
  • Loading branch information
jimmystewpot committed Aug 20, 2020
2 parents 6b46ffe + 0d222bf commit 2488a97
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 11 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,5 @@ Right now, I'd recommend using logstash to ship the logs to an elasticsearch clu

## Build and install
* clone this repo
* install libpcap, libpcap-dev
* 'go get'
* 'go build -o gopassivedns' (the -o is really just being careful, assuming you cloned the repo you shouldn't need it)
* 'cp gopassivedns /some/path/to/gopassivedns'

* make build
* make install
12 changes: 6 additions & 6 deletions cmd/gopassivedns/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
// in this thread...
func TypeString(dnsType layers.DNSType) string {
switch dnsType {
default:
//take a blind stab...at least this shouldn't *lose* data
return strconv.Itoa(int(dnsType))
case layers.DNSTypeA:
return "A"
case layers.DNSTypeAAAA:
Expand All @@ -38,6 +35,9 @@ func TypeString(dnsType layers.DNSType) string {
return "SRV"
case 255: //ANY query per http://tools.ietf.org/html/rfc1035#page-12
return "ANY"
default:
//take a blind stab...at least this shouldn't *lose* data
return strconv.Itoa(int(dnsType))
}
}

Expand All @@ -49,9 +49,6 @@ func TypeString(dnsType layers.DNSType) string {
// in this thread...
func RRString(rr layers.DNSResourceRecord) string {
switch rr.Type {
default:
//take a blind stab...at least this shouldn't *lose* data
return string(rr.Data)
case layers.DNSTypeA:
return rr.IP.String()
case layers.DNSTypeAAAA:
Expand All @@ -73,6 +70,9 @@ func RRString(rr layers.DNSResourceRecord) string {
case layers.DNSTypeSRV:
//TODO: rebuild the full SRV string
return string(rr.SRV.Name)
default:
//take a blind stab...at least this shouldn't *lose* data
return string(rr.Data)
}
}

Expand Down
131 changes: 131 additions & 0 deletions cmd/gopassivedns/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"testing"

"github.com/google/gopacket/layers"
)

func BenchmarkTypeString(b *testing.B) {
type args struct {
dnsType layers.DNSType
}
benchmarks := []struct {
name string
args args
want string
}{
{
name: "SRV",
args: args{
dnsType: layers.DNSTypeSRV,
},
want: "SRV",
},
{
name: "A",
args: args{
dnsType: layers.DNSTypeA,
},
want: "A",
},
{
name: "AAAA",
args: args{
dnsType: layers.DNSTypeAAAA,
},
want: "AAAA",
},
{
name: "TXT",
args: args{
dnsType: layers.DNSTypeTXT,
},
want: "TXT",
},
{
name: "SOA",
args: args{
dnsType: layers.DNSTypeSOA,
},
want: "SOA",
},
{
name: "ANY",
args: args{
dnsType: 255,
},
want: "ANY",
},
}
for _, bb := range benchmarks {
b.Run(bb.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
if got := TypeString(bb.args.dnsType); got != bb.want {
b.Errorf("TypeString() = %v, want %v", got, bb.want)
}
}
})
}
}

func TestTypeString(t *testing.T) {
type args struct {
dnsType layers.DNSType
}
tests := []struct {
name string
args args
want string
}{
{
name: "SRV",
args: args{
dnsType: layers.DNSTypeSRV,
},
want: "SRV",
},
{
name: "A",
args: args{
dnsType: layers.DNSTypeA,
},
want: "A",
},
{
name: "AAAA",
args: args{
dnsType: layers.DNSTypeAAAA,
},
want: "AAAA",
},
{
name: "TXT",
args: args{
dnsType: layers.DNSTypeTXT,
},
want: "TXT",
},
{
name: "SOA",
args: args{
dnsType: layers.DNSTypeSOA,
},
want: "SOA",
},
{
name: "ANY",
args: args{
dnsType: 255,
},
want: "ANY",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := TypeString(tt.args.dnsType); got != tt.want {
t.Errorf("TypeString() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 2488a97

Please sign in to comment.