From 478f25304f2ac891392764d669dcb47d1ad03536 Mon Sep 17 00:00:00 2001 From: Christian Visintin Date: Sat, 21 Mar 2026 15:20:46 +0100 Subject: [PATCH] test: add parser and bookmark regression coverage --- src/system/bookmarks_client.rs | 53 ++++++++++++++++++++++++++++++++++ src/utils/parser.rs | 21 ++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/system/bookmarks_client.rs b/src/system/bookmarks_client.rs index 601eda5..e21cfcf 100644 --- a/src/system/bookmarks_client.rs +++ b/src/system/bookmarks_client.rs @@ -839,6 +839,59 @@ mod tests { assert!(client.decrypt_str("bidoof").is_err()); } + #[test] + fn should_return_bookmark_when_password_decryption_fails() { + let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap(); + let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path()); + let mut client = + BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap(); + let mut bookmark = Bookmark::from(make_generic_ftparams( + FileTransferProtocol::Sftp, + "192.168.1.31", + 22, + "pi", + Some("mypassword"), + )); + bookmark.password = Some(String::from("not-valid-base64")); + client + .hosts + .bookmarks + .insert(String::from("raspberry"), bookmark); + + let bookmark = ftparams_to_tup(client.get_bookmark("raspberry").unwrap()); + + assert_eq!(bookmark.0, String::from("192.168.1.31")); + assert_eq!(bookmark.1, 22); + assert_eq!(bookmark.2, FileTransferProtocol::Sftp); + assert_eq!(bookmark.3, String::from("pi")); + assert_eq!(bookmark.4.as_deref(), Some("not-valid-base64")); + } + + #[test] + fn should_return_s3_bookmark_when_secret_decryption_fails() { + let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap(); + let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path()); + let mut client = + BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap(); + let mut bookmark = Bookmark::from(make_s3_ftparams()); + let s3 = bookmark.s3.as_mut().unwrap(); + s3.access_key = Some(String::from("bad-access-key")); + s3.secret_access_key = Some(String::from("bad-secret-key")); + client + .hosts + .bookmarks + .insert(String::from("my-bucket"), bookmark); + + let bookmark = client.get_bookmark("my-bucket").unwrap(); + let params = bookmark.params.s3_params().unwrap(); + + assert_eq!(bookmark.protocol, FileTransferProtocol::AwsS3); + assert_eq!(params.bucket_name.as_str(), "omar"); + assert_eq!(params.region.as_deref(), Some("eu-west-1")); + assert_eq!(params.access_key.as_deref(), Some("bad-access-key")); + assert_eq!(params.secret_access_key.as_deref(), Some("bad-secret-key")); + } + /// Get paths for configuration and key for bookmarks fn get_paths(dir: &Path) -> (PathBuf, PathBuf) { let k: PathBuf = PathBuf::from(dir); diff --git a/src/utils/parser.rs b/src/utils/parser.rs index 463a91a..a03bc9b 100644 --- a/src/utils/parser.rs +++ b/src/utils/parser.rs @@ -382,6 +382,21 @@ mod tests { assert_eq!(params.password.as_str(), "password"); } + #[test] + fn test_should_parse_webdav_opt_with_at_in_password() { + let result = + parse_remote_opt("https://omar:p@ssword@myserver:4445/myshare/dir/subdir").unwrap(); + + let params = result.params.webdav_params().unwrap(); + assert_eq!(params.uri.as_str(), "https://myserver:4445"); + assert_eq!(params.username.as_str(), "omar"); + assert_eq!(params.password.as_str(), "p@ssword"); + assert_eq!( + result.remote_path.as_deref().unwrap(), + std::path::Path::new("/myshare/dir/subdir") + ); + } + #[test] fn should_reject_malformed_webdav_options() { let result = parse_remote_opt("https://omar@myserver:4445/myshare"); @@ -389,6 +404,12 @@ mod tests { assert!(result.is_err()); } + #[test] + fn should_reject_webdav_options_with_missing_credentials() { + assert!(parse_remote_opt("https://:password@myserver:4445/myshare").is_err()); + assert!(parse_remote_opt("https://omar:@myserver:4445/myshare").is_err()); + } + #[test] fn parse_aws_s3_opt() { // Simple