Skip to content

Commit

Permalink
Clarify unknown part message on rule parse (#845)
Browse files Browse the repository at this point in the history
Clarifies the unknown subject|object messages from the parser.

Closes #802
  • Loading branch information
jw3 authored Apr 10, 2023
1 parent 13b170b commit c9eb33d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/rules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
name = "fapolicy-rules"
description = "Rule support for fapolicyd"
license = "MPL-2.0"
version = "0.4.3"
version = "0.4.4"
edition = "2018"

[lib]
path = "src/lib.rs"

[dev-dependencies]
tempfile = "3.3"
assert_matches = "1.5"

[dependencies]
nom = "7.1"
Expand Down
3 changes: 2 additions & 1 deletion crates/rules/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ impl Display for RuleParseError<Trace<&str>> {
MissingSubject(_) => f.write_str("Missing Subject"),
MissingObject(_) => f.write_str("Expected Object"),
MissingBothSubjObj(_) => f.write_str("Missing Subject and Object"),
UnknownSubjectPart(_) | UnknownObjectPart(_) => f.write_str("Expected one of ....."),
UnknownSubjectPart(_) => f.write_str("Unrecognized Subject keyword"),
UnknownObjectPart(_) => f.write_str("Unrecognized Object keyword"),
SubjectPartExpected(_) => f.write_str("Expected a Subject part"),
ObjectPartExpected(_) => f.write_str("Expected an Object part"),
ExpectedInt(_) => f.write_str("Expected integer value"),
Expand Down
13 changes: 13 additions & 0 deletions crates/rules/src/parser/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub(crate) fn parse(i: StrTrace) -> TraceResult<Object> {
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;

#[test]
fn parse_obj_part() {
Expand All @@ -97,4 +98,16 @@ mod tests {
parse("trust=1 all".into()).ok().unwrap().1
);
}

#[test]
fn unknown_part() {
assert_matches!(
obj_part("dir=/tmp".into()).ok().map(|f| f.1),
Some(ObjPart::Dir(_))
);
assert_matches!(
obj_part("foo=/tmp".into()).err(),
Some(nom::Err::Error(UnknownObjectPart(_)))
);
}
}
13 changes: 13 additions & 0 deletions crates/rules/src/parser/subject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub(crate) fn parse(i: StrTrace) -> TraceResult<Subject> {
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;

#[test]
fn parse_subj_part() {
Expand All @@ -91,4 +92,16 @@ mod tests {
);
assert_eq!(SubjPart::Gid(0), subj_part("gid=0".into()).ok().unwrap().1);
}

#[test]
fn unknown_part() {
assert_matches!(
subj_part("uid=1".into()).ok().map(|f| f.1),
Some(SubjPart::Uid(_))
);
assert_matches!(
subj_part("foo=bar".into()).err(),
Some(nom::Err::Error(UnknownSubjectPart(_)))
);
}
}
4 changes: 2 additions & 2 deletions crates/rules/tests/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ fn test_file() -> Result<(), Box<dyn Error>> {
let db = read::deserialize_rules_db(expected)?;
let file = NamedTempFile::new()?;

write::db(&db, &file.path().to_path_buf())?;
let actual = read_string(&file.path().to_path_buf())?;
write::db(&db, file.path())?;
let actual = read_string(file.path())?;
assert_eq!(expected, actual.trim());

Ok(())
Expand Down
1 change: 1 addition & 0 deletions fapolicy-analyzer.spec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ BuildRequires: desktop-file-utils
BuildRequires: rust-packaging
BuildRequires: python3dist(setuptools-rust)

BuildRequires: rust-assert_matches-devel
BuildRequires: rust-autocfg-devel
BuildRequires: rust-bitflags-devel
BuildRequires: rust-bumpalo-devel
Expand Down

0 comments on commit c9eb33d

Please sign in to comment.