nameserver.go: refactor requiresRRUpdate and add basic tests

This commit is contained in:
Thomas Preisner 2021-10-16 22:06:51 +02:00
parent 204d5eacf6
commit 1cfe465cf9
4 changed files with 101 additions and 3 deletions

68
nameserver_test.go Normal file
View file

@ -0,0 +1,68 @@
package main
import (
"net"
"testing"
"github.com/foxcpp/go-mockdns"
)
func TestRequiresRRUpdate(t *testing.T) {
tests := []struct {
name string
record string
addr net.IP
expRes bool
}{
{
"nonexistent record",
"invalid.example.org",
net.ParseIP("1.1.1.1"),
true,
},
{
"ipv4 update required",
"ipv4.example.org",
net.ParseIP("1.1.1.1"),
true,
},
{
"ipv4 up to date",
"ipv4.example.org",
net.ParseIP("2.2.2.2"),
false,
},
{
"ipv6 up to date",
"ipv6.example.org",
net.ParseIP("1:1:1:1:1:1:1:1"),
true,
},
{
"ipv6 up to date",
"ipv6.example.org",
net.ParseIP("2:2:2:2:2:2:2:2"),
false,
},
}
// setup mock resolver
resolver := mockdns.Resolver{
Zones: map[string]mockdns.Zone{
"ipv4.example.org.": {
A: []string{"2.2.2.2"},
},
"ipv6.example.org.": {
AAAA: []string{"2:2:2:2:2:2:2:2"},
},
},
}
for _, tc := range tests {
res := requiresRRUpdate(tc.record, tc.addr, &resolver)
if tc.expRes != res {
t.Errorf("%s: has=%v, wanted=%v", tc.name, res, tc.expRes)
}
}
}