valuegeneration

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSONArgumentsFromMap

func DecodeJSONArgumentsFromMap(inputs abi.Arguments, values map[string]any, deployedContractAddr map[string]common.Address) ([]any, error)

DecodeJSONArgumentsFromMap decodes JSON values into a provided values of the given types, or returns an error of one occurs. The values provided must be generic JSON types (e.g. []any, map[string]any, etc) which will be transformed into a go-ethereum ABI packable values.

func DecodeJSONArgumentsFromSlice

func DecodeJSONArgumentsFromSlice(inputs abi.Arguments, values []any, deployedContractAddr map[string]common.Address) ([]any, error)

DecodeJSONArgumentsFromSlice decodes JSON values into a provided values of the given types, or returns an error of one occurs. The values provided must be generic JSON types (e.g. []any, map[string]any, etc) which will be transformed into a go-ethereum ABI packable values.

func EncodeABIArgumentsToString

func EncodeABIArgumentsToString(inputs abi.Arguments, values []any) (string, error)

EncodeABIArgumentsToString encodes provided go-ethereum ABI package input values into string that is human-readable for console output purpose. Returns the string, or an error if one occurs.

func EncodeJSONArgumentsToMap

func EncodeJSONArgumentsToMap(inputs abi.Arguments, values []any) (map[string]any, error)

EncodeJSONArgumentsToMap encodes provided go-ethereum ABI packable input values into a generic JSON type values (e.g. []any, map[string]any, etc). Returns the encoded values, or an error if one occurs.

func EncodeJSONArgumentsToSlice

func EncodeJSONArgumentsToSlice(inputs abi.Arguments, values []any) ([]any, error)

EncodeJSONArgumentsToSlice encodes provided go-ethereum ABI packable input values into generic JSON compatible values (e.g. []any, map[string]any, etc). Returns the encoded values, or an error if one occurs.

func GenerateAbiValue

func GenerateAbiValue(generator ValueGenerator, inputType *abi.Type) any

GenerateAbiValue generates a value of the provided abi.Type using the provided ValueGenerator. The generated value is returned.

func MutateAbiValue

func MutateAbiValue(generator ValueGenerator, inputType *abi.Type, value any) (any, error)

MutateAbiValue takes an ABI packable input value, alongside its type definition and a value generator, to mutate existing ABI input values.

Types

type MutatingValueGenerator

type MutatingValueGenerator struct {

	// RandomValueGenerator is included to inherit from the random generator
	*RandomValueGenerator
	// contains filtered or unexported fields
}

MutatingValueGenerator is a provider used to generate function inputs and call arguments using mutation-based approaches against items within a base_value_set.ValueSet, such as AST literals.

func NewMutatingValueGenerator

func NewMutatingValueGenerator(config *MutatingValueGeneratorConfig, valueSet *ValueSet, randomProvider *rand.Rand) *MutatingValueGenerator

NewMutatingValueGenerator creates a new MutatingValueGenerator using a provided base_value_set.ValueSet to seed base-values for mutation.

func (*MutatingValueGenerator) GenerateAddress

func (g *MutatingValueGenerator) GenerateAddress() common.Address

GenerateAddress obtains an existing address from its underlying value set or generates a random one.

func (*MutatingValueGenerator) GenerateBytes

func (g *MutatingValueGenerator) GenerateBytes() []byte

GenerateBytes generates bytes and returns them.

func (*MutatingValueGenerator) GenerateInteger

func (g *MutatingValueGenerator) GenerateInteger(signed bool, bitLength int) *big.Int

GenerateInteger generates an integer of the provided properties and returns a big.Int representing it.

func (*MutatingValueGenerator) GenerateString

func (g *MutatingValueGenerator) GenerateString() string

GenerateString generates strings and returns them.

func (*MutatingValueGenerator) MutateAddress

func (g *MutatingValueGenerator) MutateAddress(addr common.Address) common.Address

MutateAddress takes an address input and sometimes returns a mutated value based off the input.

func (*MutatingValueGenerator) MutateArray

func (g *MutatingValueGenerator) MutateArray(value []any, fixedLength bool) []any

MutateArray takes a dynamic or fixed sized array as input, and returns a mutated value based off of the input. Returns the mutated value. If any element of the returned array is nil, the value generator will be called upon to generate it new.

func (*MutatingValueGenerator) MutateBool

func (g *MutatingValueGenerator) MutateBool(bl bool) bool

MutateBool takes a boolean input and returns a mutated value based off the input.

