test

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: Apache-2.0 Imports: 14 Imported by: 7

Documentation

Overview

Package test contains generic tests for channel backend implementations and random generators of Params, States etc. for tests.

Index

Constants

This section is empty.

Variables

View Source
var (
	// MaxBalance is the maximum balance used for testing.
	// It is set to 2 ^ 128 - 1 since when 2 ^ 256 - 1 is used, the faucet
	// key is depleted.
	// The production limit can be found in `go-perun/channel.MaxBalance`.
	MaxBalance = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 128), big.NewInt(1)) //nolint:gomnd

)

Functions

func GenericBackendTest

func GenericBackendTest(t *testing.T, s *Setup, opts ...GenericTestOption)

GenericBackendTest tests the interface functions of the global channel.Backend with the passed test data.

func GenericStateEqualTest added in v0.3.0

func GenericStateEqualTest(t *testing.T, s1, s2 *channel.State, opts ...GenericTestOption)

GenericStateEqualTest tests the State.Equal function.

func NewRandomAllocation

func NewRandomAllocation(rng *rand.Rand, opts ...RandomOpt) *channel.Allocation

NewRandomAllocation generates a new random `channel.Allocation`. Options: all from `NewRandomAssets`, `NewRandomBalances` and `NewRandomLocked`.

func NewRandomApp

func NewRandomApp(rng *rand.Rand, opts ...RandomOpt) channel.App

NewRandomApp creates a new random channel.App.

func NewRandomAppAndData added in v0.5.0

func NewRandomAppAndData(rng *rand.Rand, opts ...RandomOpt) (channel.App, channel.Data)

NewRandomAppAndData creates a new random channel.App and new random channel.Data.

func NewRandomAppID added in v0.11.0

func NewRandomAppID(rng *rand.Rand) channel.AppID

NewRandomAppID creates a new random channel.AppID.

func NewRandomAsset

func NewRandomAsset(rng *rand.Rand) channel.Asset

NewRandomAsset generates a new random `channel.Asset`.

func NewRandomAssets added in v0.2.1

func NewRandomAssets(rng *rand.Rand, opts ...RandomOpt) []channel.Asset

NewRandomAssets generates new random `channel.Asset`s. Options: `WithAssets` and `WithNumAssets`.

func NewRandomBal

func NewRandomBal(rng *rand.Rand, opts ...RandomOpt) channel.Bal

NewRandomBal generates a new random `channel.Bal`. Options: `WithBalancesRange`.

func NewRandomBalances added in v0.2.1

func NewRandomBalances(rng *rand.Rand, opts ...RandomOpt) channel.Balances

NewRandomBalances generates a new random `channel.Balances`. Options: `WithBalances`, `WithNumAssets`, `WithNumParts` and all from `NewRandomBals`.

func NewRandomBals

func NewRandomBals(rng *rand.Rand, numBals int, opts ...RandomOpt) []channel.Bal

NewRandomBals generates new random `channel.Bal`s. Options: all from `NewRandomBal`.

func NewRandomChannelID

func NewRandomChannelID(rng *rand.Rand, opts ...RandomOpt) (id channel.ID)

NewRandomChannelID generates a new random `channel.ID`. Options: `WithID`.

func NewRandomChannelIDs added in v0.7.0

func NewRandomChannelIDs(rng *rand.Rand, n int) (ids []channel.ID)

NewRandomChannelIDs generates a list of random channel IDs.

func NewRandomData

func NewRandomData(rng *rand.Rand, opts ...RandomOpt) channel.Data

NewRandomData creates new random data for an app.

func NewRandomIndexMap added in v0.7.0

func NewRandomIndexMap(rng *rand.Rand, numParts int, numPartsParent int) (m []channel.Index)

NewRandomIndexMap generates a random index map.

func NewRandomIndexMaps added in v0.7.0

func NewRandomIndexMaps(rng *rand.Rand, numParts int, numPartsParent int) (maps [][]channel.Index)

NewRandomIndexMaps generates a list of random index maps.

func NewRandomLocked added in v0.2.1

func NewRandomLocked(rng *rand.Rand, opts ...RandomOpt) []channel.SubAlloc

