1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Merge pull request #72 from mfontanini/googletest-submodule

Googletest submodule
This commit is contained in:
Matias Fontanini
2015-04-25 18:57:57 -07:00
20 changed files with 127 additions and 90 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "googletest"]
path = googletest
url = https://github.com/smarr/googletest.git

View File

@@ -9,8 +9,16 @@ ELSE(NOT CMAKE_BUILD_TYPE)
MESSAGE(STATUS "Using specified '${CMAKE_BUILD_TYPE}' build type.")
ENDIF(NOT CMAKE_BUILD_TYPE)
# Default compilation settings
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
# Compilation flags.
IF(MSVC)
# Don't always use Wall, since VC's /Wall is ridiculously verbose.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
# Disable VC secure checks, since these are not really issues.
ADD_DEFINITIONS("-D_CRT_SECURE_NO_WARNINGS=1")
ADD_DEFINITIONS("-D_SCL_SECURE_NO_WARNINGS=1")
ELSE()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
ENDIF()
# Build output checks
OPTION(LIBTINS_BUILD_SHARED "Build libtins as a shared library." ON)
@@ -28,7 +36,7 @@ ENDIF(LIBTINS_BUILD_SHARED)
# The version number.
SET(LIBTINS_VERSION_MAJOR 3)
SET(LIBTINS_VERSION_MINOR 2)
SET(LIBTINS_VERSION_MINOR 3)
SET(LIBTINS_VERSION "${LIBTINS_VERSION_MAJOR}.${LIBTINS_VERSION_MINOR}")
# Required Packages
@@ -48,7 +56,11 @@ IF(WIN32)
ADD_DEFINITIONS(-DNOMINMAX -DWIN32)
ENDIF(WIN32)
# *******************
# Compilation options
# *******************
# C++11 support
OPTION(LIBTINS_ENABLE_CXX11 "Compile libtins with c++11 features" OFF)
IF(LIBTINS_ENABLE_CXX11)
SET(HAVE_CXX11 ON)
@@ -67,6 +79,7 @@ ELSE(LIBTINS_ENABLE_CXX11)
"as it increases the library's performance")
ENDIF(LIBTINS_ENABLE_CXX11)
# IEEE 802.11 and WPA2 decryption support
OPTION(LIBTINS_ENABLE_DOT11 "Compile libtins with IEEE 802.11 support" ON)
OPTION(LIBTINS_ENABLE_WPA2 "Compile libtins with WPA2 decryption features (requires OpenSSL)" ON)
IF(LIBTINS_ENABLE_DOT11)
@@ -81,6 +94,7 @@ IF(LIBTINS_ENABLE_DOT11)
ENDIF(LIBTINS_ENABLE_WPA2)
ENDIF(LIBTINS_ENABLE_DOT11)
# Use pcap_sendpacket to send l2 packets rather than raw sockets
IF(WIN32)
SET(USE_PCAP_SENDPACKET_DEFAULT ON)
ELSE(WIN32)
@@ -110,6 +124,7 @@ IF(DOXYGEN_FOUND)
)
ENDIF(DOXYGEN_FOUND)
# The library output directory
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
@@ -120,26 +135,43 @@ CONFIGURE_FILE(
)
# Support for pkg-config
set(CMAKE_INSTALL_LIBDIR lib)
set(pkgconfig_prefix ${CMAKE_INSTALL_PREFIX})
set(pkgconfig_exec_prefix ${CMAKE_INSTALL_PREFIX})
set(pkgconfig_libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(pkgconfig_version ${LIBTINS_VERSION})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libtins.pc.in
${CMAKE_CURRENT_BINARY_DIR}/libtins.pc @ONLY)
SET(CMAKE_INSTALL_LIBDIR lib)
SET(pkgconfig_prefix ${CMAKE_INSTALL_PREFIX})
SET(pkgconfig_exec_prefix ${CMAKE_INSTALL_PREFIX})
SET(pkgconfig_libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
SET(pkgconfig_version ${LIBTINS_VERSION})
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libtins.pc.in
${CMAKE_CURRENT_BINARY_DIR}/libtins.pc @ONLY)
install(
INSTALL(
FILES
${CMAKE_CURRENT_BINARY_DIR}/libtins.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
ENABLE_TESTING()
# ******************
# Add subdirectories
# ******************
ADD_SUBDIRECTORY(include)
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(tests)
ADD_SUBDIRECTORY(examples)
# 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(gtest_force_shared_crt ON CACHE BOOL "Always use /MD")
ENABLE_TESTING()
ADD_SUBDIRECTORY(googletest)
ADD_SUBDIRECTORY(tests)
ELSE()
MESSAGE(STATUS "googletest git submodule is absent. Run `git submodule init && git submodule update` to get it")
ENDIF()
# **********************************
# CMake project configuration export
# **********************************
# Add all targets to the build-tree export set
EXPORT(
TARGETS tins

1
googletest Submodule

Submodule googletest added at 2ccb2496fe

View File

@@ -45,7 +45,7 @@ namespace Tins {
* By default, RadioTap PDUs set the necesary fields to send an 802.11
* PDU as its inner pdu, avoiding packet drops. As a consequence,
* the FCS-at-end flag is on, the channel is set to 1, TSFT is set to 0,
* dbm_signal is set to 0xce, and the rx_flag and antenna fields to 0.
* dbm_signal is set to -50, and the rx_flag and antenna fields to 0.
*/
class RadioTap : public PDU {
public:

View File

@@ -381,7 +381,6 @@ template<class ForwardIterator>
void Tins::Utils::route_entries(ForwardIterator output) {
MIB_IPFORWARDTABLE *table;
ULONG size = 0;
char iface_name[256];
GetIpForwardTable(0, &size, 0);
std::vector<uint8_t> buffer(size);
table = (MIB_IPFORWARDTABLE*)&buffer[0];

View File

@@ -201,7 +201,7 @@ DHCPv6::status_code_type DHCPv6::status_code() const {
}
bool DHCPv6::has_rapid_commit() const {
return search_option(RAPID_COMMIT);
return search_option(RAPID_COMMIT) != NULL;
}
DHCPv6::user_class_type DHCPv6::user_class() const {
@@ -225,7 +225,7 @@ uint8_t DHCPv6::reconfigure_msg() const {
}
bool DHCPv6::has_reconfigure_accept() const {
return search_option(RECONF_ACCEPT);
return search_option(RECONF_ACCEPT) != NULL;
}
DHCPv6::duid_type DHCPv6::client_id() const {

View File

@@ -87,7 +87,7 @@ uint16_t IPv4Stream::extract_offset(const IP *ip) {
}
bool IPv4Stream::extract_more_frag(const IP *ip) {
return ip->frag_off() & 0x2000;
return (ip->frag_off() & 0x2000) != 0;
}
} // namespace Internals

View File

@@ -87,7 +87,7 @@ std::string IPv6Address::to_string() const {
#ifdef WIN32
// mingw on linux somehow doesn't have InetNtop
#ifdef _MSC_VER
if(InetNtopA(AF_INET6, (PVOID)address, buffer, sizeof(buffer)) != 0)
if(InetNtopA(AF_INET6, (PVOID)address, buffer, sizeof(buffer)) == 0)
throw malformed_address();
#else
ULONG sz = sizeof(buffer);

View File

@@ -70,11 +70,7 @@ bool OfflinePacketFilter::matches_filter(const uint8_t* buffer,
pcap_pkthdr header = {};
header.len = total_sz;
header.caplen = total_sz;
return pcap_offline_filter(
&filter,
&header,
buffer
);
return pcap_offline_filter(&filter, &header, buffer) != 0;
}
bool OfflinePacketFilter::matches_filter(PDU& pdu) const

View File

@@ -384,7 +384,7 @@ PDU *PacketSender::recv_match_loop(const std::vector<int>& sockets, PDU &pdu, st
#endif
timeout.tv_sec = _timeout;
end_time.tv_sec = time(0) + _timeout;
end_time.tv_sec = static_cast<long>(time(0) + _timeout);
end_time.tv_usec = timeout.tv_usec = _timeout_usec;
while(true) {
FD_ZERO(&readfds);

View File

@@ -178,7 +178,7 @@ void RadioTap::init() {
channel(Utils::channel_to_mhz(1), 0xa0);
flags(FCS);
tsft(0);
dbm_signal(0xce);
dbm_signal(-50);
rx_flags(0);
antenna(0);
}

View File

@@ -147,7 +147,7 @@ void TCP::sack_permitted() {
}
bool TCP::has_sack_permitted() const {
return bool(search_option(SACK_OK));
return search_option(SACK_OK) != NULL;
}
void TCP::sack(const sack_type &edges) {
@@ -286,7 +286,7 @@ void TCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *par
buffer = write_option(*it, buffer);
if(_options_size < _total_options_size) {
uint8_t padding = _options_size;
uint16_t padding = _options_size;
while(padding < _total_options_size) {
*(buffer++) = 1;
padding++;

View File

@@ -1,7 +1,2 @@
FIND_PACKAGE(GTest)
IF(GTEST_FOUND)
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS})
ADD_SUBDIRECTORY(src)
ELSE(GTEST_FOUND)
MESSAGE(WARNING "Google test not found. Disabling tests.")
ENDIF(GTEST_FOUND)
INCLUDE_DIRECTORIES(${gtest_INCLUDE_DIRS})
ADD_SUBDIRECTORY(src)

View File

@@ -1,16 +1,25 @@
# Use libtins' include directories + test include directories
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include/tins/ ../include/)
INCLUDE_DIRECTORIES(
${PROJECT_SOURCE_DIR}/include/tins/
../include/
${gtest_SOURCE_DIR}/include
${PCAP_INCLUDE_DIR}
)
# Find pthread library
FIND_PACKAGE(Threads REQUIRED)
LINK_DIRECTORIES(
${gtest_BINARY_DIR}
)
# Link against GoogleTest, libtins and pthread.
# Pthread is required by GoogleTest
LINK_LIBRARIES(
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARY}
gtest
gtest_main
tins
${CMAKE_THREAD_LIBS_INIT}
${PCAP_LIBRARY}
)
IF(LIBTINS_ENABLE_WPA2)

View File

@@ -4,6 +4,9 @@
#include "ipv6_address.h"
#include "utils.h"
// Really nice and unique macro names, Windows :D
#undef IN
using namespace Tins;

View File

@@ -218,9 +218,8 @@ TEST_F(ICMPv6Test, PrefixInformation) {
TEST_F(ICMPv6Test, RedirectHeader) {
ICMPv6 icmp;
IP ip = IP("127.0.0.1") / TCP(22);
PDU::serialization_type buffer = ip.serialize();
buffer.insert(buffer.begin(), 6, 0);
EthernetII eth = EthernetII() / IP("8.8.8.8", "192.168.0.100") / TCP(22, 26);
PDU::serialization_type buffer = eth.serialize();
icmp.redirect_header(buffer);
EXPECT_EQ(buffer, icmp.redirect_header());
}

View File

@@ -1,10 +1,12 @@
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include "network_interface.h"
#include "utils.h"
#include "macros.h"
using namespace Tins;
using namespace std;
class NetworkInterfaceTest : public ::testing::Test {
public:
@@ -12,17 +14,14 @@ public:
};
#ifdef BSD
const std::string NetworkInterfaceTest::iface_name("lo0"),
NetworkInterfaceTest::iface_addr("");
#elif defined(WIN32)
// modify me on every windows environment :D
const std::string NetworkInterfaceTest::iface_name("{INSERT-SOME-INTERFACE-NAME}"),
const string NetworkInterfaceTest::iface_name("lo0"),
NetworkInterfaceTest::iface_addr("");
#else
const std::string NetworkInterfaceTest::iface_name("lo"),
const string NetworkInterfaceTest::iface_name("lo"),
NetworkInterfaceTest::iface_addr("");
#endif
#ifndef WIN32
TEST_F(NetworkInterfaceTest, ConstructorFromString) {
// just test this doesn't throw
NetworkInterface iface(iface_name);
@@ -63,5 +62,31 @@ TEST_F(NetworkInterfaceTest, DistinctOperator) {
NetworkInterface iface1(iface_name), iface2;
EXPECT_NE(iface1, iface2);
}
#endif // WIN32
// This is a more generic test that can be run on all platforms.
// The above ones won't run on windows since there's no name for the loopback interface.
// So this does more or less the same as all of the above, but iterating over the
// actual interfaces available in the system.
TEST_F(NetworkInterfaceTest, IterateOverInterfaces) {
vector<NetworkInterface> interfaces = NetworkInterface::all();
set<string> names;
set<int> ids;
for (size_t i = 0; i < interfaces.size(); ++i) {
// Expect unique names an all interfaces
EXPECT_TRUE(names.insert(interfaces[i].name()).second);
// Expect unique ids an all interfaces
EXPECT_TRUE(ids.insert(interfaces[i].id()).second);
// Expect this interface to be equal to itself
EXPECT_EQ(interfaces[i], interfaces[i]);
// We expect to be able to construct the interface from a name
// and they should still be equal
NetworkInterface iface(interfaces[i].name());
EXPECT_EQ(interfaces[i], iface);
// We expect this interface to be different from the others
for (size_t u = i + 1; u < interfaces.size(); ++u) {
EXPECT_NE(interfaces[i], interfaces[u]);
}
}
}

View File

@@ -5,6 +5,7 @@
#include <stdint.h>
#include "tcp.h"
#include "ip.h"
#include "ethernetII.h"
#include "utils.h"
using namespace std;
@@ -24,11 +25,12 @@ const uint8_t TCPTest::expected_packet[] = {
18, 52, 3, 3, 122, 4, 2, 5, 10, 0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0
};
// IP + TCP
// Ethernet + IP + TCP
const uint8_t TCPTest::checksum_packet[] = {
69, 0, 0, 40, 0, 0, 64, 0, 64, 6, 60, 206, 0, 0, 0, 0, 127, 0, 0, 1,
5, 57, 199, 49, 0, 0, 0, 0, 255, 216, 70, 222, 80, 20, 0, 0, 158, 172,
0, 0
10, 128, 57, 251, 101, 187, 76, 128, 147, 141, 144, 65, 8, 0, 69, 0, 0,
60, 152, 189, 64, 0, 64, 6, 0, 19, 10, 0, 0, 54, 198, 41, 209, 140, 180,
207, 1, 187, 114, 130, 185, 186, 0, 0, 0, 0, 160, 2, 114, 16, 44, 228, 0,
0, 2, 4, 5, 180, 4, 2, 8, 10, 3, 81, 33, 7, 0, 0, 0, 0, 1, 3, 3, 7
};
const uint8_t TCPTest::partial_packet[] = {
@@ -44,12 +46,12 @@ TEST_F(TCPTest, DefaultConstructor) {
}
TEST_F(TCPTest, ChecksumCheck) {
IP pkt1(checksum_packet, sizeof(checksum_packet));
EthernetII pkt1(checksum_packet, sizeof(checksum_packet));
const TCP &tcp1 = pkt1.rfind_pdu<TCP>();
uint16_t checksum = tcp1.checksum();
IP::serialization_type buffer = pkt1.serialize();
IP pkt2(&buffer[0], buffer.size());
PDU::serialization_type buffer = pkt1.serialize();
EthernetII pkt2(&buffer[0], buffer.size());
const TCP &tcp2 = pkt2.rfind_pdu<TCP>();
EXPECT_EQ(checksum, tcp2.checksum());
EXPECT_EQ(tcp1.checksum(), tcp2.checksum());

View File

@@ -3,7 +3,7 @@
#include <stdint.h>
#include "udp.h"
#include "ip.h"
#include "ethernetII.h"
using namespace std;
using namespace Tins;
@@ -21,9 +21,11 @@ const uint8_t UDPTest::expected_packet[] = {
};
const uint8_t UDPTest::checksum_packet[] = {
69, 0, 0, 48, 35, 109, 64, 0, 64, 17, 25, 78, 0, 0, 0, 0, 127, 0, 0,
1, 5, 57, 155, 11, 0, 28, 84, 167, 97, 115, 100, 97, 115, 100, 115,
97, 115, 100, 97, 115, 100, 115, 97, 100, 97, 115, 100, 10
10, 128, 57, 251, 101, 187, 76, 128, 147, 141, 144, 65, 8, 0, 69, 0, 0,
70, 14, 223, 64, 0, 64, 17, 138, 252, 10, 0, 0, 54, 75, 75, 75, 75, 215,
173, 0, 53, 0, 50, 206, 155, 118, 39, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 11,
48, 45, 101, 100, 103, 101, 45, 99, 104, 97, 116, 8, 102, 97, 99, 101,
98, 111, 111, 107, 3, 99, 111, 109, 0, 0, 1, 0, 1
};
@@ -44,12 +46,12 @@ TEST_F(UDPTest, DefaultConstructor) {
}
TEST_F(UDPTest, ChecksumCheck) {
IP pkt1(checksum_packet, sizeof(checksum_packet));
EthernetII pkt1(checksum_packet, sizeof(checksum_packet));
const UDP &udp1 = pkt1.rfind_pdu<UDP>();
uint16_t checksum = udp1.checksum();
IP::serialization_type buffer = pkt1.serialize();
IP pkt2(&buffer[0], buffer.size());
PDU::serialization_type buffer = pkt1.serialize();
EthernetII pkt2(&buffer[0], buffer.size());
const UDP &udp2 = pkt2.rfind_pdu<UDP>();
EXPECT_EQ(checksum, udp2.checksum());
EXPECT_EQ(udp1.checksum(), udp2.checksum());

View File

@@ -67,36 +67,7 @@ const uint32_t UtilsTest::data_len = 500;
TEST_F(UtilsTest, Crc32) {
uint32_t crc = Utils::crc32(data, data_len);
EXPECT_EQ(crc, 0x78840f54U);
}
TEST_F(UtilsTest, ResolveDomain) {
IPv4Address localhost_ip("127.0.0.1");
EXPECT_EQ(Utils::resolve_domain("localhost"), localhost_ip);
}
/*
TEST_F(UtilsTest, ResolveDomain6) {
IPv6Address localhost_ip("2606:2800:220:6d:26bf:1447:1097:aa7");
EXPECT_EQ(Utils::resolve_domain6("example.com"), localhost_ip);
}
*/
// FIXME
TEST_F(UtilsTest, Checksum) {
/*uint16_t checksum = Utils::do_checksum(data, data + data_len);
//EXPECT_EQ(checksum, 0x231a);
uint8_t my_data[] = {0, 0, 0, 0};
checksum = Utils::do_checksum(my_data, my_data + 4);
//EXPECT_EQ(checksum, 0xFFFF);
*/
}