rcom/tests/transfer_port.rs
2026-12-05 23:30:06 +03:00

194 lines
5.4 KiB
Rust

use std::sync::Arc;
use async_trait::async_trait;
use rcom::domain::{Entry, EntryKind, HostAlias, Location, SourceId};
use rcom::fs::{
BackendHandle, FileOperations, FsError, PartFile, PartWriter, ReadFileSystem, ReadPrefix,
ReadStream, StagingOperations, TransferCapabilities,
};
use tokio::io::AsyncReadExt;
struct ReadOnly;
#[async_trait]
impl ReadFileSystem for ReadOnly {
async fn list(&self, _location: &Location) -> Result<Vec<Entry>, FsError> {
Ok(Vec::new())
}
async fn canonicalize(&self, location: &Location) -> Result<Location, FsError> {
Ok(location.clone())
}
async fn metadata(&self, location: &Location) -> Result<Entry, FsError> {
Ok(Entry::new(
location.file_name_owned().unwrap_or_default(),
EntryKind::File,
))
}
async fn read_prefix(
&self,
_location: &Location,
_limit: usize,
) -> Result<ReadPrefix, FsError> {
Ok(ReadPrefix::default())
}
}
struct StubOperations {
source: SourceId,
capabilities: TransferCapabilities,
}
impl StubOperations {
fn new(source: SourceId) -> Self {
Self {
source,
capabilities: TransferCapabilities {
canonicalize: true,
regular_files: true,
atomic_rename: true,
sftp_version: Some(3),
..TransferCapabilities::default()
},
}
}
fn unsupported<T>() -> Result<T, FsError> {
Err(FsError::Unsupported("test backend".into()))
}
}
#[async_trait]
impl StagingOperations for StubOperations {}
#[async_trait]
impl FileOperations for StubOperations {
fn source_id(&self) -> &SourceId {
&self.source
}
fn capabilities(&self) -> TransferCapabilities {
self.capabilities
}
async fn canonicalize(&self, _location: &Location) -> Result<Location, FsError> {
Self::unsupported()
}
async fn metadata(
&self,
_location: &Location,
_follow_symlinks: bool,
) -> Result<Entry, FsError> {
Self::unsupported()
}
async fn open_read(&self, _location: &Location) -> Result<ReadStream, FsError> {
Self::unsupported()
}
async fn open_part(&self, _destination: &Location) -> Result<PartWriter, FsError> {
Self::unsupported()
}
async fn write_part(&self, _part: &mut PartWriter, _chunk: &[u8]) -> Result<(), FsError> {
Self::unsupported()
}
async fn flush_and_close(&self, _part: PartWriter) -> Result<PartFile, FsError> {
Self::unsupported()
}
async fn commit_part(&self, _part: PartFile) -> Result<(), FsError> {
Self::unsupported()
}
async fn discard_part(&self, _part: PartFile) -> Result<(), FsError> {
Self::unsupported()
}
async fn rename(&self, _from: &Location, _to: &Location) -> Result<(), FsError> {
Self::unsupported()
}
async fn remove_file(&self, _location: &Location) -> Result<(), FsError> {
Self::unsupported()
}
async fn remove_dir(&self, _location: &Location) -> Result<(), FsError> {
Self::unsupported()
}
}
fn assert_read_port<T: ReadFileSystem>() {}
#[tokio::test]
async fn read_port_accepts_a_backend_with_no_mutation_implementation() {
assert_read_port::<ReadOnly>();
let read: Arc<dyn ReadFileSystem> = Arc::new(ReadOnly);
let entries = read.list(&Location::local("/read-only")).await.unwrap();
assert!(entries.is_empty());
}
#[test]
fn backend_handle_rejects_a_mixed_endpoint_transfer_adapter() {
let read: Arc<dyn ReadFileSystem> = Arc::new(ReadOnly);
let transfer: Arc<dyn FileOperations> = Arc::new(StubOperations::new(SourceId::Ssh(
HostAlias::new("other-host"),
)));
let result = BackendHandle::new(SourceId::Local, read, transfer);
assert!(
matches!(result, Err(FsError::Other(message)) if message.contains("different endpoints"))
);
}
#[tokio::test]
async fn backend_handle_keeps_matching_read_and_transfer_adapters() {
let source = SourceId::Ssh(HostAlias::new("edge"));
let read: Arc<dyn ReadFileSystem> = Arc::new(ReadOnly);
let transfer: Arc<dyn FileOperations> = Arc::new(StubOperations::new(source.clone()));
let handle = match BackendHandle::new(source.clone(), read, transfer) {
Ok(handle) => handle,
Err(error) => panic!("matching endpoint was rejected: {error}"),
};
assert_eq!(handle.source_id(), &source);
assert_eq!(handle.transfer.capabilities().sftp_version, Some(3));
assert!(
handle
.read
.list(&Location::ssh(HostAlias::new("edge"), "/srv"))
.await
.unwrap()
.is_empty()
);
}
#[tokio::test]
async fn typed_handles_retain_their_locations_and_part_endpoints_must_match() {
let source = Location::local("/source/data");
let mut stream = ReadStream::new(source.clone(), &b"abcdef"[..]);
let mut buffer = [0; 2];
let read = stream.read(&mut buffer).await.unwrap();
assert_eq!(read, 2);
assert_eq!(&buffer, b"ab");
assert_eq!(stream.location(), &source);
let result = PartWriter::new(
Location::local("/destination/.rcom-part-test"),
Location::ssh(HostAlias::new("edge"), "/destination/data"),
tokio::io::sink(),
);
assert!(
matches!(result, Err(FsError::Other(message)) if message.contains("different endpoints"))
);
}