Files
tendermint/internal/libs/test/mutate.go
Marko 719e028e00 libs: internalize some packages (#6366)
## Description

Internalize some libs. This reduces the amount ot public API tendermint is supporting. The moved libraries are mainly ones that are used within Tendermint-core.
2021-05-25 16:25:31 +00:00

30 lines
688 B
Go

// nolint:gosec // G404: Use of weak random number generator
package test
import (
mrand "math/rand"
)
// Contract: !bytes.Equal(input, output) && len(input) >= len(output)
func MutateByteSlice(bytez []byte) []byte {
// If bytez is empty, panic
if len(bytez) == 0 {
panic("Cannot mutate an empty bytez")
}
// Copy bytez
mBytez := make([]byte, len(bytez))
copy(mBytez, bytez)
bytez = mBytez
// Try a random mutation
switch mrand.Int() % 2 {
case 0: // Mutate a single byte
bytez[mrand.Int()%len(bytez)] += byte(mrand.Int()%255 + 1)
case 1: // Remove an arbitrary byte
pos := mrand.Int() % len(bytez)
bytez = append(bytez[:pos], bytez[pos+1:]...)
}
return bytez
}