Skip to content

Commit

Permalink
Added minimal testing of UDPv4Probe
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniacslk committed Sep 25, 2017
1 parent 942a421 commit 9daf083
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "dependencies/libtins"]
path = external/libtins
url = https://github.com/mfontanini/libtins.git
[submodule "tests/googletest"]
path = googletest
url = https://github.com/google/googletest.git
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,33 @@ if (SETCAP_EXECUTABLE)
endif()"
)
endif()


# Testing
INCLUDE(ExternalProject)
# Only include googletest if the git submodule has been fetched
IF(EXISTS "${CMAKE_SOURCE_DIR}/googletest/CMakeLists.txt")
# Enable tests and add the test directory
MESSAGE(STATUS "Tests have been enabled")
SET(GOOGLETEST_ROOT ${CMAKE_SOURCE_DIR}/googletest)
SET(GOOGLETEST_INCLUDE ${GOOGLETEST_ROOT}/googletest/include)
SET(GOOGLETEST_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/googletest)
SET(GOOGLETEST_LIBRARY ${GOOGLETEST_BINARY_DIR}/googletest)

ExternalProject_Add(
googletest
DOWNLOAD_COMMAND ""
SOURCE_DIR ${GOOGLETEST_ROOT}
BINARY_DIR ${GOOGLETEST_BINARY_DIR}
CMAKE_CACHE_ARGS "-DBUILD_GTEST:bool=ON" "-DBUILD_GMOCK:bool=OFF"
"-Dgtest_force_shared_crt:bool=ON"
INSTALL_COMMAND ""
)
# Make sure we build googletest before anything else
ADD_DEPENDENCIES(dublintraceroute googletest)
ENABLE_TESTING()
ADD_SUBDIRECTORY(tests)
ELSE()
MESSAGE(STATUS "googletest git submodule is absent. Run `git submodule init && git submodule update` to get it")
ENDIF()

1 change: 1 addition & 0 deletions googletest
Submodule googletest added at bfc0ff
3 changes: 2 additions & 1 deletion include/dublintraceroute/udpv4probe.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* \sa udpv4probe.cc
*/

#ifndef _UDPV4PROBE_H
#ifndef _UDPV4PROBE_H
#define _UDPV4PROBE_H

#include <tins/tins.h>
Expand All @@ -30,6 +30,7 @@ class UDPv4Probe {
const IPv4Address remote_addr() const { return remote_addr_; }
const uint16_t local_port() const { return local_port_; };
const uint16_t remote_port() const { return remote_port_; };
const uint8_t ttl() const { return ttl_; };

UDPv4Probe(
IPv4Address remote_addr,
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INCLUDE_DIRECTORIES(${gtest_INCLUDE_DIRS})
ADD_SUBDIRECTORY(src)

36 changes: 36 additions & 0 deletions tests/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file is shamelessly copied and modified from libtins.
# Use dublintraceroute's include directories + test include directories
INCLUDE_DIRECTORIES(
${PROJECT_SOURCE_DIR}/include/dublintraceroute/
${GOOGLETEST_INCLUDE}
)

# Find pthread library
FIND_PACKAGE(Threads REQUIRED)

LINK_DIRECTORIES(
${GOOGLETEST_LIBRARY}
)
# Link against GoogleTest, libdublintraceroute and pthread.
# Pthread is required by GoogleTest
LINK_LIBRARIES(
gtest
gtest_main
dublintraceroute
${CMAKE_THREAD_LIBS_INIT}
)

# Add tests target
ADD_CUSTOM_TARGET(
tests DEPENDS
UDPv4Test
${OPTIONAL_TEST_TARGETS}
)

# Test executables

ADD_EXECUTABLE(UDPv4Test EXCLUDE_FROM_ALL udpv4.cxx)

# Tests

ADD_TEST(UDPv4 UDPv4Test)
26 changes: 26 additions & 0 deletions tests/src/udpv4.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <dublintraceroute/udpv4probe.h>
#include <gtest/gtest.h>


namespace {

class UDPv4Test: public ::testing::Test {
};

TEST_F(UDPv4Test, TestUDPv4Constructor) {
UDPv4Probe p = UDPv4Probe(IPv4Address("8.8.8.8"), 33434, 12345, 64, IPv4Address("127.0.0.2"));
ASSERT_EQ(p.local_port(), 12345);
ASSERT_EQ(p.remote_port(), 33434);
ASSERT_EQ(p.ttl(), 64);
ASSERT_EQ(p.remote_addr().to_string(), std::string("8.8.8.8"));
ASSERT_EQ(p.local_addr().to_string(), std::string("127.0.0.2"));
}

}


int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
std::exit(RUN_ALL_TESTS());
}

0 comments on commit 9daf083

Please sign in to comment.