axfive_libzip_sys/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4
5include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
6
7#[cfg(test)]
8mod tests {
9    use super::*;
10    use tempdir::TempDir;
11    use std::string::String;
12    use std::vec::Vec;
13    use std::ffi::CString;
14    use std::os::raw::{c_int, c_void};
15    use std::mem::zeroed;
16
17    #[test]
18    fn round_trip() {
19        let tempdir = TempDir::new("test").unwrap();
20        let zip_path = CString::new(tempdir.path().join("file.zip").to_str().unwrap()).unwrap();
21        let foo = "Lorem ipsum dolor sit amet";
22        let bar = "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
23        unsafe {
24            let mut errorp: c_int = 0;
25            let zip = zip_open(zip_path.as_ptr(), (ZIP_CREATE | ZIP_EXCL) as _, &mut errorp as _);
26            assert!(!zip.is_null());
27
28            let mut error = zeroed();
29            zip_error_init(&mut error as _);
30            let source = zip_source_buffer_create(foo.as_ptr() as _, foo.len() as zip_uint64_t, 0, &mut error as _);
31            assert!(!source.is_null());
32
33            assert_ne!(zip_file_add(zip, CString::new("foo").unwrap().as_ptr(), source, ZIP_FL_ENC_GUESS), -1);
34
35            let source = zip_source_buffer_create(bar.as_ptr() as _, bar.len() as _, 0, &mut error as _);
36            assert!(!source.is_null());
37
38            assert_ne!(zip_file_add(zip, CString::new("bar").unwrap().as_ptr(), source, ZIP_FL_ENC_GUESS), -1);
39
40            assert_eq!(zip_close(zip), 0);
41        }
42
43        unsafe {
44            let mut errorp: c_int = 0;
45            let zip = zip_open(zip_path.as_ptr(), (ZIP_RDONLY | ZIP_CHECKCONS) as _, &mut errorp as _);
46            assert!(!zip.is_null());
47
48            let foo_file = zip_fopen(zip, CString::new("foo").unwrap().as_ptr(), ZIP_FL_ENC_GUESS);
49            assert!(!foo_file.is_null());
50            let mut foo_content: Vec<u8> = Vec::with_capacity(1024);
51            let new_len = zip_fread(foo_file, foo_content.as_mut_ptr() as _, foo_content.capacity() as _);
52            assert_ne!(new_len, -1);
53            assert_eq!(zip_fclose(foo_file), 0);
54            foo_content.set_len(new_len as _);
55            assert_eq!(String::from_utf8_unchecked(foo_content), foo);
56
57            let bar_file = zip_fopen(zip, CString::new("bar").unwrap().as_ptr(), ZIP_FL_ENC_GUESS);
58            assert!(!bar_file.is_null());
59            let mut bar_content: Vec<u8> = Vec::with_capacity(1024);
60            let new_len = zip_fread(bar_file, bar_content.as_mut_ptr() as _, bar_content.capacity() as _);
61            assert_ne!(new_len, -1);
62            assert_eq!(zip_fclose(bar_file), 0);
63            bar_content.set_len(new_len as _);
64            assert_eq!(String::from_utf8_unchecked(bar_content), bar);
65
66            assert_eq!(zip_close(zip), 0);
67        }
68    }
69}