blobber/
lib.rs

1//! Utility for generating random blobs of data
2use std::ops::AddAssign;
3mod rand;
4use rand::{Rng};
5/// This function returns a string as long as the
6/// `bytes` parameter. The `numbered` param will be
7/// passed to `get_string` 
8pub fn get_lorem(bytes: usize, numbered: bool) -> String {
9    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.";
10    get_string(bytes, lorem, numbered)
11}
12
13/// This function returns a string length of the
14/// `bytes` parameter. The `template` `&str` will be repeated
15/// the number of times required to reach the `bytes`
16/// if `numbered` is true, each iteration will be extened by the number
17/// plus '.' and a space. Any overflow will be truncated
18pub fn get_string(bytes: usize, template: &str, numbered: bool) -> String {
19    let mut ret = String::new();
20    if numbered {
21        for i in 0..((bytes / template.len()) + 1) {
22            ret.add_assign(&format!("{}. {}", i, template));
23        }
24    } else {
25        while ret.as_bytes().len() < bytes {
26            ret.push_str(template);
27        }
28    }
29    ret.truncate(bytes);
30    ret
31}
32
33/// This function returns a `Vec<u8>` generated
34/// with a very simple PRNG seeded with the unix epoch
35pub fn get_rng_blob(bytes: usize) -> Vec<u8> {
36    let mut ret: Vec<u8> = Vec::new();
37    let mut rng = Rng::default();
38    while ret.len() < bytes {
39        ret.push(rng.next());
40    }
41    ret
42}
43
44/// This function returns a `Vec<u8>` generated
45/// with a very simple PRNG seeded with the provided seed param.
46/// This is useful for genrating a repeatable blob (the same
47/// seed will always provide the same sequence) if you don't want to
48/// provide a template slice.
49pub fn get_seeded_rng_blob(bytes: usize, seed: u8) -> Vec<u8> {
50    let mut ret: Vec<u8> = Vec::new();
51    let mut rng = Rng::new(seed);
52    while ret.len() < bytes {
53        ret.push(rng.next());
54    }
55    ret
56}
57
58/// Get a vector of u8 values the length of the bytes param
59/// the template param will be repeated to fill the return
60/// value
61pub fn get_blob(bytes: usize, template: &[u8]) -> Vec<u8> {
62    let mut ret: Vec<u8> = Vec::new();
63    for _ in 0..((bytes / template.len() as usize) + 1) {
64        ret.extend(template);
65    }
66    ret.truncate(bytes);
67    ret
68}
69
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    
75    #[test]
76    fn lorem_test() {
77        let test_val = get_lorem(1024, true);
78        let test_val_len = test_val.as_bytes().len();
79        assert_eq!(test_val_len, 1024);
80    }
81
82    #[test]
83    fn non_lorem_test() {
84        let template = "test";
85        let test_val = get_string(1024, template, false);
86        assert_eq!(test_val.as_bytes().len(), 1024);
87        assert!(test_val.contains(template));
88        let empty_test = test_val.replace(template, "");
89        assert_eq!(empty_test.len(), 0);
90    }
91
92    #[test]
93    fn blob_test() {
94        let template = &[1, 2, 3];
95        let test_val = get_blob(1024, template);
96        assert!(test_val.len() >= 1024);
97        let bytes = get_blob(6, template);
98        assert_eq!(bytes, &[1, 2, 3, 1, 2, 3]);
99    }
100
101    #[test]
102    fn rng_blob_test() {
103        let test_val = get_rng_blob(1024);
104        assert_eq!(test_val.len(), 1024);
105    }
106}