diff --git a/unittest/vslib/Makefile.am b/unittest/vslib/Makefile.am index afca95e255f9..dd872b524365 100644 --- a/unittest/vslib/Makefile.am +++ b/unittest/vslib/Makefile.am @@ -12,7 +12,8 @@ tests_SOURCES = main.cpp \ TestCorePortIndexMapFileParser.cpp \ TestEventPayloadNetLinkMsg.cpp \ TestEventPayloadPacket.cpp \ - TestEventQueue.cpp + TestEventQueue.cpp \ + TestFdbInfo.cpp tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/vslib/libSaiVS.a -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS) diff --git a/unittest/vslib/TestFdbInfo.cpp b/unittest/vslib/TestFdbInfo.cpp new file mode 100644 index 000000000000..4089b2c703e5 --- /dev/null +++ b/unittest/vslib/TestFdbInfo.cpp @@ -0,0 +1,194 @@ +#include "FdbInfo.h" + +#include + +#include + +using namespace saivs; + +TEST(FdbInfo, getPortId) +{ + FdbInfo fdb; + + EXPECT_EQ(fdb.getPortId(), 0); +} + +TEST(FdbInfo, getVlanId) +{ + FdbInfo fdb; + + EXPECT_EQ(fdb.getVlanId(), 0); +} + +TEST(FdbInfo, getBridgePortId) +{ + FdbInfo fdb; + + EXPECT_EQ(fdb.getBridgePortId(), 0); +} + +TEST(FdbInfo, getFdbEntry) +{ + FdbInfo fdb; + + auto entry = fdb.getFdbEntry(); + + EXPECT_EQ(entry.switch_id, 0); +} + +TEST(FdbInfo, getTimestamp) +{ + FdbInfo fdb; + + EXPECT_EQ(fdb.getTimestamp(), 0); +} + +TEST(FdbInfo, serialize) +{ + FdbInfo fdb; + + auto str = fdb.serialize(); + + EXPECT_EQ(str, + "{\"bridge_port_id\":\"oid:0x0\"," + "\"fdb_entry\":\"{\\\"bvid\\\":\\\"oid:0x0\\\",\\\"mac\\\":\\\"00:00:00:00:00:00\\\",\\\"switch_id\\\":\\\"oid:0x0\\\"}\"," + "\"port_id\":\"oid:0x0\"," + "\"timestamp\":\"0\"," + "\"vlan_id\":\"0\"}"); +} + +TEST(FdbInfo, deserialize) +{ + std::string str = + "{\"bridge_port_id\":\"oid:0x1\"," + "\"fdb_entry\":\"{\\\"bvid\\\":\\\"oid:0x0\\\",\\\"mac\\\":\\\"00:00:00:00:00:00\\\",\\\"switch_id\\\":\\\"oid:0x0\\\"}\"," + "\"port_id\":\"oid:0x0\"," + "\"timestamp\":\"0\"," + "\"vlan_id\":\"0\"}"; + + auto fdb = FdbInfo::deserialize(str); + + EXPECT_EQ(fdb.getBridgePortId(), 1); +} + +TEST(FdbInfo, setFdbEntry) +{ + FdbInfo fdb; + + sai_fdb_entry_t entry; + + entry.switch_id = 2; + + fdb.setFdbEntry(entry); + + auto en = fdb.getFdbEntry(); + + EXPECT_EQ(en.switch_id, 2); +} + +TEST(FdbInfo, setVlanId) +{ + FdbInfo info; + + info.setVlanId(7); + + EXPECT_EQ(info.getVlanId(), 7); +} + +TEST(FdbInfo, setPortId) +{ + FdbInfo info; + + info.setPortId(7); + + EXPECT_EQ(info.getPortId(), 7); +} + +TEST(FdbInfo, setBridgePortId) +{ + FdbInfo info; + + info.setBridgePortId(7); + + EXPECT_EQ(info.getBridgePortId(), 7); +} + +TEST(FdbInfo, setTimestamp) +{ + FdbInfo info; + + info.setTimestamp(7); + + EXPECT_EQ(info.getTimestamp(), 7); +} + +TEST(FdbInfo, operator_bracket) +{ + std::string strA = + "{\"bridge_port_id\":\"oid:0x1\"," + "\"fdb_entry\":\"{\\\"bvid\\\":\\\"oid:0x0\\\",\\\"mac\\\":\\\"00:00:00:00:00:00\\\",\\\"switch_id\\\":\\\"oid:0x0\\\"}\"," + "\"port_id\":\"oid:0x0\"," + "\"timestamp\":\"0\"," + "\"vlan_id\":\"0\"}"; + + std::string strB = + "{\"bridge_port_id\":\"oid:0x1\"," + "\"fdb_entry\":\"{\\\"bvid\\\":\\\"oid:0x0\\\",\\\"mac\\\":\\\"00:00:00:00:00:01\\\",\\\"switch_id\\\":\\\"oid:0x0\\\"}\"," + "\"port_id\":\"oid:0x0\"," + "\"timestamp\":\"0\"," + "\"vlan_id\":\"0\"}"; + + auto a = FdbInfo::deserialize(strA); + auto b = FdbInfo::deserialize(strB); + + EXPECT_EQ(a.operator()(a,b), true); + EXPECT_EQ(a.operator()(b,a), false); +} + +TEST(FdbInfo, operator_lt) +{ + std::string strA = + "{\"bridge_port_id\":\"oid:0x1\"," + "\"fdb_entry\":\"{\\\"bvid\\\":\\\"oid:0x0\\\",\\\"mac\\\":\\\"00:00:00:00:00:00\\\",\\\"switch_id\\\":\\\"oid:0x0\\\"}\"," + "\"port_id\":\"oid:0x0\"," + "\"timestamp\":\"0\"," + "\"vlan_id\":\"0\"}"; + + std::string strB = + "{\"bridge_port_id\":\"oid:0x1\"," + "\"fdb_entry\":\"{\\\"bvid\\\":\\\"oid:0x0\\\",\\\"mac\\\":\\\"00:00:00:00:00:01\\\",\\\"switch_id\\\":\\\"oid:0x0\\\"}\"," + "\"port_id\":\"oid:0x0\"," + "\"timestamp\":\"0\"," + "\"vlan_id\":\"0\"}"; + + auto a = FdbInfo::deserialize(strA); + auto b = FdbInfo::deserialize(strB); + + EXPECT_EQ(a < b, true); + EXPECT_EQ(b < a, false); + EXPECT_EQ(a < a, false); + EXPECT_EQ(b < b, false); + + std::string strC = + "{\"bridge_port_id\":\"oid:0x1\"," + "\"fdb_entry\":\"{\\\"bvid\\\":\\\"oid:0x0\\\",\\\"mac\\\":\\\"00:00:00:00:00:00\\\",\\\"switch_id\\\":\\\"oid:0x0\\\"}\"," + "\"port_id\":\"oid:0x0\"," + "\"timestamp\":\"0\"," + "\"vlan_id\":\"1\"}"; + + std::string strD = + "{\"bridge_port_id\":\"oid:0x1\"," + "\"fdb_entry\":\"{\\\"bvid\\\":\\\"oid:0x0\\\",\\\"mac\\\":\\\"00:00:00:00:00:00\\\",\\\"switch_id\\\":\\\"oid:0x0\\\"}\"," + "\"port_id\":\"oid:0x0\"," + "\"timestamp\":\"0\"," + "\"vlan_id\":\"2\"}"; + + auto c = FdbInfo::deserialize(strC); + auto d = FdbInfo::deserialize(strD); + + EXPECT_EQ(c < d, true); + EXPECT_EQ(d < c, false); + EXPECT_EQ(c < c, false); + EXPECT_EQ(d < d, false); + +} diff --git a/vslib/FdbInfo.cpp b/vslib/FdbInfo.cpp index 73cabfbde067..e1d381827e28 100644 --- a/vslib/FdbInfo.cpp +++ b/vslib/FdbInfo.cpp @@ -23,6 +23,8 @@ FdbInfo::FdbInfo() m_bridgePortId = SAI_NULL_OBJECT_ID; + m_timestamp = 0; + memset(&m_fdbEntry, 0, sizeof(m_fdbEntry)); } @@ -61,7 +63,6 @@ uint32_t FdbInfo::getTimestamp() const return m_timestamp; } - // TODO move to meta serialize // requires to move FdbInfo to common