Skip to content

Commit

Permalink
Add TrafficFilterPipes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Aug 23, 2021
1 parent 03e4923 commit 724ce74
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
4 changes: 3 additions & 1 deletion unittest/vslib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ tests_SOURCES = main.cpp \
TestSignal.cpp \
TestSwitchConfigContainer.cpp \
TestTrafficForwarder.cpp \
TestHostInterfaceInfo.cpp
TestHostInterfaceInfo.cpp \
TestTrafficFilterPipes.cpp \
TestSwitchConfig.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/vslib/libSaiVS.a -lhiredis -lswsscommon -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 \
Expand Down
56 changes: 56 additions & 0 deletions unittest/vslib/TestTrafficFilterPipes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "TrafficFilterPipes.h"
#include "MACsecIngressFilter.h"


#include <gtest/gtest.h>

using namespace saivs;

TEST(TrafficFilterPipes, uninstallFilter)
{
TrafficFilterPipes p;

EXPECT_FALSE(p.uninstallFilter(nullptr));

auto filter = std::make_shared<MACsecIngressFilter>("foo");
auto filter2 = std::make_shared<MACsecIngressFilter>("foo");

EXPECT_TRUE(p.installFilter(0, filter));
EXPECT_TRUE(p.installFilter(1, filter2));

EXPECT_FALSE(p.uninstallFilter(nullptr));

EXPECT_TRUE(p.uninstallFilter(filter2));
}

TEST(TrafficFilterPipes, installFilter)
{
auto filter = std::make_shared<MACsecIngressFilter>("foo");

TrafficFilterPipes p;

EXPECT_TRUE(p.installFilter(0, filter));

EXPECT_FALSE(p.installFilter(0, filter));
}

TEST(TrafficFilterPipes, execute)
{
TrafficFilterPipes p;

uint8_t buf[2];
size_t len = 2;

EXPECT_EQ(p.execute(buf, len), TrafficFilter::CONTINUE);

EXPECT_TRUE(p.installFilter(0, nullptr));
EXPECT_TRUE(p.installFilter(1, nullptr));

EXPECT_EQ(p.execute(buf, len), TrafficFilter::CONTINUE);

auto filter = std::make_shared<MACsecIngressFilter>("foo");

EXPECT_TRUE(p.installFilter(0, filter));

EXPECT_EQ(p.execute(buf, len), TrafficFilter::TERMINATE);
}
10 changes: 5 additions & 5 deletions vslib/TrafficFilterPipes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

using namespace saivs;

#define MUTEX std::unique_lock<std::mutex> guard(m_mutex)

bool TrafficFilterPipes::installFilter(
_In_ int priority,
_In_ std::shared_ptr<TrafficFilter> filter)
{
MUTEX;
SWSS_LOG_ENTER();

std::unique_lock<std::mutex> guard(m_mutex);

return m_filters.emplace(priority, filter).second;
}

bool TrafficFilterPipes::uninstallFilter(
_In_ std::shared_ptr<TrafficFilter> filter)
{
MUTEX;
SWSS_LOG_ENTER();

std::unique_lock<std::mutex> guard(m_mutex);

for (auto itr = m_filters.begin();
itr != m_filters.end();
itr ++)
Expand All @@ -41,9 +41,9 @@ TrafficFilter::FilterStatus TrafficFilterPipes::execute(
_Inout_ void *buffer,
_Inout_ size_t &length)
{
MUTEX;
SWSS_LOG_ENTER();

std::unique_lock<std::mutex> guard(m_mutex);
TrafficFilter::FilterStatus ret = TrafficFilter::CONTINUE;

for (auto itr = m_filters.begin(); itr != m_filters.end();)
Expand Down

0 comments on commit 724ce74

Please sign in to comment.