From d797b3f27ec9dbd488f16d00e690a91335ace014 Mon Sep 17 00:00:00 2001 From: Chip Senkbeil Date: Fri, 24 Sep 2021 18:04:13 -0500 Subject: [PATCH] Add tests for remaining sftp methods off of sftp channel -- 4 are failing --- wezterm-ssh/src/session/sftp.rs | 77 +++--- wezterm-ssh/tests/e2e/sftp.rs | 394 ++++++++++++++++++++++++++--- wezterm-ssh/tests/e2e/sftp/file.rs | 4 +- 3 files changed, 411 insertions(+), 64 deletions(-) diff --git a/wezterm-ssh/src/session/sftp.rs b/wezterm-ssh/src/session/sftp.rs index 566e60009..543ded481 100644 --- a/wezterm-ssh/src/session/sftp.rs +++ b/wezterm-ssh/src/session/sftp.rs @@ -19,7 +19,7 @@ impl Sftp { /// See [`Sftp::open_mode`] for more information. pub async fn open_mode( &self, - filename: PathBuf, + filename: impl Into, flags: OpenFlags, mode: i32, open_type: OpenType, @@ -27,7 +27,7 @@ impl Sftp { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::OpenMode(OpenMode { - filename, + filename: filename.into(), flags, mode, open_type, @@ -42,11 +42,11 @@ impl Sftp { /// Helper to open a file in the `Read` mode. /// /// See [`Sftp::open`] for more information. - pub async fn open(&self, filename: PathBuf) -> anyhow::Result { + pub async fn open(&self, filename: impl Into) -> anyhow::Result { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Open(Open { - filename, + filename: filename.into(), reply, }))) .await?; @@ -58,11 +58,11 @@ impl Sftp { /// Helper to create a file in write-only mode with truncation. /// /// See [`Sftp::create`] for more information. - pub async fn create(&self, filename: PathBuf) -> anyhow::Result { + pub async fn create(&self, filename: impl Into) -> anyhow::Result { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Create(Create { - filename, + filename: filename.into(), reply, }))) .await?; @@ -74,11 +74,11 @@ impl Sftp { /// Helper to open a directory for reading its contents. /// /// See [`Sftp::opendir`] for more information. - pub async fn opendir(&self, filename: PathBuf) -> anyhow::Result { + pub async fn opendir(&self, filename: impl Into) -> anyhow::Result { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Opendir(Opendir { - filename, + filename: filename.into(), reply, }))) .await?; @@ -90,11 +90,14 @@ impl Sftp { /// Convenience function to read the files in a directory. /// /// See [`Sftp::readdir`] for more information. - pub async fn readdir(&self, filename: PathBuf) -> anyhow::Result> { + pub async fn readdir( + &self, + filename: impl Into, + ) -> anyhow::Result> { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Readdir(Readdir { - filename, + filename: filename.into(), reply, }))) .await?; @@ -105,11 +108,11 @@ impl Sftp { /// Create a directory on the remote filesystem. /// /// See [`Sftp::rmdir`] for more information. - pub async fn mkdir(&self, filename: PathBuf, mode: i32) -> anyhow::Result<()> { + pub async fn mkdir(&self, filename: impl Into, mode: i32) -> anyhow::Result<()> { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Mkdir(Mkdir { - filename, + filename: filename.into(), mode, reply, }))) @@ -121,11 +124,11 @@ impl Sftp { /// Remove a directory from the remote filesystem. /// /// See [`Sftp::rmdir`] for more information. - pub async fn rmdir(&self, filename: PathBuf) -> anyhow::Result<()> { + pub async fn rmdir(&self, filename: impl Into) -> anyhow::Result<()> { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Rmdir(Rmdir { - filename, + filename: filename.into(), reply, }))) .await?; @@ -136,11 +139,11 @@ impl Sftp { /// Get the metadata for a file, performed by stat(2). /// /// See [`Sftp::stat`] for more information. - pub async fn stat(&self, filename: PathBuf) -> anyhow::Result { + pub async fn stat(&self, filename: impl Into) -> anyhow::Result { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Stat(Stat { - filename, + filename: filename.into(), reply, }))) .await?; @@ -151,11 +154,11 @@ impl Sftp { /// Get the metadata for a file, performed by lstat(2). /// /// See [`Sftp::lstat`] for more information. - pub async fn lstat(&self, filename: PathBuf) -> anyhow::Result { + pub async fn lstat(&self, filename: impl Into) -> anyhow::Result { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Lstat(Lstat { - filename, + filename: filename.into(), reply, }))) .await?; @@ -166,11 +169,15 @@ impl Sftp { /// Set the metadata for a file. /// /// See [`Sftp::setstat`] for more information. - pub async fn setstat(&self, filename: PathBuf, stat: FileStat) -> anyhow::Result<()> { + pub async fn setstat( + &self, + filename: impl Into, + stat: FileStat, + ) -> anyhow::Result<()> { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Setstat(Setstat { - filename, + filename: filename.into(), stat, reply, }))) @@ -182,12 +189,16 @@ impl Sftp { /// Create symlink at `target` pointing at `path`. /// /// See [`Sftp::symlink`] for more information. - pub async fn symlink(&self, path: PathBuf, target: PathBuf) -> anyhow::Result<()> { + pub async fn symlink( + &self, + path: impl Into, + target: impl Into, + ) -> anyhow::Result<()> { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Symlink(Symlink { - path, - target, + path: path.into(), + target: target.into(), reply, }))) .await?; @@ -198,11 +209,11 @@ impl Sftp { /// Read a symlink at `path`. /// /// See [`Sftp::readlink`] for more information. - pub async fn readlink(&self, path: PathBuf) -> anyhow::Result { + pub async fn readlink(&self, path: impl Into) -> anyhow::Result { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Readlink(Readlink { - path, + path: path.into(), reply, }))) .await?; @@ -213,11 +224,11 @@ impl Sftp { /// Resolve the real path for `path`. /// /// See [`Sftp::realpath`] for more information. - pub async fn realpath(&self, path: PathBuf) -> anyhow::Result { + pub async fn realpath(&self, path: impl Into) -> anyhow::Result { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Realpath(Realpath { - path, + path: path.into(), reply, }))) .await?; @@ -230,15 +241,15 @@ impl Sftp { /// See [`Sftp::rename`] for more information. pub async fn rename( &self, - src: PathBuf, - dst: PathBuf, + src: impl Into, + dst: impl Into, flags: Option, ) -> anyhow::Result<()> { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Rename(Rename { - src, - dst, + src: src.into(), + dst: dst.into(), flags, reply, }))) @@ -250,11 +261,11 @@ impl Sftp { /// Remove a file on the remote filesystem. /// /// See [`Sftp::unlink`] for more information. - pub async fn unlink(&self, file: PathBuf) -> anyhow::Result<()> { + pub async fn unlink(&self, file: impl Into) -> anyhow::Result<()> { let (reply, rx) = bounded(1); self.tx .send(SessionRequest::Sftp(SftpRequest::Unlink(Unlink { - file, + file: file.into(), reply, }))) .await?; diff --git a/wezterm-ssh/tests/e2e/sftp.rs b/wezterm-ssh/tests/e2e/sftp.rs index 0ad258b75..88e5d0c59 100644 --- a/wezterm-ssh/tests/e2e/sftp.rs +++ b/wezterm-ssh/tests/e2e/sftp.rs @@ -47,7 +47,7 @@ async fn readdir_should_return_list_of_directories_files_and_symlinks(#[future] let mut contents = session .sftp() - .readdir(temp.path().to_path_buf()) + .readdir(temp.path()) .await .expect("Failed to read directory") .into_iter() @@ -77,7 +77,7 @@ async fn mkdir_should_create_a_directory_on_the_remote_filesystem(#[future] sess session .sftp() - .mkdir(temp.child("dir").path().to_path_buf(), 0o644) + .mkdir(temp.child("dir").path(), 0o644) .await .expect("Failed to create directory"); @@ -95,11 +95,12 @@ async fn mkdir_should_return_error_if_unable_to_create_directory(#[future] sessi // Attempt to create a nested directory structure, which is not supported let result = session .sftp() - .mkdir(temp.child("dir").child("dir").path().to_path_buf(), 0o644) + .mkdir(temp.child("dir").child("dir").path(), 0o644) .await; assert!( result.is_err(), - "Unexpectedly succeeded in creating directory" + "Unexpectedly succeeded in creating directory: {:?}", + result ); // Verify the path is not a directory @@ -121,7 +122,7 @@ async fn rmdir_should_remove_a_remote_directory(#[future] session: Session) { dir.create_dir_all().unwrap(); session .sftp() - .rmdir(dir.path().to_path_buf()) + .rmdir(dir.path()) .await .expect("Failed to remove directory"); @@ -137,13 +138,11 @@ async fn rmdir_should_return_an_error_if_failed_to_remove_directory(#[future] se let temp = TempDir::new().unwrap(); // Attempt to remove a missing path - let result = session - .sftp() - .rmdir(temp.child("missing-dir").path().to_path_buf()) - .await; + let result = session.sftp().rmdir(temp.child("missing-dir").path()).await; assert!( result.is_err(), - "Unexpectedly succeeded in removing missing directory" + "Unexpectedly succeeded in removing missing directory: {:?}", + result ); // Attempt to remove a non-empty directory @@ -151,10 +150,11 @@ async fn rmdir_should_return_an_error_if_failed_to_remove_directory(#[future] se dir.create_dir_all().unwrap(); dir.child("file").touch().unwrap(); - let result = session.sftp().rmdir(dir.path().to_path_buf()).await; + let result = session.sftp().rmdir(dir.path()).await; assert!( result.is_err(), - "Unexpectedly succeeded in removing non-empty directory" + "Unexpectedly succeeded in removing non-empty directory: {:?}", + result ); // Verify the non-empty directory still exists @@ -163,8 +163,12 @@ async fn rmdir_should_return_an_error_if_failed_to_remove_directory(#[future] se // Attempt to remove a file (not a directory) let file = temp.child("file"); file.touch().unwrap(); - let result = session.sftp().rmdir(file.path().to_path_buf()).await; - assert!(result.is_err(), "Unexpectedly succeeded in removing file"); + let result = session.sftp().rmdir(file.path()).await; + assert!( + result.is_err(), + "Unexpectedly succeeded in removing file: {:?}", + result + ); // Verify the file still exists file.assert(predicate::path::is_file()); @@ -181,7 +185,7 @@ async fn stat_should_return_metadata_about_a_file(#[future] session: Session) { let stat = session .sftp() - .stat(file.path().to_path_buf()) + .stat(file.path()) .await .expect("Failed to stat file"); @@ -200,7 +204,7 @@ async fn stat_should_return_metadata_about_a_directory(#[future] session: Sessio let stat = session .sftp() - .stat(dir.path().to_path_buf()) + .stat(dir.path()) .await .expect("Failed to stat dir"); @@ -224,7 +228,7 @@ async fn stat_should_return_metadata_about_the_file_pointed_to_by_a_symlink( let stat = session .sftp() - .stat(link.path().to_path_buf()) + .stat(link.path()) .await .expect("Failed to stat symlink"); @@ -250,7 +254,7 @@ async fn stat_should_return_metadata_about_the_dir_pointed_to_by_a_symlink( let stat = session .sftp() - .stat(link.path().to_path_buf()) + .stat(link.path()) .await .expect("Failed to stat symlink"); @@ -267,11 +271,8 @@ async fn stat_should_fail_if_path_missing(#[future] session: Session) { let temp = TempDir::new().unwrap(); - let result = session - .sftp() - .stat(temp.child("missing").path().to_path_buf()) - .await; - assert!(result.is_err(), "Stat unexpectedly succeeded"); + let result = session.sftp().stat(temp.child("missing").path()).await; + assert!(result.is_err(), "Stat unexpectedly succeeded: {:?}", result); } #[rstest] @@ -285,7 +286,7 @@ async fn lstat_should_return_metadata_about_a_file(#[future] session: Session) { let lstat = session .sftp() - .lstat(file.path().to_path_buf()) + .lstat(file.path()) .await .expect("Failed to lstat file"); @@ -304,7 +305,7 @@ async fn lstat_should_return_metadata_about_a_directory(#[future] session: Sessi let lstat = session .sftp() - .lstat(dir.path().to_path_buf()) + .lstat(dir.path()) .await .expect("Failed to lstat dir"); @@ -326,7 +327,7 @@ async fn lstat_should_return_metadata_about_symlink_pointing_to_a_file(#[future] let lstat = session .sftp() - .lstat(link.path().to_path_buf()) + .lstat(link.path()) .await .expect("Failed to lstat symlink"); @@ -355,7 +356,7 @@ async fn lstat_should_return_metadata_about_symlink_pointing_to_a_directory( let lstat = session .sftp() - .lstat(link.path().to_path_buf()) + .lstat(link.path()) .await .expect("Failed to lstat symlink"); @@ -375,9 +376,344 @@ async fn lstat_should_fail_if_path_missing(#[future] session: Session) { let temp = TempDir::new().unwrap(); + let result = session.sftp().lstat(temp.child("missing").path()).await; + assert!( + result.is_err(), + "lstat unexpectedly succeeded: {:?}", + result + ); +} + +#[rstest] +#[smol_potat::test] +async fn symlink_should_create_symlink_pointing_to_file(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let file = temp.child("file"); + file.touch().unwrap(); + + let link = temp.child("link"); + + session + .sftp() + .symlink(file.path(), link.path()) + .await + .expect("Failed to create symlink"); + + link.assert(predicate::path::is_symlink()); +} + +#[rstest] +#[smol_potat::test] +async fn symlink_should_create_symlink_pointing_to_directory(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let dir = temp.child("dir"); + dir.create_dir_all().unwrap(); + + let link = temp.child("link"); + + session + .sftp() + .symlink(dir.path(), link.path()) + .await + .expect("Failed to create symlink"); + + link.assert(predicate::path::is_symlink()); +} + +#[rstest] +#[smol_potat::test] +async fn symlink_should_fail_if_path_missing(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let file = temp.child("file"); + + let link = temp.child("link"); + + let result = session.sftp().symlink(file.path(), link.path()).await; + assert!( + result.is_err(), + "Unexpectedly created symlink for missing path: {:?}", + result + ); +} + +#[rstest] +#[smol_potat::test] +async fn readlink_should_return_the_target_of_the_symlink(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + + // Test a symlink to a directory + let dir = temp.child("dir"); + dir.create_dir_all().unwrap(); + let link = temp.child("link"); + link.symlink_to_dir(dir.path()).unwrap(); + + let path = session + .sftp() + .readlink(link.path()) + .await + .expect("Failed to read symlink"); + assert_eq!(path, dir.path()); + + // Test a symlink to a file + let file = temp.child("file"); + file.touch().unwrap(); + let link = temp.child("link2"); + link.symlink_to_file(file.path()).unwrap(); + + let path = session + .sftp() + .readlink(link.path()) + .await + .expect("Failed to read symlink"); + assert_eq!(path, file.path()); +} + +#[rstest] +#[smol_potat::test] +async fn readlink_should_fail_if_path_is_not_a_symlink(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + + // Test missing path + let result = session.sftp().readlink(temp.child("missing").path()).await; + assert!( + result.is_err(), + "Unexpectedly read link for missing path: {:?}", + result + ); + + // Test a directory + let dir = temp.child("dir"); + dir.create_dir_all().unwrap(); + let result = session.sftp().readlink(dir.path()).await; + assert!( + result.is_err(), + "Unexpectedly read link for directory: {:?}", + result + ); + + // Test a file + let file = temp.child("file"); + file.touch().unwrap(); + let result = session.sftp().readlink(file.path()).await; + assert!( + result.is_err(), + "Unexpectedly read link for file: {:?}", + result + ); +} + +#[rstest] +#[smol_potat::test] +async fn realpath_should_resolve_absolute_path_for_relative_path(#[future] session: Session) { + let session: Session = session.await; + + // Create a relative component within a directory so we can guarantee what the path will be + let temp = TempDir::new().unwrap(); + let file = temp.child("world"); + file.touch().unwrap(); + + let rel = temp.child(".").child("hello").child("..").child("world"); + + let path = session + .sftp() + .realpath(rel.path()) + .await + .expect("Failed to get real path"); + assert_eq!(path, file.path()); +} + +#[rstest] +#[smol_potat::test] +async fn realpath_should_fail_if_path_missing(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let missing = temp.child("missing"); + + let result = session.sftp().realpath(missing.path()).await; + assert!( + result.is_err(), + "Realpath unexpectedly succeeded with missing path: {:?}", + result + ); +} + +#[rstest] +#[smol_potat::test] +async fn rename_should_support_singular_file(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let file = temp.child("file"); + file.write_str("some text").unwrap(); + + let dst = temp.child("dst"); + + session + .sftp() + .rename(file.path(), dst.path(), None) + .await + .expect("Failed to rename file"); + + // Verify that file was moved to destination + file.assert(predicate::path::missing()); + dst.assert("some text"); +} + +#[rstest] +#[smol_potat::test] +async fn rename_should_support_dirtectory(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let dir = temp.child("dir"); + dir.create_dir_all().unwrap(); + let dir_file = dir.child("file"); + dir_file.write_str("some text").unwrap(); + let dir_dir = dir.child("dir"); + dir_dir.create_dir_all().unwrap(); + + let dst = temp.child("dst"); + + session + .sftp() + .rename(dir.path(), dst.path(), None) + .await + .expect("Failed to rename directory"); + + // Verify that directory was moved to destination + dir.assert(predicate::path::missing()); + dir_file.assert(predicate::path::missing()); + dir_dir.assert(predicate::path::missing()); + + dst.assert(predicate::path::is_dir()); + dst.child("file").assert("some text"); + dst.child("dir").assert(predicate::path::is_dir()); +} + +#[rstest] +#[smol_potat::test] +async fn rename_should_fail_if_source_path_missing(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let missing = temp.child("missing"); + let dst = temp.child("dst"); + let result = session .sftp() - .lstat(temp.child("missing").path().to_path_buf()) + .rename(missing.path(), dst.path(), None) .await; - assert!(result.is_err(), "lstat unexpectedly succeeded"); + assert!( + result.is_err(), + "Rename unexpectedly succeeded with missing path: {:?}", + result + ); +} + +#[rstest] +#[smol_potat::test] +async fn unlink_should_remove_file(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let file = temp.child("file"); + file.touch().unwrap(); + + session + .sftp() + .unlink(file.path()) + .await + .expect("Failed to unlink file"); + + file.assert(predicate::path::missing()); +} + +#[rstest] +#[smol_potat::test] +async fn unlink_should_remove_symlink_to_file(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let file = temp.child("file"); + file.touch().unwrap(); + let link = temp.child("link"); + link.symlink_to_file(file.path()).unwrap(); + + session + .sftp() + .unlink(link.path()) + .await + .expect("Failed to unlink symlink"); + + // Verify link removed but file still exists + link.assert(predicate::path::missing()); + file.assert(predicate::path::is_file()); +} + +#[rstest] +#[smol_potat::test] +async fn unlink_should_remove_symlink_to_directory(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let dir = temp.child("dir"); + dir.create_dir_all().unwrap(); + let link = temp.child("link"); + link.symlink_to_dir(dir.path()).unwrap(); + + session + .sftp() + .unlink(link.path()) + .await + .expect("Failed to unlink symlink"); + + // Verify link removed but directory still exists + link.assert(predicate::path::missing()); + dir.assert(predicate::path::is_dir()); +} + +#[rstest] +#[smol_potat::test] +async fn unlink_should_fail_if_path_to_directory(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + let dir = temp.child("dir"); + dir.create_dir_all().unwrap(); + + let result = session.sftp().unlink(dir.path()).await; + assert!( + result.is_err(), + "Unexpectedly unlinked directory: {:?}", + result + ); + + // Verify directory still here + dir.assert(predicate::path::is_dir()); +} + +#[rstest] +#[smol_potat::test] +async fn unlink_should_fail_if_path_missing(#[future] session: Session) { + let session: Session = session.await; + + let temp = TempDir::new().unwrap(); + + let result = session.sftp().unlink(temp.child("missing").path()).await; + assert!( + result.is_err(), + "Unexpectedly unlinked missing path: {:?}", + result + ); } diff --git a/wezterm-ssh/tests/e2e/sftp/file.rs b/wezterm-ssh/tests/e2e/sftp/file.rs index 81c60bcb8..4d5cd3b1b 100644 --- a/wezterm-ssh/tests/e2e/sftp/file.rs +++ b/wezterm-ssh/tests/e2e/sftp/file.rs @@ -16,7 +16,7 @@ async fn should_support_async_reading(#[future] session: Session) { let mut remote_file = session .sftp() - .open(file.path().to_path_buf()) + .open(file.path()) .await .expect("Failed to open remote file"); @@ -41,7 +41,7 @@ async fn should_support_async_writing(#[future] session: Session) { let mut remote_file = session .sftp() - .create(file.path().to_path_buf()) + .create(file.path()) .await .expect("Failed to open remote file");