Skip to content

Commit

Permalink
TMP: add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
ol-imorozko committed Sep 3, 2024
1 parent 1afe262 commit 5abcb27
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions controlplane/acl_value.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "acl_value.h"

#include "common/variant.h"

using namespace acl::compiler;

value_t::value_t()
Expand Down
133 changes: 133 additions & 0 deletions controlplane/unittest/acl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,137 @@ add allow ip from any to any keep-state
}
}

/* [DEBUG] 1723559422.729562 ../controlplane/acl_total_table.cpp:56: Processing group 1: */
/* [DEBUG] 1723559422.729563 ../controlplane/acl_total_table.cpp:63: Filling key {1, 1} */
/* [DEBUG] 1723559422.729564 ../controlplane/acl_total_table.cpp:70: Adding rule 0 */
/* [DEBUG] 1723559422.729567 ../controlplane/acl_total_table.cpp:77: That rule was not terminating, so continue */
/* [DEBUG] 1723559422.729568 ../controlplane/acl_total_table.cpp:84: Appending rule 1 */
/* [DEBUG] 1723559422.729569 ../controlplane/acl_total_table.cpp:84: Appending rule 2 */
/* [DEBUG] 1723559422.729570 ../controlplane/acl_total_table.cpp:88: That rule was terminating, so break from processing for this acl_id and group */
/* [INFO] 1723559422.729571 ../controlplane/acl_compiler.cpp:486: acl::compile: size: 1 */
/* [INFO] 1723559422.729572 ../controlplane/acl_compiler.cpp:54: acl::compile: value_compile */
/* [INFO] 1723559422.729573 ../controlplane/acl_compiler.cpp:493: acl::compile: size: 1 */
/* [INFO] 1723559422.729574 ../controlplane/acl_compiler.cpp:57: acl::compile: result */
/* [INFO] 1723559422.729576 ../controlplane/acl_compiler.cpp:107: acl::compile: done */

/* ../controlplane/unittest/acl.cpp:736: Failure */
/* Value of: actions.get_actions()[0].raw_action.index() */
/* Expected: is equal to 0 */
/* Actual: 3 (of type unsigned long) */
/* unknown file: Failure */
/* C++ exception with description "std::get: wrong index for variant" thrown in the test body. */

TEST(ACL, StateTimeout_Basic)
{
auto fw = make_default_acl(R"IPFW(
:BEGIN
add state-timeout 5000 ip from any to any
add allow ip from any to any keep-state
add deny ip from any to any
)IPFW");

std::map<std::string, controlplane::base::acl_t> acls{{"acl0", std::move(fw)}};
acl::result_t result;
acl::compile(acls, {{1, {{true, "vlan1"}}}}, result);

ASSERT_EQ(result.acl_total_table.size(), 1);

for (const auto& [total_table_key, total_table_value] : result.acl_total_table)
{
(void)total_table_key;

const auto& value = result.acl_values[total_table_value];
std::visit([&](const auto& actions) {
if constexpr (std::is_same_v<std::decay_t<decltype(actions)>, common::BaseActions<true>>)
{
// Check that the regular path includes the allow action and state-timeout action
EXPECT_THAT(actions.get_actions().size(), 2);
EXPECT_THAT(actions.get_actions()[0].raw_action.index(), ::testing::Eq(FlowIndex));

const auto& stateTimeoutAction = std::get<common::StateTimeoutAction>(actions.get_actions()[1].raw_action);
EXPECT_EQ(stateTimeoutAction.timeout, 5000);

// Check that the check-state path includes the check-state action and state-timeout action
EXPECT_THAT(actions.get_check_state_actions().size(), 2);
EXPECT_THAT(actions.get_check_state_actions()[0].raw_action.index(), ::testing::Eq(CheckStateIndex));

const auto& checkStateTimeoutAction = std::get<common::StateTimeoutAction>(actions.get_check_state_actions()[1].raw_action);
EXPECT_EQ(checkStateTimeoutAction.timeout, 5000);
}
},
value);
}
}

