dnscontrol/commands/gz_test.go
Tom Limoncelli 1b2f5d4d34
BUGFIX: IDN support is broken for domain names (#3845)
# Issue

Fixes https://github.com/StackExchange/dnscontrol/issues/3842

CC @das7pad

# Resolution

Convert domain.Name to IDN earlier in the pipeline. Hack the --domains
processing to convert everything to IDN.

* Domain names are now stored 3 ways: The original input from
dnsconfig.js, canonical IDN format (`xn--...`), and Unicode format. All
are downcased. Providers that haven't been updated will receive the IDN
format instead of the original input format. This might break some
providers but only for users with unicode in their D("domain.tld").
PLEASE TEST YOUR PROVIDER.
* BIND filename formatting options have been added to access the new
formats.

# Breaking changes

* BIND zonefiles may change. The default used the name input in the D()
statement. It now defaults to the IDN name + "!tag" if there is a tag.
* Providers that are not IDN-aware may break (hopefully only if they
weren't processing IDN already)

---------

Co-authored-by: Jakob Ackermann <das7pad@outlook.com>
2025-11-29 12:17:44 -05:00

77 lines
2.2 KiB
Go

package commands
import (
"fmt"
"log"
"os"
"testing"
_ "github.com/StackExchange/dnscontrol/v4/providers/_all"
"github.com/andreyvit/diff"
)
func TestFormatTypes(t *testing.T) {
/*
Input: Converted to: Should match contents of:
test_data/$DOMAIN.zone js test_data/$DOMAIN.zone.js
test_data/$DOMAIN.zone tsv test_data/$DOMAIN.zone.tsv
test_data/$DOMAIN.zone zone test_data/$DOMAIN.zone.zone
*/
for _, domain := range []string{"simple.com", "example.org", "apex.com", "ds.com"} {
t.Run(domain+"/js", func(t *testing.T) { testFormat(t, domain, "js") })
t.Run(domain+"/djs", func(t *testing.T) { testFormat(t, domain, "djs") })
t.Run(domain+"/tsv", func(t *testing.T) { testFormat(t, domain, "tsv") })
t.Run(domain+"/zone", func(t *testing.T) { testFormat(t, domain, "zone") })
}
}
func testFormat(t *testing.T, domain, format string) {
t.Helper()
expectedFilename := fmt.Sprintf("test_data/%s.zone.%s", domain, format)
outputFiletmpl := fmt.Sprintf("%s.zone.%s.*.txt", domain, format)
outfile, err := os.CreateTemp(t.TempDir(), outputFiletmpl)
if err != nil {
log.Fatal(fmt.Errorf("gz can't TempFile %q: %w", outputFiletmpl, err))
}
defer os.Remove(outfile.Name())
// Convert test data to the experiment output.
gzargs := GetZoneArgs{
ZoneNames: []string{domain},
OutputFormat: format,
OutputFile: outfile.Name(),
CredName: "bind",
ProviderName: "BIND",
}
gzargs.CredsFile = "test_data/bind-creds.json"
// Read the zonefile and convert
err = GetZone(gzargs)
if err != nil {
log.Fatal(fmt.Errorf("can't GetZone: %w", err))
}
// Read the actual result:
got, err := os.ReadFile(outfile.Name())
if err != nil {
log.Fatal(fmt.Errorf("can't read actuals %q: %w", outfile.Name(), err))
}
// Read the expected result
want, err := os.ReadFile(expectedFilename)
if err != nil {
log.Fatal(fmt.Errorf("can't read expected %q: %w", expectedFilename, err))
}
if w, g := string(want), string(got); w != g {
// If the test fails, output a file showing "got"
err = os.WriteFile(expectedFilename+".ACTUAL", got, 0o644)
if err != nil {
log.Fatal(err)
}
t.Errorf("testFormat mismatch (-got +want):\n%s", diff.LineDiff(g, w))
}
}