summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Morrow <andrew.d.morrow@gmail.com>2018-05-02 19:08:41 -0600
committerAndrew Morrow <andrew.d.morrow@gmail.com>2018-05-28 21:39:20 -0600
commit325c43c6d5b5b8945dd8bee3575500887923b2cf (patch)
tree1e92ceebfae83013c3bb216041315d87470f5539 /test
parentb87d45b349485cf2ecf7414af70133bd8e91c4ab (diff)
downloadnix-325c43c6d5b5b8945dd8bee3575500887923b2cf.zip
Implement sendfile on FreeBSD and Darwin
Diffstat (limited to 'test')
-rw-r--r--test/test.rs6
-rw-r--r--test/test_sendfile.rs109
2 files changed, 109 insertions, 6 deletions
diff --git a/test/test.rs b/test/test.rs
index 6668e06e..8083b8f9 100644
--- a/test/test.rs
+++ b/test/test.rs
@@ -22,7 +22,11 @@ mod test_net;
mod test_nix_path;
mod test_poll;
mod test_pty;
-#[cfg(any(target_os = "linux", target_os = "android"))]
+#[cfg(any(target_os = "android",
+ target_os = "freebsd",
+ target_os = "ios",
+ target_os = "linux",
+ target_os = "macos"))]
mod test_sendfile;
mod test_stat;
mod test_unistd;
diff --git a/test/test_sendfile.rs b/test/test_sendfile.rs
index 7b51a1a6..3bc7932f 100644
--- a/test/test_sendfile.rs
+++ b/test/test_sendfile.rs
@@ -1,15 +1,22 @@
use std::io::prelude::*;
use std::os::unix::prelude::*;
-use tempfile::tempfile;
-
use libc::off_t;
+use nix::sys::sendfile::*;
+use tempfile::tempfile;
-use nix::unistd::{close, pipe, read};
-use nix::sys::sendfile::sendfile;
+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"))] {
+ use std::net::Shutdown;
+ use std::os::unix::net::UnixStream;
+ }
+}
+#[cfg(any(target_os = "android", target_os = "linux"))]
#[test]
-fn test_sendfile() {
+fn test_sendfile_linux() {
const CONTENTS: &[u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write_all(CONTENTS).unwrap();
@@ -28,3 +35,95 @@ fn test_sendfile() {
close(rd).unwrap();
close(wr).unwrap();
}
+
+#[cfg(target_os = "freebsd")]
+#[test]
+fn test_sendfile_freebsd() {
+ // 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()),
+ SfFlags::empty(),
+ 0,
+ );
+ 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() {
+ // 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);
+}