1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

ROUTE53 cleanups

This commit is contained in:
Tom Limoncelli
2023-11-12 13:03:14 -05:00
parent 09582484ec
commit 4940628df6
4 changed files with 38 additions and 23 deletions

View File

@@ -3,11 +3,12 @@ package commands
import (
"bytes"
"fmt"
"github.com/google/go-cmp/cmp"
"strings"
"testing"
"text/template"
"github.com/google/go-cmp/cmp"
"github.com/urfave/cli/v2"
"golang.org/x/exp/slices"
)
@@ -178,7 +179,7 @@ func TestShellCompletionCommand(t *testing.T) {
for _, tt := range shellsAndCompletionScripts {
testCases[0].expected = append(testCases[0].expected, tt.shellName)
for i, _ := range tt.shellName {
for i := range tt.shellName {
testCases = append(testCases, testCase{
shellArg: tt.shellName[:i+1],
expected: []string{tt.shellName},

View File

@@ -15,8 +15,6 @@ func AuditRecords(records []*models.RecordConfig) []error {
a.Add("R53_ALIAS", rejectifTargetEqualsLabel) // Last verified 2023-03-01
a.Add("TXT", rejectif.TxtIsEmpty) // Last verified 2023-10-28
return a.Audit(records)
}

View File

@@ -6,8 +6,6 @@ import (
"bytes"
"fmt"
"strings"
"github.com/StackExchange/dnscontrol/v4/pkg/printer"
)
type State int
@@ -25,19 +23,14 @@ func isRemaining(s string, i, r int) bool {
return (len(s) - 1 - i) > r
}
// txtDecode is like strings.Fields except individual fields
// might be quoted using `"`.
// txtDecode decodes TXT strings received from ROUTE53.
func txtDecode(s string) (string, error) {
printer.Printf("DEBUG: route53 txt inboundv=%v\n", s)
// Parse according to RFC1035 zonefile specifications.
// "foo" -> one string: `foo``
// "foo" "bar" -> two strings: `foo` and `bar`
// quotes and backslashes are escaped using \
// if s == `""` {
// r := []string{}
// printer.Printf("DEBUG: route53 txt Z decodedv=%v\n", r)
// return r, nil
// }
//printer.Printf("DEBUG: route53 txt inboundv=%v\n", s)
b := &bytes.Buffer{}
state := StateStart
@@ -106,18 +99,13 @@ func txtDecode(s string) (string, error) {
}
r := b.String()
printer.Printf("DEBUG: route53 txt decodedv=%v\n", r)
//printer.Printf("DEBUG: route53 txt decodedv=%v\n", r)
return r, nil
}
// txtEncode encodes TXT strings as expected by ROUTE53.
func txtEncode(ts []string) string {
printer.Printf("DEBUG: route53 txt outboundv=%v\n", ts)
if len(ts) == 0 {
t := `""`
printer.Printf("DEBUG: route53 txt Z encodedv=%v\n", t)
return t
}
//printer.Printf("DEBUG: route53 txt outboundv=%v\n", ts)
for i := range ts {
ts[i] = strings.ReplaceAll(ts[i], `\`, `\\`)
@@ -125,6 +113,6 @@ func txtEncode(ts []string) string {
}
t := `"` + strings.Join(ts, `" "`) + `"`
printer.Printf("DEBUG: route53 txt encodedv=%v\n", t)
//printer.Printf("DEBUG: route53 txt encodedv=%v\n", t)
return t
}

View File

@@ -12,6 +12,8 @@ func TestTxtDecode(t *testing.T) {
data string
expected []string
}{
{``, []string{``}},
{`""`, []string{``}},
{`foo`, []string{`foo`}},
{`"foo"`, []string{`foo`}},
{`"foo bar"`, []string{`foo bar`}},
@@ -51,3 +53,29 @@ func TestTxtDecode(t *testing.T) {
}
}
}
func TestTxtEncode(t *testing.T) {
tests := []struct {
data []string
expected string
}{
{[]string{``}, `""`},
{[]string{`foo`}, `"foo"`},
{[]string{`aaa`, `bbb`}, `"aaa" "bbb"`},
{[]string{`ccc`, `ddd`, `eee`}, `"ccc" "ddd" "eee"`},
{[]string{`a"a`, `bbb`}, `"a\"a" "bbb"`},
{[]string{`quo'te`}, "\"quo'te\""},
{[]string{"blah`blah"}, "\"blah`blah\""},
{[]string{`quo"te`}, "\"quo\\\"te\""},
{[]string{`q"uo"te`}, "\"q\\\"uo\\\"te\""},
{[]string{`backs\lash`}, "\"backs\\\\lash\""},
}
for i, test := range tests {
got := txtEncode(test.data)
want := test.expected
if got != want {
t.Errorf("%v: expected TxtStrings=v(%v) got (%v)", i, want, got)
}
}
}