Skip to content

Commit 6014307

Browse files
committed
Test for cache extraction to /dev/fd/{n}
1 parent 124f71d commit 6014307

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/cache/cache.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,4 +867,38 @@ mod test {
867867
let result = runtime.block_on(cache_read.extract_objects(objects, pool));
868868
assert!(result.is_ok(), "Extracting to /dev/null should succeed");
869869
}
870+
871+
#[cfg(unix)]
872+
#[test]
873+
fn test_extract_object_to_dev_fd_something() {
874+
// Open a pipe, write to `/dev/fd/{fd}` and check the other end that the correct data was written.
875+
use std::os::fd::AsRawFd;
876+
use tokio::io::AsyncReadExt;
877+
let runtime = tokio::runtime::Builder::new_current_thread()
878+
.enable_all()
879+
.worker_threads(1)
880+
.build()
881+
.unwrap();
882+
let pool = runtime.handle();
883+
let mut cache_data = CacheWrite::new();
884+
let data = b"test data";
885+
cache_data.put_bytes("test_key", data).unwrap();
886+
let cache_read = CacheRead::from(io::Cursor::new(cache_data.finish().unwrap())).unwrap();
887+
runtime.block_on(async {
888+
let (sender, mut receiver) = tokio::net::unix::pipe::pipe().unwrap();
889+
let sender_fd = sender.into_blocking_fd().unwrap();
890+
let raw_fd = sender_fd.as_raw_fd();
891+
let objects = vec![FileObjectSource {
892+
key: "test_key".to_string(),
893+
path: PathBuf::from(format!("/dev/fd/{}", raw_fd)),
894+
optional: false,
895+
}];
896+
let result = cache_read.extract_objects(objects, pool).await;
897+
assert!(result.is_ok(), "Extracting to /dev/fd/{} should succeed", raw_fd);
898+
let mut buf = vec![0; data.len()];
899+
let n = receiver.read_exact(&mut buf).await.unwrap();
900+
assert_eq!(n, data.len(), "Read the correct number of bytes");
901+
assert_eq!(buf, data, "Read the correct data from /dev/fd/{}", raw_fd);
902+
});
903+
}
870904
}

0 commit comments

Comments
 (0)