mod common;
use common::*;
#[tokio::test]
async fn whole_file_push_over_server_child() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("hello.txt"), b"hello world")
.await
.unwrap();
tokio::fs::create_dir(src.path().join("sub")).await.unwrap();
tokio::fs::write(src.path().join("sub/nested.txt"), b"nested content")
.await
.unwrap();
let stats = push_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(stats.files_sent, 3);
assert_eq!(
std::fs::read(dst.path().join("hello.txt")).unwrap(),
b"hello world"
);
assert_eq!(
std::fs::read(dst.path().join("sub/nested.txt")).unwrap(),
b"nested content"
);
assert!(dst.path().join("sub").is_dir());
}
#[tokio::test]
async fn pull_over_server_child() {
let serve = tempfile::tempdir().unwrap();
let restore = tempfile::tempdir().unwrap();
tokio::fs::write(serve.path().join("a.txt"), b"aaa")
.await
.unwrap();
tokio::fs::write(serve.path().join("b.txt"), b"bbb")
.await
.unwrap();
let stats = pull_tree(serve.path(), restore.path(), &default_options()).await;
assert_eq!(stats.files_received, 2);
assert_eq!(std::fs::read(restore.path().join("a.txt")).unwrap(), b"aaa");
assert_eq!(std::fs::read(restore.path().join("b.txt")).unwrap(), b"bbb");
}
#[tokio::test]
async fn delta_update_over_server_child() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
let data: Vec<u8> = pseudo_random(2 * 1024 * 1024);
tokio::fs::write(src.path().join("big.bin"), &data)
.await
.unwrap();
push_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(std::fs::read(dst.path().join("big.bin")).unwrap(), data);
let mut v2 = data.clone();
v2[1_000_000] ^= 0xFF;
tokio::fs::write(src.path().join("big.bin"), &v2)
.await
.unwrap();
set_future_mtime(&src.path().join("big.bin"));
let stats = push_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(stats.files_sent, 1);
assert_eq!(std::fs::read(dst.path().join("big.bin")).unwrap(), v2);
}
#[tokio::test]
async fn large_file_push_and_delta_update() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
let data: Vec<u8> = pseudo_random(20 * 1024 * 1024);
tokio::fs::write(src.path().join("large.bin"), &data)
.await
.unwrap();
push_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(std::fs::read(dst.path().join("large.bin")).unwrap(), data);
let mut v2 = Vec::with_capacity(data.len() + 1024);
v2.extend_from_slice(&data[..10 * 1024 * 1024]);
v2.extend(std::iter::repeat_n(0xAB, 1024));
v2.extend_from_slice(&data[10 * 1024 * 1024..]);
tokio::fs::write(src.path().join("large.bin"), &v2)
.await
.unwrap();
push_tree(src.path(), dst.path(), &default_options()).await;
let received = std::fs::read(dst.path().join("large.bin")).unwrap();
assert_eq!(received.len(), v2.len());
assert_eq!(received, v2);
}
#[tokio::test]
async fn multi_delta_window_over_server_child() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
let mut files = Vec::new();
for i in 0..5 {
let data: Vec<u8> = pseudo_random(12 * 1024 * 1024 + i);
tokio::fs::write(src.path().join(format!("f{i}.bin")), &data)
.await
.unwrap();
files.push(data);
}
push_tree(src.path(), dst.path(), &default_options()).await;
let mut options = default_options();
options.jobs = Some(2);
let mut edited = Vec::new();
for (i, data) in files.iter().enumerate() {
let mut v2 = data.clone();
v2.push(u8::try_from(i).unwrap());
tokio::fs::write(src.path().join(format!("f{i}.bin")), &v2)
.await
.unwrap();
edited.push(v2);
}
let stats = push_tree(src.path(), dst.path(), &options).await;
assert_eq!(stats.files_sent, 5, "all five edited files must transfer");
for (i, v2) in edited.iter().enumerate() {
let received = std::fs::read(dst.path().join(format!("f{i}.bin"))).unwrap();
assert_eq!(received.len(), v2.len());
assert_eq!(received, *v2);
}
}
#[tokio::test]
async fn checksum_push_skips_unchanged_files() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("a.txt"), b"content a")
.await
.unwrap();
tokio::fs::write(src.path().join("b.txt"), b"content b")
.await
.unwrap();
let mut options = default_options();
options.checksum = true;
let first = push_tree_with_server_args(src.path(), dst.path(), &options, &["--checksum"]).await;
assert_eq!(first.files_sent, 2);
let second = push_tree_with_server_args(src.path(), dst.path(), &options, &["--checksum"]).await;
assert_eq!(second.files_sent, 0, "checksum mode must skip identical files");
}
#[tokio::test]
async fn large_update_over_empty_basis_streams_chunks() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(dst.path().join("big.bin"), b"").await.unwrap();
let data: Vec<u8> = pseudo_random(12 * 1024 * 1024);
tokio::fs::write(src.path().join("big.bin"), &data)
.await
.unwrap();
set_future_mtime(&src.path().join("big.bin"));
let stats = push_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(stats.files_sent, 1);
assert_eq!(std::fs::read(dst.path().join("big.bin")).unwrap(), data);
let mut v2 = data.clone();
v2.insert(6 * 1024 * 1024, 0xEE);
tokio::fs::write(src.path().join("big.bin"), &v2)
.await
.unwrap();
set_future_mtime(&src.path().join("big.bin"));
push_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(std::fs::read(dst.path().join("big.bin")).unwrap(), v2);
}
#[tokio::test]
async fn empty_dir_and_file_dir_replacement() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::create_dir(src.path().join("empty"))
.await
.unwrap();
push_tree(src.path(), dst.path(), &default_options()).await;
assert!(dst.path().join("empty").is_dir());
tokio::fs::write(src.path().join("swap"), b"i am a file")
.await
.unwrap();
push_tree(src.path(), dst.path(), &default_options()).await;
assert!(dst.path().join("swap").is_file());
tokio::fs::remove_file(src.path().join("swap"))
.await
.unwrap();
tokio::fs::create_dir(src.path().join("swap"))
.await
.unwrap();
tokio::fs::write(src.path().join("swap/inner.txt"), b"now a dir")
.await
.unwrap();
push_tree(src.path(), dst.path(), &default_options()).await;
assert!(dst.path().join("swap").is_dir());
assert_eq!(
std::fs::read(dst.path().join("swap/inner.txt")).unwrap(),
b"now a dir"
);
}
#[tokio::test]
async fn delete_flag_removes_stale_files() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("keep.txt"), b"keep")
.await
.unwrap();
push_tree(src.path(), dst.path(), &default_options()).await;
tokio::fs::write(dst.path().join("stale.txt"), b"stale")
.await
.unwrap();
let mut options = default_options();
options.delete = true;
push_tree_with_server_args(src.path(), dst.path(), &options, &["--delete"]).await;
assert!(dst.path().join("keep.txt").exists());
assert!(!dst.path().join("stale.txt").exists());
}
#[cfg(unix)]
#[tokio::test]
async fn unreadable_source_skipped_not_fatal() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("good.txt"), b"fine")
.await
.unwrap();
tokio::fs::write(src.path().join("secret.bin"), vec![0u8; 64])
.await
.unwrap();
let _ = std::fs::set_permissions(
src.path().join("secret.bin"),
std::os::unix::fs::PermissionsExt::from_mode(0o000),
);
let stats = push_tree(src.path(), dst.path(), &default_options()).await;
assert!(dst.path().join("good.txt").exists());
assert!(stats.files_sent >= 1);
}
#[tokio::test]
async fn long_path_skipped_not_fatal() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
let long_name = "y".repeat(244);
tokio::fs::write(src.path().join(&long_name), vec![0xCD; 64])
.await
.unwrap();
tokio::fs::write(src.path().join("ok.txt"), b"fine")
.await
.unwrap();
let stats = push_tree(src.path(), dst.path(), &default_options()).await;
assert!(dst.path().join("ok.txt").exists());
assert_eq!(stats.skipped.len(), 1);
}
#[tokio::test]
async fn remote_path_scopes_the_transfer() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
let restore = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("a.txt"), b"scoped")
.await
.unwrap();
let mut options = default_options();
options.remote_path = "backup".to_string();
push_tree(src.path(), dst.path(), &options).await;
assert_eq!(
std::fs::read(dst.path().join("backup/a.txt")).unwrap(),
b"scoped"
);
assert!(!dst.path().join("a.txt").exists());
pull_tree(dst.path(), restore.path(), &options).await;
assert_eq!(
std::fs::read(restore.path().join("backup/a.txt")).unwrap(),
b"scoped"
);
assert!(!restore.path().join("a.txt").exists());
}
#[tokio::test]
async fn compressed_transfer_roundtrip() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
let data = vec![0x42u8; 512 * 1024];
tokio::fs::write(src.path().join("zeros.bin"), &data)
.await
.unwrap();
let mut options = default_options();
options.compress = true;
push_tree(src.path(), dst.path(), &options).await;
assert_eq!(std::fs::read(dst.path().join("zeros.bin")).unwrap(), data);
}
#[cfg(unix)]
#[tokio::test]
async fn ignore_existing_preserves_dest_file_over_link() {
use std::os::unix::fs::symlink;
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("target.txt"), b"data")
.await
.unwrap();
symlink("target.txt", src.path().join("l")).unwrap();
tokio::fs::write(dst.path().join("l"), b"precious user file")
.await
.unwrap();
let mut options = default_options();
options.ignore_existing = true;
push_tree(src.path(), dst.path(), &options).await;
let meta = std::fs::symlink_metadata(dst.path().join("l")).unwrap();
assert!(
meta.file_type().is_file(),
"the dest file must survive the push"
);
assert_eq!(
std::fs::read(dst.path().join("l")).unwrap(),
b"precious user file"
);
}
#[tokio::test]
async fn push_multi_over_server_child() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("a.txt"), b"aaa")
.await
.unwrap();
tokio::fs::write(src.path().join("b.txt"), b"bbbb")
.await
.unwrap();
tokio::fs::create_dir(src.path().join("empty")).await.unwrap();
tokio::fs::create_dir(src.path().join("sub")).await.unwrap();
tokio::fs::write(src.path().join("sub/c.txt"), b"c")
.await
.unwrap();
let roots: Vec<std::path::PathBuf> = vec![
src.path().join("a.txt"),
src.path().join("b.txt"),
src.path().join("empty"),
src.path().join("sub"),
];
let (mut child, send, recv) = spawn_server(dst.path());
let mut executor = Executor::new(send, recv);
let stats = executor
.push_multi(src.path(), &roots, &ExecutorOptions::default())
.await
.expect("push_multi failed");
drop(executor);
let exit_status = child.wait().await.expect("wait server");
assert!(exit_status.success(), "server exited with {exit_status}");
assert_eq!(stats.files_sent, 5);
assert_eq!(std::fs::read(dst.path().join("a.txt")).unwrap(), b"aaa");
assert_eq!(std::fs::read(dst.path().join("b.txt")).unwrap(), b"bbbb");
assert!(dst.path().join("empty").is_dir());
assert_eq!(std::fs::read(dst.path().join("sub/c.txt")).unwrap(), b"c");
}
#[tokio::test]
async fn push_multi_delete_removes_entries_outside_matches() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(dst.path().join("stale.txt"), b"s")
.await
.unwrap();
tokio::fs::create_dir(dst.path().join("old")).await.unwrap();
tokio::fs::write(dst.path().join("old/x.txt"), b"x")
.await
.unwrap();
tokio::fs::write(src.path().join("a.txt"), b"aaa")
.await
.unwrap();
let roots = vec![src.path().join("a.txt")];
let (mut child, send, recv) = spawn_server_with_args(dst.path(), &["--delete"]);
let mut executor = Executor::new(send, recv);
let options = ExecutorOptions {
delete: true,
..ExecutorOptions::default()
};
executor
.push_multi(src.path(), &roots, &options)
.await
.expect("push_multi failed");
drop(executor);
let exit_status = child.wait().await.expect("wait server");
assert!(exit_status.success(), "server exited with {exit_status}");
assert_eq!(std::fs::read(dst.path().join("a.txt")).unwrap(), b"aaa");
assert!(!dst.path().join("stale.txt").exists());
assert!(!dst.path().join("old").exists());
}
#[tokio::test]
async fn absolute_remote_path_used_as_is() {
let src = tempfile::tempdir().unwrap();
let target = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("a.txt"), b"abs")
.await
.unwrap();
let mut options = default_options();
options.remote_path = target.path().to_string_lossy().into_owned();
let (mut child, send, recv) = spawn_server(src.path());
let mut executor = Executor::new(send, recv);
executor
.push(src.path(), &options)
.await
.expect("push failed");
drop(executor);
let exit_status = child.wait().await.expect("wait server");
assert!(exit_status.success(), "server exited with {exit_status}");
assert_eq!(std::fs::read(target.path().join("a.txt")).unwrap(), b"abs");
}
#[tokio::test]
async fn push_skips_and_updates_against_decoys() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("same.txt"), b"same content")
.await
.unwrap();
tokio::fs::write(src.path().join("changed.txt"), b"changed source")
.await
.unwrap();
let same_mtime = tokio::fs::metadata(src.path().join("same.txt"))
.await
.unwrap()
.modified()
.unwrap();
tokio::fs::write(dst.path().join("same.txt"), b"same content")
.await
.unwrap();
std::fs::File::options()
.write(true)
.open(dst.path().join("same.txt"))
.unwrap()
.set_times(std::fs::FileTimes::new().set_modified(same_mtime))
.unwrap();
tokio::fs::write(dst.path().join("changed.txt"), b"old content")
.await
.unwrap();
tokio::fs::write(dst.path().join("decoy.txt"), b"decoy")
.await
.unwrap();
tokio::fs::create_dir(dst.path().join("decoy_dir")).await.unwrap();
tokio::fs::write(dst.path().join("decoy_dir/deep.txt"), b"deep")
.await
.unwrap();
let stats = push_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(stats.files_sent, 1);
assert_eq!(
std::fs::read(dst.path().join("same.txt")).unwrap(),
b"same content"
);
assert_eq!(
std::fs::read(dst.path().join("changed.txt")).unwrap(),
b"changed source"
);
assert_eq!(std::fs::read(dst.path().join("decoy.txt")).unwrap(), b"decoy");
assert_eq!(
std::fs::read(dst.path().join("decoy_dir/deep.txt")).unwrap(),
b"deep"
);
}
#[tokio::test]
async fn pull_ignores_local_decoys_without_delete() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("a.txt"), b"from server")
.await
.unwrap();
tokio::fs::create_dir(src.path().join("sub")).await.unwrap();
tokio::fs::write(src.path().join("sub/b.txt"), b"nested")
.await
.unwrap();
tokio::fs::write(dst.path().join("decoy.txt"), b"local decoy")
.await
.unwrap();
tokio::fs::create_dir(dst.path().join("local_dir")).await.unwrap();
tokio::fs::write(dst.path().join("local_dir/x.txt"), b"x")
.await
.unwrap();
let stats = pull_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(stats.files_received, 3);
assert_eq!(
std::fs::read(dst.path().join("a.txt")).unwrap(),
b"from server"
);
assert_eq!(
std::fs::read(dst.path().join("sub/b.txt")).unwrap(),
b"nested"
);
assert_eq!(
std::fs::read(dst.path().join("decoy.txt")).unwrap(),
b"local decoy"
);
assert_eq!(
std::fs::read(dst.path().join("local_dir/x.txt")).unwrap(),
b"x"
);
}
#[tokio::test]
async fn remote_list_only_returns_the_tree() {
let serve = tempfile::tempdir().unwrap();
tokio::fs::write(serve.path().join("a.txt"), b"aaa")
.await
.unwrap();
tokio::fs::create_dir_all(serve.path().join("sub"))
.await
.unwrap();
tokio::fs::write(serve.path().join("sub/b.txt"), b"bbb")
.await
.unwrap();
let stats = list_tree(serve.path(), "", &default_options()).await;
let paths: Vec<&str> = stats.changes.iter().map(|e| e.path.as_str()).collect();
assert!(paths.contains(&"a.txt"), "listing must include a.txt: {paths:?}");
assert!(
paths.contains(&"sub/b.txt"),
"listing must include sub/b.txt: {paths:?}"
);
let a = stats.changes.iter().find(|e| e.path == "a.txt").unwrap();
assert_eq!(a.kind, 'f');
assert_eq!(a.size, 3);
let b = stats.changes.iter().find(|e| e.path == "sub/b.txt").unwrap();
assert_eq!(b.kind, 'f');
assert_eq!(b.size, 3);
}
#[tokio::test]
async fn remote_glob_pull_expands_server_side() {
let serve = tempfile::tempdir().unwrap();
tokio::fs::create_dir_all(serve.path().join("sub"))
.await
.unwrap();
tokio::fs::write(serve.path().join("sub/a.rs"), b"a")
.await
.unwrap();
tokio::fs::write(serve.path().join("sub/b.rs"), b"bb")
.await
.unwrap();
tokio::fs::write(serve.path().join("sub/c.txt"), b"ccc")
.await
.unwrap();
let restore = tempfile::tempdir().unwrap();
let mut options = default_options();
options.remote_path = "sub/*.rs".to_string();
let stats = pull_tree(serve.path(), restore.path(), &options).await;
assert_eq!(stats.files_received, 2);
assert_eq!(std::fs::read(restore.path().join("a.rs")).unwrap(), b"a");
assert_eq!(std::fs::read(restore.path().join("b.rs")).unwrap(), b"bb");
assert!(!restore.path().join("c.txt").exists());
}
#[tokio::test]
async fn remote_files_from_pull_merges_absolute_paths() {
let serve = tempfile::tempdir().unwrap();
let d1 = serve.path().join("d1");
let d2 = serve.path().join("d2");
tokio::fs::create_dir_all(&d1).await.unwrap();
tokio::fs::create_dir_all(&d2).await.unwrap();
tokio::fs::write(d1.join("a.txt"), b"a").await.unwrap();
tokio::fs::write(d2.join("b.txt"), b"bb").await.unwrap();
let restore = tempfile::tempdir().unwrap();
let mut options = default_options();
options.remote_paths = vec![
d1.join("a.txt").to_string_lossy().into_owned(),
d2.join("b.txt").to_string_lossy().into_owned(),
];
let stats = pull_tree(serve.path(), restore.path(), &options).await;
assert_eq!(stats.files_received, 2);
let abs1 = d1.join("a.txt");
let abs2 = d2.join("b.txt");
let root_relative = |p: &std::path::Path| -> std::path::PathBuf {
p.components()
.filter(|c| {
!matches!(
c,
std::path::Component::RootDir | std::path::Component::Prefix(_)
)
})
.collect()
};
let rel1 = root_relative(&abs1);
let rel2 = root_relative(&abs2);
assert_eq!(
std::fs::read(restore.path().join(&rel1)).unwrap(),
b"a",
"{} must land at {}",
d1.join("a.txt").display(),
restore.path().join(rel1).display()
);
assert_eq!(
std::fs::read(restore.path().join(rel2)).unwrap(),
b"bb"
);
}
#[tokio::test]
async fn multi_batch_push_verify_acks_every_batch() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
for i in 0..200 {
let data = pseudo_random(1_900_000 + i);
tokio::fs::write(src.path().join(format!("file_{:02}{:02}.bin", i / 26, i % 26)), &data)
.await
.unwrap();
}
let mut options = default_options();
options.verify = true;
let stats = push_tree(src.path(), dst.path(), &options).await;
assert_eq!(stats.files_sent, 200);
assert!(
stats.skipped.is_empty(),
"every batch's verification hashes must reach the sender: {:?}",
stats.skipped
);
let mut landed = 0;
let mut stack = vec![dst.path().to_path_buf()];
while let Some(dir) = stack.pop() {
for entry in std::fs::read_dir(&dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if entry.file_type().unwrap().is_dir() {
stack.push(path);
} else if path.extension().is_some_and(|e| e == "bin") {
assert!(std::fs::metadata(&path).unwrap().len() >= 1_900_000);
landed += 1;
}
}
}
assert_eq!(landed, 200);
}
#[tokio::test]
async fn update_skips_newer_destination_on_push_and_pull() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("f.txt"), b"v1-src")
.await
.unwrap();
tokio::fs::write(dst.path().join("f.txt"), b"v2-dst")
.await
.unwrap();
set_future_mtime(&dst.path().join("f.txt"));
let mut options = default_options();
options.update_only = true;
let stats = push_tree(src.path(), dst.path(), &options).await;
assert_eq!(stats.files_sent, 0, "newer destination must be skipped");
assert_eq!(std::fs::read(dst.path().join("f.txt")).unwrap(), b"v2-dst");
assert!(
std::fs::metadata(dst.path().join("f.txt"))
.unwrap()
.modified()
.unwrap()
> std::fs::metadata(src.path().join("f.txt"))
.unwrap()
.modified()
.unwrap(),
"-u must not rewind the newer destination's mtime"
);
let serve = tempfile::tempdir().unwrap();
let restore = tempfile::tempdir().unwrap();
tokio::fs::write(serve.path().join("f.txt"), b"v1-src")
.await
.unwrap();
tokio::fs::write(restore.path().join("f.txt"), b"v2-restore")
.await
.unwrap();
set_future_mtime(&restore.path().join("f.txt"));
let stats = pull_tree(serve.path(), restore.path(), &options).await;
assert_eq!(stats.files_received, 0, "newer restore file must be skipped");
assert_eq!(
std::fs::read(restore.path().join("f.txt")).unwrap(),
b"v2-restore"
);
assert!(
std::fs::metadata(restore.path().join("f.txt"))
.unwrap()
.modified()
.unwrap()
> std::fs::metadata(serve.path().join("f.txt"))
.unwrap()
.modified()
.unwrap(),
"-u on pull must not rewind the newer restore mtime"
);
}
#[tokio::test]
async fn existing_only_updates_present_files_on_push_and_pull() {
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("a.txt"), b"new-a")
.await
.unwrap();
tokio::fs::write(src.path().join("b.txt"), b"new-b")
.await
.unwrap();
tokio::fs::write(dst.path().join("a.txt"), b"old")
.await
.unwrap();
let mut options = default_options();
options.existing = true;
let stats = push_tree(src.path(), dst.path(), &options).await;
assert_eq!(stats.files_sent, 1, "only the present file transfers");
assert_eq!(std::fs::read(dst.path().join("a.txt")).unwrap(), b"new-a");
assert!(
!dst.path().join("b.txt").exists(),
"--existing must not create b.txt"
);
let serve = tempfile::tempdir().unwrap();
let restore = tempfile::tempdir().unwrap();
tokio::fs::write(serve.path().join("a.txt"), b"new-a")
.await
.unwrap();
tokio::fs::write(serve.path().join("b.txt"), b"new-b")
.await
.unwrap();
tokio::fs::write(restore.path().join("a.txt"), b"old")
.await
.unwrap();
let stats = pull_tree(serve.path(), restore.path(), &options).await;
assert_eq!(stats.files_received, 1, "only the present file transfers");
assert_eq!(
std::fs::read(restore.path().join("a.txt")).unwrap(),
b"new-a"
);
assert!(
!restore.path().join("b.txt").exists(),
"--existing must not create b.txt"
);
}
#[tokio::test]
async fn ignore_times_forces_equal_time_size_transfer_on_push_and_pull() {
let data1 = pseudo_random(4096);
let mut data2 = data1.clone();
data2[100] ^= 0xFF;
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("f.bin"), &data2).await.unwrap();
tokio::fs::write(dst.path().join("f.bin"), &data1).await.unwrap();
set_file_mtime_ns(&src.path().join("f.bin"), 1_700_000_000, 0);
set_file_mtime_ns(&dst.path().join("f.bin"), 1_700_000_000, 0);
let mut options = default_options();
options.ignore_times = true;
push_tree(src.path(), dst.path(), &options).await;
assert_eq!(
std::fs::read(dst.path().join("f.bin")).unwrap(),
data2,
"--ignore-times must re-send the equal-mtime/size file"
);
let serve = tempfile::tempdir().unwrap();
let restore = tempfile::tempdir().unwrap();
tokio::fs::write(serve.path().join("f.bin"), &data2).await.unwrap();
tokio::fs::write(restore.path().join("f.bin"), &data1).await.unwrap();
set_file_mtime_ns(&serve.path().join("f.bin"), 1_700_000_000, 0);
set_file_mtime_ns(&restore.path().join("f.bin"), 1_700_000_000, 0);
pull_tree(serve.path(), restore.path(), &options).await;
assert_eq!(std::fs::read(restore.path().join("f.bin")).unwrap(), data2);
}
#[tokio::test]
async fn checksum_catches_equal_time_size_tamper_on_push_and_pull() {
let data1 = pseudo_random(4096);
let mut data2 = data1.clone();
data2[200] ^= 0xAA;
let src = tempfile::tempdir().unwrap();
let dst = tempfile::tempdir().unwrap();
tokio::fs::write(src.path().join("f.bin"), &data2).await.unwrap();
tokio::fs::write(dst.path().join("f.bin"), &data1).await.unwrap();
set_file_mtime_ns(&src.path().join("f.bin"), 1_700_000_000, 0);
set_file_mtime_ns(&dst.path().join("f.bin"), 1_700_000_000, 0);
push_tree(src.path(), dst.path(), &default_options()).await;
assert_eq!(
std::fs::read(dst.path().join("f.bin")).unwrap(),
data1,
"size+mtime quick check must skip the tampered file"
);
let mut options = default_options();
options.checksum = true;
push_tree(src.path(), dst.path(), &options).await;
assert_eq!(std::fs::read(dst.path().join("f.bin")).unwrap(), data2);
let serve = tempfile::tempdir().unwrap();
let restore = tempfile::tempdir().unwrap();
tokio::fs::write(serve.path().join("f.bin"), &data2).await.unwrap();
tokio::fs::write(restore.path().join("f.bin"), &data1).await.unwrap();
set_file_mtime_ns(&serve.path().join("f.bin"), 1_700_000_000, 0);
set_file_mtime_ns(&restore.path().join("f.bin"), 1_700_000_000, 0);
pull_tree(serve.path(), restore.path(), &default_options()).await;
assert_eq!(
std::fs::read(restore.path().join("f.bin")).unwrap(),
data1,
"pull quick check must skip too"
);
pull_tree(serve.path(), restore.path(), &options).await;
assert_eq!(std::fs::read(restore.path().join("f.bin")).unwrap(), data2);
}
#[tokio::test]
async fn compressed_pull_roundtrips() {
let serve = tempfile::tempdir().unwrap();
let restore = tempfile::tempdir().unwrap();
let data = vec![0x42u8; 512 * 1024];
tokio::fs::write(serve.path().join("zeros.bin"), &data)
.await
.unwrap();
let mut options = default_options();
options.compress = true;
pull_tree(serve.path(), restore.path(), &options).await;
assert_eq!(
std::fs::read(restore.path().join("zeros.bin")).unwrap(),
data
);
}
#[tokio::test]
async fn bwlimit_pull_throttles_the_wire() {
let serve = tempfile::tempdir().unwrap();
let restore = tempfile::tempdir().unwrap();
let data = pseudo_random(4 * 1024 * 1024);
tokio::fs::write(serve.path().join("big.bin"), &data)
.await
.unwrap();
let mut options = default_options();
options.bwlimit = Some(1024 * 1024);
let start = std::time::Instant::now();
pull_tree(serve.path(), restore.path(), &options).await;
let elapsed = start.elapsed();
assert_eq!(
std::fs::read(restore.path().join("big.bin")).unwrap(),
data
);
assert!(
elapsed >= std::time::Duration::from_secs(3),
"pull finished in {elapsed:?} — the bwlimit on the pull sender did not engage"
);
}