randxdr

package
v0.0.0-...-2e41b90 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 License: Apache-2.0, Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMaxBytesSize is the MaxBytesSize value in the Generator returned by NewGenerator()
	DefaultMaxBytesSize = 1024
	// DefaultMaxVecLen is the MaxVecLen value in the Generator returned by NewGenerator()
	DefaultMaxVecLen = 10
	// DefaultSeed is the seed for the Source value in the Generator returned by NewGenerator()
	DefaultSeed = 99
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {
	// MaxBytesSize configures the upper bound limit for variable length
	// opaque data and variable length strings
	// https://tools.ietf.org/html/rfc4506#section-4.10
	MaxBytesSize uint32
	// MaxVecLen configures the upper bound limit for variable length arrays
	// https://tools.ietf.org/html/rfc4506#section-4.13
	MaxVecLen uint32
	// Source is the rand.Source which is used by the Generator to create
	// random values
	Source rand.Source
}

Generator generates random XDR values.

func NewGenerator

func NewGenerator() Generator

NewGenerator returns a new Generator instance configured with default settings. The returned Generator is deterministic but it is not thread-safe.

func (Generator) Next

func (g Generator) Next(shape goxdr.XdrType, presets []Preset)

Next modifies the given shape and populates it with random value fields.

type Preset

type Preset struct {
	Selector Selector
	Setter   Setter
}

Preset can be used to restrict values for specific fields of a goxdr.XdrType.

type Selector

type Selector func(string, goxdr.XdrType) bool

Selector is function used to match fields of a goxdr.XdrType

var IsNestedInnerSet Selector = func(name string, xdrType goxdr.XdrType) bool {
	if strings.HasSuffix(name, ".innerSets") && strings.Count(name, ".innerSets[") > 0 {
		_, ok := goxdr.XdrBaseType(xdrType).(goxdr.XdrVec)
		return ok
	}
	return false
}

IsNestedInnerSet is a Selector which identifies nesting for the following xdr type:

struct SCPQuorumSet
{
	uint32 threshold;
	PublicKey validators<>;
	SCPQuorumSet innerSets<>;
};

supports things like: A,B,C,(D,E,F),(G,H,(I,J,K,L)) only allows 2 levels of nesting

var IsPtr Selector = func(name string, xdrType goxdr.XdrType) bool {
	_, ok := goxdr.XdrBaseType(xdrType).(goxdr.XdrPtr)
	return ok
}

IsPtr is a Selector which matches on all XDR pointer fields

func And

func And(a, b Selector) Selector

And is a Selector which returns true if the given pair of selectors match the field.

func FieldEquals

func FieldEquals(toMatch string) Selector

FieldEquals returns a Selector which matches on a field name by equality

func FieldMatches

func FieldMatches(r *regexp.Regexp) Selector

FieldMatches returns a Selector which matches on a field name by regexp

type Setter

type Setter func(*randMarshaller, string, goxdr.XdrType)

Setter is a function used to set field values for a goxdr.XdrType

var SetAssetCode Setter = func(x *randMarshaller, field string, xdrType goxdr.XdrType) {
	f := goxdr.XdrBaseType(xdrType).(goxdr.XdrBytes)
	slice := f.GetByteSlice()
	var end int
	switch len(slice) {
	case 4:
		end = int(x.rand.Int31n(4))
	case 12:
		end = int(4 + x.rand.Int31n(8))
	}

	for i := 0; i <= end; i++ {
		slice[i] = alphaNumeric[x.rand.Int31n(int32(len(alphaNumeric)))]
	}
}

SetAssetCode returns a Setter which sets an asset code XDR field to a random alphanumeric string right-padded with 0 bytes

var SetPositiveNum32 Setter = func(x *randMarshaller, field string, xdrType goxdr.XdrType) {

	f := goxdr.XdrBaseType(xdrType).(goxdr.XdrNum32)
	f.SetU32(uint32(x.rand.Int31n(math.MaxInt32)))
}

SetPositiveNum32 returns a Setter which sets a uint32 XDR field to a random positive value

var SetPositiveNum64 Setter = func(x *randMarshaller, field string, xdrType goxdr.XdrType) {
	f := goxdr.XdrBaseType(xdrType).(goxdr.XdrNum64)
	f.SetU64(uint64(x.rand.Int63n(math.MaxInt64)))
}

SetPositiveNum64 returns a Setter which sets a uint64 XDR field to a random positive value

var SetPrintableASCII Setter = func(x *randMarshaller, field string, xdrType goxdr.XdrType) {
	f := goxdr.XdrBaseType(xdrType).(goxdr.XdrString)
	end := int(x.rand.Int31n(int32(f.Bound)))
	var text []byte
	for i := 0; i <= end; i++ {

		printableChar := byte(32 + x.rand.Int31n(95))
		text = append(text, printableChar)
	}
	f.SetString(string(text))
}

SetPrintableASCII returns a Setter which sets a home domain string32 with a random printable ascii string

func SetPtr

func SetPtr(present bool) Setter

SetPtr is a Setter which sets the xdr pointer to null if present is false

func SetU32

func SetU32(val uint32) Setter

SetU32 returns a Setter which sets a uint32 XDR field to a fixed value

func SetVecLen

func SetVecLen(vecLen uint32) Setter

SetVecLen returns a Setter which sets the length of a variable length array ( https://tools.ietf.org/html/rfc4506#section-4.13 ) to a fixed value

Jump to

Keyboard shortcuts

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