Skip to content

Commit

Permalink
test: Adding unit test for swssnet.h (sonic-net#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiluo-msft authored and stcheng committed Sep 16, 2016
1 parent 3a68a96 commit 41e3a60
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ SUBDIRS = fpmsyncd neighsyncd intfsyncd portsyncd orchagent swssconfig
if HAVE_LIBTEAM
SUBDIRS += teamsyncd
endif

if GTEST
SUBDIRS += tests
endif
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ AC_ARG_ENABLE(debug,
esac],[debug=false])
AM_CONDITIONAL(DEBUG, test x$debug = xtrue)

AC_ARG_ENABLE(gtest,
[ --enable-gtest Compile with googletest flags],
[case "${enableval}" in
yes) gtest=true ;;
no) gtest=false ;;
*) AC_MSG_ERROR(bad value ${enableval} for --enable-gtest) ;;
esac],[gtest=false])
AM_CONDITIONAL(GTEST, test x$gtest = xtrue)

CFLAGS_COMMON="-std=c++11 -Wall -fPIC -Wno-write-strings -I/usr/include/libnl3 -I/usr/include/swss"
AC_SUBST(CFLAGS_COMMON)
Expand All @@ -42,6 +50,7 @@ AC_CONFIG_FILES([
portsyncd/Makefile
teamsyncd/Makefile
swssconfig/Makefile
tests/Makefile
])

AC_OUTPUT
4 changes: 2 additions & 2 deletions fpmsyncd/routesync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void RouteSync::onMsg(int nlmsg_type, struct nl_object *obj)
{
case RTN_BLACKHOLE:
{
std::vector<FieldValueTuple> fvVector;
vector<FieldValueTuple> fvVector;
FieldValueTuple fv("blackhole", "true");
fvVector.push_back(fv);
m_routeTable.set(destipprefix, fvVector);
Expand Down Expand Up @@ -114,7 +114,7 @@ void RouteSync::onMsg(int nlmsg_type, struct nl_object *obj)
}
}

std::vector<FieldValueTuple> fvVector;
vector<FieldValueTuple> fvVector;
FieldValueTuple nh("nexthop", nexthops);
FieldValueTuple idx("ifname", ifnames);
fvVector.push_back(nh);
Expand Down
20 changes: 20 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CFLAGS_SAI = -I /usr/include/sai
INCLUDES = -I ../orchagent

bin_PROGRAMS = tests

if DEBUG
DBGFLAGS = -ggdb -DDEBUG
else
DBGFLAGS = -g -DNDEBUG
endif

CFLAGS_GTEST =
LDADD_GTEST =

tests_SOURCES = swssnet_ut.cpp

tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI)
tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI)
tests_LDADD = $(LDADD_GTEST) -lnl-genl-3 -lhiredis -lhiredis -lpthread \
-lswsscommon -lswsscommon -lgtest -lgtest_main
118 changes: 118 additions & 0 deletions tests/swssnet_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <arpa/inet.h>
#include <gtest/gtest.h>
#include <string>
#include <iostream>
#include "swssnet.h"

using namespace std;
using namespace swss;

TEST(swssnet, copy1_v6)
{
IpAddress ip("2001:4898:f0:f153:357c:77b2:49c9:627c");
sai_ip_address_t dst;
copy(dst, ip);
EXPECT_EQ(dst.addr_family, SAI_IP_ADDR_FAMILY_IPV6);

char buf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, dst.addr.ip6, buf, INET6_ADDRSTRLEN);
EXPECT_STREQ(buf, "2001:4898:f0:f153:357c:77b2:49c9:627c");
}

TEST(swssnet, copy1_v4)
{
IpAddress ip("10.23.45.126");
sai_ip_address_t dst;
copy(dst, ip);
EXPECT_EQ(dst.addr_family, SAI_IP_ADDR_FAMILY_IPV4);

char buf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET, &dst.addr.ip4, buf, INET_ADDRSTRLEN);
EXPECT_STREQ(buf, "10.23.45.126");
}

