mirror of
https://github.com/tailscale/tailscale.git
synced 2026-02-09 09:41:49 +01:00
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>
283 lines
6.8 KiB
Go
283 lines
6.8 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package dnscache
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/net/dns/dnsmessage"
|
|
"tailscale.com/tstest"
|
|
)
|
|
|
|
func TestMessageCache(t *testing.T) {
|
|
clock := tstest.NewClock(tstest.ClockOpts{
|
|
Start: time.Date(1987, 11, 1, 0, 0, 0, 0, time.UTC),
|
|
})
|
|
mc := &MessageCache{Clock: clock.Now}
|
|
mc.SetMaxCacheSize(2)
|
|
clock.Advance(time.Second)
|
|
|
|
var out bytes.Buffer
|
|
if err := mc.ReplyFromCache(&out, makeQ(1, "foo.com.")); err != ErrCacheMiss {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if err := mc.AddCacheEntry(
|
|
makeQ(2, "foo.com."),
|
|
makeRes(2, "FOO.COM.", ttlOpt(10),
|
|
&dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}},
|
|
&dnsmessage.AResource{A: [4]byte{127, 0, 0, 2}})); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Expect cache hit, with 10 seconds remaining.
|
|
out.Reset()
|
|
if err := mc.ReplyFromCache(&out, makeQ(3, "foo.com.")); err != nil {
|
|
t.Fatalf("expected cache hit; got: %v", err)
|
|
}
|
|
if p := mustParseResponse(t, out.Bytes()); p.TxID != 3 {
|
|
t.Errorf("TxID = %v; want %v", p.TxID, 3)
|
|
} else if p.TTL != 10 {
|
|
t.Errorf("TTL = %v; want 10", p.TTL)
|
|
}
|
|
|
|
// One second elapses, expect a cache hit, with 9 seconds
|
|
// remaining.
|
|
clock.Advance(time.Second)
|
|
out.Reset()
|
|
if err := mc.ReplyFromCache(&out, makeQ(4, "foo.com.")); err != nil {
|
|
t.Fatalf("expected cache hit; got: %v", err)
|
|
}
|
|
if p := mustParseResponse(t, out.Bytes()); p.TxID != 4 {
|
|
t.Errorf("TxID = %v; want %v", p.TxID, 4)
|
|
} else if p.TTL != 9 {
|
|
t.Errorf("TTL = %v; want 9", p.TTL)
|
|
}
|
|
|
|
// Expect cache miss on MX record.
|
|
if err := mc.ReplyFromCache(&out, makeQ(4, "foo.com.", dnsmessage.TypeMX)); err != ErrCacheMiss {
|
|
t.Fatalf("expected cache miss on MX; got: %v", err)
|
|
}
|
|
// Expect cache miss on CHAOS class.
|
|
if err := mc.ReplyFromCache(&out, makeQ(4, "foo.com.", dnsmessage.ClassCHAOS)); err != ErrCacheMiss {
|
|
t.Fatalf("expected cache miss on CHAOS; got: %v", err)
|
|
}
|
|
|
|
// Ten seconds elapses; expect a cache miss.
|
|
clock.Advance(10 * time.Second)
|
|
if err := mc.ReplyFromCache(&out, makeQ(5, "foo.com.")); err != ErrCacheMiss {
|
|
t.Fatalf("expected cache miss, got: %v", err)
|
|
}
|
|
}
|
|
|
|
type parsedMeta struct {
|
|
TxID uint16
|
|
TTL uint32
|
|
}
|
|
|
|
func mustParseResponse(t testing.TB, r []byte) (ret parsedMeta) {
|
|
t.Helper()
|
|
var p dnsmessage.Parser
|
|
h, err := p.Start(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ret.TxID = h.ID
|
|
qq, err := p.AllQuestions()
|
|
if err != nil {
|
|
t.Fatalf("AllQuestions: %v", err)
|
|
}
|
|
if len(qq) != 1 {
|
|
t.Fatalf("num questions = %v; want 1", len(qq))
|
|
}
|
|
aa, err := p.AllAnswers()
|
|
if err != nil {
|
|
t.Fatalf("AllAnswers: %v", err)
|
|
}
|
|
for _, r := range aa {
|
|
if ret.TTL == 0 {
|
|
ret.TTL = r.Header.TTL
|
|
}
|
|
if ret.TTL != r.Header.TTL {
|
|
t.Fatal("mixed TTLs")
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
type responseOpt bool
|
|
|
|
type ttlOpt uint32
|
|
|
|
func makeQ(txID uint16, name string, opt ...any) []byte {
|
|
opt = append(opt, responseOpt(false))
|
|
return makeDNSPkt(txID, name, opt...)
|
|
}
|
|
|
|
func makeRes(txID uint16, name string, opt ...any) []byte {
|
|
opt = append(opt, responseOpt(true))
|
|
return makeDNSPkt(txID, name, opt...)
|
|
}
|
|
|
|
func makeDNSPkt(txID uint16, name string, opt ...any) []byte {
|
|
typ := dnsmessage.TypeA
|
|
class := dnsmessage.ClassINET
|
|
var response bool
|
|
var answers []dnsmessage.ResourceBody
|
|
var ttl uint32 = 1 // one second by default
|
|
for _, o := range opt {
|
|
switch o := o.(type) {
|
|
case dnsmessage.Type:
|
|
typ = o
|
|
case dnsmessage.Class:
|
|
class = o
|
|
case responseOpt:
|
|
response = bool(o)
|
|
case dnsmessage.ResourceBody:
|
|
answers = append(answers, o)
|
|
case ttlOpt:
|
|
ttl = uint32(o)
|
|
default:
|
|
panic(fmt.Sprintf("unknown opt type %T", o))
|
|
}
|
|
}
|
|
qname := dnsmessage.MustNewName(name)
|
|
msg := dnsmessage.Message{
|
|
Header: dnsmessage.Header{ID: txID, Response: response},
|
|
Questions: []dnsmessage.Question{
|
|
{
|
|
Name: qname,
|
|
Type: typ,
|
|
Class: class,
|
|
},
|
|
},
|
|
}
|
|
for _, rb := range answers {
|
|
msg.Answers = append(msg.Answers, dnsmessage.Resource{
|
|
Header: dnsmessage.ResourceHeader{
|
|
Name: qname,
|
|
Type: typ,
|
|
Class: class,
|
|
TTL: ttl,
|
|
},
|
|
Body: rb,
|
|
})
|
|
}
|
|
buf, err := msg.Pack()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return buf
|
|
}
|
|
|
|
func TestASCIILowerName(t *testing.T) {
|
|
n := asciiLowerName(dnsmessage.MustNewName("Foo.COM."))
|
|
if got, want := n.String(), "foo.com."; got != want {
|
|
t.Errorf("got = %q; want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestGetDNSQueryCacheKey(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
pkt []byte
|
|
want msgQ
|
|
txID uint16
|
|
anyTX bool
|
|
}{
|
|
{
|
|
name: "empty",
|
|
},
|
|
{
|
|
name: "a",
|
|
pkt: makeQ(123, "foo.com."),
|
|
want: msgQ{"foo.com.", dnsmessage.TypeA},
|
|
txID: 123,
|
|
},
|
|
{
|
|
name: "aaaa",
|
|
pkt: makeQ(6, "foo.com.", dnsmessage.TypeAAAA),
|
|
want: msgQ{"foo.com.", dnsmessage.TypeAAAA},
|
|
txID: 6,
|
|
},
|
|
{
|
|
name: "normalize_case",
|
|
pkt: makeQ(123, "FoO.CoM."),
|
|
want: msgQ{"foo.com.", dnsmessage.TypeA},
|
|
txID: 123,
|
|
},
|
|
{
|
|
name: "ignore_response",
|
|
pkt: makeRes(123, "foo.com."),
|
|
},
|
|
{
|
|
name: "ignore_question_with_answers",
|
|
pkt: makeQ(2, "foo.com.", &dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}}),
|
|
},
|
|
{
|
|
name: "whatever_go_generates", // in case Go's net package grows functionality we don't handle
|
|
pkt: getGoNetPacketDNSQuery("from-go.foo."),
|
|
want: msgQ{"from-go.foo.", dnsmessage.TypeA},
|
|
anyTX: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, gotTX, ok := getDNSQueryCacheKey(tt.pkt)
|
|
if !ok {
|
|
if tt.txID == 0 && got == (msgQ{}) {
|
|
return
|
|
}
|
|
t.Fatal("failed")
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("got %+v, want %+v", got, tt.want)
|
|
}
|
|
if gotTX != tt.txID && !tt.anyTX {
|
|
t.Errorf("got tx %v, want %v", gotTX, tt.txID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func getGoNetPacketDNSQuery(name string) []byte {
|
|
res := make(chan []byte, 1)
|
|
r := &net.Resolver{
|
|
PreferGo: true,
|
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
return goResolverConn(res), nil
|
|
},
|
|
}
|
|
r.LookupIP(context.Background(), "ip4", name)
|
|
return <-res
|
|
}
|
|
|
|
type goResolverConn chan<- []byte
|
|
|
|
func (goResolverConn) Close() error { return nil }
|
|
func (goResolverConn) LocalAddr() net.Addr { return todoAddr{} }
|
|
func (goResolverConn) RemoteAddr() net.Addr { return todoAddr{} }
|
|
func (goResolverConn) SetDeadline(t time.Time) error { return nil }
|
|
func (goResolverConn) SetReadDeadline(t time.Time) error { return nil }
|
|
func (goResolverConn) SetWriteDeadline(t time.Time) error { return nil }
|
|
func (goResolverConn) Read([]byte) (int, error) { return 0, errors.New("boom") }
|
|
func (c goResolverConn) Write(p []byte) (int, error) {
|
|
select {
|
|
case c <- p[2:]: // skip 2 byte length for TCP mode DNS query
|
|
default:
|
|
}
|
|
return 0, errors.New("boom")
|
|
}
|
|
|
|
type todoAddr struct{}
|
|
|
|
func (todoAddr) Network() string { return "unused" }
|
|
func (todoAddr) String() string { return "unused-todoAddr" }
|