tailscale/tstest/chonktest/chonktest.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

304 lines
9.1 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
// Package chonktest contains a shared set of tests for the Chonk
// interface used to store AUM messages in Tailnet Lock, which we can
// share between different implementations.
package chonktest
import (
"bytes"
"encoding/binary"
"errors"
"math/rand"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"golang.org/x/crypto/blake2s"
"tailscale.com/tka"
"tailscale.com/util/must"
)
// returns a random source based on the test name + extraSeed.
func testingRand(t *testing.T, extraSeed int64) *rand.Rand {
var seed int64
if err := binary.Read(bytes.NewBuffer([]byte(t.Name())), binary.LittleEndian, &seed); err != nil {
panic(err)
}
return rand.New(rand.NewSource(seed + extraSeed))
}
// randHash derives a fake blake2s hash from the test name
// and the given seed.
func randHash(t *testing.T, seed int64) [blake2s.Size]byte {
var out [blake2s.Size]byte
testingRand(t, seed).Read(out[:])
return out
}
func hashesLess(x, y tka.AUMHash) bool {
return bytes.Compare(x[:], y[:]) < 0
}
func aumHashesLess(x, y tka.AUM) bool {
return hashesLess(x.Hash(), y.Hash())
}
// RunChonkTests is a set of tests for the behaviour of a Chonk.
//
// Any implementation of Chonk should pass these tests, so we know all
// Chonks behave in the same way. If you want to test behaviour that's
// specific to one implementation, write a separate test.
func RunChonkTests(t *testing.T, newChonk func(*testing.T) tka.Chonk) {
t.Run("ChildAUMs", func(t *testing.T) {
t.Parallel()
chonk := newChonk(t)
parentHash := randHash(t, 1)
data := []tka.AUM{
{
MessageKind: tka.AUMRemoveKey,
KeyID: []byte{1, 2},
PrevAUMHash: parentHash[:],
},
{
MessageKind: tka.AUMRemoveKey,
KeyID: []byte{3, 4},
PrevAUMHash: parentHash[:],
},
}
if err := chonk.CommitVerifiedAUMs(data); err != nil {
t.Fatalf("CommitVerifiedAUMs failed: %v", err)
}
stored, err := chonk.ChildAUMs(parentHash)
if err != nil {
t.Fatalf("ChildAUMs failed: %v", err)
}
if diff := cmp.Diff(data, stored, cmpopts.SortSlices(aumHashesLess)); diff != "" {
t.Errorf("stored AUM differs (-want, +got):\n%s", diff)
}
})
t.Run("AUMMissing", func(t *testing.T) {
t.Parallel()
chonk := newChonk(t)
var notExists tka.AUMHash
notExists[:][0] = 42
if _, err := chonk.AUM(notExists); err != os.ErrNotExist {
t.Errorf("chonk.AUM(notExists).err = %v, want %v", err, os.ErrNotExist)
}
})
t.Run("ReadChainFromHead", func(t *testing.T) {
t.Parallel()
chonk := newChonk(t)
genesis := tka.AUM{MessageKind: tka.AUMRemoveKey, KeyID: []byte{1, 2}}
gHash := genesis.Hash()
intermediate := tka.AUM{PrevAUMHash: gHash[:]}
iHash := intermediate.Hash()
leaf := tka.AUM{PrevAUMHash: iHash[:]}
commitSet := []tka.AUM{
genesis,
intermediate,
leaf,
}
if err := chonk.CommitVerifiedAUMs(commitSet); err != nil {
t.Fatalf("CommitVerifiedAUMs failed: %v", err)
}
t.Logf("genesis hash = %X", genesis.Hash())
t.Logf("intermediate hash = %X", intermediate.Hash())
t.Logf("leaf hash = %X", leaf.Hash())
// Read the chain from the leaf backwards.
gotLeafs, err := chonk.Heads()
if err != nil {
t.Fatalf("Heads failed: %v", err)
}
if diff := cmp.Diff([]tka.AUM{leaf}, gotLeafs); diff != "" {
t.Fatalf("leaf AUM differs (-want, +got):\n%s", diff)
}
parent, _ := gotLeafs[0].Parent()
gotIntermediate, err := chonk.AUM(parent)
if err != nil {
t.Fatalf("AUM(<intermediate>) failed: %v", err)
}
if diff := cmp.Diff(intermediate, gotIntermediate); diff != "" {
t.Errorf("intermediate AUM differs (-want, +got):\n%s", diff)
}
parent, _ = gotIntermediate.Parent()
gotGenesis, err := chonk.AUM(parent)
if err != nil {
t.Fatalf("AUM(<genesis>) failed: %v", err)
}
if diff := cmp.Diff(genesis, gotGenesis); diff != "" {
t.Errorf("genesis AUM differs (-want, +got):\n%s", diff)
}
})
t.Run("LastActiveAncestor", func(t *testing.T) {
t.Parallel()
chonk := newChonk(t)
aum := tka.AUM{MessageKind: tka.AUMRemoveKey, KeyID: []byte{1, 2}}
hash := aum.Hash()
if err := chonk.SetLastActiveAncestor(hash); err != nil {
t.Fatal(err)
}
got, err := chonk.LastActiveAncestor()
if err != nil {
t.Fatal(err)
}
if got == nil || hash.String() != got.String() {
t.Errorf("LastActiveAncestor=%s, want %s", got, hash)
}
})
}
// RunCompactableChonkTests is a set of tests for the behaviour of a
// CompactableChonk.
//
// Any implementation of CompactableChonk should pass these tests, so we
// know all CompactableChonk behave in the same way. If you want to test
// behaviour that's specific to one implementation, write a separate test.
func RunCompactableChonkTests(t *testing.T, newChonk func(t *testing.T) tka.CompactableChonk) {
t.Run("PurgeAUMs", func(t *testing.T) {
t.Parallel()
chonk := newChonk(t)
parentHash := randHash(t, 1)
aum := tka.AUM{MessageKind: tka.AUMNoOp, PrevAUMHash: parentHash[:]}
if err := chonk.CommitVerifiedAUMs([]tka.AUM{aum}); err != nil {
t.Fatal(err)
}
if err := chonk.PurgeAUMs([]tka.AUMHash{aum.Hash()}); err != nil {
t.Fatal(err)
}
if _, err := chonk.AUM(aum.Hash()); err != os.ErrNotExist {
t.Errorf("AUM() on purged AUM returned err = %v, want ErrNotExist", err)
}
})
t.Run("AllAUMs", func(t *testing.T) {
chonk := newChonk(t)
genesis := tka.AUM{MessageKind: tka.AUMRemoveKey, KeyID: []byte{1, 2}}
gHash := genesis.Hash()
intermediate := tka.AUM{PrevAUMHash: gHash[:]}
iHash := intermediate.Hash()
leaf := tka.AUM{PrevAUMHash: iHash[:]}
commitSet := []tka.AUM{
genesis,
intermediate,
leaf,
}
if err := chonk.CommitVerifiedAUMs(commitSet); err != nil {
t.Fatalf("CommitVerifiedAUMs failed: %v", err)
}
hashes, err := chonk.AllAUMs()
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff([]tka.AUMHash{genesis.Hash(), intermediate.Hash(), leaf.Hash()}, hashes, cmpopts.SortSlices(hashesLess)); diff != "" {
t.Fatalf("AllAUMs() output differs (-want, +got):\n%s", diff)
}
})
t.Run("ChildAUMsOfPurgedAUM", func(t *testing.T) {
t.Parallel()
chonk := newChonk(t)
parent := tka.AUM{MessageKind: tka.AUMRemoveKey, KeyID: []byte{0, 0}}
parentHash := parent.Hash()
child1 := tka.AUM{MessageKind: tka.AUMAddKey, KeyID: []byte{1, 1}, PrevAUMHash: parentHash[:]}
child2 := tka.AUM{MessageKind: tka.AUMAddKey, KeyID: []byte{2, 2}, PrevAUMHash: parentHash[:]}
child3 := tka.AUM{MessageKind: tka.AUMAddKey, KeyID: []byte{3, 3}, PrevAUMHash: parentHash[:]}
child2Hash := child2.Hash()
grandchild2A := tka.AUM{MessageKind: tka.AUMAddKey, KeyID: []byte{2, 2, 2, 2}, PrevAUMHash: child2Hash[:]}
grandchild2B := tka.AUM{MessageKind: tka.AUMAddKey, KeyID: []byte{2, 2, 2, 2, 2}, PrevAUMHash: child2Hash[:]}
commitSet := []tka.AUM{parent, child1, child2, child3, grandchild2A, grandchild2B}
if err := chonk.CommitVerifiedAUMs(commitSet); err != nil {
t.Fatalf("CommitVerifiedAUMs failed: %v", err)
}
// Check the set of hashes is correct
childHashes := must.Get(chonk.ChildAUMs(parentHash))
if diff := cmp.Diff([]tka.AUM{child1, child2, child3}, childHashes, cmpopts.SortSlices(aumHashesLess)); diff != "" {
t.Fatalf("ChildAUMs() output differs (-want, +got):\n%s", diff)
}
// Purge the parent AUM, and check the set of child AUMs is unchanged
chonk.PurgeAUMs([]tka.AUMHash{parent.Hash()})
childHashes = must.Get(chonk.ChildAUMs(parentHash))
if diff := cmp.Diff([]tka.AUM{child1, child2, child3}, childHashes, cmpopts.SortSlices(aumHashesLess)); diff != "" {
t.Fatalf("ChildAUMs() output differs (-want, +got):\n%s", diff)
}
// Now purge one of the child AUMs, and check it no longer appears as a child of the parent
chonk.PurgeAUMs([]tka.AUMHash{child3.Hash()})
childHashes = must.Get(chonk.ChildAUMs(parentHash))
if diff := cmp.Diff([]tka.AUM{child1, child2}, childHashes, cmpopts.SortSlices(aumHashesLess)); diff != "" {
t.Fatalf("ChildAUMs() output differs (-want, +got):\n%s", diff)
}
})
t.Run("RemoveAll", func(t *testing.T) {
t.Parallel()
chonk := newChonk(t)
parentHash := randHash(t, 1)
data := []tka.AUM{
{
MessageKind: tka.AUMRemoveKey,
KeyID: []byte{1, 2},
PrevAUMHash: parentHash[:],
},
{
MessageKind: tka.AUMRemoveKey,
KeyID: []byte{3, 4},
PrevAUMHash: parentHash[:],
},
}
if err := chonk.CommitVerifiedAUMs(data); err != nil {
t.Fatalf("CommitVerifiedAUMs failed: %v", err)
}
// Check we can retrieve the AUMs we just stored
for _, want := range data {
got, err := chonk.AUM(want.Hash())
if err != nil {
t.Fatalf("could not get %s: %v", want.Hash(), err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("stored AUM %s differs (-want, +got):\n%s", want.Hash(), diff)
}
}
// Call RemoveAll() to drop all the AUM state
if err := chonk.RemoveAll(); err != nil {
t.Fatalf("RemoveAll failed: %v", err)
}
// Check we can no longer retrieve the previously-stored AUMs
for _, want := range data {
aum, err := chonk.AUM(want.Hash())
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("expected os.ErrNotExist for %s, instead got aum=%v, err=%v", want.Hash(), aum, err)
}
}
})
}