From 724ce74246f7d9649c604cbe0791dd0eaf457f9f Mon Sep 17 00:00:00 2001 From: kcudnik Date: Fri, 20 Aug 2021 09:58:39 +0200 Subject: [PATCH] Add TrafficFilterPipes tests --- unittest/vslib/Makefile.am | 4 +- unittest/vslib/TestTrafficFilterPipes.cpp | 56 +++++++++++++++++++++++ vslib/TrafficFilterPipes.cpp | 10 ++-- 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 unittest/vslib/TestTrafficFilterPipes.cpp diff --git a/unittest/vslib/Makefile.am b/unittest/vslib/Makefile.am index 420ec80e0683..e03533aa4e68 100644 --- a/unittest/vslib/Makefile.am +++ b/unittest/vslib/Makefile.am @@ -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 \ diff --git a/unittest/vslib/TestTrafficFilterPipes.cpp b/unittest/vslib/TestTrafficFilterPipes.cpp new file mode 100644 index 000000000000..c7ed68ea2d99 --- /dev/null +++ b/unittest/vslib/TestTrafficFilterPipes.cpp @@ -0,0 +1,56 @@ +#include "TrafficFilterPipes.h" +#include "MACsecIngressFilter.h" + + +#include + +using namespace saivs; + +TEST(TrafficFilterPipes, uninstallFilter) +{ + TrafficFilterPipes p; + + EXPECT_FALSE(p.uninstallFilter(nullptr)); + + auto filter = std::make_shared("foo"); + auto filter2 = std::make_shared("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("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("foo"); + + EXPECT_TRUE(p.installFilter(0, filter)); + + EXPECT_EQ(p.execute(buf, len), TrafficFilter::TERMINATE); +} diff --git a/vslib/TrafficFilterPipes.cpp b/vslib/TrafficFilterPipes.cpp index 8d7b8f855978..0a85ffd45580 100644 --- a/vslib/TrafficFilterPipes.cpp +++ b/vslib/TrafficFilterPipes.cpp @@ -4,24 +4,24 @@ using namespace saivs; +#define MUTEX std::unique_lock guard(m_mutex) + bool TrafficFilterPipes::installFilter( _In_ int priority, _In_ std::shared_ptr filter) { + MUTEX; SWSS_LOG_ENTER(); - std::unique_lock guard(m_mutex); - return m_filters.emplace(priority, filter).second; } bool TrafficFilterPipes::uninstallFilter( _In_ std::shared_ptr filter) { + MUTEX; SWSS_LOG_ENTER(); - std::unique_lock guard(m_mutex); - for (auto itr = m_filters.begin(); itr != m_filters.end(); itr ++) @@ -41,9 +41,9 @@ TrafficFilter::FilterStatus TrafficFilterPipes::execute( _Inout_ void *buffer, _Inout_ size_t &length) { + MUTEX; SWSS_LOG_ENTER(); - std::unique_lock guard(m_mutex); TrafficFilter::FilterStatus ret = TrafficFilter::CONTINUE; for (auto itr = m_filters.begin(); itr != m_filters.end();)