/* [DEBUG] 1723559422.916811 ../controlplane/acl_total_table.cpp:56: Processing group 1: */
/* [DEBUG] 1723559422.916812 ../controlplane/acl_total_table.cpp:63: Filling key {1, 1} */
/* [DEBUG] 1723559422.916813 ../controlplane/acl_total_table.cpp:70: Adding rule 0 */
/* [DEBUG] 1723559422.916816 ../controlplane/acl_total_table.cpp:77: That rule was not terminating, so continue */
/* [DEBUG] 1723559422.916818 ../controlplane/acl_total_table.cpp:84: Appending rule 2 */
/* [DEBUG] 1723559422.916819 ../controlplane/acl_total_table.cpp:84: Appending rule 3 */
/* [DEBUG] 1723559422.916819 ../controlplane/acl_total_table.cpp:88: That rule was terminating, so break from processing for this acl_id and group */
/* [DEBUG] 1723559422.916820 ../controlplane/acl_total_table.cpp:56: Processing group 2: */
/* [DEBUG] 1723559422.916821 ../controlplane/acl_total_table.cpp:63: Filling key {2, 1} */
/* [DEBUG] 1723559422.916822 ../controlplane/acl_total_table.cpp:70: Adding rule 0 */
/* [DEBUG] 1723559422.916823 ../controlplane/acl_total_table.cpp:77: That rule was not terminating, so continue */
/* [DEBUG] 1723559422.916824 ../controlplane/acl_total_table.cpp:84: Appending rule 1 */
/* [DEBUG] 1723559422.916825 ../controlplane/acl_total_table.cpp:84: Appending rule 2 */
/* [DEBUG] 1723559422.916825 ../controlplane/acl_total_table.cpp:84: Appending rule 3 */
/* [DEBUG] 1723559422.916826 ../controlplane/acl_total_table.cpp:88: That rule was terminating, so break from processing for this acl_id and group */
/* [INFO] 1723559422.916827 ../controlplane/acl_compiler.cpp:486: acl::compile: size: 2 */
/* [INFO] 1723559422.916828 ../controlplane/acl_compiler.cpp:54: acl::compile: value_compile */
/* [INFO] 1723559422.916829 ../controlplane/acl_compiler.cpp:493: acl::compile: size: 2 */
/* [INFO] 1723559422.916830 ../controlplane/acl_compiler.cpp:57: acl::compile: result */
/* [INFO] 1723559422.916832 ../controlplane/acl_compiler.cpp:107: acl::compile: done */
/* ../controlplane/unittest/acl.cpp:779: Failure */
/* Value of: actions.get_actions()[0].raw_action.index() */
/* Expected: is equal to 0 */
/* Actual: 3 (of type unsigned long) */
/* unknown file: Failure */
/* C++ exception with description "std::get: wrong index for variant" thrown in the test body. */


TEST(ACL, StateTimeout_RestrictiveSubset)
{
auto fw = make_default_acl(R"IPFW(
:BEGIN
add state-timeout 8000 ip from any to any
add state-timeout 3000 ip from 192.168.1.0/24 to any
add allow ip from any to any keep-state
add deny ip from any to any
)IPFW");

std::map<std::string, controlplane::base::acl_t> acls{{"acl0", std::move(fw)}};
acl::result_t result;
acl::compile(acls, {{1, {{true, "vlan1"}}}}, result);

ASSERT_EQ(result.acl_total_table.size(), 2);

for (const auto& [total_table_key, total_table_value] : result.acl_total_table)
{
(void)total_table_key;

const auto& value = result.acl_values[total_table_value];
std::visit([&](const auto& actions) {
if constexpr (std::is_same_v<std::decay_t<decltype(actions)>, common::BaseActions<true>>)
{
// Check that the regular path includes the allow action and the correct state-timeout action
EXPECT_THAT(actions.get_actions().size(), 2);
EXPECT_THAT(actions.get_actions()[0].raw_action.index(), ::testing::Eq(FlowIndex));

const auto& stateTimeoutAction = std::get<common::StateTimeoutAction>(actions.get_actions()[1].raw_action);
EXPECT_EQ(stateTimeoutAction.timeout, 3000); // Verify that the timeout for 192.168.1.0/24 is 3000

// Check that the check-state path includes the check-state action and the correct state-timeout action
EXPECT_THAT(actions.get_check_state_actions().size(), 2);
EXPECT_THAT(actions.get_check_state_actions()[0].raw_action.index(), ::testing::Eq(CheckStateIndex));

const auto& checkStateTimeoutAction = std::get<common::StateTimeoutAction>(actions.get_check_state_actions()[1].raw_action);
EXPECT_EQ(checkStateTimeoutAction.timeout, 3000); // Again, verify it's 3000 for the specific subset
}
},
value);
}
}

} // namespace

0 comments on commit 5abcb27

Please sign in to comment.