forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Adding unit test for swssnet.h (sonic-net#82)
- Loading branch information
1 parent
3a68a96
commit 41e3a60
Showing
5 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |