Skip to content

Commit

Permalink
Add FdbInfo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Aug 23, 2021
1 parent 7ed640d commit c6e7ac0
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 2 deletions.
3 changes: 2 additions & 1 deletion unittest/vslib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
194 changes: 194 additions & 0 deletions unittest/vslib/TestFdbInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#include "FdbInfo.h"

#include <linux/if.h>

#include <gtest/gtest.h>

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);

}
3 changes: 2 additions & 1 deletion vslib/FdbInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ FdbInfo::FdbInfo()

m_bridgePortId = SAI_NULL_OBJECT_ID;

m_timestamp = 0;

memset(&m_fdbEntry, 0, sizeof(m_fdbEntry));
}

Expand Down Expand Up @@ -61,7 +63,6 @@ uint32_t FdbInfo::getTimestamp() const
return m_timestamp;
}


// TODO move to meta serialize
// requires to move FdbInfo to common

Expand Down

0 comments on commit c6e7ac0

Please sign in to comment.