test: Add a first test for Logger::list_recordings().
authorTilman Sauerbeck <tilman@code-monkey.de>
Sat, 11 Jan 2020 09:43:51 +0000 (10:43 +0100)
committerTilman Sauerbeck <tilman@code-monkey.de>
Sun, 12 Jan 2020 12:03:28 +0000 (13:03 +0100)
test/logger_test.rs

index 614108bf5eb2cc5bd5c70a8e72a017cfe1a51145..93568bb3f85ea00f7fe7bbfd4b032d448bbd27b0 100644 (file)
@@ -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());
+}