Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
/Cargo.lock
/.idea/
/tests/test_disk.qcow2
/tests/*.qcow2
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ In this example:

### Notes:

- Only Read Request (RRQ) is supported.
- If a file exists in both the local directory and the NBD-based filesystem, the **local file takes precedence**.
- The NBD disk is connected lazily on the first read request.
- Initial setup of the virtual NBD filesystem takes **1.5 to 3 seconds**, so the first request usually need to be retried automatically by the client.
- The NBD disk is either:
- Connected proactively when config is created to avoid the first read request delay.
- Connected lazily on the first read request.
- An inactive NBD disk is automatically disconnected after a period of inactivity. This timeout is configurable via the `idle_timeout` daemon argument.
- Supported TFTP options:
- timeout
Expand Down
222 changes: 0 additions & 222 deletions src/_tests.rs

This file was deleted.

59 changes: 3 additions & 56 deletions src/cursor.rs → src/cursor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::fmt::{Display, Formatter};

#[cfg(test)]
mod tests;

pub(super) struct ReadCursor<'a> {
datagram: &'a [u8],
index: usize,
Expand Down Expand Up @@ -107,59 +110,3 @@ impl BufferError {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn extract_ushort() {
let buffer: Vec<u8> = vec![0x00, 0x0A, 0x00, 0x00, 0x00, 0xab, 0xcd, 0xef];
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_ushort();
assert_eq!(result.unwrap(), 0x0A);
}

#[test]
fn extract_ushort_not_enough_data() {
let buffer: Vec<u8> = vec![0x00, 0x0A, 0xFF];
let mut cursor = ReadCursor::new(&buffer);
cursor.extract_ushort().unwrap();
let result = cursor.extract_ushort();
assert!(matches!(result.unwrap_err(), ParseError::NotEnoughData));
}

#[test]
fn extract_string() {
let buffer: Vec<u8> = b"Arbitrary_string\x00\x0A".to_vec();
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_string();
assert_eq!(result.unwrap(), "Arbitrary_string");
}

#[test]
fn extract_string_not_enough_data() {
let buffer: Vec<u8> = b"Arbitrary_string\x00".to_vec();
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_string();
assert_eq!(result.unwrap(), "Arbitrary_string");
let error = cursor.extract_string();
assert!(matches!(error.unwrap_err(), ParseError::NotEnoughData));
}

#[test]
fn extract_string_non_utf() {
let buffer: Vec<u8> = b"Arbitrary_\xFFstring\x00\x0A".to_vec();
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_string();
assert!(matches!(result.unwrap_err(), ParseError::Generic(_)));
}

#[test]
fn extract_non_terminated_string() {
let buffer: Vec<u8> = b"Arbitrary_string".to_vec();
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_string();
assert!(matches!(result.unwrap_err(), ParseError::Generic(_)));
}
}
52 changes: 52 additions & 0 deletions src/cursor/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::*;

#[test]
fn extract_ushort() {
let buffer: Vec<u8> = vec![0x00, 0x0A, 0x00, 0x00, 0x00, 0xab, 0xcd, 0xef];
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_ushort();
assert_eq!(result.unwrap(), 0x0A);
}

#[test]
fn extract_ushort_not_enough_data() {
let buffer: Vec<u8> = vec![0x00, 0x0A, 0xFF];
let mut cursor = ReadCursor::new(&buffer);
cursor.extract_ushort().unwrap();
let result = cursor.extract_ushort();
assert!(matches!(result.unwrap_err(), ParseError::NotEnoughData));
}

#[test]
fn extract_string() {
let buffer: Vec<u8> = b"Arbitrary_string\x00\x0A".to_vec();
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_string();
assert_eq!(result.unwrap(), "Arbitrary_string");
}

#[test]
fn extract_string_not_enough_data() {
let buffer: Vec<u8> = b"Arbitrary_string\x00".to_vec();
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_string();
assert_eq!(result.unwrap(), "Arbitrary_string");
let error = cursor.extract_string();
assert!(matches!(error.unwrap_err(), ParseError::NotEnoughData));
}

#[test]
fn extract_string_non_utf() {
let buffer: Vec<u8> = b"Arbitrary_\xFFstring\x00\x0A".to_vec();
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_string();
assert!(matches!(result.unwrap_err(), ParseError::Generic(_)));
}

#[test]
fn extract_non_terminated_string() {
let buffer: Vec<u8> = b"Arbitrary_string".to_vec();
let mut cursor = ReadCursor::new(&buffer);
let result = cursor.extract_string();
assert!(matches!(result.unwrap_err(), ParseError::Generic(_)));
}
Loading