Browse Source

Some additional tests and validations

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
master
Erik Hollensbe 10 months ago
parent
commit
a9bc949aa1
3 changed files with 82 additions and 1 deletions
  1. +4
    -1
      dnsdb/db.go
  2. +8
    -0
      dnsdb/db_test.go
  3. +70
    -0
      ldnsd_test.go

+ 4
- 1
dnsdb/db.go View File

@ -2,6 +2,7 @@ package dnsdb
import (
"net"
"regexp"
"strings"
dnsserverDB "github.com/erikh/dnsserver/db"
@ -11,6 +12,8 @@ import (
"github.com/sirupsen/logrus"
)
var hostMatch = regexp.MustCompile(`^[a-z][0-9a-z-]{0,62}$`)
var (
// ErrNotSupported is for when something is not supported by this interface
ErrNotSupported = errors.New("not supported")
@ -70,7 +73,7 @@ func (r *Record) validateHost() error {
}
for _, name := range strings.Split(r.Host, ".") {
if len(name) > 63 {
if !hostMatch.MatchString(name) {
return errors.New("names in DNS must be 63 characters or less, per part")
}
}


+ 8
- 0
dnsdb/db_test.go View File

@ -47,6 +47,14 @@ func TestRecordValidation(t *testing.T) {
r: &Record{Host: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Address: "127.0.0.1"},
success: false,
},
"long domain is looooooong and has garbage": {
r: &Record{Host: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Address: "127.0.0.1"},
success: false,
},
"long domain is looooooong and has a leading number": {
r: &Record{Host: "0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Address: "127.0.0.1"},
success: false,
},
}
for testName, result := range table {


+ 70
- 0
ldnsd_test.go View File

@ -237,3 +237,73 @@ func BenchmarkRecordInsertThenQuery(b *testing.B) {
})
})
}
func TestProtoRecordValidation(t *testing.T) {
srv, err := startService()
if err != nil {
t.Fatal(err)
}
defer os.Remove("test.db")
defer srv.Shutdown()
table := map[string]struct {
r *proto.Record
success bool
}{
"basic": {
r: &proto.Record{Host: "test", Address: "127.0.0.1"},
success: true,
},
"empty host": {
r: &proto.Record{Host: "", Address: "127.0.0.1"},
success: false,
},
"empty ip": {
r: &proto.Record{Host: "test", Address: ""},
success: false,
},
"bad ip": {
r: &proto.Record{Host: "test", Address: "abcdefgh"},
success: false,
},
"ipv6 ip": {
r: &proto.Record{Host: "test", Address: "fe80::1"},
success: false,
},
"invalid ipv4 ip": {
r: &proto.Record{Host: "test", Address: "256.1.1.1"},
success: false,
},
"long string is looooooong": {
r: &proto.Record{Host: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Address: "fe80::1"},
success: false,
},
"long string is too looooooong": {
r: &proto.Record{Host: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Address: "fe80::1"},
success: false,
},
"long domain is looooooong": {
r: &proto.Record{Host: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Address: "127.0.0.1"},
success: true,
},
"long domain has a really long part": {
r: &proto.Record{Host: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Address: "127.0.0.1"},
success: false,
},
}
client, err := proto.NewClient(config.DefaultGRPCListen, defaultCAFile, defaultCertFile, defaultKeyFile)
if err != nil {
t.Fatal(err)
}
for testName, result := range table {
_, resultErr := client.SetA(context.Background(), result.r)
if result.success && resultErr != nil {
t.Fatalf("Result for %q should be success but was %v", testName, resultErr)
}
if !result.success && resultErr == nil {
t.Fatalf("Result for %q should NOT be success but was.", testName)
}
}
}

Loading…
Cancel
Save