Skip to content

Commit

Permalink
worker: generate valid UTF-8 payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
WillemKauf committed Nov 20, 2024
1 parent 5c5b31d commit ccd4a34
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,26 @@ func (vg *ValueGenerator) Generate() []byte {
// huge message sizes (e.g. 128MIB of zeros compresses down to <1MiB.
return compressible_payload
} else {
payload := make([]byte, vg.PayloadSize)
// An incompressible high entropy payload
n, err := rand.Read(payload)
randBytes := make([]byte, vg.PayloadSize)
// An incompressible high entropy payload. This will likely not be UTF-8 decodable.
n, err := rand.Read(randBytes)
if err != nil {
panic(err.Error())
}
if n != int(vg.PayloadSize) {
panic("Unexpected byte count from rand.Read")
}
// Convert to a valid UTF-8 string, replacing bad chars with " ".
// A valid UTF-8 string is needed to avoid any decoding issues
// for services on the consuming end.
payload := []byte(strings.ToValidUTF8(string(randBytes), " "))

// In converting to valid UTF-8, we may have lost some bytes.
// Append back the difference.
diff := int(vg.PayloadSize) - len(payload)
if diff > 0 {
payload = append(payload, make([]byte, diff)...)
}
return payload
}
}
Expand Down

0 comments on commit ccd4a34

Please sign in to comment.