Files
tendermint/libs/test/mutate.go
T
Marko afc4d7a61f libs/common: refactor libs/common 2 (#4231)
* libs/common: refactor libs/common 2

- move random function to there own pkg

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* change imports and usage throughout repo

* fix goimports

* add changelog entry
2019-12-10 18:38:34 +01:00

29 lines
666 B
Go

package test
import (
"github.com/tendermint/tendermint/libs/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 rand.RandInt() % 2 {
case 0: // Mutate a single byte
bytez[rand.RandInt()%len(bytez)] += byte(rand.RandInt()%255 + 1)
case 1: // Remove an arbitrary byte
pos := rand.RandInt() % len(bytez)
bytez = append(bytez[:pos], bytez[pos+1:]...)
}
return bytez
}