1
use std::{ env::temp_dir, process::Command };
2

            
3
#[cfg(feature = "flate2")]
4
use ::flate2::Compression;
5

            
6
use super::*;
7
use crate::string::AdhocWriter;
8

            
9
6
fn write_and_read_string<B>(builder: B, input: &str) -> Result<String> where B: RwBuilder {
10
6
    let string = builder.string();
11
6
    string.write_string(input)?;
12
6
    Ok(string.to_string())
13
6
}
14

            
15
#[cfg(any(feature = "flate2", feature = "chacha20", feature = "salsa20"))]
16
5
fn test_string<B>(builder: B) where B: RwBuilder {
17
5
    let text = String::from("This text is written from a String and read back into a String.");
18
5
    let actual = write_and_read_string(builder, &text).expect("String couldn't be written");
19
5
    assert_eq!(actual, text);
20
5
}
21

            
22
#[cfg(feature = "flate2")]
23
#[test]
24
1
fn compression() {
25
1
    test_string(VecBuilder::default().zlib(Compression::fast()));
26
1
    test_string(VecBuilder::default().gz(Compression::fast()));
27
1
    test_string(VecBuilder::default().deflate(Compression::fast()));
28
1
}
29

            
30
#[cfg(feature = "flate2")]
31
#[test]
32
1
fn crc() {
33
    use std::io::{ Read, Write };
34
1
    let expected_crc = 1_191_942_644;
35
1
    let data = [1, 2, 3, 4, 5];
36
1
    let builder = VecBuilder::default().crc();
37
    {
38
1
        let mut writer = builder.writer().expect("Writer couldn't be created.");
39
1
        writer.write_all(&data).expect("Couldn't write data.");
40
1
        assert_eq!(writer.crc().sum(), expected_crc);
41
    }
42
    {
43
1
        let mut reader = builder.reader().expect("Reader couldn't be created.");
44
1
        let mut buffer = vec![];
45
1
        let bytes_read = reader.read_to_end(&mut buffer).expect("Couldn't read into buffer.");
46
1
        assert_eq!(bytes_read, 5);
47
1
        assert_eq!(reader.crc().sum(), expected_crc);
48
1
        assert_eq!(buffer, data);
49
    }
50
1
}
51

            
52
#[cfg(feature = "chacha20")]
53
#[test]
54
1
fn chacha20() {
55
1
    let key = [0x42; 32];
56
1
    let nonce = [0x24; 12];
57
1
    test_string(VecBuilder::default().chacha20(key.into(), nonce.into()));
58
1
}
59

            
60
#[cfg(feature = "salsa20")]
61
#[test]
62
1
fn salsa20() {
63
1
    let key = [0x42; 32];
64
1
    let nonce = [0x24; 8];
65
1
    test_string(VecBuilder::default().salsa20(key.into(), nonce.into()));
66
1
}
67

            
68
#[test]
69
1
fn file() {
70
1
    let path = temp_dir().join("test_file.txt");
71
1
    let text = String::from("This text is written from a String and read back into a String.");
72
1
    let builder = FileBuilder::new(path.clone()).buffered();
73
1
    let result = write_and_read_string(builder, &text);
74
1
    std::fs::remove_file(path).expect("File couldn't be removed.");
75
1
    let actual = result.expect("String couldn't be written");
76
1
    assert_eq!(actual, text);
77
1
}
78

            
79
#[test]
80
1
fn process_stdout() {
81
1
    let mut command = Command::new("rustc");
82
1
    let _ = command.arg("--help");
83
1
    let help = ProcessBuilder::new(command).string().to_string();
84
1
    assert!(help.starts_with("Usage: rustc"));
85
1
}
86

            
87
#[cfg(target_os = "linux")]
88
#[test]
89
1
fn process_child() {
90
1
    let command = Command::new("tee");
91
1
    let builder = ProcessBuilder::new(command).spawn().expect("Couldn't spawn process").string();
92
1
    builder.write_string("Hello world.\n").expect("Couldn't write string.");
93
1
    assert_eq!(builder.to_string(), "Hello world.\n");
94
1
}
95

            
96
#[cfg(feature = "wincode")]
97
#[test]
98
1
fn wincode() {
99
1
    let builder = VecBuilder::default().wincode();
100
1
    let text = "This string is serialized and deserialized using wincode.";
101
1
    builder.save(&text).expect("Serialization failed.");
102
1
    let actual: String = builder.load().expect("Deserialization failed.");
103
1
    assert_eq!(actual, text);
104
1
}