Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unittests] Add unittests for SwitchStateBaseHostif #1133

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meta/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ libsaimeta_la_SOURCES = \
SaiObjectCollection.cpp \
SaiSerialize.cpp \
SelectableChannel.cpp \
System.cpp \
ZeroMQSelectableChannel.cpp

libsaimeta_la_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS)
Expand Down
95 changes: 95 additions & 0 deletions meta/System.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "System.h"

#include "swss/logger.h"

#include <stdarg.h>

using namespace saimeta;

int (*System::open_ptr)(const char *pathname, int flags, ...) = nullptr;
int (*System::ioctl_ptr)(int fd, unsigned long request, ...) = nullptr;
int (*System::socket_ptr)(int domain, int type, int protocol) = nullptr;
int (*System::setsockopt_ptr)(int sockfd, int level, int optname, const void *optval, socklen_t optlen) = nullptr;
int (*System::bind_ptr)(int sockfd, const struct sockaddr *addr, socklen_t addrlen) = nullptr;
unsigned int (*System::if_nametoindex_ptr)(const char *ifname);

void System::reset()
{
SWSS_LOG_ENTER();

open_ptr = &::open;
ioctl_ptr = &::ioctl;
socket_ptr = &::socket;
setsockopt_ptr = &::setsockopt;
bind_ptr = &::bind;
}

int System::open(const char *pathname, int flags, ...)
{
SWSS_LOG_ENTER();

va_list args;

va_start(args, flags);

int ret = (open_ptr == nullptr)
? ::open(pathname, flags, args)
: open_ptr(pathname, flags, args);

va_end(args);

return ret;
}

int System::ioctl(int fd, unsigned long request, ...)
{
SWSS_LOG_ENTER();

va_list args;

va_start(args, request);

int ret = (ioctl_ptr == nullptr)
? ::ioctl(fd, request, args)
: ioctl_ptr(fd, request, args);

va_end(args);

return ret;
}

int System::socket(int domain, int type, int protocol)
{
SWSS_LOG_ENTER();

return (socket_ptr == nullptr)
? ::socket(domain, type, protocol)
: socket_ptr(domain, type, protocol);
}

int System::setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
{
SWSS_LOG_ENTER();

return (setsockopt_ptr == nullptr)
? ::setsockopt(sockfd, level, optname, optval, optlen)
: setsockopt_ptr(sockfd, level, optname, optval, optlen);
}

int System::bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
SWSS_LOG_ENTER();

return (bind_ptr == nullptr)
? ::bind(sockfd, addr, addrlen)
: bind_ptr(sockfd, addr, addrlen);
}

unsigned int System::if_nametoindex(const char *ifname)
{
SWSS_LOG_ENTER();

return (if_nametoindex_ptr == nullptr)
? ::if_nametoindex(ifname)
: if_nametoindex_ptr(ifname);
}
39 changes: 39 additions & 0 deletions meta/System.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <fcntl.h>

namespace saimeta
{
class System
{
private:

System() = delete;
~System() = delete;

public:

static void reset();

static int open(const char *pathname, int flags, ...);
static int ioctl(int fd, unsigned long request, ...);
static int socket(int domain, int type, int protocol);
static int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
static int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
static unsigned int if_nametoindex(const char *ifname);

public:

static int (*open_ptr)(const char *pathname, int flags, ...);
static int (*ioctl_ptr)(int fd, unsigned long request, ...);
static int (*socket_ptr)(int domain, int type, int protocol);
static int (*setsockopt_ptr)(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
static int (*bind_ptr)(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
static unsigned int (*if_nametoindex_ptr)(const char *ifname);
};
}
1 change: 1 addition & 0 deletions unittest/meta/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tests_SOURCES = \
TestLegacyRouteEntry.cpp \
TestLegacyOther.cpp \
TestZeroMQSelectableChannel.cpp \
TestSystem.cpp \
TestMeta.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
Expand Down
90 changes: 90 additions & 0 deletions unittest/meta/TestSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "System.h"

#include <gtest/gtest.h>

#include <memory>

using namespace saimeta;

TEST(System, reset)
{
System::reset();
}

TEST(System, open)
{
System::open_ptr = nullptr;

int ret = System::open("/f/o/o", O_RDWR);

EXPECT_TRUE(ret < 0);

System::open_ptr = &::open;

ret = System::open("/f/o/o", O_RDWR);

EXPECT_TRUE(ret < 0);
}

TEST(System, socket)
{
System::socket_ptr = nullptr;

int ret = System::socket(100,100,100);

EXPECT_TRUE(ret < 0);

System::socket_ptr = &::socket;

ret = System::socket(100,100,100);

EXPECT_TRUE(ret < 0);

}

TEST(System, ioctl)
{
System::ioctl_ptr = nullptr;

int ret = System::ioctl(-1,0);

EXPECT_TRUE(ret < 0);

System::ioctl_ptr= &::ioctl;

ret = System::ioctl(-1,0);

EXPECT_TRUE(ret < 0);

}

TEST(System, setsockopt)
{
System::setsockopt_ptr = nullptr;

int ret = System::setsockopt(-1,0,0,0,0);

EXPECT_TRUE(ret < 0);

System::setsockopt_ptr= &::setsockopt;

ret = System::setsockopt(-1,0,0,0,0);

EXPECT_TRUE(ret < 0);

}

TEST(System, if_nametoindex)
{
System::bind_ptr = nullptr;

unsigned int ret = System::if_nametoindex("foo");

EXPECT_TRUE(ret == 0);

System::if_nametoindex_ptr = &::if_nametoindex;

ret = System::if_nametoindex("foo");

EXPECT_TRUE(ret == 0);
}
2 changes: 2 additions & 0 deletions unittest/vslib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ bin_PROGRAMS = tests testslibsaivs
LDADD_GTEST = -L/usr/src/gtest -lgtest -lgtest_main

tests_SOURCES = main.cpp \
MockSwitchStateBase.cpp \
TestBuffer.cpp \
TestContextConfigContainer.cpp \
TestCorePortIndexMap.cpp \
Expand Down Expand Up @@ -43,6 +44,7 @@ tests_SOURCES = main.cpp \
TestSwitchBCM81724.cpp \
TestSwitchStateBaseMACsec.cpp \
TestMACsecManager.cpp \
TestSwitchStateBaseHostif.cpp \
TestSwitchStateBase.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) -fno-access-control
Expand Down
51 changes: 51 additions & 0 deletions unittest/vslib/MockSwitchStateBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "MockSwitchStateBase.h"

using namespace saivs;

MockSwitchStateBase::MockSwitchStateBase(
_In_ sai_object_id_t switch_id,
_In_ std::shared_ptr<RealObjectIdManager> manager,
_In_ std::shared_ptr<SwitchConfig> config):
SwitchStateBase(switch_id, manager, config)
{
SWSS_LOG_ENTER();
}

int MockSwitchStateBase::call_vs_create_tap_device(
_In_ const char *dev,
_In_ int flags)
{
SWSS_LOG_ENTER();

return SwitchStateBase::vs_create_tap_device(dev, flags);
}

int MockSwitchStateBase::call_ifup(
_In_ const char *dev,
_In_ sai_object_id_t port_id,
_In_ bool up,
_In_ bool explicitNotification)
{
SWSS_LOG_ENTER();

return ifup(dev, port_id, up, explicitNotification);
}

bool MockSwitchStateBase::call_hostif_create_tap_veth_forwarding(
_In_ const std::string &tapname,
_In_ int tapfd,
_In_ sai_object_id_t port_id)
{
SWSS_LOG_ENTER();

return hostif_create_tap_veth_forwarding(tapname, tapfd, port_id);
}

sai_status_t MockSwitchStateBase::call_vs_create_hostif_tap_interface(
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
SWSS_LOG_ENTER();

return vs_create_hostif_tap_interface(attr_count, attr_list);
}
37 changes: 37 additions & 0 deletions unittest/vslib/MockSwitchStateBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "SwitchStateBase.h"

namespace saivs
{
class MockSwitchStateBase:
SwitchStateBase
{
public:

MockSwitchStateBase(
_In_ sai_object_id_t switch_id,
_In_ std::shared_ptr<RealObjectIdManager> manager,
_In_ std::shared_ptr<SwitchConfig> config);

public:

static int call_vs_create_tap_device(
_In_ const char *dev,
_In_ int flags);

int call_ifup(
_In_ const char *dev,
_In_ sai_object_id_t port_id,
_In_ bool up,
_In_ bool explicitNotification);

bool call_hostif_create_tap_veth_forwarding(
_In_ const std::string &tapname,
_In_ int tapfd,
_In_ sai_object_id_t port_id);

sai_status_t call_vs_create_hostif_tap_interface(
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list);

};
}
Loading