NewRandomLocked generates new random `channel.SubAlloc`s. Options: `WithLocked`, `WithNumLocked` and all from `NewRandomLockedIDs`.

func NewRandomLockedIDs added in v0.2.1

func NewRandomLockedIDs(rng *rand.Rand, opts ...RandomOpt) []channel.ID

NewRandomLockedIDs generates new random `channel.ID`s used in `channel.SubAlloc`. Options: `WithLockedIDs` and `WithNumLocked`.

func NewRandomParams

func NewRandomParams(rng *rand.Rand, opts ...RandomOpt) *channel.Params

NewRandomParams generates a new random `channel.Params`. Options: `WithParams`, `WithNumParts`, `WithParts`, `WithFirstPart`, `WithNonce`, `WithChallengeDuration` and all from `NewRandomApp`.

func NewRandomParamsAndState added in v0.2.1

func NewRandomParamsAndState(rng *rand.Rand, opts ...RandomOpt) (params *channel.Params, state *channel.State)

NewRandomParamsAndState generates a new random `channel.Params` and `channel.State`. Options: all from `NewRandomParams` and `NewRandomState`.

func NewRandomPhase added in v0.6.0

func NewRandomPhase(rng *rand.Rand) channel.Phase

NewRandomPhase generates a random channel machine phase.

func NewRandomState

func NewRandomState(rng *rand.Rand, opts ...RandomOpt) (state *channel.State)

NewRandomState generates a new random `channel.State`. Options: `WithState`, `WithVersion`, `WithIsFinal` and all from `NewRandomChannelID`, `NewRandomApp`, `NewRandomAllocation` and `NewRandomData`.

func NewRandomSubAlloc

func NewRandomSubAlloc(rng *rand.Rand, opts ...RandomOpt) *channel.SubAlloc

NewRandomSubAlloc generates a new random `channel.SubAlloc`. Options: `WithLockedID`, `WithLockedBals` and all from `NewRandomBals`.

func NewRandomTimeout added in v0.6.0

func NewRandomTimeout(rng *rand.Rand) channel.Timeout

NewRandomTimeout creates a new random timeout object.

func NewRandomTransaction added in v0.2.1

func NewRandomTransaction(rng *rand.Rand, sigMask []bool, opts ...RandomOpt) *channel.Transaction

NewRandomTransaction generates a new random `channel.Transaction`. `sigMask` defines which signatures are generated. `len(sigmask)` is assumed to be the number of participants. If an entry is false, nil is set as signature for that index. Options: all from `NewRandomParamsAndState`.

func SetAppRandomizer

func SetAppRandomizer(r AppRandomizer)

SetAppRandomizer sets the global `appRandomizer`.

func SetNewRandomAppID added in v0.11.0

func SetNewRandomAppID(f NewRandomAppIDFunc)

SetNewRandomAppID sets the function generating a new app identifier.

func SetRandomizer

func SetRandomizer(r Randomizer)

SetRandomizer sets the global Randomizer variable.

func ShuffleBalances added in v0.5.0

func ShuffleBalances(rng *rand.Rand, b channel.Balances) channel.Balances

ShuffleBalances shuffles the balances of the participants per asset and returns it. The returned `Balance` has the same `Sum()` value.

Types

type AppRandomizer

type AppRandomizer interface {
	NewRandomApp(*rand.Rand) channel.App
	NewRandomData(*rand.Rand) channel.Data
}

The AppRandomizer interface provides functionality for creating random data and apps which is useful for testing.

type GenericTestOption added in v0.8.0

type GenericTestOption int

GenericTestOption can be used to control the behaviour of generic tests.

const (
	// IgnoreAssets ignores the Assets in tests that support it.
	IgnoreAssets GenericTestOption = iota
	// IgnoreApp ignores the App in tests that support it.
	IgnoreApp
)

type GenericTestOptions added in v0.8.0

type GenericTestOptions map[GenericTestOption]bool

GenericTestOptions is a collection of GenericTestOption.

type MockAppRandomizer

type MockAppRandomizer struct {
	// contains filtered or unexported fields
}

