Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/src/file/unlocked_keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ impl UnlockedKeyring {
Err(e) => {
#[cfg(feature = "tracing")]
tracing::error!("Failed to write keyring after item creation");
let mut keyring = self.keyring.write().await;
keyring.items.pop();
Err(e)
}
Ok(_) => {
Expand Down
48 changes: 48 additions & 0 deletions client/tests/file_unlocked_keyring/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,54 @@ async fn delete_broken_items() -> Result<(), Error> {
Ok(())
}

#[tokio::test]
async fn create_item_write_failure_leaves_item_in_memory() -> Result<(), Error> {
let temp_dir = tempdir()?;
let keyring_path = temp_dir.path().join("write_fail.keyring");

let keyring = UnlockedKeyring::load(&keyring_path, Some(Secret::blob("test"))).await?;
keyring
.create_item("Initial", &[("app", "test")], "secret", false)
.await?;
assert_eq!(keyring.n_items().await, 1);

// Make the parent directory unwritable so the temp-file creation fails
let dir = temp_dir.path();
let mut perms = std::fs::metadata(dir)?.permissions();
perms.set_readonly(true);
std::fs::set_permissions(dir, perms)?;

// create_item should fail on write
let result = keyring
.create_item("Ghost", &[("app", "ghost")], "ghost-secret", false)
.await;
assert!(result.is_err(), "create_item should fail when write fails");

// The failed item must not remain in memory
let items = keyring.items().await?;
assert_eq!(
items.len(),
1,
"Failed create_item should not leave a ghost item in memory"
);

// Restore permissions for cleanup
let mut perms = std::fs::metadata(dir)?.permissions();
#[allow(clippy::permissions_set_readonly_false)]
perms.set_readonly(false);
std::fs::set_permissions(dir, perms)?;

// Reload from disk
let reloaded = UnlockedKeyring::load(&keyring_path, Some(Secret::blob("test"))).await?;
assert_eq!(
reloaded.n_items().await,
1,
"On-disk keyring should only have the initial item"
);

Ok(())
}

#[tokio::test]
async fn partially_corrupted_keyring_error() -> Result<(), Error> {
let temp_dir = tempdir().unwrap();
Expand Down
7 changes: 3 additions & 4 deletions client/tests/file_unlocked_keyring/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use oo7::{Key, Secret, file::*};
use tempfile::tempdir;
use std::path::PathBuf;

#[cfg(feature = "async-std")]
use async_std::fs;
use oo7::{Key, Secret, file::*};
use tempfile::tempdir;
#[cfg(feature = "tokio")]
use tokio::fs;

use std::path::PathBuf;

use super::strong_key;

#[tokio::test]
Expand Down
57 changes: 57 additions & 0 deletions server/src/collection/tests/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,63 @@ async fn create_item_with_replace_matches_attributes() -> Result<(), Box<dyn std
Ok(())
}

#[tokio::test]
async fn create_item_with_replace_removes_multiple_duplicates()
-> Result<(), Box<dyn std::error::Error>> {
let setup = TestServiceSetup::plain_session(true).await?;
let attrs = &[("application", "dup-app"), ("username", "same-user")];

// Create 3 items with identical attributes (replace=false)
let item1 = setup
.create_item("Dup 1", attrs, "password-1", false)
.await?;
let item2 = setup
.create_item("Dup 2", attrs, "password-2", false)
.await?;
let item3 = setup
.create_item("Dup 3", attrs, "password-3", false)
.await?;

assert_eq!(setup.collections[0].items().await?.len(), 3);

let first_path = item1.inner().path().to_owned();

// Replace with a 4th
let replacement = setup
.create_item("Replacement", attrs, "password-final", true)
.await?;

let items = setup.collections[0].items().await?;
assert_eq!(
items.len(),
1,
"Should have exactly one item after replace dedup"
);

// The kept item should be at the first match's path
assert_eq!(
replacement.inner().path().as_str(),
first_path.as_str(),
"Replacement should reuse the first duplicate's D-Bus path"
);

// The removed items' D-Bus objects should be gone
assert!(
item2.secret(&setup.session).await.is_err(),
"Removed dup should not be accessible"
);
assert!(
item3.secret(&setup.session).await.is_err(),
"Removed dup should not be accessible"
);

// Verify the secret is correct
let secret = replacement.secret(&setup.session).await?;
assert_eq!(secret.value(), b"password-final");

Ok(())
}

#[tokio::test]
async fn create_item_invalid_session() -> Result<(), Box<dyn std::error::Error>> {
let setup = TestServiceSetup::plain_session(true).await?;
Expand Down
4 changes: 1 addition & 3 deletions server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ struct MockPrompters {
}

impl MockPrompters {
async fn register(
conn: &zbus::Connection,
) -> Result<Self, Box<dyn std::error::Error>> {
async fn register(conn: &zbus::Connection) -> Result<Self, Box<dyn std::error::Error>> {
#[cfg(any(feature = "gnome_native_crypto", feature = "gnome_openssl_crypto"))]
let gnome = {
let prompter = MockPrompterServiceGNOME::new();
Expand Down
Loading