| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log/slog" |
| 6 |
"net" |
| 7 |
"net/netip" |
| 8 |
"strconv" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/gaissmai/bart" |
| 13 |
"github.com/miekg/dns" |
| 14 |
"github.com/slackhq/nebula/cert" |
| 15 |
"github.com/slackhq/nebula/cert_test" |
| 16 |
"github.com/slackhq/nebula/config" |
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
) |
| 20 |
|
| 21 |
type stubDNSWriter struct{} |
| 22 |
|
| 23 |
func (stubDNSWriter) LocalAddr() net.Addr { return &net.UDPAddr{} } |
| 24 |
func (stubDNSWriter) RemoteAddr() net.Addr { |
| 25 |
return &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5353} |
| 26 |
} |
| 27 |
func (stubDNSWriter) Write([]byte) (int, error) { return 0, nil } |
| 28 |
func (stubDNSWriter) WriteMsg(*dns.Msg) error { return nil } |
| 29 |
func (stubDNSWriter) Close() error { return nil } |
| 30 |
func (stubDNSWriter) TsigStatus() error { return nil } |
| 31 |
func (stubDNSWriter) TsigTimersOnly(bool) {} |
| 32 |
func (stubDNSWriter) Hijack() {} |
| 33 |
|
| 34 |
func TestParsequery(t *testing.T) { |
| 35 |
l := slog.New(slog.DiscardHandler) |
| 36 |
hostMap := &HostMap{} |
| 37 |
ds := &dnsServer{ |
| 38 |
l: l, |
| 39 |
dnsMap4: make(map[string]netip.Addr), |
| 40 |
dnsMap6: make(map[string]netip.Addr), |
| 41 |
hostMap: hostMap, |
| 42 |
} |
| 43 |
ds.enabled.Store(true) |
| 44 |
addrs := []netip.Addr{ |
| 45 |
netip.MustParseAddr("1.2.3.4"), |
| 46 |
netip.MustParseAddr("1.2.3.5"), |
| 47 |
netip.MustParseAddr("fd01::24"), |
| 48 |
netip.MustParseAddr("fd01::25"), |
| 49 |
} |
| 50 |
ds.Add("test.com.com", addrs) |
| 51 |
ds.Add("v4only.com.com", []netip.Addr{netip.MustParseAddr("1.2.3.6")}) |
| 52 |
ds.Add("v6only.com.com", []netip.Addr{netip.MustParseAddr("fd01::26")}) |
| 53 |
|
| 54 |
m := &dns.Msg{} |
| 55 |
m.SetQuestion("test.com.com", dns.TypeA) |
| 56 |
ds.parseQuery(m, nil) |
| 57 |
assert.NotNil(t, m.Answer) |
| 58 |
assert.Equal(t, "1.2.3.4", m.Answer[0].(*dns.A).A.String()) |
| 59 |
assert.Equal(t, dns.RcodeSuccess, m.Rcode) |
| 60 |
|
| 61 |
m = &dns.Msg{} |
| 62 |
m.SetQuestion("test.com.com", dns.TypeAAAA) |
| 63 |
ds.parseQuery(m, nil) |
| 64 |
assert.NotNil(t, m.Answer) |
| 65 |
assert.Equal(t, "fd01::24", m.Answer[0].(*dns.AAAA).AAAA.String()) |
| 66 |
assert.Equal(t, dns.RcodeSuccess, m.Rcode) |
| 67 |
|
| 68 |
// A known name with no record of the requested type should return NODATA |
| 69 |
// (NOERROR with empty answer), not NXDOMAIN. |
| 70 |
m = &dns.Msg{} |
| 71 |
m.SetQuestion("v4only.com.com", dns.TypeAAAA) |
| 72 |
ds.parseQuery(m, nil) |
| 73 |
assert.Empty(t, m.Answer) |
| 74 |
assert.Equal(t, dns.RcodeSuccess, m.Rcode) |
| 75 |
|
| 76 |
m = &dns.Msg{} |
| 77 |
m.SetQuestion("v6only.com.com", dns.TypeA) |
| 78 |
ds.parseQuery(m, nil) |
| 79 |
assert.Empty(t, m.Answer) |
| 80 |
assert.Equal(t, dns.RcodeSuccess, m.Rcode) |
| 81 |
|
| 82 |
// An unknown name should still return NXDOMAIN. |
| 83 |
m = &dns.Msg{} |
| 84 |
m.SetQuestion("unknown.com.com", dns.TypeA) |
| 85 |
ds.parseQuery(m, nil) |
| 86 |
assert.Empty(t, m.Answer) |
| 87 |
assert.Equal(t, dns.RcodeNameError, m.Rcode) |
| 88 |
|
| 89 |
// short lookups should not fail |
| 90 |
m = &dns.Msg{} |
| 91 |
m.Question = []dns.Question{{Name: "", Qtype: dns.TypeTXT, Qclass: dns.ClassINET}} |
| 92 |
ds.parseQuery(m, stubDNSWriter{}) |
| 93 |
assert.Empty(t, m.Answer) |
| 94 |
assert.Equal(t, dns.RcodeNameError, m.Rcode) |
| 95 |
|
| 96 |
m = &dns.Msg{} |
| 97 |
m.Question = []dns.Question{{Name: ".", Qtype: dns.TypeTXT, Qclass: dns.ClassINET}} |
| 98 |
ds.parseQuery(m, stubDNSWriter{}) |
| 99 |
assert.Empty(t, m.Answer) |
| 100 |
assert.Equal(t, dns.RcodeNameError, m.Rcode) |
| 101 |
} |
| 102 |
|
| 103 |
func Test_getDnsServerAddr(t *testing.T) { |
| 104 |
c := config.NewC(nil) |
| 105 |
|
| 106 |
c.Settings["lighthouse"] = map[string]any{ |
| 107 |
"dns": map[string]any{ |
| 108 |
"host": "0.0.0.0", |
| 109 |
"port": "1", |
| 110 |
}, |
| 111 |
} |
| 112 |
assert.Equal(t, "0.0.0.0:1", getDnsServerAddr(c)) |
| 113 |
|
| 114 |
c.Settings["lighthouse"] = map[string]any{ |
| 115 |
"dns": map[string]any{ |
| 116 |
"host": "::", |
| 117 |
"port": "1", |
| 118 |
}, |
| 119 |
} |
| 120 |
assert.Equal(t, "[::]:1", getDnsServerAddr(c)) |
| 121 |
|
| 122 |
c.Settings["lighthouse"] = map[string]any{ |
| 123 |
"dns": map[string]any{ |
| 124 |
"host": "[::]", |
| 125 |
"port": "1", |
| 126 |
}, |
| 127 |
} |
| 128 |
assert.Equal(t, "[::]:1", getDnsServerAddr(c)) |
| 129 |
|
| 130 |
// Make sure whitespace doesn't mess us up |
| 131 |
c.Settings["lighthouse"] = map[string]any{ |
| 132 |
"dns": map[string]any{ |
| 133 |
"host": "[::] ", |
| 134 |
"port": "1", |
| 135 |
}, |
| 136 |
} |
| 137 |
assert.Equal(t, "[::]:1", getDnsServerAddr(c)) |
| 138 |
} |
| 139 |
|
| 140 |
func newTestDnsServer(t *testing.T) (*dnsServer, *config.C) { |
| 141 |
t.Helper() |
| 142 |
sl := slog.New(slog.DiscardHandler) |
| 143 |
ds := &dnsServer{ |
| 144 |
l: sl, |
| 145 |
ctx: context.Background(), |
| 146 |
dnsMap4: make(map[string]netip.Addr), |
| 147 |
dnsMap6: make(map[string]netip.Addr), |
| 148 |
hostMap: &HostMap{}, |
| 149 |
} |
| 150 |
ds.mux = dns.NewServeMux() |
| 151 |
ds.mux.HandleFunc(".", ds.handleDnsRequest) |
| 152 |
return ds, config.NewC(nil) |
| 153 |
} |
| 154 |
|
| 155 |
func setDnsConfig(c *config.C, host string, port string, amLighthouse, serveDns bool) { |
| 156 |
c.Settings["lighthouse"] = map[string]any{ |
| 157 |
"am_lighthouse": amLighthouse, |
| 158 |
"serve_dns": serveDns, |
| 159 |
"dns": map[string]any{ |
| 160 |
"host": host, |
| 161 |
"port": port, |
| 162 |
}, |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestDnsServer_reload_initial_disabled(t *testing.T) { |
| 167 |
ds, c := newTestDnsServer(t) |
| 168 |
setDnsConfig(c, "127.0.0.1", "0", true, false) |
| 169 |
|
| 170 |
require.NoError(t, ds.reload(c, true)) |
| 171 |
assert.False(t, ds.enabled.Load()) |
| 172 |
assert.Equal(t, "127.0.0.1:0", ds.addr) |
| 173 |
assert.Nil(t, ds.server) |
| 174 |
} |
| 175 |
|
| 176 |
func TestDnsServer_reload_initial_enabled(t *testing.T) { |
| 177 |
ds, c := newTestDnsServer(t) |
| 178 |
setDnsConfig(c, "127.0.0.1", "0", true, true) |
| 179 |
|
| 180 |
require.NoError(t, ds.reload(c, true)) |
| 181 |
assert.True(t, ds.enabled.Load()) |
| 182 |
assert.Equal(t, "127.0.0.1:0", ds.addr) |
| 183 |
// initial never starts a runner; that's Control.Start's job |
| 184 |
assert.Nil(t, ds.server) |
| 185 |
} |
| 186 |
|
| 187 |
func TestDnsServer_reload_initial_serveDnsWithoutLighthouse(t *testing.T) { |
| 188 |
ds, c := newTestDnsServer(t) |
| 189 |
setDnsConfig(c, "127.0.0.1", "0", false, true) |
| 190 |
|
| 191 |
require.NoError(t, ds.reload(c, true)) |
| 192 |
// Wants DNS but isn't a lighthouse: gated off, no runner. |
| 193 |
assert.False(t, ds.enabled.Load()) |
| 194 |
} |
| 195 |
|
| 196 |
func TestDnsServer_reload_sameAddr_noOp(t *testing.T) { |
| 197 |
port := freeUDPPort(t) |
| 198 |
ds, c := newTestDnsServer(t) |
| 199 |
setDnsConfig(c, "127.0.0.1", port, true, true) |
| 200 |
require.NoError(t, ds.reload(c, true)) |
| 201 |
|
| 202 |
go ds.Start() |
| 203 |
waitForBind(t, ds) |
| 204 |
|
| 205 |
ds.serverMu.Lock() |
| 206 |
before := ds.server |
| 207 |
ds.serverMu.Unlock() |
| 208 |
require.NotNil(t, before) |
| 209 |
|
| 210 |
// Same address, so the running listener must be left alone rather than rebuilt under live queries |
| 211 |
require.NoError(t, ds.reload(c, false)) |
| 212 |
assert.True(t, ds.enabled.Load()) |
| 213 |
|
| 214 |
ds.serverMu.Lock() |
| 215 |
after := ds.server |
| 216 |
ds.serverMu.Unlock() |
| 217 |
assert.Same(t, before, after, "a same-address reload must not restart the listener") |
| 218 |
|
| 219 |
ds.Stop() |
| 220 |
} |
| 221 |
|
| 222 |
// The branch the old sameAddr test was accidentally hitting: enabled with nothing running means reload starts it. |
| 223 |
func TestDnsServer_reload_whenNotRunning_starts(t *testing.T) { |
| 224 |
port := freeUDPPort(t) |
| 225 |
ds, c := newTestDnsServer(t) |
| 226 |
setDnsConfig(c, "127.0.0.1", port, true, true) |
| 227 |
|
| 228 |
// initial only records config, it never starts anything |
| 229 |
require.NoError(t, ds.reload(c, true)) |
| 230 |
ds.serverMu.Lock() |
| 231 |
assert.Nil(t, ds.server, "the initial reload must not start a listener") |
| 232 |
ds.serverMu.Unlock() |
| 233 |
|
| 234 |
require.NoError(t, ds.reload(c, false)) |
| 235 |
waitForBind(t, ds) |
| 236 |
|
| 237 |
ds.serverMu.Lock() |
| 238 |
assert.NotNil(t, ds.server, "a reload with nothing running should bring DNS up") |
| 239 |
ds.serverMu.Unlock() |
| 240 |
|
| 241 |
ds.Stop() |
| 242 |
} |
| 243 |
|
| 244 |
func TestDnsServer_StartStop_lifecycle(t *testing.T) { |
| 245 |
// Bind to a real (random) UDP port so we exercise the actual |
| 246 |
// ListenAndServe + Shutdown plumbing including the started-chan race fix. |
| 247 |
port := freeUDPPort(t) |
| 248 |
|
| 249 |
ds, c := newTestDnsServer(t) |
| 250 |
setDnsConfig(c, "127.0.0.1", port, true, true) |
| 251 |
require.NoError(t, ds.reload(c, true)) |
| 252 |
|
| 253 |
done := make(chan struct{}) |
| 254 |
go func() { |
| 255 |
ds.Start() |
| 256 |
close(done) |
| 257 |
}() |
| 258 |
|
| 259 |
waitFor(t, func() bool { |
| 260 |
ds.serverMu.Lock() |
| 261 |
started := ds.started |
| 262 |
ds.serverMu.Unlock() |
| 263 |
if started == nil { |
| 264 |
return false |
| 265 |
} |
| 266 |
select { |
| 267 |
case <-started: |
| 268 |
return true |
| 269 |
default: |
| 270 |
return false |
| 271 |
} |
| 272 |
}) |
| 273 |
|
| 274 |
ds.Stop() |
| 275 |
select { |
| 276 |
case <-done: |
| 277 |
case <-time.After(5 * time.Second): |
| 278 |
t.Fatal("Start did not return after Stop") |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func TestDnsServer_Stop_beforeBind_doesNotHang(t *testing.T) { |
| 283 |
// Stop called immediately after Start should not deadlock even if bind |
| 284 |
// hasn't completed yet. This exercises the started-chan close-on-bind-fail |
| 285 |
// path: by binding to an obviously bad port (privileged) we get a fast |
| 286 |
// bind error before NotifyStartedFunc fires. |
| 287 |
ds, c := newTestDnsServer(t) |
| 288 |
// Use a port that should fail to bind (negative would be invalid, use a |
| 289 |
// host that won't resolve to ensure listenUDP fails quickly). |
| 290 |
setDnsConfig(c, "256.256.256.256", "53", true, true) |
| 291 |
require.NoError(t, ds.reload(c, true)) |
| 292 |
|
| 293 |
done := make(chan struct{}) |
| 294 |
go func() { |
| 295 |
ds.Start() |
| 296 |
close(done) |
| 297 |
}() |
| 298 |
|
| 299 |
// Give Start a moment to attempt the bind and fail. |
| 300 |
select { |
| 301 |
case <-done: |
| 302 |
// Bind failed and Start returned; Stop should be a no-op. |
| 303 |
case <-time.After(time.Second): |
| 304 |
t.Fatal("Start did not return after a bad bind") |
| 305 |
} |
| 306 |
|
| 307 |
stopped := make(chan struct{}) |
| 308 |
go func() { |
| 309 |
ds.Stop() |
| 310 |
close(stopped) |
| 311 |
}() |
| 312 |
select { |
| 313 |
case <-stopped: |
| 314 |
case <-time.After(time.Second): |
| 315 |
t.Fatal("Stop hung after a failed bind") |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
// newTestPKI builds a minimal *PKI with a single v1 cert whose name and |
| 320 |
// VPN addresses are caller-provided, suitable for exercising seedSelf and |
| 321 |
// QueryCert self handling. |
| 322 |
func newTestPKI(t *testing.T, name string, addrs []netip.Addr) *PKI { |
| 323 |
t.Helper() |
| 324 |
networks := make([]netip.Prefix, 0, len(addrs)) |
| 325 |
for _, a := range addrs { |
| 326 |
bits := 32 |
| 327 |
if a.Is6() { |
| 328 |
bits = 128 |
| 329 |
} |
| 330 |
networks = append(networks, netip.PrefixFrom(a, bits)) |
| 331 |
} |
| 332 |
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil) |
| 333 |
c, _, _, _ := cert_test.NewTestCert(cert.Version2, cert.Curve_CURVE25519, ca, caKey, name, time.Time{}, time.Time{}, networks, nil, nil) |
| 334 |
|
| 335 |
addrsTable := new(bart.Lite) |
| 336 |
for _, a := range addrs { |
| 337 |
addrsTable.Insert(netip.PrefixFrom(a, a.BitLen())) |
| 338 |
} |
| 339 |
|
| 340 |
cs := &CertState{ |
| 341 |
v2Cert: c, |
| 342 |
initiatingVersion: cert.Version2, |
| 343 |
myVpnAddrs: addrs, |
| 344 |
myVpnAddrsTable: addrsTable, |
| 345 |
} |
| 346 |
pki := &PKI{} |
| 347 |
pki.cs.Store(cs) |
| 348 |
return pki |
| 349 |
} |
| 350 |
|
| 351 |
func TestDnsServer_seedSelf_addsOwnRecord(t *testing.T) { |
| 352 |
ds, c := newTestDnsServer(t) |
| 353 |
myV4 := netip.MustParseAddr("10.0.0.1") |
| 354 |
myV6 := netip.MustParseAddr("fd00::1") |
| 355 |
ds.pki = newTestPKI(t, "lighthouse", []netip.Addr{myV4, myV6}) |
| 356 |
setDnsConfig(c, "127.0.0.1", "0", true, true) |
| 357 |
require.NoError(t, ds.reload(c, true)) |
| 358 |
|
| 359 |
ds.seedSelf() |
| 360 |
got4, exists := ds.Query(dns.TypeA, "lighthouse.") |
| 361 |
assert.True(t, exists) |
| 362 |
assert.Equal(t, myV4, got4) |
| 363 |
got6, exists := ds.Query(dns.TypeAAAA, "lighthouse.") |
| 364 |
assert.True(t, exists) |
| 365 |
assert.Equal(t, myV6, got6) |
| 366 |
} |
| 367 |
|
| 368 |
func TestDnsServer_seedSelf_disabled_noOp(t *testing.T) { |
| 369 |
ds, c := newTestDnsServer(t) |
| 370 |
ds.pki = newTestPKI(t, "lighthouse", []netip.Addr{netip.MustParseAddr("10.0.0.1")}) |
| 371 |
setDnsConfig(c, "127.0.0.1", "0", true, false) |
| 372 |
require.NoError(t, ds.reload(c, true)) |
| 373 |
|
| 374 |
ds.seedSelf() |
| 375 |
_, exists := ds.Query(dns.TypeA, "lighthouse.") |
| 376 |
assert.False(t, exists) |
| 377 |
} |
| 378 |
|
| 379 |
func TestDnsServer_clearRecords_dropsSelfHost(t *testing.T) { |
| 380 |
ds, c := newTestDnsServer(t) |
| 381 |
ds.pki = newTestPKI(t, "lighthouse", []netip.Addr{netip.MustParseAddr("10.0.0.1")}) |
| 382 |
setDnsConfig(c, "127.0.0.1", "0", true, true) |
| 383 |
require.NoError(t, ds.reload(c, true)) |
| 384 |
ds.seedSelf() |
| 385 |
require.NotEmpty(t, ds.selfHost) |
| 386 |
|
| 387 |
ds.clearRecords() |
| 388 |
assert.Empty(t, ds.selfHost) |
| 389 |
_, exists := ds.Query(dns.TypeA, "lighthouse.") |
| 390 |
assert.False(t, exists) |
| 391 |
} |
| 392 |
|
| 393 |
func TestDnsServer_QueryCert_returnsOwnCert(t *testing.T) { |
| 394 |
ds, _ := newTestDnsServer(t) |
| 395 |
myV4 := netip.MustParseAddr("10.0.0.1") |
| 396 |
ds.pki = newTestPKI(t, "lighthouse", []netip.Addr{myV4}) |
| 397 |
|
| 398 |
got := ds.QueryCert(myV4.String() + ".") |
| 399 |
assert.NotEmpty(t, got, "TXT lookup of our own VPN address should return our cert") |
| 400 |
|
| 401 |
other := netip.MustParseAddr("10.0.0.99") |
| 402 |
assert.Empty(t, ds.QueryCert(other.String()+"."), "unknown peer IP should return nothing") |
| 403 |
} |
| 404 |
|
| 405 |
func TestDnsServer_reload_disable_stopsRunningServer(t *testing.T) { |
| 406 |
port := freeUDPPort(t) |
| 407 |
ds, c := newTestDnsServer(t) |
| 408 |
setDnsConfig(c, "127.0.0.1", port, true, true) |
| 409 |
require.NoError(t, ds.reload(c, true)) |
| 410 |
|
| 411 |
startReturned := make(chan struct{}) |
| 412 |
go func() { |
| 413 |
ds.Start() |
| 414 |
close(startReturned) |
| 415 |
}() |
| 416 |
waitForBind(t, ds) |
| 417 |
|
| 418 |
// Toggle serve_dns off; reload should shut the running server down. |
| 419 |
setDnsConfig(c, "127.0.0.1", port, true, false) |
| 420 |
require.NoError(t, ds.reload(c, false)) |
| 421 |
select { |
| 422 |
case <-startReturned: |
| 423 |
case <-time.After(5 * time.Second): |
| 424 |
t.Fatal("Start did not return after reload disabled DNS") |
| 425 |
} |
| 426 |
assert.False(t, ds.enabled.Load()) |
| 427 |
} |
| 428 |
|
| 429 |
func freeUDPPort(t *testing.T) string { |
| 430 |
t.Helper() |
| 431 |
conn, err := net.ListenPacket("udp", "127.0.0.1:0") |
| 432 |
require.NoError(t, err) |
| 433 |
port := conn.LocalAddr().(*net.UDPAddr).Port |
| 434 |
require.NoError(t, conn.Close()) |
| 435 |
return strconv.Itoa(port) |
| 436 |
} |
| 437 |
|
| 438 |
func waitForBind(t *testing.T, ds *dnsServer) { |
| 439 |
t.Helper() |
| 440 |
waitFor(t, func() bool { |
| 441 |
ds.serverMu.Lock() |
| 442 |
started := ds.started |
| 443 |
ds.serverMu.Unlock() |
| 444 |
if started == nil { |
| 445 |
return false |
| 446 |
} |
| 447 |
select { |
| 448 |
case <-started: |
| 449 |
return true |
| 450 |
default: |
| 451 |
return false |
| 452 |
} |
| 453 |
}) |
| 454 |
} |
| 455 |
|
| 456 |
func waitFor(t *testing.T, cond func() bool) { |
| 457 |
t.Helper() |
| 458 |
deadline := time.Now().Add(5 * time.Second) |
| 459 |
for time.Now().Before(deadline) { |
| 460 |
if cond() { |
| 461 |
return |
| 462 |
} |
| 463 |
time.Sleep(5 * time.Millisecond) |
| 464 |
} |
| 465 |
t.Fatal("timed out waiting for condition") |
| 466 |
} |
| 467 |
|
| 468 |
// Two reloads in quick succession, or a HUP before Control.Start, can race two Starts at the same listener. |
| 469 |
func TestDnsServer_Start_isIdempotent(t *testing.T) { |
| 470 |
port := freeUDPPort(t) |
| 471 |
ds, c := newTestDnsServer(t) |
| 472 |
setDnsConfig(c, "127.0.0.1", port, true, true) |
| 473 |
require.NoError(t, ds.reload(c, true)) |
| 474 |
|
| 475 |
go ds.Start() |
| 476 |
waitForBind(t, ds) |
| 477 |
|
| 478 |
ds.serverMu.Lock() |
| 479 |
first := ds.server |
| 480 |
ds.serverMu.Unlock() |
| 481 |
require.NotNil(t, first) |
| 482 |
|
| 483 |
// If the second Start replaces the tracked server, Stop kills the wrong one and the port leaks |
| 484 |
done := make(chan struct{}) |
| 485 |
go func() { |
| 486 |
ds.Start() |
| 487 |
close(done) |
| 488 |
}() |
| 489 |
select { |
| 490 |
case <-done: |
| 491 |
case <-time.After(time.Second * 5): |
| 492 |
t.Fatal("second Start never returned") |
| 493 |
} |
| 494 |
|
| 495 |
ds.serverMu.Lock() |
| 496 |
second := ds.server |
| 497 |
ds.serverMu.Unlock() |
| 498 |
assert.Same(t, first, second, "a second Start must not replace the running server") |
| 499 |
|
| 500 |
// The real proof, after Stop the port must actually be free |
| 501 |
ds.Stop() |
| 502 |
waitFor(t, func() bool { |
| 503 |
pc, err := net.ListenPacket("udp", "127.0.0.1:"+port) |
| 504 |
if err != nil { |
| 505 |
return false |
| 506 |
} |
| 507 |
_ = pc.Close() |
| 508 |
return true |
| 509 |
}) |
| 510 |
} |
| 511 |
|
| 512 |
// An address change must actually end up listening on the new port. Start's guard refuses when a server is already |
| 513 |
// installed, so reload has to clear the slot before shutting the old one down. |
| 514 |
func TestDnsServer_reload_addrChange_restarts(t *testing.T) { |
| 515 |
first := freeUDPPort(t) |
| 516 |
second := freeUDPPort(t) |
| 517 |
|
| 518 |
ds, c := newTestDnsServer(t) |
| 519 |
setDnsConfig(c, "127.0.0.1", first, true, true) |
| 520 |
require.NoError(t, ds.reload(c, true)) |
| 521 |
|
| 522 |
go ds.Start() |
| 523 |
waitForBind(t, ds) |
| 524 |
|
| 525 |
// Cycle a few times, the failure this guards against depends on which goroutine wins serverMu |
| 526 |
for i := range 8 { |
| 527 |
want := second |
| 528 |
if i%2 == 1 { |
| 529 |
want = first |
| 530 |
} |
| 531 |
setDnsConfig(c, "127.0.0.1", want, true, true) |
| 532 |
require.NoError(t, ds.reload(c, false)) |
| 533 |
waitForBind(t, ds) |
| 534 |
|
| 535 |
ds.serverMu.Lock() |
| 536 |
srv := ds.server |
| 537 |
ds.serverMu.Unlock() |
| 538 |
require.NotNil(t, srv, "reload left DNS down instead of restarting it") |
| 539 |
require.Equal(t, "127.0.0.1:"+want, srv.Addr, "reload should be serving the new address") |
| 540 |
} |
| 541 |
|
| 542 |
// Land back on second so the port assertions below are meaningful |
| 543 |
setDnsConfig(c, "127.0.0.1", second, true, true) |
| 544 |
require.NoError(t, ds.reload(c, false)) |
| 545 |
waitForBind(t, ds) |
| 546 |
|
| 547 |
// The old port must be released and the new one actually held |
| 548 |
waitFor(t, func() bool { |
| 549 |
pc, err := net.ListenPacket("udp", "127.0.0.1:"+first) |
| 550 |
if err != nil { |
| 551 |
return false |
| 552 |
} |
| 553 |
_ = pc.Close() |
| 554 |
return true |
| 555 |
}) |
| 556 |
_, err := net.ListenPacket("udp", "127.0.0.1:"+second) |
| 557 |
require.Error(t, err, "the new address should be bound by the DNS responder") |
| 558 |
|
| 559 |
ds.Stop() |
| 560 |
} |
| 561 |
|
| 562 |
// A listener that dies on its own must release the slot, or a later same-addr reload sees it as running and no-ops. |
| 563 |
func TestDnsServer_Start_bindFailure_releasesSlot(t *testing.T) { |
| 564 |
port := freeUDPPort(t) |
| 565 |
blocker, err := net.ListenPacket("udp", "127.0.0.1:"+port) |
| 566 |
require.NoError(t, err) |
| 567 |
|
| 568 |
ds, c := newTestDnsServer(t) |
| 569 |
setDnsConfig(c, "127.0.0.1", port, true, true) |
| 570 |
require.NoError(t, ds.reload(c, true)) |
| 571 |
|
| 572 |
ds.Start() // returns once the bind fails |
| 573 |
|
| 574 |
ds.serverMu.Lock() |
| 575 |
assert.Nil(t, ds.server, "a listener that failed to bind must not stay parked in the slot") |
| 576 |
ds.serverMu.Unlock() |
| 577 |
|
| 578 |
// With the slot released, a reload can retry once the port frees up |
| 579 |
require.NoError(t, blocker.Close()) |
| 580 |
require.NoError(t, ds.reload(c, false)) |
| 581 |
waitForBind(t, ds) |
| 582 |
|
| 583 |
ds.serverMu.Lock() |
| 584 |
assert.NotNil(t, ds.server, "a same-addr reload should retry after a failed bind") |
| 585 |
ds.serverMu.Unlock() |
| 586 |
|
| 587 |
ds.Stop() |
| 588 |
} |
| 589 |
|
| 590 |
// A disable that lands while Start is between its unlocked check and the guard must not leave a listener behind. |
| 591 |
func TestDnsServer_Start_refusesWhenDisabledUnderLock(t *testing.T) { |
| 592 |
port := freeUDPPort(t) |
| 593 |
ds, c := newTestDnsServer(t) |
| 594 |
setDnsConfig(c, "127.0.0.1", port, true, true) |
| 595 |
require.NoError(t, ds.reload(c, true)) |
| 596 |
require.True(t, ds.enabled.Load()) |
| 597 |
|
| 598 |
// Holding serverMu parks Start on the lock, the only way to land the disable in that window on purpose |
| 599 |
ds.serverMu.Lock() |
| 600 |
|
| 601 |
done := make(chan struct{}) |
| 602 |
go func() { |
| 603 |
ds.Start() |
| 604 |
close(done) |
| 605 |
}() |
| 606 |
|
| 607 |
select { |
| 608 |
case <-done: |
| 609 |
ds.serverMu.Unlock() |
| 610 |
t.Fatal("Start returned early, the test never exercised the window") |
| 611 |
case <-time.After(time.Millisecond * 100): |
| 612 |
} |
| 613 |
|
| 614 |
// The disable reload's critical section. It sees nothing running, so it never calls Stop. |
| 615 |
ds.enabled.Store(false) |
| 616 |
ds.serverMu.Unlock() |
| 617 |
|
| 618 |
select { |
| 619 |
case <-done: |
| 620 |
case <-time.After(time.Second * 5): |
| 621 |
t.Fatal("Start never returned") |
| 622 |
} |
| 623 |
|
| 624 |
ds.serverMu.Lock() |
| 625 |
assert.Nil(t, ds.server, "Start must not install a listener a disable already cancelled") |
| 626 |
ds.serverMu.Unlock() |
| 627 |
|
| 628 |
pc, err := net.ListenPacket("udp", "127.0.0.1:"+port) |
| 629 |
require.NoError(t, err, "an orphaned listener is still holding the port") |
| 630 |
_ = pc.Close() |
| 631 |
} |