From: Tilman Sauerbeck Date: Sat, 11 Jan 2020 09:43:51 +0000 (+0100) Subject: test: Add a first test for Logger::list_recordings(). X-Git-Url: http://git.code-monkey.de/?a=commitdiff_plain;h=7ec91a770baf0bf3514cef995d310890630a7869;p=gps-watch.git test: Add a first test for Logger::list_recordings(). --- diff --git a/test/logger_test.rs b/test/logger_test.rs index 614108b..93568bb 100644 --- a/test/logger_test.rs +++ b/test/logger_test.rs @@ -21,6 +21,8 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +use std::io::{Read, Write}; +use std::os::unix::io::FromRawFd; use common::buffer::{Buffer, BufferUserData}; use common::gps; use common::storage::{Storage, Error}; @@ -346,3 +348,50 @@ fn get_recording_valid() { assert!(logger.get_recording(1, &mut yenc_buffer).is_ok()); } + +// Verifies that Logger::list_recordings() shows a listing +// of a single recording in the very first sector. +#[test] +fn list_recordings1() { + let pipe = Pipe::new(); + + let mut file = unsafe { std::fs::File::from_raw_fd(pipe.write_fd()) }; + + let mut ls_buffer_space = [0u8; 4096]; + let mut ls_buffer = Buffer::alloc(); + + let user_data = (&mut file as *mut std::fs::File) as *mut BufferUserData; + + ls_buffer.init(ls_buffer_space.as_mut_ptr(), + ls_buffer_space.len(), + flush_write_buffer, + user_data); + + let mut fake_storage = FakeStorage::new(); + + // Mark first sector as in use. + let recording0 = [ + // Header: + 0x01, 0x00, 0x01, 0x00, + 0xa2, 0xdb, 0x14, 0x5e, + ]; + + fake_storage.expected[0..recording0.len()].copy_from_slice(&recording0); + fake_storage.actual[0..recording0.len()].copy_from_slice(&recording0); + + let mut logger = Logger::new(&mut fake_storage); + logger.init(); + + logger.list_recordings(&mut ls_buffer); + + let mut file = unsafe { std::fs::File::from_raw_fd(pipe.read_fd()) }; + + let mut listing_raw = [0u8; 256]; + let num_bytes_read = file.read(&mut listing_raw).unwrap(); + + let expected_bytes = b"\ +2020-01-07 19:27:30 1\n"; + + assert_eq!(String::from_utf8(expected_bytes.to_vec()).unwrap(), + String::from_utf8(listing_raw[0..num_bytes_read].to_vec()).unwrap()); +}