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:
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
#include "ipv6_address.h"
|
||||
#include "utils.h"
|
||||
|
||||
// Really nice and unique macro names, Windows :D
|
||||
#undef IN
|
||||
|
||||
using namespace Tins;
|
||||
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user