tailscale/tstest/nettest/nettest.go
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
This file was never truly necessary and has never actually been used in
the history of Tailscale's open source releases.

A Brief History of AUTHORS files
---

The AUTHORS file was a pattern developed at Google, originally for
Chromium, then adopted by Go and a bunch of other projects. The problem
was that Chromium originally had a copyright line only recognizing
Google as the copyright holder. Because Google (and most open source
projects) do not require copyright assignemnt for contributions, each
contributor maintains their copyright. Some large corporate contributors
then tried to add their own name to the copyright line in the LICENSE
file or in file headers. This quickly becomes unwieldy, and puts a
tremendous burden on anyone building on top of Chromium, since the
license requires that they keep all copyright lines intact.

The compromise was to create an AUTHORS file that would list all of the
copyright holders. The LICENSE file and source file headers would then
include that list by reference, listing the copyright holder as "The
Chromium Authors".

This also become cumbersome to simply keep the file up to date with a
high rate of new contributors. Plus it's not always obvious who the
copyright holder is. Sometimes it is the individual making the
contribution, but many times it may be their employer. There is no way
for the proejct maintainer to know.

Eventually, Google changed their policy to no longer recommend trying to
keep the AUTHORS file up to date proactively, and instead to only add to
it when requested: https://opensource.google/docs/releasing/authors.
They are also clear that:

> Adding contributors to the AUTHORS file is entirely within the
> project's discretion and has no implications for copyright ownership.

It was primarily added to appease a small number of large contributors
that insisted that they be recognized as copyright holders (which was
entirely their right to do). But it's not truly necessary, and not even
the most accurate way of identifying contributors and/or copyright
holders.

In practice, we've never added anyone to our AUTHORS file. It only lists
Tailscale, so it's not really serving any purpose. It also causes
confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header
in other open source repos which don't actually have an AUTHORS file, so
it's ambiguous what that means.

Instead, we just acknowledge that the contributors to Tailscale (whoever
they are) are copyright holders for their individual contributions. We
also have the benefit of using the DCO (developercertificate.org) which
provides some additional certification of their right to make the
contribution.

The source file changes were purely mechanical with:

    git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g'

Updates #cleanup

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <will@tailscale.com>
2026-01-23 15:49:45 -08:00

119 lines
3.2 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
// Package nettest contains additional test helpers related to network state
// that can't go into tstest for circular dependency reasons.
package nettest
import (
"context"
"flag"
"net"
"net/http"
"net/http/httptest"
"sync"
"testing"
"tailscale.com/net/memnet"
"tailscale.com/net/netmon"
"tailscale.com/net/netx"
"tailscale.com/util/testenv"
)
var useMemNet = flag.Bool("use-test-memnet", false, "prefer using in-memory network for tests")
// SkipIfNoNetwork skips the test if it looks like there's no network
// access.
func SkipIfNoNetwork(t testing.TB) {
nm := netmon.NewStatic()
if !nm.InterfaceState().AnyInterfaceUp() {
t.Skip("skipping; test requires network but no interface is up")
}
}
// PreferMemNetwork reports whether the --use-test-memnet flag is set.
func PreferMemNetwork() bool {
return *useMemNet
}
// GetNetwork returns the appropriate Network implementation based on
// whether the --use-test-memnet flag is set.
//
// Each call generates a new network.
func GetNetwork(tb testing.TB) netx.Network {
var n netx.Network
if PreferMemNetwork() {
n = &memnet.Network{}
} else {
n = netx.RealNetwork()
}
detectLeaks := PreferMemNetwork() || !testenv.InParallelTest(tb)
if detectLeaks {
tb.Cleanup(func() {
// TODO: leak detection, making sure no connections
// remain at the end of the test. For real network,
// snapshot conns in pid table before & after.
})
}
return n
}
// NewHTTPServer starts and returns a new [httptest.Server].
// The caller should call Close when finished, to shut it down.
func NewHTTPServer(net netx.Network, handler http.Handler) *httptest.Server {
ts := NewUnstartedHTTPServer(net, handler)
ts.Start()
return ts
}
// NewUnstartedHTTPServer returns a new [httptest.Server] but doesn't start it.
//
// After changing its configuration, the caller should call Start or
// StartTLS.
//
// The caller should call Close when finished, to shut it down.
func NewUnstartedHTTPServer(nw netx.Network, handler http.Handler) *httptest.Server {
s := &httptest.Server{
Config: &http.Server{Handler: handler},
}
ln := nw.NewLocalTCPListener()
s.Listener = &listenerOnAddrOnce{
Listener: ln,
fn: func() {
c := s.Client()
if c == nil {
// This httptest.Server.Start initialization order has been true
// for over 10 years. Let's keep counting on it.
panic("httptest.Server: Client not initialized before Addr called")
}
if c.Transport == nil {
c.Transport = &http.Transport{}
}
tr := c.Transport.(*http.Transport)
if tr.Dial != nil || tr.DialContext != nil {
panic("unexpected non-nil Dial or DialContext in httptest.Server.Client.Transport")
}
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return nw.Dial(ctx, network, addr)
}
},
}
return s
}
// listenerOnAddrOnce is a net.Listener that wraps another net.Listener
// and calls a function the first time its Addr is called.
type listenerOnAddrOnce struct {
net.Listener
once sync.Once
fn func()
}
func (ln *listenerOnAddrOnce) Addr() net.Addr {
ln.once.Do(func() {
ln.fn()
})
return ln.Listener.Addr()
}