From 99c805f45e471c22cd27746c3c2fb7a836939d8d Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Fri, 10 Jan 2020 23:20:28 +0100 Subject: [PATCH] test: Use a pipe to back the write buffer for the Logger test. --- test/logger_test.rs | 50 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/test/logger_test.rs b/test/logger_test.rs index efdfcf2..614108b 100644 --- a/test/logger_test.rs +++ b/test/logger_test.rs @@ -68,6 +68,36 @@ impl Storage for FakeStorage { } } +struct Pipe { + pipe_fd: [i32; 2], +} + +impl Pipe { + fn new() -> Pipe { + let mut pipe_fd = [-1i32; 2]; + + unsafe { + extern { + fn pipe(pipefd: *mut i32) -> i32; + } + + pipe(pipe_fd.as_mut_ptr()); + } + + Pipe { + pipe_fd: pipe_fd, + } + } + + fn read_fd(&self) -> i32 { + self.pipe_fd[0] + } + + fn write_fd(&self) -> i32 { + self.pipe_fd[1] + } +} + // Runs a couple of recordings on fully erased flash memory. #[test] fn first_recording() { @@ -219,11 +249,12 @@ extern "C" fn flush_write_buffer(user_data: *mut BufferUserData, buf: *const u8, _bufsiz: usize, count: usize) -> isize { - let _final_buffer = user_data as *mut [u8; 8192]; - let final_buffer = unsafe { &mut * _final_buffer }; + let _file = user_data as *mut std::fs::File; unsafe { - core::ptr::copy_nonoverlapping(buf, final_buffer.as_mut_ptr(), count); + let file = &mut * _file; + + file.write_all(std::slice::from_raw_parts(buf, count)).unwrap(); } count as isize @@ -232,17 +263,20 @@ extern "C" fn flush_write_buffer(user_data: *mut BufferUserData, // Verifies that Logger::get_recording() detects unknown recording IDs. #[test] fn get_recording_invalid() { - let mut final_buffer = [0u8; 8192]; + let pipe = Pipe::new(); + + let mut file = unsafe { std::fs::File::from_raw_fd(pipe.write_fd()) }; let mut yenc_buffer_space = [0u8; 8192]; let mut yenc_buffer = Buffer::alloc(); - let user_data = (&mut final_buffer as *mut [u8; 8192]) as *mut BufferUserData; + let user_data = (&mut file as *mut std::fs::File) as *mut BufferUserData; yenc_buffer.init(yenc_buffer_space.as_mut_ptr(), yenc_buffer_space.len(), flush_write_buffer, user_data); + let mut fake_storage = FakeStorage::new(); let mut logger = Logger::new(&mut fake_storage); @@ -259,12 +293,14 @@ fn get_recording_invalid() { // Verifies that Logger::get_recording() can retrieve finished recordings. #[test] fn get_recording_valid() { - let mut final_buffer = [0u8; 8192]; + let pipe = Pipe::new(); + + let mut file = unsafe { std::fs::File::from_raw_fd(pipe.write_fd()) }; let mut yenc_buffer_space = [0u8; 8192]; let mut yenc_buffer = Buffer::alloc(); - let user_data = (&mut final_buffer as *mut [u8; 8192]) as *mut BufferUserData; + let user_data = (&mut file as *mut std::fs::File) as *mut BufferUserData; yenc_buffer.init(yenc_buffer_space.as_mut_ptr(), yenc_buffer_space.len(), -- 2.30.2