Documentation
¶
Overview ¶
Package helpers provides composable test utilities for go-tuf tests.
Design principles:
- All helpers accept *testing.T or *testing.B and call t.Helper() first.
- Use t.TempDir() instead of custom directory managers; the testing package cleans up automatically.
- No third-party assertion libraries: helpers signal failures via t.Errorf and t.Fatalf, matching the standard library style.
- JSON fixture builders do not depend on *testing.T so they can be used in fuzz seed functions without the &testing.T{} anti-pattern.
Index ¶
- func AssertErrorContains(t *testing.T, err error, expectedMsg string)
- func AssertNoError(t *testing.T, err error)
- func BuildRootJSON() []byte
- func BuildSnapshotJSON() []byte
- func BuildTargetsJSON() []byte
- func BuildTimestampJSON() []byte
- func CompareJSON(t *testing.T, got, want []byte)
- func CreateInvalidJSON() map[string][]byte
- func CreateTestRootJSON(t *testing.T) []byte
- func CreateTestSnapshotJSON(t *testing.T) []byte
- func CreateTestTargetsJSON(t *testing.T) []byte
- func CreateTestTimestampJSON(t *testing.T) []byte
- func FuzzMetadataOperations(f *testing.F, operation func(data []byte) error)
- func GenerateTestKeyPair(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey)
- func MustMarshal(t *testing.T, v any) []byte
- func MustUnmarshal[T any](t *testing.T, data []byte) T
- func ReadTestFile(t *testing.T, path string) []byte
- func StripWhitespace(data []byte) []byte
- func WriteTestFile(t *testing.T, dir, filename string, content []byte) string
- type FuzzDataGenerator
- func (f *FuzzDataGenerator) CreateFuzzTestMetadata(metadataType string) []byte
- func (f *FuzzDataGenerator) GenerateCorruptedJSON() []byte
- func (f *FuzzDataGenerator) GenerateRandomBytes(n int) []byte
- func (f *FuzzDataGenerator) GenerateRandomInt(max int) int
- func (f *FuzzDataGenerator) GenerateRandomJSON() []byte
- func (f *FuzzDataGenerator) GenerateRandomKey() map[string]any
- func (f *FuzzDataGenerator) GenerateRandomMetadataFields() map[string]any
- func (f *FuzzDataGenerator) GenerateRandomSignature() map[string]any
- func (f *FuzzDataGenerator) GenerateRandomString(length int) string
- func (f *FuzzDataGenerator) GenerateRandomTime() time.Time
- type HexBytes
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertErrorContains ¶
AssertErrorContains fails the test if err is nil or if its message does not contain expectedMsg.
func AssertNoError ¶
AssertNoError fails the test immediately if err is non-nil.
func BuildRootJSON ¶
func BuildRootJSON() []byte
BuildRootJSON returns a minimal, valid root.json body as JSON bytes.
func BuildSnapshotJSON ¶
func BuildSnapshotJSON() []byte
BuildSnapshotJSON returns a minimal, valid snapshot.json body as JSON bytes.
func BuildTargetsJSON ¶
func BuildTargetsJSON() []byte
BuildTargetsJSON returns a minimal, valid targets.json body as JSON bytes.
func BuildTimestampJSON ¶
func BuildTimestampJSON() []byte
BuildTimestampJSON returns a minimal, valid timestamp.json body as JSON bytes.
func CompareJSON ¶
CompareJSON asserts that got and want represent the same JSON value, normalising away whitespace differences. The test is marked failed (but not stopped) when they differ.
func CreateInvalidJSON ¶
CreateInvalidJSON returns a map of named byte slices that represent various kinds of invalid or malformed TUF metadata. Useful for error-path tests.
func CreateTestRootJSON ¶
CreateTestRootJSON returns BuildRootJSON() and registers a test helper. Prefer calling BuildRootJSON() directly when not inside a test function.
func CreateTestSnapshotJSON ¶
CreateTestSnapshotJSON returns BuildSnapshotJSON() and registers a test helper.
func CreateTestTargetsJSON ¶
CreateTestTargetsJSON returns BuildTargetsJSON() and registers a test helper.
func CreateTestTimestampJSON ¶
CreateTestTimestampJSON returns BuildTimestampJSON() and registers a test helper.
func FuzzMetadataOperations ¶
FuzzMetadataOperations registers seeds and runs f.Fuzz against operation. The operation must never panic regardless of input; errors are acceptable.
Seed data is taken from BuildRootJSON, BuildTargetsJSON, BuildSnapshotJSON, and BuildTimestampJSON (no *testing.T required) plus a selection of corrupted JSON variants.
func GenerateTestKeyPair ¶
GenerateTestKeyPair generates a fresh Ed25519 key pair. The test fails immediately if key generation fails.
func MustMarshal ¶
MustMarshal marshals v to JSON. The test fails immediately if marshalling fails.
func MustUnmarshal ¶
MustUnmarshal unmarshals data into a value of type T. The test fails immediately if unmarshalling fails.
func ReadTestFile ¶
ReadTestFile reads and returns the content of a file. The test fails immediately if the read fails.
func StripWhitespace ¶
StripWhitespace removes all ASCII whitespace characters from b.
Types ¶
type FuzzDataGenerator ¶
type FuzzDataGenerator struct {
// contains filtered or unexported fields
}
FuzzDataGenerator produces deterministic pseudo-random data for fuzz seeds. Use NewFuzzDataGenerator to construct one with a fixed seed for reproducible seed corpora, or use 0 for a randomly seeded source.
func NewFuzzDataGenerator ¶
func NewFuzzDataGenerator(seed1, seed2 uint64) *FuzzDataGenerator
NewFuzzDataGenerator returns a generator backed by a PCG source seeded with seed1 and seed2. For a single uint64 seed, pass the same value twice.
func (*FuzzDataGenerator) CreateFuzzTestMetadata ¶
func (f *FuzzDataGenerator) CreateFuzzTestMetadata(metadataType string) []byte
CreateFuzzTestMetadata returns a JSON-encoded TUF metadata structure for metadataType ("root", "targets", "snapshot", "timestamp") with random field values. Useful as fuzz seed input.
func (*FuzzDataGenerator) GenerateCorruptedJSON ¶
func (f *FuzzDataGenerator) GenerateCorruptedJSON() []byte
GenerateCorruptedJSON returns one of several varieties of intentionally malformed JSON, chosen pseudo-randomly.
func (*FuzzDataGenerator) GenerateRandomBytes ¶
func (f *FuzzDataGenerator) GenerateRandomBytes(n int) []byte
GenerateRandomBytes returns a slice of n pseudo-random bytes.
func (*FuzzDataGenerator) GenerateRandomInt ¶
func (f *FuzzDataGenerator) GenerateRandomInt(max int) int
GenerateRandomInt returns a non-negative pseudo-random int in [0, max).
func (*FuzzDataGenerator) GenerateRandomJSON ¶
func (f *FuzzDataGenerator) GenerateRandomJSON() []byte
GenerateRandomJSON returns a JSON object that resembles TUF metadata but with random field values.
func (*FuzzDataGenerator) GenerateRandomKey ¶
func (f *FuzzDataGenerator) GenerateRandomKey() map[string]any
GenerateRandomKey returns a random TUF-like key map.
func (*FuzzDataGenerator) GenerateRandomMetadataFields ¶
func (f *FuzzDataGenerator) GenerateRandomMetadataFields() map[string]any
GenerateRandomMetadataFields returns a map of random values for common TUF metadata fields.
func (*FuzzDataGenerator) GenerateRandomSignature ¶
func (f *FuzzDataGenerator) GenerateRandomSignature() map[string]any
GenerateRandomSignature returns a random TUF-like signature map.
func (*FuzzDataGenerator) GenerateRandomString ¶
func (f *FuzzDataGenerator) GenerateRandomString(length int) string
GenerateRandomString returns a random alphanumeric string of the given length.
func (*FuzzDataGenerator) GenerateRandomTime ¶
func (f *FuzzDataGenerator) GenerateRandomTime() time.Time
GenerateRandomTime returns a pseudo-random time between 2020 and 2030 (UTC).