helpers

package
v2.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertErrorContains

func AssertErrorContains(t *testing.T, err error, expectedMsg string)

AssertErrorContains fails the test if err is nil or if its message does not contain expectedMsg.

func AssertNoError

func AssertNoError(t *testing.T, err error)

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

func CompareJSON(t *testing.T, got, want []byte)

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

func CreateInvalidJSON() map[string][]byte

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

func CreateTestRootJSON(t *testing.T) []byte

CreateTestRootJSON returns BuildRootJSON() and registers a test helper. Prefer calling BuildRootJSON() directly when not inside a test function.

func CreateTestSnapshotJSON

func CreateTestSnapshotJSON(t *testing.T) []byte

CreateTestSnapshotJSON returns BuildSnapshotJSON() and registers a test helper.

func CreateTestTargetsJSON

func CreateTestTargetsJSON(t *testing.T) []byte

CreateTestTargetsJSON returns BuildTargetsJSON() and registers a test helper.

func CreateTestTimestampJSON

func CreateTestTimestampJSON(t *testing.T) []byte

CreateTestTimestampJSON returns BuildTimestampJSON() and registers a test helper.

func FuzzMetadataOperations

func FuzzMetadataOperations(f *testing.F, operation func(data []byte) error)

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

func GenerateTestKeyPair(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey)

GenerateTestKeyPair generates a fresh Ed25519 key pair. The test fails immediately if key generation fails.

func MustMarshal

func MustMarshal(t *testing.T, v any) []byte

MustMarshal marshals v to JSON. The test fails immediately if marshalling fails.

func MustUnmarshal

func MustUnmarshal[T any](t *testing.T, data []byte) T

MustUnmarshal unmarshals data into a value of type T. The test fails immediately if unmarshalling fails.

func ReadTestFile

func ReadTestFile(t *testing.T, path string) []byte

ReadTestFile reads and returns the content of a file. The test fails immediately if the read fails.

func StripWhitespace

func StripWhitespace(data []byte) []byte

StripWhitespace removes all ASCII whitespace characters from b.

func WriteTestFile

func WriteTestFile(t *testing.T, dir, filename string, content []byte) string

WriteTestFile writes content to a file in the given directory and returns the full path. The test fails immediately if the write fails.

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).

type HexBytes

type HexBytes []byte

HexBytes is a convenience type alias for testing scenarios that require hex-encoded byte slices without importing the full metadata package.

func (HexBytes) String

func (h HexBytes) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL