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.

29 lines
688 B

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