use super::super::{
Polarity, RuleCategory, RuleException, RuleOrigin, RuleRecord, RuleScope, RuleState,
};
use super::{AuthorityStanding, PredicateAnswer, RuleOutcome, RuleSubject, conflict_reason};
fn subject() -> RuleSubject {
RuleSubject {
roles: vec!["LocalPosition".to_owned()],
selector: "control.object.button".to_owned(),
configuration: "base".to_owned(),
projection: "base".to_owned(),
object: None,
}
}
fn rule(id: &str, polarity: Polarity) -> RuleRecord {
RuleRecord {
rule_id: id.to_owned(),
revision: 2,
category: RuleCategory::Protocol,
origin: RuleOrigin::AcceptedPolicy,
authority_ref: Some("policy:control".to_owned()),
scope: RuleScope {
roles: vec!["LocalPosition".to_owned()],
selector: "control.object.button".to_owned(),
configurations: Vec::new(),
projection: "base".to_owned(),
},
object: None,
requirement: "provides cap.render".to_owned(),
polarity,
expects: Some(("provides".to_owned(), "cap.render".to_owned())),
exceptions: Vec::new(),
sources: vec![("policy.txt".to_owned(), "accepted".to_owned())],
state: RuleState::Accepted,
}
}
fn current() -> Vec<(String, String)> {
vec![("policy.txt".to_owned(), "accepted".to_owned())]
}
fn authority() -> AuthorityStanding {
AuthorityStanding::in_force("policy:control")
}
#[test]
fn a_forbidden_predicate_is_satisfied_when_it_does_not_hold() {
let forbid = rule("no-render", Polarity::Forbid);
assert_eq!(
forbid.decide(
&subject(),
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::NotSatisfied)
),
RuleOutcome::Satisfied,
"the object no longer provides the capability, so the prohibition holds"
);
assert_eq!(
forbid.decide(
&subject(),
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::Violated,
"the object provides what the rule forbids, so the rule is violated"
);
let require = rule("keep-render", Polarity::Require);
assert_eq!(
require.decide(
&subject(),
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::Satisfied
);
assert_eq!(
require.decide(
&subject(),
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::NotSatisfied)
),
RuleOutcome::Violated
);
}
#[test]
fn unknown_is_unknown_for_both_polarities() {
for polarity in [Polarity::Require, Polarity::Forbid] {
let decided = rule("r", polarity).decide(
&subject(),
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Unknown(
"the source did not parse".to_owned(),
)),
);
assert_eq!(
decided,
RuleOutcome::Unknown {
reason: "the source did not parse".to_owned()
},
"{polarity:?} must not decide an unknown predicate"
);
assert!(!decided.is_satisfied());
}
let blind = rule("blind", Polarity::Require).decide(
&subject(),
¤t(),
Some(&authority()),
None,
None,
);
assert!(matches!(blind, RuleOutcome::Unknown { .. }), "{blind:?}");
}
#[test]
fn an_exception_exempts_its_scope_and_nothing_else() {
let mut exempt = rule("keep-render", Polarity::Require);
exempt.exceptions = vec![
RuleException {
scope: "control.object.button".to_owned(),
reason: "predates the provider".to_owned(),
authority: "policy:control".to_owned(),
},
RuleException {
scope: "control.object.slider".to_owned(),
reason: "no authority, so it covers nothing".to_owned(),
authority: " ".to_owned(),
},
];
let decided = exempt.decide(
&subject(),
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::NotSatisfied),
);
assert_eq!(
decided,
RuleOutcome::Exempt {
scope: "control.object.button".to_owned(),
authority: "policy:control".to_owned(),
reason: "predates the provider".to_owned(),
}
);
assert!(
!decided.is_satisfied(),
"an exemption is not proof that the requirement holds"
);
let mut unattributed_only = rule("keep-render", Polarity::Require);
unattributed_only.exceptions = vec![RuleException {
scope: "control.object.button".to_owned(),
reason: "nobody attributed this".to_owned(),
authority: " ".to_owned(),
}];
assert_eq!(
unattributed_only.decide(
&subject(),
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::NotSatisfied)
),
RuleOutcome::Violated,
"an unattributed exception covers nothing, even inside its own scope"
);
}
#[test]
fn a_lapsed_authority_stops_the_rule_from_constraining() {
let decided = rule("keep-render", Polarity::Require).decide(
&subject(),
¤t(),
Some(&AuthorityStanding::lapsed(
"policy:control",
"the bytes moved after the confirmation",
)),
None,
Some(&PredicateAnswer::Satisfied),
);
match decided {
RuleOutcome::NotConstraining { reason } => {
assert!(reason.contains("policy:control"), "{reason}");
assert!(reason.contains("no longer in force"), "{reason}");
}
other => panic!("a lapsed authority must not constrain: {other:?}"),
}
let unresolved = rule("keep-render", Polarity::Require).decide(
&subject(),
¤t(),
None,
None,
Some(&PredicateAnswer::Satisfied),
);
assert!(
matches!(unresolved, RuleOutcome::NotConstraining { .. }),
"an unresolved authority cannot be read as constraining: {unresolved:?}"
);
}
#[test]
fn missing_context_is_unknown_and_a_mismatch_is_not_applicable() {
let mut scoped = rule("keep-render", Polarity::Require);
scoped.scope.configurations = vec!["release".to_owned()];
let no_configuration = RuleSubject {
configuration: String::new(),
..subject()
};
assert!(
matches!(
scoped.decide(
&no_configuration,
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::Unknown { .. }
),
"a rule accepted for `release` cannot be declared inapplicable when nobody named a configuration"
);
let debug = RuleSubject {
configuration: "debug".to_owned(),
..subject()
};
assert_eq!(
scoped.decide(
&debug,
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::NotApplicable {
reason: "the subject is not under a configuration this rule was accepted for",
}
);
let mut other_object = subject();
other_object.selector = "control.object.slider".to_owned();
assert!(matches!(
rule("keep-render", Polarity::Require).decide(
&other_object,
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::NotApplicable { .. }
));
let mut no_roles = subject();
no_roles.roles.clear();
assert!(matches!(
rule("keep-render", Polarity::Require).decide(
&no_roles,
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::Unknown { .. }
));
}
#[test]
fn a_changed_source_stops_the_rule_before_the_predicate() {
let moved = vec![("policy.txt".to_owned(), "changed".to_owned())];
let decided = rule("no-render", Polarity::Forbid).decide(
&subject(),
&moved,
Some(&authority()),
None,
Some(&PredicateAnswer::NotSatisfied),
);
match decided {
RuleOutcome::NotConstraining { reason } => {
assert!(reason.contains("changed"), "{reason}");
}
other => panic!("a stale rule must not answer met: {other:?}"),
}
}
#[test]
fn a_typed_contradiction_is_found_despite_different_wording() {
let require = rule("keep-render", Polarity::Require);
let mut forbid = rule("no-render", Polarity::Forbid);
forbid.requirement = "must not provide cap.render".to_owned();
let reason = conflict_reason(&require, &forbid).expect("the same typed expectation conflicts");
assert!(
reason.contains("keep-render") && reason.contains("no-render"),
"{reason}"
);
assert!(require.contradicts(&forbid));
let mut unrelated = rule("other", Polarity::Forbid);
unrelated.expects = Some(("provides".to_owned(), "cap.paint".to_owned()));
unrelated.requirement = "must not provide cap.paint".to_owned();
assert!(
!require.contradicts(&unrelated),
"different typed expectations are not a conflict"
);
}
#[test]
fn a_bound_rule_asks_the_identity_not_the_spelling() {
use super::super::{RuleBinding, RuleSubject as Subject};
use crate::identity::StableFaceId;
let mut bound = rule("keep-render", Polarity::Require);
bound.object = Some(RuleBinding {
stable: StableFaceId::from_name("fixture-provider"),
path: "root/panel/provider".to_owned(),
source: "src/panel/provider/provider.rs".to_owned(),
});
let impostor = Subject {
selector: "root/panel/provider".to_owned(),
object: Some(StableFaceId::from_name("somebody-else")),
..subject()
};
assert_eq!(
bound.decide(
&impostor,
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::NotApplicable {
reason: "the subject is a different object than the one this rule is bound to",
}
);
let moved = Subject {
selector: "root/elsewhere/provider".to_owned(),
object: Some(StableFaceId::from_name("fixture-provider")),
..subject()
};
assert_eq!(
bound.decide(
&moved,
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::Satisfied
);
let unknown = Subject {
selector: "root/panel/provider".to_owned(),
object: None,
..subject()
};
assert!(matches!(
bound.decide(
&unknown,
¤t(),
Some(&authority()),
None,
Some(&PredicateAnswer::Satisfied)
),
RuleOutcome::Unknown { .. }
));
}
#[test]
fn a_conflicted_rule_needs_review() {
let decided = rule("keep-render", Polarity::Require).decide(
&subject(),
¤t(),
Some(&authority()),
Some("`keep-render` and `no-render` contradict each other"),
Some(&PredicateAnswer::Satisfied),
);
assert!(
matches!(decided, RuleOutcome::NeedsReview { .. }),
"{decided:?}"
);
}