Skip to content

Commit 936245f

Browse files
committed
use std read to read file contents
1 parent 2dbc2b4 commit 936245f

File tree

1 file changed

+6
-27
lines changed
  • test-libz-rs-sys/src

1 file changed

+6
-27
lines changed

test-libz-rs-sys/src/gz.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,15 +1001,8 @@ fn gzputc_basic() {
10011001
assert_eq!(unsafe { gzclose(file) }, Z_OK);
10021002

10031003
// Validate that the file contains the expected bytes.
1004-
let mode = binary_mode(libc::O_RDONLY);
1005-
let fd = unsafe { libc::open(CString::new(file_name.as_str()).unwrap().as_ptr(), mode) };
1006-
assert_ne!(fd, -1);
1007-
// Try to read more than the expected amount of data, to ensure we get everything.
1008-
let mut buf = [0u8; CONTENT.len() + 1];
1009-
let bytes_read = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as _) };
1010-
assert_ne!(bytes_read, -1);
1011-
assert_eq!(&buf[..bytes_read as usize], CONTENT);
1012-
assert_eq!(unsafe { libc::close(fd) }, 0);
1004+
let actual = std::fs::read(file_name).unwrap();
1005+
assert_eq!(actual, CONTENT);
10131006
}
10141007

10151008
#[test]
@@ -1073,15 +1066,8 @@ fn gzputs_basic() {
10731066

10741067
// Validate that the file contains the expected bytes.
10751068
const EXPECTED: &str = "zlib string larger than the buffer size";
1076-
let mode = binary_mode(libc::O_RDONLY);
1077-
let fd = unsafe { libc::open(CString::new(file_name.as_str()).unwrap().as_ptr(), mode) };
1078-
assert_ne!(fd, -1);
1079-
// Try to read more than the expected amount of data, to ensure we get everything.
1080-
let mut buf = [0u8; EXPECTED.len() + 1];
1081-
let bytes_read = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as _) };
1082-
assert_ne!(bytes_read, -1);
1083-
assert_eq!(&buf[..bytes_read as usize], EXPECTED.as_bytes());
1084-
assert_eq!(unsafe { libc::close(fd) }, 0);
1069+
let actual = std::fs::read(file_name).unwrap();
1070+
assert_eq!(actual, EXPECTED.as_bytes());
10851071
}
10861072

10871073
#[test]
@@ -1526,16 +1512,9 @@ fn gzfwrite_basic() {
15261512
assert_eq!(unsafe { gzclose(file) }, Z_OK);
15271513

15281514
// Read in the file and verify that the contents match what was passed to gzfwrite.
1529-
let mode = binary_mode(libc::O_RDONLY);
1530-
let fd = unsafe { libc::open(CString::new(file_name.as_str()).unwrap().as_ptr(), mode) };
1531-
assert_ne!(fd, -1);
15321515
const EXPECTED: &[u8] = b"test of gzfwrite";
1533-
let mut buf = [0u8; EXPECTED.len() + 1];
1534-
let ret = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as _) };
1535-
assert_eq!(ret, EXPECTED.len() as _);
1536-
assert_eq!(&buf[..EXPECTED.len()], EXPECTED);
1537-
1538-
assert_eq!(unsafe { libc::close(fd) }, 0);
1516+
let actual = std::fs::read(file_name).unwrap();
1517+
assert_eq!(actual, EXPECTED);
15391518
}
15401519

15411520
#[test]

0 commit comments

Comments
 (0)