TEST(swssnet, copy2_v6)
{
IpPrefix ip("2001:4898:f0:f153:357c:77b2:49c9:627c/27");
sai_ip_prefix_t dst;
copy(dst, ip);
EXPECT_EQ(dst.addr_family, SAI_IP_ADDR_FAMILY_IPV6);

char buf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, dst.addr.ip6, buf, INET6_ADDRSTRLEN);
EXPECT_STREQ(buf, "2001:4898:f0:f153:357c:77b2:49c9:627c");
inet_ntop(AF_INET6, dst.mask.ip6, buf, INET6_ADDRSTRLEN);
EXPECT_STREQ(buf, "ffff:ffe0::");
}

TEST(swssnet, copy2_v4)
{
IpPrefix ip("10.23.45.126/31");
sai_ip_prefix_t dst;
copy(dst, ip);
EXPECT_EQ(dst.addr_family, SAI_IP_ADDR_FAMILY_IPV4);

char buf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET, &dst.addr.ip4, buf, INET_ADDRSTRLEN);
EXPECT_STREQ(buf, "10.23.45.126");
inet_ntop(AF_INET, &dst.mask.ip4, buf, INET_ADDRSTRLEN);
EXPECT_STREQ(buf, "255.255.255.254");
}

TEST(swssnet, copy3_v6)
{
IpAddress ip("2001:4898:f0:f153:357c:77b2:49c9:627c");
sai_ip_prefix_t dst;
copy(dst, ip);
EXPECT_EQ(dst.addr_family, SAI_IP_ADDR_FAMILY_IPV6);

char buf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, dst.addr.ip6, buf, INET6_ADDRSTRLEN);
EXPECT_STREQ(buf, "2001:4898:f0:f153:357c:77b2:49c9:627c");
inet_ntop(AF_INET6, dst.mask.ip6, buf, INET6_ADDRSTRLEN);
EXPECT_STREQ(buf, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
}

TEST(swssnet, copy3_v4)
{
IpAddress ip("10.23.45.126");
sai_ip_prefix_t dst;
copy(dst, ip);
EXPECT_EQ(dst.addr_family, SAI_IP_ADDR_FAMILY_IPV4);

char buf[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &dst.addr.ip4, buf, INET_ADDRSTRLEN);
EXPECT_STREQ(buf, "10.23.45.126");
inet_ntop(AF_INET, &dst.mask.ip4, buf, INET_ADDRSTRLEN);
EXPECT_STREQ(buf, "255.255.255.255");
}

TEST(swssnet, subnet_v6)
{
sai_ip_prefix_t dst, src;
src.addr_family = SAI_IP_ADDR_FAMILY_IPV6;
inet_pton(AF_INET6, "2001:4898:f0:f153:357c:77b2:49c9:627c", src.addr.ip6);
inet_pton(AF_INET6, "ffff:ffe0::", src.mask.ip6);

subnet(dst, src);
char buf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, dst.addr.ip6, buf, INET6_ADDRSTRLEN);
EXPECT_STREQ(buf, "2001:4880::");
inet_ntop(AF_INET6, dst.mask.ip6, buf, INET6_ADDRSTRLEN);
EXPECT_STREQ(buf, "ffff:ffe0::");
}

TEST(swssnet, subnet_v4)
{
sai_ip_prefix_t dst, src;
src.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
inet_pton(AF_INET, "10.23.45.126", &src.addr.ip4);
inet_pton(AF_INET, "255.254.0.0", &src.mask.ip4);

subnet(dst, src);
char buf[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &dst.addr.ip4, buf, INET_ADDRSTRLEN);
EXPECT_STREQ(buf, "10.22.0.0");
inet_ntop(AF_INET, &dst.mask.ip4, buf, INET_ADDRSTRLEN);
EXPECT_STREQ(buf, "255.254.0.0");
}

0 comments on commit 41e3a60

Please sign in to comment.