Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX committed Nov 25, 2024
1 parent a2b38ae commit 7786445
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
42 changes: 42 additions & 0 deletions crates/rules/src/matrix_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,45 @@ impl MatrixBuilder {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use sargon::FactorSource;

#[allow(clippy::upper_case_acronyms)]
type SUT = MatrixBuilder;

fn make() -> SUT {
SUT::new()
}

mod shield_configs {
use super::*;

mod mvp {

use super::*;

#[test]
fn config_1_1_recovery_device() {
let mut sut = make();
sut.primary_role
.add_factor_source_to_list(
FactorSource::sample_device(),
FactorListKind::Threshold,
)
.unwrap();

sut.primary_role
.add_factor_source_to_list(
FactorSource::sample_ledger(),
FactorListKind::Threshold,
)
.unwrap();

// sut.primary_role.set_threshold(threshold)
}
}
}
}
49 changes: 43 additions & 6 deletions crates/rules/src/roles_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ impl RoleBuilder {

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RoleBuilderValidation {
/// e.g. tried to remove a factor source which was not found.
FactorSourceNotFound,
BasicViolation(BasicViolation),
ForeverInvalid(ForeverInvalidReason),
NotYetValid(NotYetValidReason),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
pub enum BasicViolation {
/// e.g. tried to remove a factor source which was not found.
#[error("FactorSource not found")]
FactorSourceNotFound,

#[error("Recovery cannot set threshold")]
RecoveryCannotSetThreshold,

#[error("Confirmation cannot set threshold")]
ConfirmationCannotSetThreshold,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
pub enum NotYetValidReason {
#[error("Primary role with password in threshold list must have another factor")]
Expand Down Expand Up @@ -108,6 +120,15 @@ impl<T> FromNotYetValid for std::result::Result<T, RoleBuilderValidation> {
}
}

trait FromBasicViolation {
fn basic_violation(reason: BasicViolation) -> Self;
}
impl<T> FromBasicViolation for std::result::Result<T, RoleBuilderValidation> {
fn basic_violation(reason: BasicViolation) -> Self {
Err(RoleBuilderValidation::BasicViolation(reason))
}
}

impl ForeverInvalidReason {
fn threshold_list_not_supported_for_role(role: RoleKind) -> Self {
match role {
Expand All @@ -131,6 +152,7 @@ pub type RoleBuilderMutateResult = Result<(), RoleBuilderValidation>;
pub type RoleBuilderBuildResult = Result<RoleWithFactors, RoleBuilderValidation>;

use serde::{Deserialize, Serialize};
use BasicViolation::*;
use ForeverInvalidReason::*;
use NotYetValidReason::*;

Expand All @@ -145,8 +167,18 @@ pub trait BuilderOfFactorsInRole: Sized + private::UncheckedBuilderOfFactorsInRo

#[allow(dead_code)]
fn set_threshold(&mut self, threshold: u8) -> RoleBuilderMutateResult {
self.unchecked_set_threshold(threshold);
self.validate()
match self.role() {
RoleKind::Primary => {
self.unchecked_set_threshold(threshold);
self.validate()
}
RoleKind::Recovery => {
RoleBuilderMutateResult::basic_violation(RecoveryCannotSetThreshold)
}
RoleKind::Confirmation => {
RoleBuilderMutateResult::basic_violation(ConfirmationCannotSetThreshold)
}
}
}

fn override_contains_factor_source(&self, factor_source: &FactorSource) -> bool {
Expand Down Expand Up @@ -195,7 +227,7 @@ pub trait BuilderOfFactorsInRole: Sized + private::UncheckedBuilderOfFactorsInRo
self.unchecked_add_factor_source_to_list(factor_source, factor_list_kind);
}
Err(RoleBuilderValidation::ForeverInvalid(_))
| Err(RoleBuilderValidation::FactorSourceNotFound) => {}
| Err(RoleBuilderValidation::BasicViolation(_)) => {}
}
validation
}
Expand Down Expand Up @@ -365,9 +397,13 @@ impl BuilderOfFactorsInRole for RoleBuilder {

/// Lowers the threshold if the deleted factor source is in the threshold list
/// and if after removal of `factor_source` `self.threshold > self.threshold_factors.len()`
///
/// Returns `Ok` if `factor_source` was found and deleted. However, does not call `self.validate()`,
/// So state might still be invalid, i.e. we return the result of the action of removal, not the
/// state validation status.
fn remove_factor_source(&mut self, factor_source: &FactorSource) -> RoleBuilderMutateResult {
if !self.contains_factor_source(factor_source) {
return RoleBuilderMutateResult::Err(RoleBuilderValidation::FactorSourceNotFound);
return RoleBuilderMutateResult::basic_violation(FactorSourceNotFound);
}
let remove = |xs: &mut Vec<FactorSource>| {
let index = xs
Expand All @@ -387,6 +423,7 @@ impl BuilderOfFactorsInRole for RoleBuilder {
self.set_threshold(threshold_factors_len)?;
}
}

Ok(())
}

Expand Down

0 comments on commit 7786445

Please sign in to comment.