Skip to content

Commit

Permalink
fs: Test try_compat_children()
Browse files Browse the repository at this point in the history
Add path_beneath_try_compat_children() tests to check error ordering
with both children and inner errors.  This enables us to identify the
behavior change introduced with the upcoming error priority change.

Signed-off-by: Mickaël Salaün <[email protected]>
  • Loading branch information
l0kod committed Jun 3, 2024
1 parent 35b016c commit 1149dd4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
38 changes: 36 additions & 2 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,49 @@ where
}
}

#[test]
fn path_beneath_try_compat_children() {
use crate::*;

// AccessFs::Refer is not handled by ABI::V1 and only for directories.
let access_file = AccessFs::ReadFile | AccessFs::Refer;

// Test error ordering with ABI::V1
let mut ruleset = Ruleset::from(ABI::V1).handle_access(access_file).unwrap();
// Do not actually perform any syscall.
ruleset.compat.state = CompatState::Dummy;
assert!(matches!(
RulesetCreated::new(ruleset, -1)
.set_compatibility(CompatLevel::HardRequirement)
.add_rule(PathBeneath::new(PathFd::new("/dev/null").unwrap(), access_file))
.unwrap_err(),
RulesetError::AddRules(AddRulesError::Fs(AddRuleError::Compat(
CompatError::Access(AccessError::PartiallyCompatible { access, incompatible }))
)) if access == access_file && incompatible == AccessFs::Refer
));

// Test error ordering with ABI::V2
let mut ruleset = Ruleset::from(ABI::V2).handle_access(access_file).unwrap();
// Do not actually perform any syscall.
ruleset.compat.state = CompatState::Dummy;
assert!(matches!(
RulesetCreated::new(ruleset, -1)
.set_compatibility(CompatLevel::HardRequirement)
.add_rule(PathBeneath::new(PathFd::new("/dev/null").unwrap(), access_file))
.unwrap_err(),
RulesetError::AddRules(AddRulesError::Fs(AddRuleError::Compat(
CompatError::PathBeneath(PathBeneathError::DirectoryAccess { access, incompatible })
))) if access == access_file && incompatible == AccessFs::Refer
));
}

#[test]
fn path_beneath_try_compat() {
use crate::*;

let abi = ABI::V1;

for file in &["/etc/passwd", "/dev/null"] {
// TODO: test try_compat_children

let mut compat_state = CompatState::Init;
let ro_access = AccessFs::ReadDir | AccessFs::ReadFile;
assert!(matches!(
Expand Down
2 changes: 1 addition & 1 deletion src/ruleset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ pub struct RulesetCreated {
}

impl RulesetCreated {
fn new(ruleset: Ruleset, fd: RawFd) -> Self {
pub(crate) fn new(ruleset: Ruleset, fd: RawFd) -> Self {
// The compatibility state is initialized by Ruleset::create().
#[cfg(test)]
assert!(!matches!(ruleset.compat.state, CompatState::Init));
Expand Down

0 comments on commit 1149dd4

Please sign in to comment.