178 lines
6.1 KiB
Rust
178 lines
6.1 KiB
Rust
use rcom::domain::{EntryKind, HostAlias, Location};
|
|
use rcom::fs::{FileOperations, LocalFileOperations, StagingOperations};
|
|
use tempfile::TempDir;
|
|
|
|
#[tokio::test]
|
|
async fn create_file_creates_an_empty_regular_file() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let destination = Location::local(fixture.path().join("new.txt"));
|
|
|
|
LocalFileOperations::new()
|
|
.create_file(&destination)
|
|
.await
|
|
.unwrap();
|
|
|
|
let metadata = std::fs::symlink_metadata(destination.path()).unwrap();
|
|
assert!(metadata.file_type().is_file());
|
|
assert_eq!(metadata.len(), 0);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_dir_creates_an_empty_directory() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let destination = Location::local(fixture.path().join("new-directory"));
|
|
|
|
LocalFileOperations::new()
|
|
.create_dir(&destination)
|
|
.await
|
|
.unwrap();
|
|
|
|
let operations = LocalFileOperations::new();
|
|
let entry = operations.metadata(&destination, false).await.unwrap();
|
|
assert_eq!(entry.kind, EntryKind::Directory);
|
|
assert_eq!(std::fs::read_dir(destination.path()).unwrap().count(), 0);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_file_rejects_every_existing_destination_without_overwrite() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let operations = LocalFileOperations::new();
|
|
let file = fixture.path().join("file");
|
|
let directory = fixture.path().join("directory");
|
|
let target = fixture.path().join("target");
|
|
let symlink = fixture.path().join("symlink");
|
|
std::fs::write(&file, b"keep file").unwrap();
|
|
std::fs::create_dir(&directory).unwrap();
|
|
std::fs::write(directory.join("child"), b"keep child").unwrap();
|
|
std::fs::write(&target, b"keep target").unwrap();
|
|
std::os::unix::fs::symlink(&target, &symlink).unwrap();
|
|
|
|
for path in [&file, &directory, &symlink] {
|
|
assert!(
|
|
operations
|
|
.create_file(&Location::local(path))
|
|
.await
|
|
.is_err()
|
|
);
|
|
}
|
|
|
|
assert_eq!(std::fs::read(file).unwrap(), b"keep file");
|
|
assert_eq!(
|
|
std::fs::read(directory.join("child")).unwrap(),
|
|
b"keep child"
|
|
);
|
|
assert_eq!(std::fs::read(target).unwrap(), b"keep target");
|
|
assert!(
|
|
std::fs::symlink_metadata(symlink)
|
|
.unwrap()
|
|
.file_type()
|
|
.is_symlink()
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_dir_rejects_every_existing_destination_without_overwrite() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let operations = LocalFileOperations::new();
|
|
let file = fixture.path().join("file");
|
|
let directory = fixture.path().join("directory");
|
|
let target = fixture.path().join("target");
|
|
let symlink = fixture.path().join("symlink");
|
|
std::fs::write(&file, b"keep file").unwrap();
|
|
std::fs::create_dir(&directory).unwrap();
|
|
std::fs::write(directory.join("child"), b"keep child").unwrap();
|
|
std::fs::write(&target, b"keep target").unwrap();
|
|
std::os::unix::fs::symlink(&target, &symlink).unwrap();
|
|
|
|
for path in [&file, &directory, &symlink] {
|
|
assert!(operations.create_dir(&Location::local(path)).await.is_err());
|
|
}
|
|
|
|
assert_eq!(std::fs::read(file).unwrap(), b"keep file");
|
|
assert_eq!(
|
|
std::fs::read(directory.join("child")).unwrap(),
|
|
b"keep child"
|
|
);
|
|
assert_eq!(std::fs::read(target).unwrap(), b"keep target");
|
|
assert!(
|
|
std::fs::symlink_metadata(symlink)
|
|
.unwrap()
|
|
.file_type()
|
|
.is_symlink()
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_rejects_remote_locations_without_touching_the_local_path() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let operations = LocalFileOperations::new();
|
|
let file = Location::ssh(HostAlias::new("edge"), fixture.path().join("remote-file"));
|
|
let directory = Location::ssh(
|
|
HostAlias::new("edge"),
|
|
fixture.path().join("remote-directory"),
|
|
);
|
|
|
|
assert!(operations.create_file(&file).await.is_err());
|
|
assert!(operations.create_dir(&directory).await.is_err());
|
|
assert!(!file.path().exists());
|
|
assert!(!directory.path().exists());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_rejects_parent_traversal_before_mutation() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let parent = fixture.path().join("parent");
|
|
std::fs::create_dir(&parent).unwrap();
|
|
let operations = LocalFileOperations::new();
|
|
let file = Location::local(parent.join("../escaped-file"));
|
|
let directory = Location::local(parent.join("../escaped-directory"));
|
|
|
|
assert!(operations.create_file(&file).await.is_err());
|
|
assert!(operations.create_dir(&directory).await.is_err());
|
|
assert!(!fixture.path().join("escaped-file").exists());
|
|
assert!(!fixture.path().join("escaped-directory").exists());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_rejects_a_direct_symlink_parent_without_redirecting_writes() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let actual_parent = fixture.path().join("actual");
|
|
let linked_parent = fixture.path().join("linked");
|
|
std::fs::create_dir(&actual_parent).unwrap();
|
|
std::os::unix::fs::symlink(&actual_parent, &linked_parent).unwrap();
|
|
let operations = LocalFileOperations::new();
|
|
|
|
assert!(
|
|
operations
|
|
.create_file(&Location::local(linked_parent.join("file")))
|
|
.await
|
|
.is_err()
|
|
);
|
|
assert!(
|
|
operations
|
|
.create_dir(&Location::local(linked_parent.join("directory")))
|
|
.await
|
|
.is_err()
|
|
);
|
|
assert!(!actual_parent.join("file").exists());
|
|
assert!(!actual_parent.join("directory").exists());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_rejects_a_noncanonical_parent_ancestry() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let actual_parent = fixture.path().join("actual");
|
|
let linked_parent = fixture.path().join("linked");
|
|
std::fs::create_dir(&actual_parent).unwrap();
|
|
std::fs::create_dir(actual_parent.join("nested")).unwrap();
|
|
std::os::unix::fs::symlink(&actual_parent, &linked_parent).unwrap();
|
|
let destination = Location::local(linked_parent.join("nested/file"));
|
|
|
|
assert!(
|
|
LocalFileOperations::new()
|
|
.create_file(&destination)
|
|
.await
|
|
.is_err()
|
|
);
|
|
assert!(!actual_parent.join("nested/file").exists());
|
|
}
|