Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
uniffi tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Nov 26, 2024
1 parent d46b026 commit 963c242
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
86 changes: 78 additions & 8 deletions crates/rules-uniffi/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

use std::sync::{Arc, RwLock};

use sargon::HasSampleValues;

use crate::prelude::*;

#[derive(Debug, uniffi::Object)]
pub struct SecurityShieldBuilder {
wrapped: RwLock<Option<MatrixBuilder>>,
}

#[derive(Debug, uniffi::Object)]
#[derive(Debug, PartialEq, Eq, Hash, uniffi::Object)]
#[uniffi::export(Debug, Eq, Hash)]
pub struct SecurityStructureOfFactorSourceIds {
wrapped_matrix: MatrixWithFactorSourceIds,
name: String,
Expand Down Expand Up @@ -39,15 +42,20 @@ impl Into<CommonError> for MatrixBuilderValidation {
pub struct FactorSourceID {
pub inner: sargon::FactorSourceID,
}
impl FactorSourceID {
pub fn new(inner: sargon::FactorSourceID) -> Arc<Self> {
Arc::new(Self { inner })
}
}

impl SecurityShieldBuilder {
fn with<R, E: Into<CommonError>>(
&self,
mut with_non_consumed_builder: impl FnMut(&mut MatrixBuilder) -> Result<R, E>,
) -> Result<R, CommonError> {
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);
Expand All @@ -59,10 +67,10 @@ impl SecurityShieldBuilder {
#[uniffi::export]
impl SecurityShieldBuilder {
#[uniffi::constructor]
pub fn new() -> Self {
Self {
pub fn new() -> Arc<Self> {
Arc::new(Self {
wrapped: RwLock::new(Some(MatrixBuilder::new())),
}
})
}

/// Adds the factor source to the primary role threshold list.
Expand All @@ -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<FactorSourceID>) -> 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))
}
Expand All @@ -104,7 +116,10 @@ impl SecurityShieldBuilder {
self: Arc<Self>,
name: String,
) -> Result<SecurityStructureOfFactorSourceIds, CommonError> {
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()
Expand All @@ -119,8 +134,36 @@ impl SecurityShieldBuilder {
}
}

#[cfg(test)]
impl FactorSourceID {
fn sample_device() -> Arc<Self> {
Self::new(sargon::FactorSourceID::sample_device())
}

fn sample_device_other() -> Arc<Self> {
Self::new(sargon::FactorSourceID::sample_device_other())
}

fn sample_ledger() -> Arc<Self> {
Self::new(sargon::FactorSourceID::sample_ledger())
}

fn sample_ledger_other() -> Arc<Self> {
Self::new(sargon::FactorSourceID::sample_ledger_other())
}

fn sample_arculus() -> Arc<Self> {
Self::new(sargon::FactorSourceID::sample_arculus())
}

fn sample_arculus_other() -> Arc<Self> {
Self::new(sargon::FactorSourceID::sample_arculus_other())
}
}

#[cfg(test)]
mod tests {

use super::*;

#[allow(clippy::upper_case_acronyms)]
Expand All @@ -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]
);
}
}
2 changes: 1 addition & 1 deletion crates/rules/src/move_to_sargon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 963c242

Please sign in to comment.