From 67f1c247cc45fe8d065031b95a60becaeba3f27e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 7 Apr 2024 22:23:25 +0200 Subject: [PATCH] Have `.delete_ikm()` return an error if the requested id is not found --- src/ikm.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ikm.rs b/src/ikm.rs index 9921cfc..cd14a65 100644 --- a/src/ikm.rs +++ b/src/ikm.rs @@ -266,8 +266,14 @@ impl InputKeyMaterialList { /// # Ok::<(), coffio::Error>(()) /// ``` #[cfg(feature = "ikm-management")] - pub fn delete_ikm(&mut self, id: IkmId) { + pub fn delete_ikm(&mut self, id: IkmId) -> Result<()> { + let initial_len = self.ikm_lst.len(); self.ikm_lst.retain(|ikm| ikm.id != id); + if self.ikm_lst.len() == initial_len { + Err(Error::IkmNotFound(id)) + } else { + Ok(()) + } } /// Revoke the specified IKM from the list. @@ -563,13 +569,18 @@ mod ikm_management { let latest_ikm = lst.get_latest_ikm(SystemTime::now()).unwrap(); assert_eq!(latest_ikm.id, 2); - lst.delete_ikm(2); + let res = lst.delete_ikm(2); + assert!(res.is_ok(), "res: {res:?}"); let latest_ikm = lst.get_latest_ikm(SystemTime::now()).unwrap(); assert_eq!(latest_ikm.id, 1); - lst.delete_ikm(1); + let res = lst.delete_ikm(1); + assert!(res.is_ok(), "res: {res:?}"); let res = lst.get_latest_ikm(SystemTime::now()); assert!(res.is_err()); + + let res = lst.delete_ikm(42); + assert!(res.is_err(), "res: {res:?}"); } #[test]