Skip to content

Commit

Permalink
test: properly run against suite
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Mar 8, 2024
1 parent 80c66dd commit 81d2434
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
23 changes: 17 additions & 6 deletions crates/redos/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod tests {

if line.starts_with('\t') {
let mut line = line.to_string();
line.pop();
line.remove(0);
tests.last_mut().unwrap().1.push(line);
} else {
tests.push((line.to_string(), vec![]));
Expand All @@ -27,20 +27,31 @@ mod tests {
}

fn assert_safe(regex: &str, message: &str) {
let vulnerabilities = vulnerabilities(regex, &Default::default())
.map(|r| r.vulnerabilities);

assert!(
vulnerabilities.is_ok(),
"{} failed to get vulnerabilities: {}",
message,
regex
);

assert_eq!(
vulnerabilities(regex, &Default::default())
.unwrap()
.vulnerabilities,
vulnerabilities.unwrap(),
vec![],
"{} failed: {}",
"{} was not safe: {}",
message,
regex
);
}

#[test]
fn check_safe() {
for (name, tests) in parse_test_file(SAFE) {
let test_suite = parse_test_file(SAFE);
assert!(!test_suite.is_empty());
for (name, tests) in test_suite {
assert!(!tests.is_empty());
for test in tests {
assert_safe(&test, &name);
}
Expand Down
40 changes: 20 additions & 20 deletions crates/redos/tests/safe.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
base example
abc
abc
lets try out groups
(abc|def)|[nhi]?
(abc|def)|[nhi]?
tiny repeating string
a{1,10}
a{1,10}
bigger repeated string that's just right
a{}
a{}
nested group
(((a)))
(((a)))
try putting some quantifiers outside
((a+)+)+
((a+)+)+
are optionals detected?
(a?)+
(a?)+
lets try lookarounds
(?<=a)
(?<!a)
(?!a)
(?=a)
(?>a)
(?>a+)
(?<=a)
(?<!a)
(?!a)
(?=a)
(?>a)
(?>a+)
can we decompose alternations?
(a+)|(b+)|(((a)))
(a+)|(b+)|(((a)))
how about alternations in lookarounds?
(?<=a|b)
(?<!a|b)
(?!a|b)
(?=a|b)
(?>a|b)
(?>a+|b+)
(?<=a|b)
(?<!a|b)
(?!a|b)
(?=a|b)
(?>a|b)
(?>a+|b+)

0 comments on commit 81d2434

Please sign in to comment.