github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/internal/libs/test/mutate.go (about)

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