Skip to content

Commit 27518da

Browse files
josharianbradfitz
authored andcommitted
[tailscale] net: add TS_PANIC_ON_TEST_LISTEN_UNSPEC env to panic on unspecified addr listens
For debugging Mac firewall dialog boxes blocking+failing tests. Updates #48 Change-Id: Ic1a0cd51de7fe553de1c1c9333fa1cc75b8490f2 (cherry picked from commit cd7323c) (cherry picked from commit 8c9eff4)
1 parent eb462ce commit 27518da

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/net/tcpsock_posix.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,34 @@ func (ln *TCPListener) file() (*os.File, error) {
175175
return f, nil
176176
}
177177

178+
// Tailscale addition: if TS_PANIC_ON_TEST_LISTEN_UNSPEC is set, panic
179+
// if a listen tries to listen on all interfaces (for debugging Mac
180+
// firewall dialogs in tests).
181+
func panicOnUnspecListen(ip IP) bool {
182+
if ip != nil && !ip.IsUnspecified() {
183+
return false
184+
}
185+
v := os.Getenv("TS_PANIC_ON_TEST_LISTEN_UNSPEC")
186+
if v == "" {
187+
return false
188+
}
189+
switch v[0] {
190+
case 't', 'T', '1':
191+
return true
192+
}
193+
return false
194+
}
195+
178196
func (sl *sysListener) listenTCP(ctx context.Context, laddr *TCPAddr) (*TCPListener, error) {
179197
return sl.listenTCPProto(ctx, laddr, 0)
180198
}
181199

182200
func (sl *sysListener) listenTCPProto(ctx context.Context, laddr *TCPAddr, proto int) (*TCPListener, error) {
183-
var ctrlCtxFn func(ctx context.Context, network, address string, c syscall.RawConn) error
201+
if panicOnUnspecListen(laddr.IP) {
202+
panic("tailscale: can't listen on unspecified address in test")
203+
}
204+
205+
var ctrlCtxFn func(cxt context.Context, network, address string, c syscall.RawConn) error
184206
if sl.ListenConfig.Control != nil {
185207
ctrlCtxFn = func(ctx context.Context, network, address string, c syscall.RawConn) error {
186208
return sl.ListenConfig.Control(network, address, c)

src/net/udpsock_posix.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ func (sd *sysDialer) dialUDP(ctx context.Context, laddr, raddr *UDPAddr) (*UDPCo
217217
}
218218

219219
func (sl *sysListener) listenUDP(ctx context.Context, laddr *UDPAddr) (*UDPConn, error) {
220-
var ctrlCtxFn func(ctx context.Context, network, address string, c syscall.RawConn) error
220+
if panicOnUnspecListen(laddr.IP) {
221+
panic("tailscale: can't listen on unspecified address in test")
222+
}
223+
224+
var ctrlCtxFn func(cxt context.Context, network, address string, c syscall.RawConn) error
221225
if sl.ListenConfig.Control != nil {
222226
ctrlCtxFn = func(ctx context.Context, network, address string, c syscall.RawConn) error {
223227
return sl.ListenConfig.Control(network, address, c)

0 commit comments

Comments
 (0)