func (*MutatingValueGenerator) MutateBytes

func (g *MutatingValueGenerator) MutateBytes(b []byte) []byte

MutateBytes takes a dynamic-sized byte array input and returns a mutated value based off the input.

func (*MutatingValueGenerator) MutateFixedBytes

func (g *MutatingValueGenerator) MutateFixedBytes(b []byte) []byte

MutateFixedBytes takes a fixed-sized byte array input and returns a mutated value based off the input.

func (*MutatingValueGenerator) MutateInteger

func (g *MutatingValueGenerator) MutateInteger(i *big.Int, signed bool, bitLength int) *big.Int

MutateInteger takes an integer input and applies optional mutations to the provided value. Returns an optionally mutated copy of the input.

func (*MutatingValueGenerator) MutateString

func (g *MutatingValueGenerator) MutateString(s string) string

MutateString takes a string input and returns a mutated value based off the input.

type MutatingValueGeneratorConfig

type MutatingValueGeneratorConfig struct {
	// MinMutationRounds describes the minimum amount of mutations which should occur when generating a value.
	// This parameter is used when generating a new value by mutating a value in the value set, or when mutating
	// an existing value.
	MinMutationRounds int
	// MaxMutationRounds describes the maximum amount of mutations which should occur when generating a value.
	// This parameter is used when generating a new value by mutating a value in the value set, or when mutating
	// an existing value.
	MaxMutationRounds int

	// GenerateRandomIntegerBias defines the probability in which an address generated by the value generator is
	// entirely random, rather than selected from the ValueSet provided by MutatingValueGenerator.SetValueSet. Value
	// range is [0.0, 1.0].
	GenerateRandomAddressBias float32
	// GenerateRandomIntegerBias defines the probability in which an integer generated by the value generator is
	// entirely random, rather than mutated. Value range is [0.0, 1.0].
	GenerateRandomIntegerBias float32
	// GenerateRandomStringBias defines the probability in which a string generated by the value generator is entirely
	// random, rather than mutated. Value range is [0.0, 1.0].
	GenerateRandomStringBias float32
	// GenerateRandomStringBias defines the probability in which a byte array generated by the value generator is
	// entirely random, rather than mutated. Value range is [0.0, 1.0].
	GenerateRandomBytesBias float32

	// MutateAddressProbability defines the probability in which an existing address value will be mutated by
	// the value generator. Value range is [0.0, 1.0].
	MutateAddressProbability float32
	// MutateArrayStructureProbability defines the probability in which an existing array value will be mutated by
	// the value generator. Value range is [0.0, 1.0].
	MutateArrayStructureProbability float32
	// MutateAddressProbability defines the probability in which an existing boolean value will be mutated by
	// the value generator. Value range is [0.0, 1.0].
	MutateBoolProbability float32
	// MutateBytesProbability defines the probability in which an existing dynamic-sized byte array value will be
	// mutated by the value generator. Value range is [0.0, 1.0].
	MutateBytesProbability float32
	// MutateBytesGenerateNewBias defines the probability that when an existing dynamic-sized byte array will be
	// mutated, it is done so by being replaced with a newly generated one instead. Value range is [0.0, 1.0].
	MutateBytesGenerateNewBias float32
	// MutateFixedBytesProbability defines the probability in which an existing fixed-sized byte array value will be
	// mutated by the value generator. Value range is [0.0, 1.0].
	MutateFixedBytesProbability float32
	// MutateStringProbability defines the probability in which an existing string value will be mutated by
	// the value generator. Value range is [0.0, 1.0].
	MutateStringProbability float32
	// MutateStringGenerateNewBias defines the probability that when an existing string will be mutated,
	// it is done so by being replaced with a newly generated one instead. Value range is [0.0, 1.0].
	MutateStringGenerateNewBias float32
	// MutateIntegerProbability defines the probability in which an existing integer value will be mutated by
	// the value generator. Value range is [0.0, 1.0].
	MutateIntegerProbability float32
	// MutateIntegerGenerateNewBias defines the probability that when an existing integer will be mutated,
	// it is done so by being replaced with a newly generated one instead. Value range is [0.0, 1.0].
	MutateIntegerGenerateNewBias float32

	// RandomValueGeneratorConfig is adhered to in this structure, to power the underlying RandomValueGenerator.
	*RandomValueGeneratorConfig
}

MutatingValueGeneratorConfig defines the operating parameters for a MutatingValueGenerator.

