Exemple #1
0
// MontePathIn computes montecarlo distribution and flow for pathing
// in to the set minimum depth, N samples per start location.
func (f *Fill) MontePathIn(r *rand.Rand, start []Location, N int, MinDepth uint16) (dist []int, flow [][4]int) {
	dist = make([]int, len(f.Depth))
	flow = make([][4]int, len(f.Depth))

	for _, origloc := range start {
		for n := 0; n < N; n++ {
			loc := origloc
			d := 0

			for d < 4 {
				depth := f.Depth[loc]
				nperm := r.Intn(24)
				for d = 0; d < 4; d++ {
					nloc := f.LocStep[loc][Perm4[nperm][d]]
					if f.Depth[nloc] < depth && f.Depth[nloc] > MinDepth {
						flow[loc][Perm4[nperm][d]]++
						loc = nloc
						dist[loc]++
						break
					}
				}
			}
		}
	}
	return
}
func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &certificateMsg{}
	numCerts := rand.Intn(20)
	m.certificates = make([][]byte, numCerts)
	for i := 0; i < numCerts; i++ {
		m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
	}
	return reflect.ValueOf(m)
}
func (*certificateStatusMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &certificateStatusMsg{}
	if rand.Intn(10) > 5 {
		m.statusType = statusTypeOCSP
		m.response = randomBytes(rand.Intn(10)+1, rand)
	} else {
		m.statusType = 42
	}
	return reflect.ValueOf(m)
}
func (*certificateRequestMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &certificateRequestMsg{}
	m.certificateTypes = randomBytes(rand.Intn(5)+1, rand)
	numCAs := rand.Intn(100)
	m.certificateAuthorities = make([][]byte, numCAs)
	for i := 0; i < numCAs; i++ {
		m.certificateAuthorities[i] = randomBytes(rand.Intn(15)+1, rand)
	}
	return reflect.ValueOf(m)
}
func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &clientHelloMsg{}
	m.major = uint8(rand.Intn(256))
	m.minor = uint8(rand.Intn(256))
	m.random = randomBytes(32, rand)
	m.sessionId = randomBytes(rand.Intn(32), rand)
	m.cipherSuites = make([]uint16, rand.Intn(63)+1)
	for i := 0; i < len(m.cipherSuites); i++ {
		m.cipherSuites[i] = uint16(rand.Int31())
	}
	m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)

	return reflect.NewValue(m)
}
Exemple #6
0
func (b Bitmask) Generate(rand *rand.Rand, size int) reflect.Value {
	result := Bitmask{
		x: size - rand.Intn(size),
		y: size - rand.Intn(size),
		w: rand.Intn(size),
		h: rand.Intn(size),
	}
	result.lines = make([][]part, result.h)
	completeness := rand.Intn(size)
	for y := 0; y < result.h; y++ {
		result.lines[y] = make([]part, result.w/sz+1)
		for x := 0; x < result.w; x++ {
			result.SetRel(x, y, rand.Intn(completeness) == 0)
		}
	}

	return reflect.NewValue(result)
}
func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &serverHelloMsg{}
	m.vers = uint16(rand.Intn(65536))
	m.random = randomBytes(32, rand)
	m.sessionId = randomBytes(rand.Intn(32), rand)
	m.cipherSuite = uint16(rand.Int31())
	m.compressionMethod = uint8(rand.Intn(256))

	if rand.Intn(10) > 5 {
		m.nextProtoNeg = true

		n := rand.Intn(10)
		m.nextProtos = make([]string, n)
		for i := 0; i < n; i++ {
			m.nextProtos[i] = randomString(20, rand)
		}
	}

	return reflect.ValueOf(m)
}
func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &clientHelloMsg{}
	m.vers = uint16(rand.Intn(65536))
	m.random = randomBytes(32, rand)
	m.sessionId = randomBytes(rand.Intn(32), rand)
	m.cipherSuites = make([]uint16, rand.Intn(63)+1)
	for i := 0; i < len(m.cipherSuites); i++ {
		m.cipherSuites[i] = uint16(rand.Int31())
	}
	m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)
	if rand.Intn(10) > 5 {
		m.nextProtoNeg = true
	}
	if rand.Intn(10) > 5 {
		m.serverName = randomString(rand.Intn(255), rand)
	}
	m.ocspStapling = rand.Intn(10) > 5

	return reflect.NewValue(m)
}
Exemple #9
0
// Draw from the arrays with rand.Intn(120) for a random permutation of directions including NoMovement
func Permute5(r *rand.Rand) *[5]Direction {
	return &Perm5[r.Intn(120)]
}
Exemple #10
0
// Return a set of directions with d first, opposite last an the other 3 permuted.
func Permute5D(d Direction, r *rand.Rand) *[5]Direction {
	return &PermStepD5[d][r.Intn(6)]
}
Exemple #11
0
// Draw from the arrays with rand.Intn(24) for a random permutation of directions.
func Permute4(r *rand.Rand) *[4]Direction {
	return &Perm4[r.Intn(24)]
}
Exemple #12
0
// Returns a permute4 + guard used in eg NPathIn
func Permute4G(r *rand.Rand) *[5]Direction {
	return &Perm4G[r.Intn(24)]
}
func (*nextProtoMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &nextProtoMsg{}
	m.proto = randomString(rand.Intn(255), rand)
	return reflect.ValueOf(m)
}
func (*clientKeyExchangeMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &clientKeyExchangeMsg{}
	m.ciphertext = randomBytes(rand.Intn(1000)+1, rand)
	return reflect.ValueOf(m)
}
Exemple #15
0
// Value returns an arbitrary value of the given type.
// If the type implements the Generator interface, that will be used.
// Note: in order to create arbitrary values for structs, all the members must be public.
func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
	if m, ok := reflect.MakeZero(t).Interface().(Generator); ok {
		return m.Generate(rand, complexSize), true
	}

	switch concrete := t.(type) {
	case *reflect.BoolType:
		return reflect.NewValue(rand.Int()&1 == 0), true
	case *reflect.Float32Type:
		return reflect.NewValue(randFloat32(rand)), true
	case *reflect.Float64Type:
		return reflect.NewValue(randFloat64(rand)), true
	case *reflect.FloatType:
		if t.Size() == 4 {
			return reflect.NewValue(float(randFloat32(rand))), true
		} else {
			return reflect.NewValue(float(randFloat64(rand))), true
		}
	case *reflect.Int16Type:
		return reflect.NewValue(int16(randInt64(rand))), true
	case *reflect.Int32Type:
		return reflect.NewValue(int32(randInt64(rand))), true
	case *reflect.Int64Type:
		return reflect.NewValue(randInt64(rand)), true
	case *reflect.Int8Type:
		return reflect.NewValue(int8(randInt64(rand))), true
	case *reflect.IntType:
		return reflect.NewValue(int(randInt64(rand))), true
	case *reflect.MapType:
		numElems := rand.Intn(complexSize)
		m := reflect.MakeMap(concrete)
		for i := 0; i < numElems; i++ {
			key, ok1 := Value(concrete.Key(), rand)
			value, ok2 := Value(concrete.Elem(), rand)
			if !ok1 || !ok2 {
				return nil, false
			}
			m.SetElem(key, value)
		}
		return m, true
	case *reflect.PtrType:
		v, ok := Value(concrete.Elem(), rand)
		if !ok {
			return nil, false
		}
		p := reflect.MakeZero(concrete)
		p.(*reflect.PtrValue).PointTo(v)
		return p, true
	case *reflect.SliceType:
		numElems := rand.Intn(complexSize)
		s := reflect.MakeSlice(concrete, numElems, numElems)
		for i := 0; i < numElems; i++ {
			v, ok := Value(concrete.Elem(), rand)
			if !ok {
				return nil, false
			}
			s.Elem(i).SetValue(v)
		}
		return s, true
	case *reflect.StringType:
		numChars := rand.Intn(complexSize)
		codePoints := make([]int, numChars)
		for i := 0; i < numChars; i++ {
			codePoints[i] = rand.Intn(0x10ffff)
		}
		return reflect.NewValue(string(codePoints)), true
	case *reflect.StructType:
		s := reflect.MakeZero(t).(*reflect.StructValue)
		for i := 0; i < s.NumField(); i++ {
			v, ok := Value(concrete.Field(i).Type, rand)
			if !ok {
				return nil, false
			}
			s.Field(i).SetValue(v)
		}
		return s, true
	case *reflect.Uint16Type:
		return reflect.NewValue(uint16(randInt64(rand))), true
	case *reflect.Uint32Type:
		return reflect.NewValue(uint32(randInt64(rand))), true
	case *reflect.Uint64Type:
		return reflect.NewValue(uint64(randInt64(rand))), true
	case *reflect.Uint8Type:
		return reflect.NewValue(uint8(randInt64(rand))), true
	case *reflect.UintType:
		return reflect.NewValue(uint(randInt64(rand))), true
	case *reflect.UintptrType:
		return reflect.NewValue(uintptr(randInt64(rand))), true
	default:
		return nil, false
	}

	return
}
func (*certificateVerifyMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &certificateVerifyMsg{}
	m.signature = randomBytes(rand.Intn(15)+1, rand)
	return reflect.ValueOf(m)
}
Exemple #17
0
// Value returns an arbitrary value of the given type.
// If the type implements the Generator interface, that will be used.
// Note: in order to create arbitrary values for structs, all the members must be public.
func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
	if m, ok := reflect.Zero(t).Interface().(Generator); ok {
		return m.Generate(rand, complexSize), true
	}

	switch concrete := t; concrete.Kind() {
	case reflect.Bool:
		return reflect.ValueOf(rand.Int()&1 == 0), true
	case reflect.Float32:
		return reflect.ValueOf(randFloat32(rand)), true
	case reflect.Float64:
		return reflect.ValueOf(randFloat64(rand)), true
	case reflect.Complex64:
		return reflect.ValueOf(complex(randFloat32(rand), randFloat32(rand))), true
	case reflect.Complex128:
		return reflect.ValueOf(complex(randFloat64(rand), randFloat64(rand))), true
	case reflect.Int16:
		return reflect.ValueOf(int16(randInt64(rand))), true
	case reflect.Int32:
		return reflect.ValueOf(int32(randInt64(rand))), true
	case reflect.Int64:
		return reflect.ValueOf(randInt64(rand)), true
	case reflect.Int8:
		return reflect.ValueOf(int8(randInt64(rand))), true
	case reflect.Int:
		return reflect.ValueOf(int(randInt64(rand))), true
	case reflect.Uint16:
		return reflect.ValueOf(uint16(randInt64(rand))), true
	case reflect.Uint32:
		return reflect.ValueOf(uint32(randInt64(rand))), true
	case reflect.Uint64:
		return reflect.ValueOf(uint64(randInt64(rand))), true
	case reflect.Uint8:
		return reflect.ValueOf(uint8(randInt64(rand))), true
	case reflect.Uint:
		return reflect.ValueOf(uint(randInt64(rand))), true
	case reflect.Uintptr:
		return reflect.ValueOf(uintptr(randInt64(rand))), true
	case reflect.Map:
		numElems := rand.Intn(complexSize)
		m := reflect.MakeMap(concrete)
		for i := 0; i < numElems; i++ {
			key, ok1 := Value(concrete.Key(), rand)
			value, ok2 := Value(concrete.Elem(), rand)
			if !ok1 || !ok2 {
				return reflect.Value{}, false
			}
			m.SetMapIndex(key, value)
		}
		return m, true
	case reflect.Ptr:
		v, ok := Value(concrete.Elem(), rand)
		if !ok {
			return reflect.Value{}, false
		}
		p := reflect.New(concrete.Elem())
		p.Elem().Set(v)
		return p, true
	case reflect.Slice:
		numElems := rand.Intn(complexSize)
		s := reflect.MakeSlice(concrete, numElems, numElems)
		for i := 0; i < numElems; i++ {
			v, ok := Value(concrete.Elem(), rand)
			if !ok {
				return reflect.Value{}, false
			}
			s.Index(i).Set(v)
		}
		return s, true
	case reflect.String:
		numChars := rand.Intn(complexSize)
		codePoints := make([]int, numChars)
		for i := 0; i < numChars; i++ {
			codePoints[i] = rand.Intn(0x10ffff)
		}
		return reflect.ValueOf(string(codePoints)), true
	case reflect.Struct:
		s := reflect.New(t).Elem()
		for i := 0; i < s.NumField(); i++ {
			v, ok := Value(concrete.Field(i).Type, rand)
			if !ok {
				return reflect.Value{}, false
			}
			s.Field(i).Set(v)
		}
		return s, true
	default:
		return reflect.Value{}, false
	}

	return
}