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
665 B

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package test
  2. import (
  3. tmrand "github.com/tendermint/tendermint/libs/rand"
  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 tmrand.Int() % 2 {
  17. case 0: // Mutate a single byte
  18. bytez[tmrand.Int()%len(bytez)] += byte(tmrand.Int()%255 + 1)
  19. case 1: // Remove an arbitrary byte
  20. pos := tmrand.Int() % len(bytez)
  21. bytez = append(bytez[:pos], bytez[pos+1:]...)
  22. }
  23. return bytez
  24. }