type RandomValueGenerator

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

RandomValueGenerator represents an interface for a provider used to generate transaction fields and call arguments using a random provider. As such it may not be accurate in many test results with tightly-bound pre-conditions. This provider does not mutate existing values and will leave them unaltered.

func NewRandomValueGenerator

func NewRandomValueGenerator(config *RandomValueGeneratorConfig, randomProvider *rand.Rand) *RandomValueGenerator

NewRandomValueGenerator creates a new RandomValueGenerator with a new random provider.

func (*RandomValueGenerator) GenerateAddress

func (g *RandomValueGenerator) GenerateAddress() common.Address

GenerateAddress generates a random address to use when populating inputs.

func (*RandomValueGenerator) GenerateArrayOfLength

func (g *RandomValueGenerator) GenerateArrayOfLength() int

GenerateArrayOfLength generates a random array length to use when populating inputs. This is used to determine how many elements a non-byte, non-string array should have.

func (*RandomValueGenerator) GenerateBool

func (g *RandomValueGenerator) GenerateBool() bool

GenerateBool generates a random bool to use when populating inputs.

func (*RandomValueGenerator) GenerateBytes

func (g *RandomValueGenerator) GenerateBytes() []byte

GenerateBytes generates a random dynamic-sized byte array to use when populating inputs.

func (*RandomValueGenerator) GenerateFixedBytes

func (g *RandomValueGenerator) GenerateFixedBytes(length int) []byte

GenerateFixedBytes generates a random fixed-sized byte array to use when populating inputs.

func (*RandomValueGenerator) GenerateInteger

func (g *RandomValueGenerator) GenerateInteger(signed bool, bitLength int) *big.Int

GenerateInteger generates a random integer to use when populating inputs.

func (*RandomValueGenerator) GenerateString

func (g *RandomValueGenerator) GenerateString() string

GenerateString generates a random dynamic-sized string to use when populating inputs.

func (*RandomValueGenerator) MutateAddress

func (g *RandomValueGenerator) MutateAddress(addr common.Address) common.Address

MutateAddress takes an address input and returns a mutated value based off the input.

func (*RandomValueGenerator) MutateArray

func (g *RandomValueGenerator) MutateArray(value []any, fixedLength bool) []any

MutateArray takes a dynamic or fixed sized array as input, and returns a mutated value based off of the input. Returns the mutated value. If any element of the returned array is nil, the value generator will be called upon to generate it new.

func (*RandomValueGenerator) MutateBool

func (g *RandomValueGenerator) MutateBool(bl bool) bool

MutateBool takes a boolean input and returns a mutated value based off the input.

func (*RandomValueGenerator) MutateBytes

func (g *RandomValueGenerator) MutateBytes(b []byte) []byte

MutateBytes takes a dynamic-sized byte array input and returns a mutated value based off the input.

func (*RandomValueGenerator) MutateFixedBytes

func (g *RandomValueGenerator) MutateFixedBytes(b []byte) []byte

MutateFixedBytes takes a fixed-sized byte array input and returns a mutated value based off the input.

func (*RandomValueGenerator) MutateInteger

func (g *RandomValueGenerator) MutateInteger(i *big.Int, signed bool, bitLength int) *big.Int

MutateInteger takes an integer input and returns a mutated value based off the input.

func (*RandomValueGenerator) MutateString

func (g *RandomValueGenerator) MutateString(s string) string

MutateString takes a string input and returns a mutated value based off the input.

func (*RandomValueGenerator) RandomProvider

func (g *RandomValueGenerator) RandomProvider() *rand.Rand

RandomProvider returns the internal random provider used for value generation.

type RandomValueGeneratorConfig

type RandomValueGeneratorConfig struct {
	// GenerateRandomArrayMinSize defines the minimum size which a generated array should be.
	GenerateRandomArrayMinSize int
	// GenerateRandomArrayMaxSize defines the maximum size which a generated array should be.
	GenerateRandomArrayMaxSize int
	// GenerateRandomBytesMinSize defines the minimum size which a generated byte slice should be.
	GenerateRandomBytesMinSize int
	// GenerateRandomBytesMaxSize defines the maximum size which a generated byte slice should be.
	GenerateRandomBytesMaxSize int
	// GenerateRandomStringMinSize defines the minimum size which a generated string should be.
	GenerateRandomStringMinSize int
	// GenerateRandomStringMaxSize defines the maximum size which a generated string should be.
	GenerateRandomStringMaxSize int
}

