From 963c24203f206809a1d854dfebc59f5675eb31fc Mon Sep 17 00:00:00 2001 From: Alexander Cyon Date: Tue, 26 Nov 2024 20:46:22 +0100 Subject: [PATCH] uniffi tests working --- crates/rules-uniffi/src/builder.rs | 86 +++++++++++++++++++++++++++--- crates/rules/src/move_to_sargon.rs | 2 +- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/crates/rules-uniffi/src/builder.rs b/crates/rules-uniffi/src/builder.rs index 06b8719f..e3324d68 100644 --- a/crates/rules-uniffi/src/builder.rs +++ b/crates/rules-uniffi/src/builder.rs @@ -4,6 +4,8 @@ use std::sync::{Arc, RwLock}; +use sargon::HasSampleValues; + use crate::prelude::*; #[derive(Debug, uniffi::Object)] @@ -11,7 +13,8 @@ pub struct SecurityShieldBuilder { wrapped: RwLock>, } -#[derive(Debug, uniffi::Object)] +#[derive(Debug, PartialEq, Eq, Hash, uniffi::Object)] +#[uniffi::export(Debug, Eq, Hash)] pub struct SecurityStructureOfFactorSourceIds { wrapped_matrix: MatrixWithFactorSourceIds, name: String, @@ -39,15 +42,20 @@ impl Into for MatrixBuilderValidation { pub struct FactorSourceID { pub inner: sargon::FactorSourceID, } +impl FactorSourceID { + pub fn new(inner: sargon::FactorSourceID) -> Arc { + Arc::new(Self { inner }) + } +} impl SecurityShieldBuilder { fn with>( &self, mut with_non_consumed_builder: impl FnMut(&mut MatrixBuilder) -> Result, ) -> Result { - let Ok(mut binding) = self.wrapped.write() else { - return Err(CommonError::MatrixBuilderRwLockPoisoned); - }; + let guard = self.wrapped.write(); + + let mut binding = guard.map_err(|_| CommonError::MatrixBuilderRwLockPoisoned)?; let Some(builder) = binding.as_mut() else { return Err(CommonError::AlreadyBuilt); @@ -59,10 +67,10 @@ impl SecurityShieldBuilder { #[uniffi::export] impl SecurityShieldBuilder { #[uniffi::constructor] - pub fn new() -> Self { - Self { + pub fn new() -> Arc { + Arc::new(Self { wrapped: RwLock::new(Some(MatrixBuilder::new())), - } + }) } /// Adds the factor source to the primary role threshold list. @@ -80,6 +88,10 @@ impl SecurityShieldBuilder { self.with(|builder| builder.add_factor_source_to_primary_override(factor_source_id.inner)) } + pub fn remove_factor(&self, factor_source_id: Arc) -> Result<(), CommonError> { + self.with(|builder| builder.remove_factor(&factor_source_id.inner)) + } + pub fn set_threshold(&self, threshold: u8) -> Result<(), CommonError> { self.with(|builder| builder.set_threshold(threshold)) } @@ -104,7 +116,10 @@ impl SecurityShieldBuilder { self: Arc, name: String, ) -> Result { - let mut binding = self.wrapped.write().unwrap(); + let mut binding = self + .wrapped + .write() + .map_err(|_| CommonError::MatrixBuilderRwLockPoisoned)?; let builder = binding.take().ok_or(CommonError::AlreadyBuilt)?; let wrapped_matrix = builder .build() @@ -119,8 +134,36 @@ impl SecurityShieldBuilder { } } +#[cfg(test)] +impl FactorSourceID { + fn sample_device() -> Arc { + Self::new(sargon::FactorSourceID::sample_device()) + } + + fn sample_device_other() -> Arc { + Self::new(sargon::FactorSourceID::sample_device_other()) + } + + fn sample_ledger() -> Arc { + Self::new(sargon::FactorSourceID::sample_ledger()) + } + + fn sample_ledger_other() -> Arc { + Self::new(sargon::FactorSourceID::sample_ledger_other()) + } + + fn sample_arculus() -> Arc { + Self::new(sargon::FactorSourceID::sample_arculus()) + } + + fn sample_arculus_other() -> Arc { + Self::new(sargon::FactorSourceID::sample_arculus_other()) + } +} + #[cfg(test)] mod tests { + use super::*; #[allow(clippy::upper_case_acronyms)] @@ -129,5 +172,32 @@ mod tests { #[test] fn test() { let sut = SUT::new(); + sut.add_factor_source_to_primary_override(FactorSourceID::sample_arculus()) + .unwrap(); + sut.add_factor_source_to_primary_override(FactorSourceID::sample_arculus_other()) + .unwrap(); + sut.add_factor_source_to_recovery_override(FactorSourceID::sample_ledger()) + .unwrap(); + sut.add_factor_source_to_recovery_override(FactorSourceID::sample_ledger_other()) + .unwrap(); + sut.add_factor_source_to_confirmation_override(FactorSourceID::sample_device()) + .unwrap(); + + sut.remove_factor(FactorSourceID::sample_arculus_other()).unwrap(); + sut.remove_factor(FactorSourceID::sample_ledger_other()).unwrap(); + + let shield = sut.build("test".to_owned()).unwrap(); + assert_eq!( + shield.wrapped_matrix.primary().override_factors(), + &vec![FactorSourceID::sample_arculus().inner] + ); + assert_eq!( + shield.wrapped_matrix.recovery().override_factors(), + &vec![FactorSourceID::sample_ledger().inner] + ); + assert_eq!( + shield.wrapped_matrix.confirmation().override_factors(), + &vec![FactorSourceID::sample_device().inner] + ); } } diff --git a/crates/rules/src/move_to_sargon.rs b/crates/rules/src/move_to_sargon.rs index ea9998cc..a10da35d 100644 --- a/crates/rules/src/move_to_sargon.rs +++ b/crates/rules/src/move_to_sargon.rs @@ -22,7 +22,7 @@ impl HasFactorSourceKindObjectSafe for FactorSourceID { #[allow(dead_code)] // TODO REMOVE once migrated to sargon -pub(crate) trait SampleValues: Sized { +pub trait SampleValues: Sized { fn sample_device() -> Self; fn sample_device_other() -> Self; fn sample_ledger() -> Self;