MockAppRandomizer implements the AppRandomizer interface.

func NewMockAppRandomizer added in v0.11.0

func NewMockAppRandomizer() *MockAppRandomizer

NewMockAppRandomizer creates a new instance of MockAppRandomizer with a unique identifier.

func (*MockAppRandomizer) Equal added in v0.11.0

func (m *MockAppRandomizer) Equal(other *MockAppRandomizer) bool

Equal returns false for any comparison, ensuring two MockAppRandomizers are always considered different.

func (MockAppRandomizer) NewRandomApp

func (MockAppRandomizer) NewRandomApp(rng *rand.Rand) channel.App

NewRandomApp creates a new MockApp with a random address.

func (MockAppRandomizer) NewRandomData

func (MockAppRandomizer) NewRandomData(rng *rand.Rand) channel.Data

NewRandomData creates a new MockOp with a random operation.

type NewRandomAppIDFunc added in v0.11.0

type NewRandomAppIDFunc = func(*rand.Rand) channel.AppID

NewRandomAppIDFunc is an app identifier randomizer function.

type RandomOpt added in v0.2.1

type RandomOpt map[string]interface{}

RandomOpt defines a map of options than can be passed to `NewRandomX` functions in order to alter their default behaviour. Should only be constructed by `WithX` functions.

func WithAllocation added in v0.2.1

func WithAllocation(alloc *channel.Allocation) RandomOpt

WithAllocation sets the `Allocation` that should be used. Also defines `WithAssets`, `WithBalances` and `WithLocked`.

func WithApp added in v0.2.1

func WithApp(app channel.App) RandomOpt

WithApp sets the `App` that should be used. Also defines `WithDef`.

func WithAppData added in v0.2.1

func WithAppData(data channel.Data) RandomOpt

WithAppData sets the `AppData` that should be used.

func WithAppDef added in v0.2.1

func WithAppDef(appDef wallet.Address) RandomOpt

WithAppDef sets the `AppDef` that should be used.

func WithAppRandomizer added in v0.5.0

func WithAppRandomizer(randomizer AppRandomizer) RandomOpt

WithAppRandomizer sets the `AppRandomizer` that should be used.

func WithAssets added in v0.2.1

func WithAssets(assets ...channel.Asset) RandomOpt

WithAssets sets the `Assets` that should be used. Also sets `WithNumAssets`.

func WithBalances added in v0.2.1

func WithBalances(balances ...[]channel.Bal) RandomOpt

WithBalances sets the `Balances` that should be used in a generated Allocation. Also sets `WithNumAssets` and `WithNumParts` iff `balances` is not empty.

func WithBalancesInRange added in v0.2.1

func WithBalancesInRange(min, max channel.Bal) RandomOpt

WithBalancesInRange sets the range within which balances are randomly generated to [min, max].

func WithChallengeDuration added in v0.2.1

func WithChallengeDuration(d uint64) RandomOpt

WithChallengeDuration sets the `ChallengeDuration` that should be used.

func WithFirstPart added in v0.2.1

func WithFirstPart(part wallet.Address) RandomOpt

WithFirstPart sets the first participant that should be used in randomly generated Params. Overrides `WithParts`.

func WithID added in v0.2.1

func WithID(id channel.ID) RandomOpt

WithID sets the channel ID that should be used.

func WithIsFinal added in v0.2.1

func WithIsFinal(isFinal bool) RandomOpt

WithIsFinal sets whether the generated State is final.

func WithLedgerChannel added in v0.7.0

func WithLedgerChannel(ledger bool) RandomOpt

WithLedgerChannel sets the `LedgerChannel` attribute.

func WithLocked added in v0.2.1

func WithLocked(locked ...channel.SubAlloc) RandomOpt

WithLocked sets the `Locked` sub-allocations in the generated Allocation. Also sets `WithNumLocked` and `WithNumAssets` iff `locked` is not empty.

func WithLockedBals added in v0.2.1

func WithLockedBals(bals ...channel.Bal) RandomOpt

WithLockedBals causes exactly one sub-allocation with the given balances to be generated in the Allocation. Also sets `WithNumAssets` and `WithNumLocked` to 1.

