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

Commit

Permalink
tarpaulin accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Nov 29, 2024
1 parent cb0ecdc commit 8cccc58
Showing 1 changed file with 55 additions and 73 deletions.
128 changes: 55 additions & 73 deletions crates/rules/src/roles/builder/roles_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::prelude::*;

use FactorListKind::*;

pub type PrimaryRoleBuilder = RoleBuilder<{ ROLE_PRIMARY }>;
pub type RecoveryRoleBuilder = RoleBuilder<{ ROLE_RECOVERY }>;
pub type ConfirmationRoleBuilder = RoleBuilder<{ ROLE_CONFIRMATION }>;
Expand Down Expand Up @@ -44,7 +46,7 @@ pub enum RoleBuilderValidation {
#[error("Not yet valid: {0}")]
NotYetValid(#[from] NotYetValidReason),
}
type Validation = RoleBuilderValidation;
use RoleBuilderValidation::*;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, thiserror::Error)]
pub enum BasicViolation {
Expand Down Expand Up @@ -115,7 +117,7 @@ pub(crate) trait FromForeverInvalid {
}
impl<T> FromForeverInvalid for std::result::Result<T, RoleBuilderValidation> {
fn forever_invalid(reason: ForeverInvalidReason) -> Self {
Err(Validation::ForeverInvalid(reason))
Err(ForeverInvalid(reason))
}
}

Expand All @@ -124,7 +126,7 @@ pub(crate) trait FromNotYetValid {
}
impl<T> FromNotYetValid for std::result::Result<T, RoleBuilderValidation> {
fn not_yet_valid(reason: NotYetValidReason) -> Self {
Err(Validation::NotYetValid(reason))
Err(NotYetValid(reason))
}
}

Expand All @@ -133,7 +135,7 @@ pub(crate) trait FromBasicViolation {
}
impl<T> FromBasicViolation for std::result::Result<T, RoleBuilderValidation> {
fn basic_violation(reason: BasicViolation) -> Self {
Err(Validation::BasicViolation(reason))
Err(BasicViolation(reason))
}
}

Expand Down Expand Up @@ -223,7 +225,7 @@ where
&mut self,
factor_source_id: FactorSourceID,
) -> RoleBuilderMutateResult {
self._add_factor_source_to_list(factor_source_id, FactorListKind::Threshold)
self._add_factor_source_to_list(factor_source_id, Threshold)
}

/// If we would add a factor of kind `factor_source_kind` to the list of kind `factor_list_kind`
Expand All @@ -232,10 +234,7 @@ where
&self,
factor_source_kind: FactorSourceKind,
) -> RoleBuilderMutateResult {
self._validation_for_addition_of_factor_source_of_kind_to_list(
factor_source_kind,
FactorListKind::Threshold,
)
self._validation_add(factor_source_kind, Threshold)
}

#[cfg(test)]
Expand All @@ -244,7 +243,7 @@ where
factor_source_kind: FactorSourceKind,
list: FactorListKind,
) -> RoleBuilderMutateResult {
self._validation_for_addition_of_factor_source_of_kind_to_list(factor_source_kind, list)
self._validation_add(factor_source_kind, list)
}
}

Expand All @@ -271,7 +270,7 @@ impl<const R: u8> RoleBuilder<R> {
&mut self,
factor_source_id: FactorSourceID,
) -> RoleBuilderMutateResult {
self._add_factor_source_to_list(factor_source_id, FactorListKind::Override)
self._add_factor_source_to_list(factor_source_id, Override)
}