RandomValueGeneratorConfig defines the parameters for a RandomValueGenerator.

type ValueGenerator

type ValueGenerator interface {
	// RandomProvider returns the internal random provider used for value generation.
	RandomProvider() *rand.Rand

	// GenerateAddress generates/selects an address to use when populating inputs.
	GenerateAddress() common.Address
	// MutateAddress takes an address input and returns a mutated value based off the input.
	MutateAddress(addr common.Address) common.Address

	// GenerateArrayOfLength generates/selects an array length to use when populating inputs.
	GenerateArrayOfLength() int
	// MutateArray takes a dynamic or fixed sized array as input, and returns a mutated value based off of the input.
	// Returns the mutated value. If any element of the returned array is nil, the value generator will be called upon
	// to generate it new.
	MutateArray(value []any, fixedLength bool) []any

	// GenerateBool generates/selects a bool to use when populating inputs.
	GenerateBool() bool
	// MutateBool takes a boolean input and returns a mutated value based off the input.
	MutateBool(bl bool) bool

	// GenerateBytes generates/selects a dynamic-sized byte array to use when populating inputs.
	GenerateBytes() []byte
	// MutateBytes takes a dynamic-sized byte array input and returns a mutated value based off the input.
	MutateBytes(b []byte) []byte

	// GenerateFixedBytes generates/selects a fixed-sized byte array to use when populating inputs.
	GenerateFixedBytes(length int) []byte
	// MutateFixedBytes takes a fixed-sized byte array input and returns a mutated value based off the input.
	MutateFixedBytes(b []byte) []byte

	// GenerateString generates/selects a dynamic-sized string to use when populating inputs.
	GenerateString() string
	// MutateString takes a string input and returns a mutated value based off the input.
	MutateString(s string) string

	// GenerateInteger generates/selects an integer to use when populating inputs.
	GenerateInteger(signed bool, bitLength int) *big.Int
	// MutateInteger takes an integer input and returns a mutated value based off the input.
	MutateInteger(i *big.Int, signed bool, bitLength int) *big.Int
}

ValueGenerator represents an interface for a provider used to generate function inputs and call arguments for use in fuzzing campaigns.

type ValueSet

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

ValueSet represents potential values of significance within the source code to be used in fuzz tests.

func NewValueSet

func NewValueSet() *ValueSet

NewValueSet initializes a new ValueSet object for use with a Fuzzer.

func (*ValueSet) AddAddress

func (vs *ValueSet) AddAddress(a common.Address)

AddAddress adds an address item to the ValueSet.

func (*ValueSet) AddBytes

func (vs *ValueSet) AddBytes(b []byte)

AddBytes adds a byte sequence to the ValueSet.

func (*ValueSet) AddInteger

func (vs *ValueSet) AddInteger(b *big.Int)

AddInteger adds an integer item to the ValueSet.

func (*ValueSet) AddString

func (vs *ValueSet) AddString(s string)

AddString adds a string item to the ValueSet.

func (*ValueSet) Addresses

func (vs *ValueSet) Addresses() []common.Address

Addresses returns a list of addresses contained within the set.

func (*ValueSet) Bytes

func (vs *ValueSet) Bytes() [][]byte

Bytes returns a list of bytes contained within the set.

func (*ValueSet) Clone

func (vs *ValueSet) Clone() *ValueSet

Clone creates a copy of the current ValueSet.

func (*ValueSet) Integers

func (vs *ValueSet) Integers() []*big.Int

Integers returns a list of integers contained within the set.

func (*ValueSet) RemoveAddress

func (vs *ValueSet) RemoveAddress(a common.Address)

RemoveAddress removes an address item from the ValueSet.

func (*ValueSet) RemoveBytes

func (vs *ValueSet) RemoveBytes(b []byte)

RemoveBytes removes a byte sequence item from the ValueSet.

func (*ValueSet) RemoveInteger

func (vs *ValueSet) RemoveInteger(b *big.Int)

RemoveInteger removes an integer item from the ValueSet.

func (*ValueSet) RemoveString

func (vs *ValueSet) RemoveString(s string)

RemoveString removes a string item from the ValueSet.

func (*ValueSet) SeedFromAst

func (vs *ValueSet) SeedFromAst(ast any)

SeedFromAst allows a ValueSet to be seeded from an AST interface.

func (*ValueSet) Strings

func (vs *ValueSet) Strings() []string

Strings returns a list of strings contained within the set.

Jump to

Keyboard shortcuts

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