From 32c972736bf8baba4a41134a75eeb5fe88a57080 Mon Sep 17 00:00:00 2001 From: Vishwanatha Subbanna Date: Tue, 4 Apr 2017 14:10:08 +0530 Subject: Add gtest cases to test callback handler Change-Id: If6c1e1616bcf73441648c8e0cb20017a4b218f70 Signed-off-by: Vishwanatha Subbanna --- Makefile.am | 2 + configure.ac | 24 ++++++++++- test/Makefile.am | 12 ++++++ test/utest.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 test/Makefile.am create mode 100644 test/utest.cpp diff --git a/Makefile.am b/Makefile.am index 777dffc..93273d8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -13,3 +13,5 @@ phosphor_gpio_monitor_LDFLAGS = $(SYSTEMD_LIBS) \ phosphor_gpio_monitor_CFLAGS = $(SYSTEMD_CFLAGS) \ $(SDBUSPLUS_CFLAGS) \ $(PHOSPHOR_LOGGING_CFLAGS) + +SUBDIRS = test diff --git a/configure.ac b/configure.ac index c80d778..694d2b3 100644 --- a/configure.ac +++ b/configure.ac @@ -9,7 +9,6 @@ AM_SILENT_RULES([yes]) # Checks for programs. AC_PROG_CXX AC_PROG_INSTALL -AC_PROG_MAKE_SET # Checks for typedefs, structures, and compiler characteristics. AX_CXX_COMPILE_STDCXX_14([noext]) @@ -24,6 +23,27 @@ PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging],, [AC_MSG_ERROR([Could PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus],, [AC_MSG_ERROR([Could not find sdbusplus...openbmc/sdbusplus package required])]) PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces],, [AC_MSG_ERROR([Could not find phosphor-dbus-interfaces...openbmc/phosphor-dbus-interfaces package required])]) +# Check/set gtest specific functions. +AX_PTHREAD([GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=1"],[GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=0"]) +AC_SUBST(GTEST_CPPFLAGS) +AC_ARG_ENABLE([oe-sdk], + AS_HELP_STRING([--enable-oe-sdk], [Link testcases absolutely against OE SDK so they can be ran within it.]) +) +AC_ARG_VAR(OECORE_TARGET_SYSROOT, + [Path to the OE SDK SYSROOT]) +AS_IF([test "x$enable_oe_sdk" == "xyes"], + AS_IF([test "x$OECORE_TARGET_SYSROOT" == "x"], + AC_MSG_ERROR([OECORE_TARGET_SYSROOT must be set with --enable-oe-sdk]) + ) + AC_MSG_NOTICE([Enabling OE-SDK at $OECORE_TARGET_SYSROOT]) + [ + testcase_flags="-Wl,-rpath,\${OECORE_TARGET_SYSROOT}/lib" + testcase_flags="${testcase_flags} -Wl,-rpath,\${OECORE_TARGET_SYSROOT}/usr/lib" + testcase_flags="${testcase_flags} -Wl,-dynamic-linker,`find \${OECORE_TARGET_SYSROOT}/lib/ld-*.so | sort -r -n | head -n1`" + ] + AC_SUBST([OESDK_TESTCASE_FLAGS], [$testcase_flags]) +) + # Create configured output -AC_CONFIG_FILES([Makefile]) +AC_CONFIG_FILES([Makefile test/Makefile]) AC_OUTPUT diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..db82844 --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,12 @@ +AM_CPPFLAGS = -I$(top_srcdir) + +# Run all 'check' test programs +TESTS = $(check_PROGRAMS) + +# # Build/add utest to test suite +check_PROGRAMS = utest +utest_CPPFLAGS = -Igtest $(GTEST_CPPFLAGS) $(AM_CPPFLAGS) $(SDBUSPLUS_CFLAGS) $(PHOSPHOR_DBUS_INTERFACES_CFLAGS) +utest_CXXFLAGS = $(PTHREAD_CFLAGS) +utest_LDFLAGS = -lgtest_main -lgtest $(PTHREAD_LIBS) $(OESDK_TESTCASE_FLAGS) $(SYSTEMD_LIBS) $(SDBUSPLUS_LIBS) $(PHOSPHOR_DBUS_INTERFACES_LIBS) +utest_SOURCES = utest.cpp +utest_LDADD = $(top_builddir)/monitor.o diff --git a/test/utest.cpp b/test/utest.cpp new file mode 100644 index 0000000..78e1b7e --- /dev/null +++ b/test/utest.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include "monitor.hpp" + +using namespace phosphor::gpio; + +// Exit helper. Ideally should be class but need +// this to be used inside a static method. +bool completed {}; + +class GpioTest : public ::testing::Test +{ + public: + static constexpr auto DEVICE = "/tmp/test_fifo"; + + // systemd event handler + sd_event* events; + + // Really needed just for the constructor + decltype(input_event::code) code = 10; + + // Really needed just for the constructor + decltype(input_event::value) value = 10; + + // Need this so that events can be initialized. + int rc; + + // Gets called as part of each TEST_F construction + GpioTest() + : rc(sd_event_default(&events)) + { + // Check for successful creation of event handler + EXPECT_GE(rc, 0); + + // FIFO created to simulate data available + EXPECT_EQ(0, mknod(DEVICE, S_IFIFO|0666, 0)); + } + + // Gets called as part of each TEST_F destruction + ~GpioTest() + { + EXPECT_EQ(0, remove(DEVICE)); + + events = sd_event_unref(events); + EXPECT_EQ(events, nullptr); + } + + // Callback handler on data + static int callbackHandler(sd_event_source* es, int fd, + uint32_t revents, void* userData) + { + std::cout <<"Event fired" << std::endl; + completed = true; + return 0; + } +}; + +/** @brief Makes sure that event never comes for 3 seconds + */ +TEST_F(GpioTest, noEventIn3Seconds) +{ + using namespace std::chrono; + + phosphor::gpio::EventPtr eventP { events }; + events = nullptr; + + const std::string emptyTarget = ""; + Monitor gpio(DEVICE, code, value, emptyTarget, + eventP, callbackHandler); + + // Waiting 3 seconds and check if the completion status is set + int count = 0; + while(count < 3) + { + // Returns -0- on timeout and positive number on dispatch + auto sleepTime = duration_cast(seconds(1)); + if(!sd_event_run(eventP.get(), sleepTime.count())) + { + count++; + } + } + EXPECT_EQ(false, completed); + + // 3 to cater to another uptick that happens prior to breaking. + EXPECT_EQ(3, count); +} + +/** @brief Pump data in the middle and expect the callback to be invoked */ +TEST_F(GpioTest, pumpDataAndExpectCallBack) +{ + using namespace std::chrono; + + phosphor::gpio::EventPtr eventP { events }; + events = nullptr; + + const std::string emptyTarget = ""; + Monitor gpio(DEVICE, code, value, emptyTarget, + eventP, callbackHandler); + + // Pump the data in the middle + int count = 0; + while(count < 2 && !completed) + { + if (count == 1) + { + auto pumpData = std::string("echo 'foo' > ") + DEVICE; + EXPECT_GE(0, system(pumpData.c_str())); + } + + // Returns -0- on timeout + auto sleepTime = duration_cast(seconds(1)); + if(!sd_event_run(eventP.get(), sleepTime.count())) + { + count++; + } + } + EXPECT_EQ(true, completed); + EXPECT_EQ(1, count); +} -- cgit v1.2.1