/// If Ok => self is mutated
Expand All @@ -285,10 +284,10 @@ impl<const R: u8> RoleBuilder<R> {
let validation = self
.validation_for_addition_of_factor_source_to_list(&factor_source_id, factor_list_kind);

Check warning on line 285 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L285

Added line #L285 was not covered by tests
match validation.as_ref() {
Ok(()) | Err(Validation::NotYetValid(_)) => {
Ok(()) | Err(NotYetValid(_)) => {

Check warning on line 287 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L287

Added line #L287 was not covered by tests
self.unchecked_add_factor_source_to_list(factor_source_id, factor_list_kind);
}
Err(Validation::ForeverInvalid(_)) | Err(Validation::BasicViolation(_)) => {}
Err(ForeverInvalid(_)) | Err(BasicViolation(_)) => {}

Check warning on line 290 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L290

Added line #L290 was not covered by tests
}
validation
}
Expand All @@ -299,31 +298,35 @@ impl<const R: u8> RoleBuilder<R> {
&self,
factor_source_kind: FactorSourceKind,
) -> RoleBuilderMutateResult {
self._validation_for_addition_of_factor_source_of_kind_to_list(
factor_source_kind,
FactorListKind::Override,
)
self._validation_add(factor_source_kind, Override)
}

/// If we would add a factor of kind `factor_source_kind` to the list of kind `factor_list_kind`
/// what would be the validation status?
fn _validation_for_addition_of_factor_source_of_kind_to_list(
fn _validation_add(
&self,
factor_source_kind: FactorSourceKind,
factor_list_kind: FactorListKind,
) -> RoleBuilderMutateResult {
match self.role() {
RoleKind::Primary => self.validation_for_addition_of_factor_source_of_kind_to_list_for_primary(factor_source_kind, factor_list_kind),
RoleKind::Primary => {

Check warning on line 312 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L312

Added line #L312 was not covered by tests
return self.validation_for_addition_of_factor_source_of_kind_to_list_for_primary(
factor_source_kind,
factor_list_kind,

Check warning on line 315 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L314-L315

Added lines #L314 - L315 were not covered by tests
)
}
RoleKind::Recovery | RoleKind::Confirmation => match factor_list_kind {
FactorListKind::Threshold => {
RoleBuilderMutateResult::forever_invalid(ForeverInvalidReason::threshold_list_not_supported_for_role(self.role()))
Threshold => {

Check warning on line 319 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L319

Added line #L319 was not covered by tests
return Result::forever_invalid(
ForeverInvalidReason::threshold_list_not_supported_for_role(self.role()),
)
}
FactorListKind::Override => self
.validation_for_addition_of_factor_source_of_kind_to_override_for_non_primary_role(
factor_source_kind,
),
Override => {}

Check warning on line 324 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L324

Added line #L324 was not covered by tests
},
}
self.validation_for_addition_of_factor_source_of_kind_to_override_for_non_primary_role(
factor_source_kind,

Check warning on line 328 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L328

Added line #L328 was not covered by tests
)
}
}

Expand Down Expand Up @@ -384,26 +387,20 @@ impl<const R: u8> RoleBuilder<R> {
let mut simulation = Self::new();

// Validate override factors
for override_factor in self.get_override_factors() {
let validation =
simulation._add_factor_source_to_list(*override_factor, FactorListKind::Override);
for f in self.get_override_factors() {
let validation = simulation.add_factor_source_to_override(*f);
match validation.as_ref() {
Ok(()) | Err(Validation::NotYetValid(_)) => continue,
Err(Validation::ForeverInvalid(_)) | Err(Validation::BasicViolation(_)) => {
return validation
}
Ok(()) | Err(NotYetValid(_)) => continue,

Check warning on line 393 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L393

Added line #L393 was not covered by tests
Err(ForeverInvalid(_)) | Err(BasicViolation(_)) => return validation,
}
}

// Validate threshold factors
for threshold_factor in self.get_threshold_factors() {
let validation =
simulation._add_factor_source_to_list(*threshold_factor, FactorListKind::Threshold);
for f in self.get_threshold_factors() {
let validation = simulation._add_factor_source_to_list(*f, Threshold);
match validation.as_ref() {
Ok(()) | Err(Validation::NotYetValid(_)) => continue,
Err(Validation::ForeverInvalid(_)) | Err(Validation::BasicViolation(_)) => {
return validation
}
Ok(()) | Err(NotYetValid(_)) => continue,

Check warning on line 402 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L402

Added line #L402 was not covered by tests
Err(ForeverInvalid(_)) | Err(BasicViolation(_)) => return validation,
}
}

Expand Down Expand Up @@ -490,10 +487,7 @@ impl<const R: u8> RoleBuilder<R> {
return RoleBuilderMutateResult::forever_invalid(FactorSourceAlreadyPresent);
}
let factor_source_kind = factor_source_id.get_factor_source_kind();
self._validation_for_addition_of_factor_source_of_kind_to_list(
factor_source_kind,
factor_list_kind,
)
self._validation_add(factor_source_kind, factor_list_kind)
}

fn contains_factor_source(&self, factor_source_id: &FactorSourceID) -> bool {
Expand Down Expand Up @@ -624,12 +618,9 @@ impl<const R: u8> RoleBuilder<R> {
assert_eq!(self.role(), RoleKind::Primary);
let factor_source_kind = FactorSourceKind::Passphrase;
match factor_list_kind {
FactorListKind::Threshold => {
Threshold => {

Check warning on line 621 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L621

Added line #L621 was not covered by tests
let is_alone = self
.factor_sources_not_of_kind_to_list_of_kind(
factor_source_kind,
FactorListKind::Threshold,
)
.factor_sources_not_of_kind_to_list_of_kind(factor_source_kind, Threshold)
.is_empty();
if is_alone {
return RoleBuilderMutateResult::not_yet_valid(
Expand All @@ -642,7 +633,7 @@ impl<const R: u8> RoleBuilder<R> {
);
}
}
FactorListKind::Override => {
Override => {

Check warning on line 636 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L636

Added line #L636 was not covered by tests
return RoleBuilderMutateResult::forever_invalid(
PrimaryCannotHavePasswordInOverrideList,
);
Expand All @@ -664,8 +655,8 @@ impl<const R: u8> RoleBuilder<R> {
.collect()
};
match factor_list_kind {
FactorListKind::Override => filter(self.get_override_factors()),
FactorListKind::Threshold => filter(self.get_threshold_factors()),
Override => filter(self.get_override_factors()),
Threshold => filter(self.get_threshold_factors()),
}
}
}
Expand All @@ -691,7 +682,7 @@ pub(crate) fn test_duplicates_not_allowed<const R: u8>(
// Assert
assert!(matches!(
res,
RoleBuilderMutateResult::Err(Validation::ForeverInvalid(
RoleBuilderMutateResult::Err(ForeverInvalid(
ForeverInvalidReason::FactorSourceAlreadyPresent

Check warning on line 686 in crates/rules/src/roles/builder/roles_builder.rs

View check run for this annotation

Codecov / codecov/patch

crates/rules/src/roles/builder/roles_builder.rs#L684-L686

Added lines #L684 - L686 were not covered by tests
))
));
Expand All @@ -706,12 +697,12 @@ mod tests {
fn primary_duplicates_not_allowed() {
test_duplicates_not_allowed(
PrimaryRoleBuilder::new(),
FactorListKind::Override,
Override,
FactorSourceID::sample_arculus(),
);
test_duplicates_not_allowed(
PrimaryRoleBuilder::new(),
FactorListKind::Threshold,
Threshold,
FactorSourceID::sample_arculus(),
);
}
Expand All @@ -720,7 +711,7 @@ mod tests {
fn recovery_duplicates_not_allowed() {
test_duplicates_not_allowed(
RecoveryRoleBuilder::new(),
FactorListKind::Override,
Override,
FactorSourceID::sample_arculus(),
);
}
Expand All @@ -729,19 +720,18 @@ mod tests {
fn confirmation_duplicates_not_allowed() {
test_duplicates_not_allowed(
ConfirmationRoleBuilder::new(),
FactorListKind::Override,
Override,
FactorSourceID::sample_arculus(),
);
}

#[test]
fn recovery_cannot_add_factors_to_threshold() {
let mut sut = RecoveryRoleBuilder::new();
let res = sut
._add_factor_source_to_list(FactorSourceID::sample_ledger(), FactorListKind::Threshold);
let res = sut._add_factor_source_to_list(FactorSourceID::sample_ledger(), Threshold);
assert_eq!(
res,
Err(Validation::ForeverInvalid(
Err(ForeverInvalid(
ForeverInvalidReason::RecoveryRoleThresholdFactorsNotSupported
))
);
Expand All @@ -750,23 +740,19 @@ mod tests {
#[test]
fn confirmation_cannot_add_factors_to_threshold() {
let mut sut = ConfirmationRoleBuilder::new();
let res = sut
._add_factor_source_to_list(FactorSourceID::sample_ledger(), FactorListKind::Threshold);
let res = sut._add_factor_source_to_list(FactorSourceID::sample_ledger(), Threshold);
assert_eq!(
res,
Err(Validation::ForeverInvalid(
Err(ForeverInvalid(
ForeverInvalidReason::ConfirmationRoleThresholdFactorsNotSupported
))
);
}

#[test]
fn recovery_validation_for_addition_of_factor_source_of_kind_to_list_is_err_for_threshold() {
fn recovery_validation_add_is_err_for_threshold() {
let sut = RecoveryRoleBuilder::new();
let res = sut._validation_for_addition_of_factor_source_of_kind_to_list(
FactorSourceKind::Device,
FactorListKind::Threshold,
);
let res = sut._validation_add(FactorSourceKind::Device, Threshold);
assert_eq!(
res,
RoleBuilderMutateResult::forever_invalid(
Expand All @@ -776,13 +762,9 @@ mod tests {
}

#[test]
fn confirmation_validation_for_addition_of_factor_source_of_kind_to_list_is_err_for_threshold()
{
fn confirmation_validation_add_is_err_for_threshold() {
let sut = ConfirmationRoleBuilder::new();
let res = sut._validation_for_addition_of_factor_source_of_kind_to_list(
FactorSourceKind::Device,
FactorListKind::Threshold,
);
let res = sut._validation_add(FactorSourceKind::Device, Threshold);
assert_eq!(
res,
RoleBuilderMutateResult::forever_invalid(
Expand Down

0 comments on commit 8cccc58

Please sign in to comment.