1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
package certmagic
// Code in this file adapted from go-acme/lego, July 2020:
// https://github.com/go-acme/lego
// by Ludovic Fernandez and Dominik Menke
//
// It has been modified.
import (
"net"
"reflect"
"sort"
"strings"
"testing"
)
func TestLookupNameserversErr(t *testing.T) {
testCases := []struct {
desc string
fqdn string
error string
}{
{
desc: "invalid tld",
fqdn: "_null.n0n0.",
error: "could not determine the zone",
},
}
for i, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
_, err := lookupNameservers(test.fqdn, nil)
if err == nil {
t.Errorf("expected error, got none")
}
if !strings.Contains(err.Error(), test.error) {
t.Errorf("Test %d: Expected error to contain '%s' but got '%s'", i, test.error, err.Error())
}
})
}
}
var findXByFqdnTestCases = []struct {
desc string
fqdn string
zone string
primaryNs string
nameservers []string
expectedError string
}{
{
desc: "domain is a CNAME",
fqdn: "scholar.google.com.",
zone: "google.com.",
primaryNs: "ns1.google.com.",
nameservers: recursiveNameservers(nil),
},
{
desc: "domain is a non-existent subdomain",
fqdn: "foo.google.com.",
zone: "google.com.",
primaryNs: "ns1.google.com.",
nameservers: recursiveNameservers(nil),
},
{
desc: "domain is a eTLD",
fqdn: "example.com.ac.",
zone: "ac.",
primaryNs: "a0.nic.ac.",
nameservers: recursiveNameservers(nil),
},
//{
// desc: "domain is a cross-zone CNAME",
// fqdn: "cross-zone-example.assets.sh.",
// zone: "assets.sh.",
// primaryNs: "gina.ns.cloudflare.com.",
// nameservers: recursiveNameservers(nil),
//},
{
desc: "NXDOMAIN",
fqdn: "test.loho.jkl.",
zone: "loho.jkl.",
nameservers: []string{"1.1.1.1:53"},
expectedError: "could not find the start of authority for test.loho.jkl.: NXDOMAIN",
},
{
desc: "several non existent nameservers",
fqdn: "scholar.google.com.",
zone: "google.com.",
primaryNs: "ns1.google.com.",
nameservers: []string{":7053", ":8053", "1.1.1.1:53"},
},
{
desc: "only non existent nameservers",
fqdn: "scholar.google.com.",
zone: "google.com.",
nameservers: []string{":7053", ":8053", ":9053"},
expectedError: "could not find the start of authority for scholar.google.com.:",
},
{
desc: "no nameservers",
fqdn: "test.ldez.com.",
zone: "ldez.com.",
nameservers: []string{},
expectedError: "could not find the start of authority for test.ldez.com.",
},
}
func TestResolveConfServers(t *testing.T) {
var testCases = []struct {
fixture string
expected []string
defaults []string
}{
{
fixture: "testdata/resolv.conf.1",
defaults: []string{"127.0.0.1:53"},
expected: []string{"10.200.3.249", "10.200.3.250:5353", "2001:4860:4860::8844", "[10.0.0.1]:5353"},
},
{
fixture: "testdata/resolv.conf.nonexistant",
defaults: []string{"127.0.0.1:53"},
expected: []string{"127.0.0.1:53"},
},
}
for i, test := range testCases {
t.Run(test.fixture, func(t *testing.T) {
result := systemOrDefaultNameservers(test.fixture, test.defaults)
sort.Strings(result)
sort.Strings(test.expected)
if !reflect.DeepEqual(test.expected, result) {
t.Errorf("Test %d: Expected %v but got %v", i, test.expected, result)
}
})
}
}
func TestRecursiveNameserversAddsPort(t *testing.T) {
type want struct {
port string
}
custom := []string{"127.0.0.1", "ns1.google.com:43"}
expectations := []want{{port: "53"}, {port: "43"}}
results := recursiveNameservers(custom)
if !reflect.DeepEqual(custom, []string{"127.0.0.1", "ns1.google.com:43"}) {
t.Errorf("Expected custom nameservers to be unmodified. got %v", custom)
}
if len(results) != len(expectations) {
t.Errorf("%v wrong results length. got %d, want %d", results, len(results), len(expectations))
}
var hasCustom bool
for i, res := range results {
hasCustom = hasCustom || strings.HasPrefix(res, custom[0])
if _, port, err := net.SplitHostPort(res); err != nil {
t.Errorf("%v Error splitting result %d into host and port: %v", results, i, err)
} else {
if port != expectations[i].port {
t.Errorf("%v Expected result %d to have port %s but got %s", results, i, expectations[i].port, port)
}
}
}
if !hasCustom {
t.Errorf("%v Expected custom resolvers to be included, but they weren't: %v", results, custom)
}
}
func TestRecursiveNameserversDefaults(t *testing.T) {
results := recursiveNameservers(nil)
if len(results) < 1 {
t.Errorf("%v Expected at least 1 records as default when nil custom", results)
}
results = recursiveNameservers([]string{})
if len(results) < 1 {
t.Errorf("%v Expected at least 1 records as default when empty custom", results)
}
}
func clearFqdnCache() {
fqdnSOACacheMu.Lock()
fqdnSOACache = make(map[string]*soaCacheEntry)
fqdnSOACacheMu.Unlock()
}
|