Skip to content

Commit b6c0ba7

Browse files
A6GibKmbilelmoussaoui
authored andcommitted
server: collection: Move sanitizer into helper
And add tests.
1 parent 97d31b6 commit b6c0ba7

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

server/src/collection/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,23 +386,26 @@ impl Collection {
386386
) -> zbus::Result<()>;
387387
}
388388

389+
fn collection_path(label: &str) -> Result<OwnedObjectPath, zvariant::Error> {
390+
let sanitized_label = label.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "_");
391+
392+
OwnedObjectPath::try_from(format!(
393+
"/org/freedesktop/secrets/collection/{sanitized_label}"
394+
))
395+
}
396+
389397
impl Collection {
390398
pub async fn new(label: &str, alias: &str, service: Service, keyring: Keyring) -> Self {
391399
let modified = keyring.modified_time().await;
392400
let created = keyring.created_time().await.unwrap_or(modified);
393401

394-
let sanitized_label = label.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "_");
395-
396402
Self {
397403
items: Default::default(),
398404
label: Arc::new(Mutex::new(label.to_owned())),
399405
modified: Arc::new(Mutex::new(modified)),
400406
alias: Arc::new(Mutex::new(alias.to_owned())),
401407
item_index: Arc::new(RwLock::new(0)),
402-
path: OwnedObjectPath::try_from(format!(
403-
"/org/freedesktop/secrets/collection/{sanitized_label}"
404-
))
405-
.expect("Sanitized label should always produce valid object path"),
408+
path: collection_path(label).expect("Label should produce a valid object path"),
406409
created,
407410
service,
408411
keyring: Arc::new(RwLock::new(Some(keyring))),

server/src/collection/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,14 @@ async fn locked_collection_operations() -> Result<(), Box<dyn std::error::Error>
723723

724724
Ok(())
725725
}
726+
727+
#[test]
728+
fn test_collection_path() {
729+
use crate::collection::collection_path;
730+
731+
assert!(collection_path("").is_err());
732+
assert_eq!(
733+
collection_path("bär").unwrap().as_str(),
734+
"/org/freedesktop/secrets/collection/b_r"
735+
);
736+
}

0 commit comments

Comments
 (0)