func WithLockedID added in v0.2.1

func WithLockedID(id channel.ID) RandomOpt

WithLockedID sets the channel id that should be used when generating a single sub-allocation with `NewRandomSubAlloc`.

func WithLockedIDs added in v0.2.1

func WithLockedIDs(ids ...channel.ID) RandomOpt

WithLockedIDs sets the locked channel ids that should be used. Also sets `WithNumLocked`.

func WithNonce added in v0.2.1

func WithNonce(nonce channel.Nonce) RandomOpt

WithNonce sets the `Nonce` that should be used.

func WithNumAssets added in v0.2.1

func WithNumAssets(numAssets int) RandomOpt

WithNumAssets sets the `NumAssets` that should be used.

func WithNumLocked added in v0.2.1

func WithNumLocked(numLocked int) RandomOpt

WithNumLocked sets the `NumLocked` that should be used.

func WithNumParts added in v0.2.1

func WithNumParts(numParts int) RandomOpt

WithNumParts sets the `NumParts` that should be used.

func WithParams added in v0.2.1

func WithParams(params *channel.Params) RandomOpt

WithParams sets the `Params` that should be used. Also sets `WithID`, `WithChallengeDuration`, `WithParts`, `WithApp` and `WithNonce`.

func WithParts added in v0.2.1

func WithParts(parts ...wallet.Address) RandomOpt

WithParts sets the `Parts` that should be used when generating Params. Also sets `WithNumParts`.

func WithState added in v0.2.1

func WithState(state *channel.State) RandomOpt

WithState sets the `State` that should be used. Also sets `WithID`, `WithVersion`, `WithApp`, `WithAllocation`, `WithAppData` and `WithIsFinal`.

func WithVersion added in v0.2.1

func WithVersion(version uint64) RandomOpt

WithVersion sets the `Version` that should be used when generating a State.

func WithVirtualChannel added in v0.7.0

func WithVirtualChannel(b bool) RandomOpt

WithVirtualChannel sets the `VirtualChannel` attribute.

func WithoutApp added in v0.5.0

func WithoutApp() RandomOpt

WithoutApp configures a NoApp and NoData.

func (RandomOpt) Allocation added in v0.2.1

func (o RandomOpt) Allocation() *channel.Allocation

Allocation returns the `Allocation` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) App added in v0.2.1

func (o RandomOpt) App() channel.App

App returns the `App` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) AppData added in v0.2.1

func (o RandomOpt) AppData() channel.Data

AppData returns the `AppData` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) AppDef added in v0.2.1

func (o RandomOpt) AppDef() channel.AppID

AppDef returns the `AppDef` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) AppRandomizer added in v0.5.0

func (o RandomOpt) AppRandomizer() AppRandomizer

AppRandomizer returns the `AppRandomizer` value of the `RandomOpt`. If not present, returns the default appRandomizer.

func (RandomOpt) Append added in v0.2.1

func (o RandomOpt) Append(opts ...RandomOpt) RandomOpt

Append inserts all `opts` into the receiving object and returns the result. Overrides entries that occur more than once with the last occurrence.

func (RandomOpt) Assets added in v0.2.1

func (o RandomOpt) Assets() []channel.Asset

Assets returns the `Assets` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) Balances added in v0.2.1

func (o RandomOpt) Balances() channel.Balances

Balances returns the `Balances` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) BalancesRange added in v0.2.1

func (o RandomOpt) BalancesRange() (min, max channel.Bal)

BalancesRange returns the `BalancesRange` value of the `RandomOpt`. If not present, returns nil,nil.

func (RandomOpt) ChallengeDuration added in v0.2.1

func (o RandomOpt) ChallengeDuration(rng *rand.Rand) uint64

ChallengeDuration returns the `ChallengeDuration` value of the `RandomOpt`. If not present, a random value is generated with `rng` as entropy source.

func (RandomOpt) FirstPart added in v0.2.1

func (o RandomOpt) FirstPart() wallet.Address

FirstPart returns the `FirstPart` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) ID added in v0.2.1

