summaryrefslogtreecommitdiff
path: root/test/test_sendfile.rs
diff options
context:
space:
mode:
authorRyan Zoeller <rtzoeller@rtzoeller.com>2021-12-24 17:57:20 -0600
committerRyan Zoeller <rtzoeller@rtzoeller.com>2022-01-03 04:05:09 -0600
commit4d5c090fcf9f6f51d4303e903676f2d487d60272 (patch)
tree3c6b6aa5610518380ec79c287f3f0f9effda18f4 /test/test_sendfile.rs
parente4e17f2969469d83c9af21e3ba1fdd0020f0124e (diff)
downloadnix-4d5c090fcf9f6f51d4303e903676f2d487d60272.zip
Add sendfile(2) for DragonFly
Diffstat (limited to 'test/test_sendfile.rs')
-rw-r--r--test/test_sendfile.rs47
1 files changed, 46 insertions, 1 deletions
diff --git a/test/test_sendfile.rs b/test/test_sendfile.rs
index b6559d32..e56ff12f 100644
--- a/test/test_sendfile.rs
+++ b/test/test_sendfile.rs
@@ -8,7 +8,7 @@ use tempfile::tempfile;
cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux"))] {
use nix::unistd::{close, pipe, read};
- } else if #[cfg(any(target_os = "freebsd", target_os = "ios", target_os = "macos"))] {
+ } else if #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos"))] {
use std::net::Shutdown;
use std::os::unix::net::UnixStream;
}
@@ -105,6 +105,51 @@ fn test_sendfile_freebsd() {
assert_eq!(expected_string, read_string);
}
+#[cfg(target_os = "dragonfly")]
+#[test]
+fn test_sendfile_dragonfly() {
+ // Declare the content
+ let header_strings = vec!["HTTP/1.1 200 OK\n", "Content-Type: text/plain\n", "\n"];
+ let body = "Xabcdef123456";
+ let body_offset = 1;
+ let trailer_strings = vec!["\n", "Served by Make Believe\n"];
+
+ // Write the body to a file
+ let mut tmp = tempfile().unwrap();
+ tmp.write_all(body.as_bytes()).unwrap();
+
+ // Prepare headers and trailers for sendfile
+ let headers: Vec<&[u8]> = header_strings.iter().map(|s| s.as_bytes()).collect();
+ let trailers: Vec<&[u8]> = trailer_strings.iter().map(|s| s.as_bytes()).collect();
+
+ // Prepare socket pair
+ let (mut rd, wr) = UnixStream::pair().unwrap();
+
+ // Call the test method
+ let (res, bytes_written) = sendfile(
+ tmp.as_raw_fd(),
+ wr.as_raw_fd(),
+ body_offset as off_t,
+ None,
+ Some(headers.as_slice()),
+ Some(trailers.as_slice()),
+ );
+ assert!(res.is_ok());
+ wr.shutdown(Shutdown::Both).unwrap();
+
+ // Prepare the expected result
+ let expected_string =
+ header_strings.concat() + &body[body_offset..] + &trailer_strings.concat();
+
+ // Verify the message that was sent
+ assert_eq!(bytes_written as usize, expected_string.as_bytes().len());
+
+ let mut read_string = String::new();
+ let bytes_read = rd.read_to_string(&mut read_string).unwrap();
+ assert_eq!(bytes_written as usize, bytes_read);
+ assert_eq!(expected_string, read_string);
+}
+
#[cfg(any(target_os = "ios", target_os = "macos"))]
#[test]
fn test_sendfile_darwin() {