From e9152ee951ca21492b09912e34a8746e7dbe880b Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 29 Aug 2024 11:40:48 +0200 Subject: [PATCH] tests: replace static mut with a mutex rustc warns about creating references to them (although it does allow using `.as_ref()` on them for some reason), and this will become a hard error with edition 2024. Previously we could not use Mutex there as its ::new() was not a `const fn` , but not we can, so let's drop the `mut`. Signed-off-by: Wolfgang Bumiller --- pbs-client/src/tools/key_source.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/pbs-client/src/tools/key_source.rs b/pbs-client/src/tools/key_source.rs index 2c15423f..c039de26 100644 --- a/pbs-client/src/tools/key_source.rs +++ b/pbs-client/src/tools/key_source.rs @@ -302,45 +302,43 @@ pub(crate) fn read_optional_default_master_pubkey() -> Result>, Error> = Ok(None); +static TEST_DEFAULT_ENCRYPTION_KEY: std::sync::Mutex>, Error>> = + std::sync::Mutex::new(Ok(None)); #[cfg(test)] pub(crate) fn read_optional_default_encryption_key() -> Result, Error> { // not safe when multiple concurrent test cases end up here! - unsafe { - match &TEST_DEFAULT_ENCRYPTION_KEY { - Ok(Some(key)) => Ok(Some(KeyWithSource::from_default(key.clone()))), - Ok(None) => Ok(None), - Err(_) => bail!("test error"), - } + match &*TEST_DEFAULT_ENCRYPTION_KEY.lock().unwrap() { + Ok(Some(key)) => Ok(Some(KeyWithSource::from_default(key.clone()))), + Ok(None) => Ok(None), + Err(_) => bail!("test error"), } } #[cfg(test)] // not safe when multiple concurrent test cases end up here! pub(crate) unsafe fn set_test_encryption_key(value: Result>, Error>) { - TEST_DEFAULT_ENCRYPTION_KEY = value; + *TEST_DEFAULT_ENCRYPTION_KEY.lock().unwrap() = value; } #[cfg(test)] -static mut TEST_DEFAULT_MASTER_PUBKEY: Result>, Error> = Ok(None); +static TEST_DEFAULT_MASTER_PUBKEY: std::sync::Mutex>, Error>> = + std::sync::Mutex::new(Ok(None)); #[cfg(test)] pub(crate) fn read_optional_default_master_pubkey() -> Result, Error> { // not safe when multiple concurrent test cases end up here! - unsafe { - match &TEST_DEFAULT_MASTER_PUBKEY { - Ok(Some(key)) => Ok(Some(KeyWithSource::from_default(key.clone()))), - Ok(None) => Ok(None), - Err(_) => bail!("test error"), - } + match &*TEST_DEFAULT_MASTER_PUBKEY.lock().unwrap() { + Ok(Some(key)) => Ok(Some(KeyWithSource::from_default(key.clone()))), + Ok(None) => Ok(None), + Err(_) => bail!("test error"), } } #[cfg(test)] // not safe when multiple concurrent test cases end up here! pub(crate) unsafe fn set_test_default_master_pubkey(value: Result>, Error>) { - TEST_DEFAULT_MASTER_PUBKEY = value; + *TEST_DEFAULT_MASTER_PUBKEY.lock().unwrap() = value; } pub fn get_encryption_key_password() -> Result, Error> {