1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! Utility for generating random blobs of data
//! ```
//! let lorem = get_lorem(1024, true);
//! assert!(lorem.as_bytes() >= 1024);
//!
//! let junk = get_string(1024, "junk", false);
//! assert!(junk.as_bytes() >= 1024);
//!
//! let blob = get_blob(1024, &[1, 2, 3]);
//! assert!(test_val.len() >= 1024);
//! ```

mod rand;
use rand::{Rng};
/// This function returns a string at least as long as the
/// `bytes` parameter. The `numbered` will prefix each iteration
/// of lorem ipsum.
pub fn get_lorem(bytes: usize, numbered: bool) -> String {
    let lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    get_string(bytes, lorem, numbered)
}

/// This function returns a string length of the
/// `bytes` parameter. The `template` `&str` will be repeated
/// the number of times required to reach the `bytes`
/// exceeding the value if it doesn't evenly divide into the `bytes`
/// if `numbered` is true, each iteration will be extened by the number
/// plus '.' and a space. Any overflow will be truncated
pub fn get_string(bytes: usize, template: &str, numbered: bool) -> String {
    let mut ret = String::new();
    if numbered {
        for i in 0..((bytes / template.len()) + 1) {
            ret.push_str(format!("{}. {}", i, template).as_str());
        }
    } else {
        while ret.as_bytes().len() < bytes {
            ret.push_str(format!("{}", template).as_str());
        }
    }
    ret.truncate(bytes);
    ret
}

/// This function returns a vector of bytes generated
/// with a very simple PRNG provided by the Rng struct.
/// The rng will be seeded with the default seed
pub fn get_rng_blob(bytes: usize) -> Vec<u8> {
    let mut ret: Vec<u8> = Vec::new();
    let mut rng = Rng::default();
    while ret.len() < bytes {
        ret.push(rng.next());
    }
    ret
}

/// This function returns a vector of bytes generated
/// with a very simple PRNG provided by the Rng struct.
/// the rng will be seeded with the provided seed param.
/// This is useful for genrating a repeatable blob (the same
/// seed will always provide the same sequence) if you don't want to
/// provide a template slice.
pub fn get_seeded_rng_blob(bytes: usize, seed: u8) -> Vec<u8> {
    let mut ret: Vec<u8> = Vec::new();
    let mut rng = Rng::new(seed);
    while ret.len() < bytes {
        ret.push(rng.next());
    }
    ret
}

/// Get a vector of u8 values the length of the bytes param
/// the template param will be repeated to fill the return
/// value
pub fn get_blob(bytes: usize, template: &[u8]) -> Vec<u8> {
    let mut ret: Vec<u8> = Vec::new();
    for _ in 0..((bytes / template.len() as usize) + 1) {
        ret.extend(template);
    }
    ret.truncate(bytes);
    ret
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lorem_test() {
        let test_val = get_lorem(1024, true);
        let test_val_len = test_val.as_bytes().len();
        assert_eq!(test_val_len, 1024);
    }

    #[test]
    fn non_lorem_test() {
        let template = "test";
        let test_val = get_string(1024, template, false);
        assert_eq!(test_val.as_bytes().len(), 1024);
        assert!(test_val.contains(template));
        let empty_test = test_val.replace(template, "");
        assert_eq!(empty_test.len(), 0);
    }

    #[test]
    fn blob_test() {
        let template = &[1, 2, 3];
        let test_val = get_blob(1024, template);
        assert!(test_val.len() >= 1024);
        let bytes = get_blob(6, template);
        assert_eq!(bytes, &[1, 2, 3, 1, 2, 3]);
    }

    #[test]
    fn rng_blob_test() {
        let test_val = get_rng_blob(1024);
        assert_eq!(test_val.len(), 1024);
    }
}