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
pub mod reader;
pub mod writer;

pub use self::reader::TokioReader;
pub use self::writer::TokioWriter;

#[cfg(test)]
mod test {
    use super::*;
    use crate::test::VX_TEST_GAME;
    use std::io::Seek;
    use std::io::SeekFrom;
    use tokio::io::AsyncReadExt;

    #[tokio::test]
    async fn reader_smoke() {
        let file = std::fs::read(VX_TEST_GAME).expect("failed to open archive");
        let file = std::io::Cursor::new(file);
        let mut reader = TokioReader::new(file);
        reader.read_header().await.expect("failed to read header");

        // Ensure skipping works.
        let mut num_skipped_files = 0;
        while let Some(_file) = reader.read_file().await.expect("failed to read file") {
            num_skipped_files += 1;
        }

        // Reset position and recreate reader.
        let mut file = reader.into_inner();
        file.seek(SeekFrom::Start(0))
            .expect("failed to seek to start");
        let mut reader = TokioReader::new(file);
        reader.read_header().await.expect("failed to read header");

        let mut files = Vec::new();
        while let Some(mut file) = reader.read_file().await.expect("failed to read file") {
            let mut buffer: Vec<u8> = Vec::new();
            file.read_to_end(&mut buffer)
                .await
                .expect("failed to read file");

            files.push((file.name().to_string(), buffer));
        }

        assert!(files.len() == num_skipped_files);

        // Validate with sync impl
        let mut file = reader.into_inner();
        file.seek(SeekFrom::Start(0))
            .expect("failed to seek to start");
        let mut reader = crate::Reader::new(file);
        reader.read_header().expect("failed to read header");

        let mut files_sync = Vec::new();
        while let Some(mut file) = reader.read_file().expect("failed to read file") {
            use std::io::Read;

            let mut buffer: Vec<u8> = Vec::new();
            file.read_to_end(&mut buffer).expect("failed to read file");
            files_sync.push((file.name().to_string(), buffer));
        }

        assert!(files == files_sync);
    }

    #[tokio::test]
    async fn reader_writer_smoke() {
        let file = std::fs::read(VX_TEST_GAME).expect("failed to open archive");
        let file = std::io::Cursor::new(file);
        let mut reader = TokioReader::new(file);
        reader.read_header().await.expect("failed to read header");

        // Read entire archive into Vec.
        let mut files = Vec::new();
        while let Some(mut file) = reader.read_file().await.expect("failed to read file") {
            let mut buffer = Vec::new();
            file.read_to_end(&mut buffer)
                .await
                .expect("failed to read file");
            files.push((file.name().to_string(), buffer));
        }

        // Write all files into new archive.
        let mut new_file = Vec::<u8>::new();
        let mut writer = TokioWriter::new(&mut new_file);
        writer.write_header().await.expect("failed to write header");
        for (file_name, file_data) in files.iter() {
            writer
                .write_file(
                    file_name,
                    u32::try_from(file_data.len()).expect("file data too large"),
                    &**file_data,
                )
                .await
                .expect("failed to write file");
        }
        writer.finish().await.expect("failed to flush");

        let file = reader.into_inner();

        // Ensure archives are byte-for-byte equivalent.
        assert!(&new_file == file.get_ref());
    }
}