Skip to content

Commit

Permalink
Fix test compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilm3 committed Oct 20, 2023
1 parent 6aba4ec commit fbbcdb4
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 363 deletions.
2 changes: 1 addition & 1 deletion src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void context::eval_postprocessors(optional_ref<ddwaf_object> &derived, ddwaf::ti
}
}

const exclusion::context_policy &context::eval_filters(ddwaf::timer &deadline)
exclusion::context_policy &context::eval_filters(ddwaf::timer &deadline)
{
DDWAF_DEBUG("Evaluating rule filters");

Expand Down
2 changes: 1 addition & 1 deletion src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class context {
void eval_postprocessors(optional_ref<ddwaf_object> &derived, ddwaf::timer &deadline);
// This function below returns a reference to an internal object,
// however using them this way helps with testing
const exclusion::context_policy &eval_filters(ddwaf::timer &deadline);
exclusion::context_policy &eval_filters(ddwaf::timer &deadline);
std::vector<event> eval_rules(const exclusion::context_policy &policy, ddwaf::timer &deadline);

protected:
Expand Down
21 changes: 18 additions & 3 deletions src/exclusion/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ struct object_set {
std::unordered_set<const ddwaf_object *> persistent;
std::unordered_set<const ddwaf_object *> ephemeral;
bool empty() const { return persistent.empty() && ephemeral.empty(); }
[[nodiscard]] std::size_t size() const { return persistent.size() + ephemeral.size(); }

bool contains(const ddwaf_object *obj) const
{
return persistent.contains(obj) || ephemeral.contains(obj);
}
};

struct rule_policy {
Expand Down Expand Up @@ -61,10 +67,19 @@ struct rule_policy_ref {
};

struct context_policy {
std::unordered_map<rule *, rule_policy> persistent;
std::unordered_map<rule *, rule_policy> ephemeral;
std::unordered_map<const rule *, rule_policy> persistent;
std::unordered_map<const rule *, rule_policy> ephemeral;

[[nodiscard]] bool empty() const { return persistent.empty() && ephemeral.empty(); }

[[nodiscard]] std::size_t size() const { return persistent.size() + ephemeral.size(); }

bool contains(const rule *key) const
{
return persistent.contains(key) || ephemeral.contains(key);
}

rule_policy_ref find(rule *key) const
rule_policy_ref find(const rule *key) const
{
auto p_it = persistent.find(key);
auto e_it = ephemeral.find(key);
Expand Down
5 changes: 5 additions & 0 deletions src/exclusion/rule_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ std::optional<excluded_set> rule_filter::match(
{
DDWAF_DEBUG("Evaluating rule filter '%s'", id_.c_str());

// Don't return a match again if we already did
if (expression::get_result(cache)) {
return std::nullopt;
}

auto res = expr_->eval(cache, store, {}, {}, deadline);
if (!res.outcome) {
return std::nullopt;
Expand Down
34 changes: 17 additions & 17 deletions tests/collection_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TYPED_TEST(TestCollection, SingleRuleMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
rule_collection.match(events, store, cache, {}, {}, {}, deadline);
rule_collection.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 1);
}
Expand All @@ -60,7 +60,7 @@ TYPED_TEST(TestCollection, SingleRuleMatch)
store.insert(root);
std::vector<event> events;
ddwaf::timer deadline{2s};
rule_collection.match(events, store, cache, {}, {}, {}, deadline);
rule_collection.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 0);
}
Expand Down Expand Up @@ -111,7 +111,7 @@ TYPED_TEST(TestCollection, MultipleRuleCachedMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
rule_collection.match(events, store, cache, {}, {}, {}, deadline);
rule_collection.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 1);
}
Expand All @@ -125,7 +125,7 @@ TYPED_TEST(TestCollection, MultipleRuleCachedMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
rule_collection.match(events, store, cache, {}, {}, {}, deadline);
rule_collection.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 0);
}
Expand Down Expand Up @@ -176,7 +176,7 @@ TYPED_TEST(TestCollection, MultipleRuleFailAndMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
rule_collection.match(events, store, cache, {}, {}, {}, deadline);
rule_collection.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 0);
}
Expand All @@ -190,7 +190,7 @@ TYPED_TEST(TestCollection, MultipleRuleFailAndMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
rule_collection.match(events, store, cache, {}, {}, {}, deadline);
rule_collection.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 1);
}
Expand Down Expand Up @@ -225,7 +225,7 @@ TYPED_TEST(TestCollection, SingleRuleMultipleCalls)

std::vector<event> events;
ddwaf::timer deadline{2s};
rule_collection.match(events, store, cache, {}, {}, {}, deadline);
rule_collection.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 0);
}
Expand All @@ -241,7 +241,7 @@ TYPED_TEST(TestCollection, SingleRuleMultipleCalls)

