You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
668 B

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package test
  2. import (
  3. cmn "github.com/tendermint/tendermint/libs/common"
  4. )
  5. // Contract: !bytes.Equal(input, output) && len(input) >= len(output)
  6. func MutateByteSlice(bytez []byte) []byte {
  7. // If bytez is empty, panic
  8. if len(bytez) == 0 {
  9. panic("Cannot mutate an empty bytez")
  10. }
  11. // Copy bytez
  12. mBytez := make([]byte, len(bytez))
  13. copy(mBytez, bytez)
  14. bytez = mBytez
  15. // Try a random mutation
  16. switch cmn.RandInt() % 2 {
  17. case 0: // Mutate a single byte
  18. bytez[cmn.RandInt()%len(bytez)] += byte(cmn.RandInt()%255 + 1)
  19. case 1: // Remove an arbitrary byte
  20. pos := cmn.RandInt() % len(bytez)
  21. bytez = append(bytez[:pos], bytez[pos+1:]...)
  22. }
  23. return bytez
  24. }