File: large.rs

package info (click to toggle)
rust-cfb 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 580 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,435 bytes parent folder | download
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
use cfb::CompoundFile;
use rand::prelude::{Rng, SeedableRng};
use rand_pcg::Pcg32;
use std::io::{Cursor, Seek, SeekFrom, Write};
use std::path::PathBuf;

/// Regression test for https://github.com/mdsteele/rust-cfb/issues/12.
#[test]
fn large_file_issue_12() {
    // Use a reproducible PRNG sequence.
    let mut rng = Pcg32::from_seed(*b"1941039482934820");

    let cursor = Cursor::new(Vec::new());
    let mut comp = CompoundFile::create(cursor).unwrap();
    for _ in 0..50 {
        // Generate a random path.
        let mut path = PathBuf::new();
        for _ in 0..6 {
            path.push(format!("{:08x}", rng.gen::<u64>()));
        }
        comp.create_storage_all(&path).unwrap();
        path.push("file");

        // Create a stream with a random amount of data, chosen such that
        // sometimes the length is below the mini-stream cutoff of 4096, and
        // sometimes it's above.
        let length: usize = rng.gen_range(4000..4200);
        let data = vec![0x11; length];
        comp.create_stream(&path).unwrap().write_all(&data).unwrap();

        // Open the same stream again, and append a bit more data to the end.
        let mut stream = comp.open_stream(&path).unwrap();
        stream.seek(SeekFrom::End(0)).unwrap();
        stream.write_all(b"additional test data").unwrap();
    }

    let cursor = comp.into_inner();
    let _comp = CompoundFile::open_strict(cursor).expect("re-open");
}