OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package main |
| 6 |
| 7 import ( |
| 8 "io" |
| 9 "math/rand" |
| 10 ) |
| 11 |
| 12 // io.Reader which produces truly random binary data (totally uncompressible). |
| 13 func RandomBinaryGenerator(r *rand.Rand) io.Reader { |
| 14 // rand.Rand already produces random binary data via Read() |
| 15 return r |
| 16 } |
| 17 |
| 18 // io.Reader which produces a random text. |
| 19 type textRandomGenerator struct { |
| 20 r *rand.Rand |
| 21 } |
| 22 |
| 23 func (g *textRandomGenerator) Read(p []byte) (n int, err error) { |
| 24 i := 0 |
| 25 for { |
| 26 bytes := []byte(string(randChar(g.r, textChars))) |
| 27 if i+len(bytes) > len(p) { |
| 28 break |
| 29 } |
| 30 |
| 31 for j := range bytes { |
| 32 p[i+j] = bytes[j] |
| 33 } |
| 34 i += len(bytes) |
| 35 } |
| 36 return i, nil |
| 37 } |
| 38 |
| 39 // io.Reader which produces truly random text data (mostly uncompressible). |
| 40 func RandomTextGenerator(r *rand.Rand) io.Reader { |
| 41 reader := textRandomGenerator{r: r} |
| 42 return &reader |
| 43 } |
| 44 |
| 45 // Repeated sequence size range |
| 46 const ( |
| 47 SEQUENCE_MINSIZE uint64 = 16 |
| 48 SEQUENCE_MAXSIZE uint64 = 4 * 1024 |
| 49 ) |
| 50 |
| 51 // io.Reader which produces the given byte array repetitively. |
| 52 type repeatedByteGenerator struct { |
| 53 data []byte |
| 54 index int |
| 55 } |
| 56 |
| 57 func (g *repeatedByteGenerator) Read(p []byte) (n int, err error) { |
| 58 for i := range p { |
| 59 p[i] = g.data[g.index] |
| 60 g.index = (g.index + 1) % len(g.data) |
| 61 } |
| 62 return len(p), nil |
| 63 } |
| 64 |
| 65 // io.Reader which produces repeated binary data (some what compressible). |
| 66 func RepeatedBinaryGenerator(r *rand.Rand) io.Reader { |
| 67 // Figure out how big the repeated sequence will be |
| 68 sequence_size := randBetween(r, SEQUENCE_MINSIZE, SEQUENCE_MAXSIZE) |
| 69 |
| 70 repeater := repeatedByteGenerator{index: 0, data: make([]byte, sequence_
size)} |
| 71 r.Read(repeater.data) |
| 72 |
| 73 return &repeater |
| 74 } |
| 75 |
| 76 // io.Reader which produces repeated text data (very compressible). |
| 77 func RepeatedTextGenerator(r *rand.Rand) io.Reader { |
| 78 // Figure out how big the repeated sequence will be |
| 79 sequence_size := randBetween(r, SEQUENCE_MINSIZE, SEQUENCE_MAXSIZE) |
| 80 |
| 81 repeater := repeatedByteGenerator{index: 0, data: []byte(randStr(r, sequ
ence_size, textChars))} |
| 82 |
| 83 return &repeater |
| 84 } |
| 85 |
| 86 // io.Reader which produces repeated Lorem Ipsum text data (very compressible). |
| 87 func LoremTextGenerator() io.Reader { |
| 88 repeater := repeatedByteGenerator{index: 0, data: []byte(lorem)} |
| 89 return &repeater |
| 90 } |
OLD | NEW |