func (o RandomOpt) ID() (id channel.ID, valid bool)

ID returns the `ID` value of the `RandomOpt`. If not present, returns `false` as second argument.

func (RandomOpt) IsFinal added in v0.2.1

func (o RandomOpt) IsFinal(rng *rand.Rand) bool

IsFinal returns the `IsFinal` value of the `RandomOpt`. If not present, a random value is generated with `rng` as entropy source.

func (RandomOpt) LedgerChannel added in v0.7.0

func (o RandomOpt) LedgerChannel(rng io.Reader) bool

LedgerChannel returns the `LedgerChannel` value of the `RandomOpt`. If not present, a random value is generated with `rng` as entropy source.

func (RandomOpt) Locked added in v0.2.1

func (o RandomOpt) Locked() (locked []channel.SubAlloc, valid bool)

Locked returns the `Locked` value of the `RandomOpt`. If not present, returns `false` as second argument.

func (RandomOpt) LockedBals added in v0.2.1

func (o RandomOpt) LockedBals() []channel.Bal

LockedBals returns the `LockedBals` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) LockedID added in v0.2.1

func (o RandomOpt) LockedID(rng *rand.Rand) channel.ID

LockedID returns the `LockedID` value of the `RandomOpt`. If not present, a random value is generated with `rng` as entropy source.

func (RandomOpt) LockedIDs added in v0.2.1

func (o RandomOpt) LockedIDs(rng *rand.Rand) (ids []channel.ID)

LockedIDs returns the `LockedIDs` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) Nonce added in v0.2.1

func (o RandomOpt) Nonce(rng io.Reader) channel.Nonce

Nonce returns the `Nonce` value of the `RandomOpt`. If not present, a random value is generated with `rng` as entropy source.

func (RandomOpt) NumAssets added in v0.2.1

func (o RandomOpt) NumAssets(rng *rand.Rand) int

NumAssets returns the `NumAssets` value of the `RandomOpt`. If not present, a random value is generated with `rng` as entropy source.

func (RandomOpt) NumLocked added in v0.2.1

func (o RandomOpt) NumLocked(rng *rand.Rand) int

NumLocked returns the `NumLocked` value of the `RandomOpt`. If not present, returns 0.

func (RandomOpt) NumParts added in v0.2.1

func (o RandomOpt) NumParts(rng *rand.Rand) int

NumParts returns the `NumParts` value of the `RandomOpt`. If not present, a random value between 2 and 11 is generated with `rng` as entropy source.

func (RandomOpt) Params added in v0.2.1

func (o RandomOpt) Params() *channel.Params

Params returns the `Params` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) Parts added in v0.2.1

func (o RandomOpt) Parts() []wallet.Address

Parts returns the `Parts` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) State added in v0.2.1

func (o RandomOpt) State() *channel.State

State returns the `State` value of the `RandomOpt`. If not present, returns nil.

func (RandomOpt) Version added in v0.2.1

func (o RandomOpt) Version(rng *rand.Rand) uint64

Version returns the `Version` value of the `RandomOpt`. If not present, a random value is generated with `rng` as entropy source.

func (RandomOpt) VirtualChannel added in v0.7.0

func (o RandomOpt) VirtualChannel(rng io.Reader) bool

VirtualChannel returns the `VirtualChannel` value of the `RandomOpt`. If not present, a random value is generated with `rng` as entropy source.

type Randomizer

type Randomizer interface {
	NewRandomAsset(*rand.Rand) channel.Asset
}

The Randomizer interface provides the ability to create random assets. This is useful for testing.

type Setup

type Setup struct {
	// Params are the random parameters of `State`
	Params *channel.Params
	// Params2 are the parameters of `State2` and must differ in all fields from `Params`
	Params2 *channel.Params

	// State is a random state with parameters `Params`
	State *channel.State
	// State2 is a random state with parameters `Params2` and should differ in all fields from `State`
	State2 *channel.State

	// Account is a random account
	Account wallet.Account

	// RandomAddress returns a new random address
	RandomAddress addressCreator
}

Setup provides all objects needed for the generic channel tests.

Jump to

Keyboard shortcuts

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