std::vector<event> events;
ddwaf::timer deadline{2s};
rule_collection.match(events, store, cache, {}, {}, {}, deadline);
rule_collection.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 1);
}
Expand Down Expand Up @@ -294,7 +294,7 @@ TEST(TestPriorityCollection, NoRegularMatchAfterPriorityMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
priority.match(events, store, cache, {}, {}, {}, deadline);
priority.match(events, store, cache, {}, {}, deadline);

ASSERT_EQ(events.size(), 1);
ASSERT_EQ(events[0].rule->get_actions().size(), 1);
Expand All @@ -309,7 +309,7 @@ TEST(TestPriorityCollection, NoRegularMatchAfterPriorityMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
regular.match(events, store, cache, {}, {}, {}, deadline);
regular.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 0);
}
Expand Down Expand Up @@ -363,7 +363,7 @@ TEST(TestPriorityCollection, PriorityMatchAfterRegularMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
regular.match(events, store, cache, {}, {}, {}, deadline);
regular.match(events, store, cache, {}, {}, deadline);

EXPECT_EQ(events.size(), 1);
EXPECT_TRUE(events[0].rule->get_actions().empty());
Expand All @@ -378,7 +378,7 @@ TEST(TestPriorityCollection, PriorityMatchAfterRegularMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
priority.match(events, store, cache, {}, {}, {}, deadline);
priority.match(events, store, cache, {}, {}, deadline);

ASSERT_EQ(events.size(), 1);
ASSERT_EQ(events[0].rule->get_actions().size(), 1);
Expand Down Expand Up @@ -433,7 +433,7 @@ TEST(TestPriorityCollection, NoPriorityMatchAfterPriorityMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
priority.match(events, store, cache, {}, {}, {}, deadline);
priority.match(events, store, cache, {}, {}, deadline);

ASSERT_EQ(events.size(), 1);
ASSERT_EQ(events[0].rule->get_actions().size(), 1);
Expand All @@ -449,7 +449,7 @@ TEST(TestPriorityCollection, NoPriorityMatchAfterPriorityMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
priority.match(events, store, cache, {}, {}, {}, deadline);
priority.match(events, store, cache, {}, {}, deadline);

ASSERT_EQ(events.size(), 0);
}
Expand Down Expand Up @@ -504,7 +504,7 @@ TEST(TestPriorityCollection, NoPriorityMatchAfterEphemeralPriorityMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
priority.match(events, store, cache, {}, {}, {}, deadline);
priority.match(events, store, cache, {}, {}, deadline);

ASSERT_EQ(events.size(), 1);
ASSERT_EQ(events[0].rule->get_actions().size(), 1);
Expand All @@ -522,7 +522,7 @@ TEST(TestPriorityCollection, NoPriorityMatchAfterEphemeralPriorityMatch)

std::vector<event> events;
ddwaf::timer deadline{2s};
priority.match(events, store, cache, {}, {}, {}, deadline);
priority.match(events, store, cache, {}, {}, deadline);

ASSERT_EQ(events.size(), 1);
}
Expand Down Expand Up @@ -585,7 +585,7 @@ TEST(TestPriorityCollection, EphemeralPriorityMatchNoOtherMatches)
}

std::vector<event> events;
priority.match(events, store, cache, {}, {}, {}, deadline);
priority.match(events, store, cache, {}, {}, deadline);

ASSERT_EQ(events.size(), 1);
ASSERT_EQ(events[0].rule->get_actions().size(), 1);
Expand Down
Loading

0 comments on commit fbbcdb4

Please sign in to comment.