add tests

This commit is contained in:
Robert Jeutter 2021-10-24 12:52:16 +02:00
parent c2c321eb9b
commit ed977be2fd
15 changed files with 2565 additions and 11 deletions

View File

@ -0,0 +1,5 @@
{
"number_of_attack_packets_per_thread_per_iteration": "20",
"attack_type": "udp_flood",
"attack_rate": "5"
}

37
test/Attacker_test.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <catch2/catch.hpp>
#include <iostream>
#include <rte_cycles.h>
TEST_CASE("tsc timer", "[]") {
// count and print seconds since test started
// stop at 30 seconds
// const std::string clear(100, '\n');
const uint64_t MAX_SECONDS = 30;
uint64_t cycles_old = 0;
uint64_t cycles = 0;
uint64_t hz = rte_get_tsc_hz();
uint64_t seconds = 0;
uint64_t delta_t = 0;
// print initial message
std::cout << "cycles : " << cycles << "\t"
<< "hz : " << hz << "\t"
<< "seconds : " << seconds << "\t" << std::endl;
while (seconds < MAX_SECONDS) {
cycles_old = cycles;
cycles = rte_get_tsc_cycles();
hz = rte_get_tsc_hz();
//calculate
delta_t = uint64_t(1 / hz * (cycles - cycles_old));
seconds += delta_t;
// print
// std::cout << clear;
std::cout << "cycles : " << cycles << "\t"
<< "hz : " << hz << "\t"
<< "seconds : " << seconds << "\t" << std::endl;
}
}

View File

@ -0,0 +1,7 @@
{
"STRING" : "Hello World.",
"BOOLEAN" : true,
"UNSIGNED_INT" : 42,
"FLOAT" : 1.337,
"DOUBLE" : -3.001
}

View File

@ -0,0 +1,4 @@
{
"UNSIGNED_INT": 666,
"X": "80085"
}

View File

@ -0,0 +1,52 @@
#include "ConfigurationManagement/Configurator.hpp"
#include <catch2/catch.hpp>
#include <cstdio>
#include <iostream>
#include <string>
TEST_CASE("Json Datei einlesen", "[]") {
REQUIRE_NOTHROW(Configurator::instance()->read_config(
"../test/Configurator_config.json"));
REQUIRE(Configurator::instance()->get_config_as_bool("BOOLEAN") == true);
REQUIRE(Configurator::instance()->get_config_as_unsigned_int(
"UNSIGNED_INT") == 42);
REQUIRE(Configurator::instance()->get_config_as_string("STRING") ==
"Hello World.");
REQUIRE(Configurator::instance()->get_config_as_float("FLOAT") == 1.337f);
REQUIRE(Configurator::instance()->get_config_as_double("DOUBLE") == -3.001);
}
TEST_CASE("nicht existierende Json-Datei", "[]") {
REQUIRE_THROWS(Configurator::instance()->read_config("non-existent.json"));
REQUIRE_THROWS(Configurator::instance()->read_config("non-existent.json",
"typo.json"));
}
TEST_CASE("Boost-Beispiel") {
LOG_INFO << "Dies ist eine Info Message" << LOG_END;
LOG_WARNING << "Dies ist eine Warn-Nachricht" << LOG_END;
}
TEST_CASE("Entry does (not) exist") {
REQUIRE_NOTHROW(Configurator::instance()->read_config(
"../test/Configurator_config.json",
"../test/Configurator_default_config.json"));
REQUIRE(Configurator::instance()->entry_exists("fhk4bhf1mx0f") == false);
REQUIRE(Configurator::instance()->entry_exists("STRING") == true);
REQUIRE(Configurator::instance()->entry_exists("X") == true);
}
TEST_CASE("Default Config") {
REQUIRE_NOTHROW(Configurator::instance()->read_config(
"../test/Configurator_config.json",
"../test/Configurator_default_config.json"));
REQUIRE(Configurator::instance()->get_config_as_unsigned_int(
"UNSIGNED_INT") == 42);
REQUIRE(Configurator::instance()->get_config_as_unsigned_int("UNSIGNED_INT",
true) == 666);
REQUIRE(Configurator::instance()->get_config_as_string("X") == "80085");
}

View File

@ -0,0 +1,9 @@
{
"STRING" : "Hello World.",
"BOOLEAN" : true,
"UNSIGNED_INT" : 666,
"FLOAT" : 1.337,
"DOUBLE" : -3.001,
"X" : "80085",
"Y" : "Hi"
}

View File

@ -6,7 +6,7 @@
TEST_CASE("Calculate number of threads", "[]") {
/*
Configurator::instance()->read_config("../test/config.json");
Configurator::instance()->read_config("../test/Initializer_config.json");
uint16_t nb_worker_threads = 0;
// calculate default value of worker threads

View File

@ -0,0 +1,11 @@
{
"UDP_flood_weight" : 1,
"TCP_flood_weight": 1,
"ICMP_flood_weight": 1,
"SYNFIN_weight": 1,
"SMALLWINDOW_weight": 1,
"threshold_UDP": 5,
"threshold_TCP": 5,
"threshold_ICMP": 5,
"min_window_size": 5
}

231
test/Inspection_test.cpp Normal file
View File

@ -0,0 +1,231 @@
#include "Inspection.hpp"
#include "ConfigurationManagement/Configurator.hpp"
#include "PacketDissection/PacketContainer.hpp"
#include "PacketDissection/PacketInfo.hpp"
#include "PacketDissection/PacketInfoCreator.hpp"
#include "PacketDissection/PacketInfoIpv4Icmp.hpp"
#include "PacketDissection/PacketInfoIpv4Tcp.hpp"
#include "PacketDissection/PacketInfoIpv4Udp.hpp"
#include "Threads/AttackThread.h"
#include <catch2/catch.hpp>
#include <iostream>
TEST_CASE("init Inspection", "[]") {
Inspection testInspection;
REQUIRE_NOTHROW(testInspection.update_stats(0, 0, 0, 0, 0, 0, 0, 0, 0));
}
// Für folgende wird eine funktionierende Packet Dissection benötigt
TEST_CASE("check attack detection", "[]") {
// setup config
Configurator::instance()->read_config("../test/Inspection_config.json");
// create packet container
uint16_t inside_port = 0;
uint16_t outside_port = 1;
struct rte_mempool mbuf_pool_struct;
struct rte_mempool* mbuf_pool = &mbuf_pool_struct;
CHECK(mbuf_pool != nullptr);
NetworkPacketHandler* pkt_handler = new NetworkPacketHandler(0, 0);
CHECK(pkt_handler != nullptr);
PacketContainer* pkt_container =
new PacketContainer(pkt_handler, mbuf_pool, inside_port, outside_port);
CHECK(pkt_container != nullptr);
// inspection class
Inspection testInspection;
/// test SYN-FIN attack
SECTION("SYN-FIN Attack", "[]") {
PacketInfo* pkt_info = nullptr;
for (int i = 0; i < 5; ++i) {
pkt_info = pkt_container->get_empty_packet(IPv4TCP);
PacketInfoIpv4Tcp* pkt_info =
static_cast<PacketInfoIpv4Tcp*>(pkt_info);
// create packet with SYN-FIN Flag into packet container
pkt_info->fill_payloadless_tcp_packet(
{00, 00, 00, 00, 00, 00}, {00, 00, 00, 00, 00, 00}, 0, 0, 0, 0,
0, 0, 0b00000011, 100);
}
// packet container to inspection
testInspection.analyze_container(pkt_container);
// Check if packetcontainer empty
CHECK(pkt_container->get_total_number_of_packets() == 0);
}
/// test SYN-FIN-ACK attack
SECTION("SYN-FIN-ACK Attack", "[]") {
PacketInfo* pkt_info = nullptr;
for (int i = 0; i < 5; ++i) {
pkt_info = pkt_container->get_empty_packet(IPv4TCP);
PacketInfoIpv4Tcp* pkt_info =
static_cast<PacketInfoIpv4Tcp*>(pkt_info);
// create packet with SYN-FIN-ACK Flag into packet container
pkt_info->fill_payloadless_tcp_packet(
{00, 00, 00, 00, 00, 00}, {00, 00, 00, 00, 00, 00}, 0, 0, 0, 0,
0, 0, 0b00010011, 100);
}
// packet container to inspection
testInspection.analyze_container(pkt_container);
// Check if packetcontainer empty
CHECK(pkt_container->get_total_number_of_packets() == 0);
}
/// test Zero Window attack
SECTION("Zero Window Attack", "[]") {
PacketInfo* pkt_info = nullptr;
for (int i = 0; i < 5; ++i) {
pkt_info = pkt_container->get_empty_packet(IPv4TCP);
PacketInfoIpv4Tcp* pkt_info =
static_cast<PacketInfoIpv4Tcp*>(pkt_info);
// create packet with 0 window into packet container
pkt_info->fill_payloadless_tcp_packet({00, 00, 00, 00, 00, 00},
{00, 00, 00, 00, 00, 00}, 0,
0, 0, 0, 0, 0, 0, 0);
}
// packet container to inspection
testInspection.analyze_container(pkt_container);
// Check if packetcontainer empty
CHECK(pkt_container->get_total_number_of_packets() == 0);
}
/// test Small Window attack
SECTION("Small Window Attack", "[]") {
PacketInfo* pkt_info = nullptr;
for (int i = 0; i < 5; ++i) {
pkt_info = pkt_container->get_empty_packet(IPv4TCP);
PacketInfoIpv4Tcp* pkt_info =
static_cast<PacketInfoIpv4Tcp*>(pkt_info);
// create packet with small Windows into packet container
pkt_info->fill_payloadless_tcp_packet({00, 00, 00, 00, 00, 00},
{00, 00, 00, 00, 00, 00}, 0,
0, 0, 0, 0, 0, 0, i);
}
// packet container to inspection
testInspection.analyze_container(pkt_container);
// Check if packetcontainer empty
CHECK(pkt_container->get_total_number_of_packets() == 0);
}
/// test UDP Flood attack
SECTION("UDP Flood Attack", "[]") {
PacketInfo* pkt_info = nullptr;
// create udp packets into packet container
for (int i = 0; i < 25; ++i) {
pkt_info = pkt_container->get_empty_packet(IPv4UDP);
PacketInfoIpv4Udp* pkt_info =
static_cast<PacketInfoIpv4Udp*>(pkt_info);
}
// packet container to inspection
testInspection.analyze_container(pkt_container);
// Check if packetcontainer only has threshold packets left
CHECK(pkt_container->get_total_number_of_packets() == 5);
}
/// test TCP Flood attack
SECTION("TCP Flood Attack", "[]") {
PacketInfo* pkt_info = nullptr;
// create tcp packets into packet container
for (int i = 0; i < 25; ++i) {
pkt_info = pkt_container->get_empty_packet(IPv4TCP);
PacketInfoIpv4Tcp* pkt_info =
static_cast<PacketInfoIpv4Tcp*>(pkt_info);
pkt_info->fill_payloadless_tcp_packet({00, 00, 00, 00, 00, 00},
{00, 00, 00, 00, 00, 00}, 0,
0, 0, 0, i, 0, 0, 100);
}
// packet container to inspection
testInspection.analyze_container(pkt_container);
// Check if packetcontainer only has threshold packets left
CHECK(pkt_container->get_total_number_of_packets() == 5);
}
/// \todo test ICMP Flood attack
SECTION("ICMP Flood Attack", "[]") {
PacketInfo* pkt_info = nullptr;
// create icmp packets into packet container
for (int i = 0; i < 25; ++i) {
pkt_info = pkt_container->get_empty_packet(IPv4ICMP);
PacketInfoIpv4Icmp* pkt_info =
static_cast<PacketInfoIpv4Icmp*>(pkt_info);
}
// packet container to inspection
testInspection.analyze_container(pkt_container);
// Check if packetcontainer only has threshold packets left
CHECK(pkt_container->get_total_number_of_packets() == 5);
}
}
TEST_CASE("check update function", "[]") {
Inspection testInspection;
SECTION("Null", "[]") {
// update statistic with given numbers
REQUIRE_NOTHROW(testInspection.update_stats(0, 0, 0, 0, 0, 0, 0, 0, 0));
// check correct formulas
CHECK(testInspection.get_UDP_packet_rate() == 0); //< udp_pkt/duration
CHECK(testInspection.get_TCP_packet_rate() == 0); //< tcp_pkt/duration
CHECK(testInspection.get_ICMP_packet_rate() == 0); //< icmp_pkt/duration
CHECK(testInspection.get_attack_level() == 0); //< no attacks
CHECK(testInspection.get_UDP_threshold() == 5); //< _threshold_UDP
CHECK(testInspection.get_TCP_threshold() == 5); //< _threshold_TCP
CHECK(testInspection.get_ICMP_threshold() == 5); //< _threshold_ICMP
}
SECTION("UDP rate", "[]") {
REQUIRE_NOTHROW(
testInspection.update_stats(10, 0, 0, 0, 0, 0, 0, 0, 1));
CHECK(testInspection.get_UDP_packet_rate() == 10);
}
SECTION("TCP rate", "[]") {
REQUIRE_NOTHROW(
testInspection.update_stats(0, 10, 0, 0, 0, 0, 0, 0, 1));
CHECK(testInspection.get_TCP_packet_rate() == 10);
}
SECTION("ICMP rate", "[]") {
REQUIRE_NOTHROW(
testInspection.update_stats(0, 0, 0, 10, 0, 0, 0, 0, 1));
CHECK(testInspection.get_ICMP_packet_rate() == 10);
}
SECTION("UDP Flood", "[]") {
REQUIRE_NOTHROW(
testInspection.update_stats(10, 0, 0, 5, 0, 0, 0, 0, 1));
CHECK(testInspection.get_UDP_packet_rate() == 10);
CHECK(testInspection.get_attack_level() ==
5); //< UDP_Floods * _UDP_flood_weight
CHECK(testInspection.get_UDP_threshold() == 0); //< 5-1/5*5*5
}
SECTION("TCP Flood", "[]") {
REQUIRE_NOTHROW(
testInspection.update_stats(0, 10, 0, 0, 5, 0, 0, 0, 1));
CHECK(testInspection.get_TCP_packet_rate() == 10);
CHECK(testInspection.get_attack_level() == 5);
CHECK(testInspection.get_TCP_threshold() == 0);
}
SECTION("ICMP Flood", "[]") {
REQUIRE_NOTHROW(
testInspection.update_stats(0, 0, 10, 0, 0, 5, 0, 0, 1));
CHECK(testInspection.get_ICMP_packet_rate() == 10);
CHECK(testInspection.get_attack_level() == 5);
CHECK(testInspection.get_ICMP_threshold() == 0);
}
SECTION("SYN-FIN Attack", "[]") {
REQUIRE_NOTHROW(
testInspection.update_stats(0, 10, 0, 0, 0, 0, 5, 0, 1));
CHECK(testInspection.get_TCP_packet_rate() == 10);
CHECK(testInspection.get_attack_level() == 5);
CHECK(testInspection.get_TCP_threshold() == 0);
}
SECTION("SmallWindow Attack", "[]") {
REQUIRE_NOTHROW(
testInspection.update_stats(0, 10, 0, 0, 0, 0, 0, 5, 1));
CHECK(testInspection.get_TCP_packet_rate() == 10);
CHECK(testInspection.get_attack_level() == 5);
CHECK(testInspection.get_TCP_threshold() == 0);
}
SECTION("send to global Statisic", "[]") {
// sending to global statistic not implemented yet in main
}
}

View File

@ -0,0 +1,292 @@
#include <catch2/catch.hpp>
#include <boost/log/trivial.hpp>
#include <rte_mbuf.h>
#include "Definitions.hpp"
#include "PacketDissection/PacketContainer.hpp"
#include "PacketDissection/PacketInfo.hpp"
#include "PacketDissection/PacketInfoCreator.hpp"
#include "PacketDissection/PacketInfoIpv4Icmp.hpp"
#include "PacketDissection/PacketInfoIpv4Tcp.hpp"
#include "PacketDissection/PacketInfoIpv4Udp.hpp"
#include "PacketDissection/PacketInfoIpv6Icmp.hpp"
#include "PacketDissection/PacketInfoIpv6Tcp.hpp"
#include "PacketDissection/PacketInfoIpv6Udp.hpp"
TEST_CASE("PacketContainer", "[]") {
// === I N I T ===//
uint16_t inside_port = 0;
uint16_t outside_port = 1;
struct rte_mempool mbuf_pool_struct;
struct rte_mempool* mbuf_pool = &mbuf_pool_struct;
CHECK(mbuf_pool != nullptr);
PacketContainer* pkt_container =
new PacketContainer(mbuf_pool, inside_port, outside_port, 0, 0);
CHECK(pkt_container != nullptr);
// === S E C T I O N S == //
SECTION("get_empty_packet", "[]") {
SECTION("default", "[]") {
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 0);
PacketInfo* pkt_info = pkt_container->get_empty_packet();
CHECK(pkt_info != nullptr);
CHECK(pkt_info->get_mbuf() != nullptr);
CHECK(pkt_info->get_type() == IPv4TCP);
CHECK(pkt_container->get_total_number_of_packets() >=
pkt_container->get_number_of_polled_packets());
CHECK(pkt_container->get_total_number_of_packets() == 1);
CHECK(pkt_container->get_number_of_polled_packets() == 0);
}
SECTION("IPv4TCP", "[]") {
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 0);
PacketInfo* pkt_info = pkt_container->get_empty_packet(IPv4TCP);
CHECK(pkt_info != nullptr);
CHECK(pkt_info->get_mbuf() != nullptr);
CHECK(pkt_info->get_type() == IPv4TCP);
CHECK(pkt_container->get_total_number_of_packets() >=
pkt_container->get_number_of_polled_packets());
CHECK(pkt_container->get_total_number_of_packets() == 1);
CHECK(pkt_container->get_number_of_polled_packets() == 0);
}
}
SECTION("create more packets than burst size", "[]") {
SECTION("fill till BURST_SIZE", "[]") {
for (int i = 0; i < BURST_SIZE; ++i) {
PacketInfo* pkt_info = pkt_container->get_empty_packet();
CHECK(pkt_info != nullptr);
}
CHECK(pkt_container->get_total_number_of_packets() == BURST_SIZE);
}
SECTION("fill till BURST_SIZE + 1", "[]") {
for (int i = 0; i < BURST_SIZE + 1; ++i) {
PacketInfo* pkt_info = pkt_container->get_empty_packet();
CHECK(pkt_info != nullptr);
}
CHECK(pkt_container->get_total_number_of_packets() ==
BURST_SIZE + 1);
}
CHECK(pkt_container->get_total_number_of_packets() >=
pkt_container->get_number_of_polled_packets());
}
SECTION("get_packet_at_index", "[]") {
SECTION("general", "[]") {
// add empty packet
PacketInfo* pkt_info_0 = pkt_container->get_empty_packet();
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 1);
PacketInfo* pkt_info_1 = pkt_container->get_packet_at_index(
pkt_container->get_total_number_of_packets() - 1);
CHECK(pkt_info_0 == pkt_info_1);
CHECK(pkt_info_1 != nullptr);
CHECK(pkt_info_1->get_mbuf() != nullptr);
CHECK(pkt_info_1->get_type() == IPv4TCP);
CHECK(pkt_container->get_total_number_of_packets() >=
pkt_container->get_number_of_polled_packets());
CHECK_NOTHROW(pkt_container->get_packet_at_index(
pkt_container->get_total_number_of_packets() - 1));
CHECK_THROWS(pkt_container->get_packet_at_index(
pkt_container->get_total_number_of_packets()));
}
SECTION("test out of bounds error", "[]") {
for (int i = 0; i < int(BURST_SIZE / 2); ++i) {
pkt_container->get_empty_packet();
}
CHECK(pkt_container->get_total_number_of_packets() ==
int(BURST_SIZE / 2));
for (int i = 0; i < int(BURST_SIZE / 2); ++i) {
CHECK_NOTHROW(pkt_container->get_packet_at_index(i));
}
for (int i = int(BURST_SIZE / 2); i < BURST_SIZE; ++i) {
CHECK_THROWS(pkt_container->get_packet_at_index(i));
}
CHECK_THROWS(pkt_container->get_packet_at_index(
pkt_container->get_total_number_of_packets()));
CHECK_NOTHROW(pkt_container->get_packet_at_index(
pkt_container->get_total_number_of_packets() - 1));
}
}
SECTION("take_packet and add_packet", "[]") {
PacketInfo* pkt_info_0 = pkt_container->get_empty_packet();
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 1);
PacketInfo* pkt_info_1 = pkt_container->take_packet(0);
CHECK(pkt_info_0 == pkt_info_1);
CHECK(pkt_info_1 != nullptr);
CHECK(pkt_info_1->get_mbuf() != nullptr);
CHECK(pkt_container->get_packet_at_index(0) == nullptr);
CHECK(pkt_container->get_total_number_of_packets() == 1);
CHECK(pkt_container->get_total_number_of_packets() >=
pkt_container->get_number_of_polled_packets());
pkt_container->add_packet(pkt_info_1);
CHECK(pkt_container->get_total_number_of_packets() >=
pkt_container->get_number_of_polled_packets());
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 2);
CHECK(pkt_container->get_packet_at_index(1) == pkt_info_1);
CHECK(pkt_container->get_packet_at_index(0) == nullptr);
}
SECTION("drop_packet", "[]") {
SECTION("default") {
PacketInfo* pkt_info_0 = pkt_container->get_empty_packet();
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 1);
pkt_container->drop_packet(0);
CHECK(pkt_container->get_total_number_of_packets() >=
pkt_container->get_number_of_polled_packets());
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 1);
CHECK(pkt_container->get_packet_at_index(0) == nullptr);
CHECK_NOTHROW(pkt_container->drop_packet(0));
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 1);
CHECK(pkt_container->get_packet_at_index(0) == nullptr);
}
}
SECTION("poll_packets", "[]") {
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 0);
uint16_t nb_pkts_polled;
pkt_container->poll_packets(nb_pkts_polled);
CHECK(pkt_container->get_number_of_polled_packets() > 0);
CHECK(pkt_container->get_total_number_of_packets() ==
pkt_container->get_number_of_polled_packets());
CHECK(nb_pkts_polled == pkt_container->get_number_of_polled_packets());
CHECK_NOTHROW(pkt_container->get_packet_at_index(nb_pkts_polled - 1));
PacketInfo* pkt_info =
pkt_container->get_packet_at_index(nb_pkts_polled - 1);
CHECK(pkt_info != nullptr);
CHECK(pkt_info->get_mbuf() != nullptr);
}
SECTION("send_packets", "[]") {
SECTION("do not drop") {
SECTION("send created packets") {
PacketInfo* pkt_info = pkt_container->get_empty_packet();
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 1);
}
SECTION("send polled packets") {
uint16_t nb_polled_pkts;
pkt_container->poll_packets(nb_polled_pkts);
CHECK(nb_polled_pkts > 0);
}
SECTION("send polled and created packets") {
uint16_t nb_polled_pkts;
pkt_container->poll_packets(nb_polled_pkts);
CHECK(nb_polled_pkts > 0);
PacketInfo* pkt_info = pkt_container->get_empty_packet();
CHECK(pkt_container->get_total_number_of_packets() >
pkt_container->get_number_of_polled_packets());
}
pkt_container->send_packets();
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 0);
}
SECTION("drop and send created packets") {
const int PKTS_TO_POLL = 4;
int nb_pkts_to_drop = -1;
for (int i = 0; i < PKTS_TO_POLL; ++i) {
pkt_container->get_empty_packet();
}
CHECK(pkt_container->get_total_number_of_packets() >
pkt_container->get_number_of_polled_packets());
CHECK(pkt_container->get_total_number_of_packets() == PKTS_TO_POLL);
SECTION("drop first packet") {
nb_pkts_to_drop = 1;
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
PKTS_TO_POLL);
CHECK(pkt_container->get_nb_pkts_dropped() == 0);
pkt_container->drop_packet(0);
CHECK(pkt_container->get_nb_pkts_dropped() == 1);
}
SECTION("drop second packet") {
nb_pkts_to_drop = 1;
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
PKTS_TO_POLL);
CHECK(pkt_container->get_nb_pkts_dropped() == 0);
pkt_container->drop_packet(1);
CHECK(pkt_container->get_nb_pkts_dropped() == 1);
}
SECTION("drop second and third packet") {
nb_pkts_to_drop = 2;
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
PKTS_TO_POLL);
CHECK(pkt_container->get_nb_pkts_dropped() == 0);
pkt_container->drop_packet(1);
pkt_container->drop_packet(2);
CHECK(pkt_container->get_nb_pkts_dropped() == 2);
}
SECTION("drop last packet") {
nb_pkts_to_drop = 1;
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
PKTS_TO_POLL);
CHECK(pkt_container->get_nb_pkts_dropped() == 0);
pkt_container->drop_packet(PKTS_TO_POLL - 1);
CHECK(pkt_container->get_nb_pkts_dropped() == 1);
}
pkt_container->reorder_mbuf_arrays();
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
PKTS_TO_POLL - nb_pkts_to_drop);
pkt_container->send_packets();
CHECK(pkt_container->get_number_of_polled_packets() == 0);
CHECK(pkt_container->get_total_number_of_packets() == 0);
}
}
// === D E L E T E O B J E C T S ===//
delete pkt_container;
pkt_container = nullptr;
}

132
test/PacketInfo_test.cpp Normal file
View File

@ -0,0 +1,132 @@
#include "ConfigurationManagement/Configurator.hpp"
#include "Initializer.hpp"
#include "PacketDissection/PacketInfo.hpp"
#include "PacketDissection/PacketInfoCreator.hpp"
#include "PacketDissection/PacketInfoIpv4Icmp.hpp"
#include "PacketDissection/PacketInfoIpv4Tcp.hpp"
#include "PacketDissection/PacketInfoIpv4Udp.hpp"
#include <catch2/catch.hpp>
#include <iostream>
TEST_CASE("Creation","[]")
{
SECTION("PacketInfo","[]"){
PacketInfo pkt_inf;
rte_mbuf* mbuf;
//REQUIRE_NOTHROW(pkt_inf.set_mbuf(mbuf));
//REQUIRE_NOTHROW(nbuf = pkt_inf.get_mbuf());
//CHECK(mbuf == nbuf);
CHECK(pkt_inf.get_type() == NONE);
}
SECTION("Creation: IPv4ICMP", "[]") {
// PacketInfoIpv4Icmp* pkt_inf =
// static_cast<PacketInfoIpv4Icmp*>(PacketInfoCreator::create_pkt_info(IPv4ICMP));
PacketInfoIpv4Icmp pkt_inf;
CHECK(pkt_inf.get_type() == IPv4ICMP);
// pkt_inf->set_mbuf(mbuf);
// struct rte_ipv4_hdr* ip_hdr = rte_pktmbuf_mtod_offset(mbuf, struct
// rte_ipv4_hdr*, 0); REQUIRE_NOTHROW(pkt_inf->set_ip_hdr(ip_hdr));
// struct rte_icmp_hdr* l4_header = rte_pktmbuf_mtod_offset(mbuf, struct
// rte_icmp_hdr*, 20);
// REQUIRE_NOTHROW(pkt_inf->set_icmp_hdr(l4_header)); uint32_t num;
// CHECK_NOTHROW(num= pkt_inf->get_dst_ip());
// CHECK_NOTHROW(num= pkt_inf->get_src_ip());
// CHECK_NOTHROW(num= pkt_inf->get_packet_size());
// CHECK_NOTHROW(num= pkt_inf->get_payload_size());
}
SECTION("Creation: IPv4TCP", "[]") {
// PacketInfoIpv4Tcp* pkt_inf =
// static_cast<PacketInfoIpv4Tcp*>(PacketInfoCreator::create_pkt_info(IPv4TCP));
PacketInfoIpv4Tcp pkt_inf;
CHECK(pkt_inf.get_type() == IPv4TCP);
// pkt_inf->set_mbuf(mbuf);
// struct rte_ipv4_hdr* ip_hdr = rte_pktmbuf_mtod_offset(mbuf, struct
// rte_ipv4_hdr*, 0); REQUIRE_NOTHROW(pkt_inf->set_ip_hdr(ip_hdr));
// struct rte_tcp_hdr* l4_header = rte_pktmbuf_mtod_offset(mbuf, struct
// rte_tcp_hdr*, 20); REQUIRE_NOTHROW(pkt_inf->set_tcp_hdr(l4_header));
// uint32_t num;
// uint32_t num2;
// CHECK_NOTHROW(num= pkt_inf->get_dst_ip());
// CHECK_NOTHROW(num= pkt_inf->get_src_ip());
// CHECK_NOTHROW(num= pkt_inf->get_packet_size());
// CHECK_NOTHROW(num= pkt_inf->get_payload_size());
// CHECK_NOTHROW(num= pkt_inf->get_dst_port());
// CHECK_NOTHROW(num= pkt_inf->get_src_port());
// CHECK_NOTHROW(num= pkt_inf->get_flags());
// CHECK_NOTHROW(num= pkt_inf->get_window_size());
// CHECK_NOTHROW(pkt_inf->set_ack_num(num));
// CHECK_NOTHROW(num2= pkt_inf->get_ack_num());
// CHECK(num == num2);
// CHECK_NOTHROW(pkt_inf->set_seq_num(num));
// CHECK_NOTHROW(num = pkt_inf->get_seq_num());
// CHECK(num == num2);
}
SECTION("Creation: IPv4UDP", "[]") {
// PacketInfoIpv4Udp* pkt_inf =
// static_cast<PacketInfoIpv4Udp*>(PacketInfoCreator::create_pkt_info(IPv4UDP));
PacketInfoIpv4Udp pkt_inf;
CHECK(pkt_inf.get_type() == IPv4UDP);
// pkt_inf->set_mbuf(mbuf);
// struct rte_ipv4_hdr* ip_hdr = rte_pktmbuf_mtod_offset(mbuf, struct
// rte_ipv4_hdr*, 0); REQUIRE_NOTHROW(pkt_inf->set_ip_hdr(ip_hdr));
// struct rte_udp_hdr* l4_header = rte_pktmbuf_mtod_offset(mbuf, struct
// rte_udp_hdr*, 20); REQUIRE_NOTHROW(pkt_inf->set_udp_hdr(l4_header));
// uint32_t num;
// CHECK_NOTHROW(num= pkt_inf->get_dst_ip());
// CHECK_NOTHROW(num= pkt_inf->get_src_ip());
// CHECK_NOTHROW(num= pkt_inf->get_packet_size());
// CHECK_NOTHROW(num= pkt_inf->get_payload_size());
// CHECK_NOTHROW(num= pkt_inf->get_dst_port());
// CHECK_NOTHROW(num= pkt_inf->get_src_port());
}
}
TEST_CASE("Transformation", "[]") {
SECTION("keeping Type", "[]") {
PacketInfo* pkt_inf;
// pkt_inf = PacketInfoCreator::create_pkt_info(IPv4ICMP);
pkt_inf = new PacketInfoIpv4Icmp;
PacketInfoIpv4Icmp* pkt_inf_icmp;
pkt_inf_icmp = static_cast<PacketInfoIpv4Icmp*>(pkt_inf);
CHECK(pkt_inf_icmp->get_type() == IPv4ICMP);
// PacketInfoCreator::create_pkt_info(IPv4TCP)
pkt_inf = new PacketInfoIpv4Tcp;
PacketInfoIpv4Tcp* pkt_inf_tcp;
pkt_inf_tcp = static_cast<PacketInfoIpv4Tcp*>(pkt_inf);
CHECK(pkt_inf_tcp->get_type() == IPv4TCP);
// PacketInfoCreator::create_pkt_info(IPv4UDP)
pkt_inf = new PacketInfoIpv4Udp;
PacketInfoIpv4Udp* pkt_inf_udp;
pkt_inf_udp = static_cast<PacketInfoIpv4Udp*>(pkt_inf);
CHECK(pkt_inf_udp->get_type() == IPv4UDP);
// PacketInfoCreator::create_pkt_info(NONE)
pkt_inf = new PacketInfo;
CHECK(pkt_inf->get_type() == NONE);
PacketInfo* pkt_inf_arr[5];
pkt_inf_arr[0] = pkt_inf_icmp;
pkt_inf_arr[1] = pkt_inf_tcp;
pkt_inf_arr[2] = pkt_inf_udp;
pkt_inf_arr[3] = pkt_inf;
CHECK(pkt_inf_arr[0]->get_type() == IPv4ICMP);
CHECK(pkt_inf_arr[1]->get_type() == IPv4TCP);
CHECK(pkt_inf_arr[2]->get_type() == IPv4UDP);
CHECK(pkt_inf_arr[3]->get_type() == NONE);
// clean up
delete pkt_inf;
delete pkt_inf_icmp;
delete pkt_inf_tcp;
delete pkt_inf_udp;
delete pkt_inf_arr;
}
}

View File

@ -0,0 +1,456 @@
#include "RandomNumberGenerator.hpp"
#include <catch2/catch.hpp>
#include <iostream>
#include <time.h> // is used for testing the time in the 3rd test case
TEST_CASE("random_number_generator_basic", "[]") {
// This test was written to check basic functions like whether different
// numbers are generated.
SECTION("check_whether_different_16", "[]") {
// creates a new RNG object
RandomNumberGenerator* xor_shift = new RandomNumberGenerator();
// creates two pseudo random 16 bit numbers
u_int16_t test_1 = xor_shift->gen_rdm_16_bit();
u_int16_t test_2 = xor_shift->gen_rdm_16_bit();
// printes these numbers out
std::cout << "1st generated 16 bit int: " << test_1 << std::endl;
std::cout << "2nd generated 16 bit int: " << test_2 << std::endl;
// checks whether these numbers are different
// test1 == test2 wouldn't means that the test isn't random.
// This section just exists to see whether the algorithm basically works
// and generates numbers.
CHECK(test_1 != test_2);
}
// The same test like above but for the 32 bit algorithm:
SECTION("CheckWhetherDifferent32", "[]") {
RandomNumberGenerator xor_shift;
u_int32_t test_1 = xor_shift.gen_rdm_32_bit();
u_int32_t test_2 = xor_shift.gen_rdm_32_bit();
std::cout << "1st generated 32 bit int: " << test_1 << std::endl;
std::cout << "2nd generated 32 bit int: " << test_2 << std::endl;
CHECK(test_1 != test_2);
}
// The same test like above but for the 64 bit algorithm:
SECTION("CheckWhetherDifferent64", "[]") {
RandomNumberGenerator xor_shift;
u_int64_t test_1 = xor_shift.gen_rdm_64_bit();
u_int64_t test_2 = xor_shift.gen_rdm_64_bit();
std::cout << "1st generated 64 bit int: " << test_1 << std::endl;
std::cout << "2nd generated 64 bit int: " << test_2 << std::endl;
CHECK(test_1 != test_2);
// empty line for better layout in testlog
std::cout << std::endl;
}
// This test checks the type of the return value.
// This particular section is for 16 bit
SECTION("CheckSize16", "[]") {
// Creates a RNG and generates a number like above
RandomNumberGenerator xor_shift;
u_int16_t test_value = xor_shift.gen_rdm_16_bit();
std::cout << "values that are generated for checking the size: "
<< std::endl;
std::cout << "generated value: " << test_value << std::endl;
// checks wheter the size of the return value is 16 bit or 2 byte
CHECK(sizeof(test_value) == 2);
}
// The same for 32 bit
SECTION("CheckSize32", "[]") {
RandomNumberGenerator xor_shift;
u_int32_t test_value = xor_shift.gen_rdm_32_bit();
std::cout << "generated value (32 bit): " << test_value << std::endl;
// checks wheter the size of the return value is 32 bit or 4 byte
CHECK(sizeof(test_value) == 4);
}
// The same for 64 bit
SECTION("CheckSize64", "[]") {
RandomNumberGenerator xor_shift;
u_int64_t test_value = xor_shift.gen_rdm_64_bit();
std::cout << "generated value (64 bit): " << test_value << std::endl;
// checks wheter the size of the return value is 64 bit or 8 byte
CHECK(sizeof(test_value) == 8);
std::cout << std::endl;
}
// Checks whether different numbers are generated when using different RNGs
SECTION(
"Check whether different when using different RNG objects for 16 bit",
"[]") {
// creating two objects
RandomNumberGenerator xor_shift_1;
RandomNumberGenerator xor_shift_2;
// generating two values of 16 bit
std::cout << "16 bit seed 1: " << xor_shift_1._seed_x16 << std::endl;
std::cout << "16 bit seed 2: " << xor_shift_2._seed_x16 << std::endl;
u_int16_t test_1_16_bit = xor_shift_1.gen_rdm_16_bit();
u_int16_t test_2_16_bit = xor_shift_2.gen_rdm_16_bit();
CHECK(test_1_16_bit != test_2_16_bit);
}
// the same for 32 bit again:
SECTION(
"Check whether different when using different RNG objects for 32 bit",
"[]") {
RandomNumberGenerator xor_shift_1;
RandomNumberGenerator xor_shift_2;
std::cout << "32 bit seed 1: " << xor_shift_1._seed_x32 << std::endl;
std::cout << "32 bit seed 2: " << xor_shift_2._seed_x32 << std::endl;
u_int32_t test_1_32_bit = xor_shift_1.gen_rdm_32_bit();
u_int32_t test_2_32_bit = xor_shift_2.gen_rdm_32_bit();
CHECK(test_1_32_bit != test_2_32_bit);
}
// the same for 64 bit again:
SECTION(
"Check whether different when using different RNG objects for 64 bit",
"[]") {
RandomNumberGenerator xor_shift_1;
RandomNumberGenerator xor_shift_2;
std::cout << "64 bit seed 1: " << xor_shift_1._seed_x64 << std::endl;
std::cout << "64 bit seed 2: " << xor_shift_2._seed_x64 << std::endl;
u_int64_t test_1_64_bit = xor_shift_1.gen_rdm_64_bit();
u_int64_t test_2_64_bit = xor_shift_2.gen_rdm_64_bit();
CHECK(test_1_64_bit != test_2_64_bit);
std::cout << std::endl;
}
// This test checks whether two RNGs generate the same number after the seed
// is set to the same number
SECTION("Check whether the same numbers are generated with the same seed "
"for 16 bit",
"[]") {
RandomNumberGenerator xor_shift_1;
RandomNumberGenerator xor_shift_2;
// set the seed to the same value in both RNGs
xor_shift_1._seed_x16 = 30000;
xor_shift_2._seed_x16 = 30000;
std::cout << "16 bit seed for RNG 1: " << xor_shift_1._seed_x16
<< std::endl;
std::cout << "16 bit seed for RNG 2: " << xor_shift_2._seed_x16
<< std::endl;
u_int16_t test_1_16_bit = xor_shift_1.gen_rdm_16_bit();
u_int16_t test_2_16_bit = xor_shift_2.gen_rdm_16_bit();
std::cout << "number generated from RNG 1: " << xor_shift_1._seed_x16
<< std::endl;
std::cout << "number generated from RNG 2: " << xor_shift_2._seed_x16
<< std::endl;
// check whether the results are the same too
CHECK(test_1_16_bit == test_2_16_bit);
std::cout << std::endl;
}
// the same test for 32 bit
SECTION("Check whether the same numbers are generated with the same seed "
"for 32 bit",
"[]") {
RandomNumberGenerator xor_shift_1;
RandomNumberGenerator xor_shift_2;
// set the seed to the same value in both RNGs
xor_shift_1._seed_x32 = 30000000;
xor_shift_2._seed_x32 = 30000000;
std::cout << "32 bit seed for RNG 1: " << xor_shift_1._seed_x32
<< std::endl;
std::cout << "32 bit seed for RNG 2: " << xor_shift_2._seed_x32
<< std::endl;
u_int32_t test_1_32_bit = xor_shift_1.gen_rdm_32_bit();
u_int32_t test_2_32_bit = xor_shift_2.gen_rdm_32_bit();
std::cout << "number generated from RNG 1: " << xor_shift_1._seed_x32
<< std::endl;
std::cout << "number generated from RNG 2: " << xor_shift_2._seed_x32
<< std::endl;
// check whether the results are the same too
CHECK(test_1_32_bit == test_2_32_bit);
std::cout << std::endl;
}
// the same test for 64 bit
SECTION("Check whether the same numbers are generated with the same seed "
"for 64 bit",
"[]") {
RandomNumberGenerator xor_shift_1;
RandomNumberGenerator xor_shift_2;
// set the seed to the same value in both RNGs
xor_shift_1._seed_x64 = 30000000000;
xor_shift_2._seed_x64 = 30000000000;
std::cout << "64 bit seed for RNG 1: " << xor_shift_1._seed_x64
<< std::endl;
std::cout << "64 bit seed for RNG 2: " << xor_shift_2._seed_x64
<< std::endl;
u_int64_t test_1_64_bit = xor_shift_1.gen_rdm_64_bit();
u_int64_t test_2_64_bit = xor_shift_2.gen_rdm_64_bit();
std::cout << "number generated from RNG 1: " << xor_shift_1._seed_x64
<< std::endl;
std::cout << "number generated from RNG 2: " << xor_shift_2._seed_x64
<< std::endl;
// check whether the results are the same too
CHECK(test_1_64_bit == test_2_64_bit);
std::cout << std::endl;
}
SECTION("Check whether generated numbers are really in the interval",
"[]") {
RandomNumberGenerator xor_shift;
u_int16_t test_value;
int lower_limit = 1024;
int upper_limit = 49151;
bool no_number_has_been_outside_the_interval = true;
// inside the for loop an if statement checks for 1,000,000 generated
// numbers whether they are really in the interval
for (int i = 0; i < 10000000; i++) {
test_value =
xor_shift.gen_rdm_16_bit_in_interval(lower_limit, upper_limit);
if (test_value < lower_limit || test_value > upper_limit) {
no_number_has_been_outside_the_interval = false;
}
}
std::cout << "No generated number has been outside the interval? (1 "
"means true) --> "
<< no_number_has_been_outside_the_interval << std::endl;
std::cout << std::endl;
CHECK(no_number_has_been_outside_the_interval == true);
}
}
TEST_CASE("RandomNumberGeneratorStatistics", "[]") {
// The result of the chi square test shows how uniform the generated numbers
// are distributed
// A big chi square means that the actual frequencies vary widely from the
// theoretical frequencies.
SECTION("ChiSquare16", "[]") {
RandomNumberGenerator xor_shift;
// 65536 - 1 = 2 ^ 16 different numbers can be generated
int r = 65536 - 1;
// 1,000,000 numbers are generated
int n = 1000000;
u_int16_t t;
// this array counts how often each number from 0 to r is returned as a
// result
int f[r] = {};
for (int i = 0; i < r; i++) {
f[i] = 0;
}
for (int i = 1; i < n; i++) {
t = xor_shift.gen_rdm_16_bit_in_interval(1024, 49151);
f[t]++;
}
double chisquare = 0.0;
for (int i = 0; i < r; i++) {
// chi square is calculated
chisquare = chisquare + ((f[i] - n / r) * (f[i] - n / r) / (n / r));
}
std::cout << "+++ chi square test for gen_rdm_16_bit_in_interval() +++"
<< std::endl;
std::cout << "chi square is: " << chisquare << std::endl;
double k = sqrt(chisquare / (n + chisquare));
std::cout << "k is: " << k << std::endl;
// k is in [0; k_max] with k_max ≈ 1
// Calculating k_norm wouldn't make sense.
// 0 means that every number is generated equally frequent
// 1 means not random at all
// A bad result could be improved by returning _seed_x16 instead of
// _seed_x16 % 48128 + 1024 (valid port number) in
// RandomNumberGenerator.cpp.
CHECK(k < 1.0);
std::cout << std::endl;
}
SECTION("ChiSquare16", "[]") {
RandomNumberGenerator xor_shift;
// 65526 - 1 = 2 ^ 16 different numbers can be generated
int r = 65536 - 1;
// 1,000,000 numbers are generated
int n = 1000000;
u_int16_t t;
// this array counts how often each number from 0 to r is returned as a
// result
int f[r] = {};
for (int i = 0; i < r; i++) {
f[i] = 0;
}
for (int i = 1; i < n; i++) {
t = xor_shift.gen_rdm_16_bit();
f[t]++;
}
double chisquare = 0.0;
for (int i = 0; i < r; i++) {
// chi square is calculated
chisquare = chisquare + ((f[i] - n / r) * (f[i] - n / r) / (n / r));
}
std::cout << "+++ chi square test for gen_rdm_16_bit() +++"
<< std::endl;
std::cout << "chi square is: " << chisquare << std::endl;
double k = sqrt(chisquare / (n + chisquare));
std::cout << "k is: " << k << std::endl;
CHECK(k < 1.0);
std::cout << std::endl;
}
// the following test fails due to an segmentation violation signal
// 32 bit seems to be to big
/*SECTION("ChiSquare32", "[]") {
RandomNumberGenerator xor_shift;
u_int32_t r = 4294967296 - 1;
u_int64_t n = 10000000000;
u_int32_t t;
int f[r] = {};
for (u_int64_t i = 0; i < r; i++) {
f[i] = 0;
}
for (u_int64_t i = 1; i < n; i++) {
t = xor_shift.gen_rdm_32_bit();
f[t]++;
}
double chisquare = 0.0;
for (int i = 0; i < r; i++) {
chisquare = chisquare + ((f[i] - n / r) * (f[i] - n / r) / (n / r));
}
std::cout << "chi square is: " << chisquare << std::endl;
double k = sqrt(chisquare / (n + chisquare));
std::cout << "k is: " << k << std::endl;
// k is in [0; k_max] with k_max ≈ 1
// 0 means independence
// 1 means not random at all
CHECK(k < 1.0);
}*/
}
TEST_CASE("RandomNumberGeneratorTime", "[]") {
// The following section is calculating the time for generating n
// 16 bit numbers with a single RNG object
SECTION("TestTime16", "[]") {
// the following two lines initialize and start the timer
double time1 = 0.0, tstart;
tstart = clock();
// creating a RNG object
RandomNumberGenerator xor_shift;
// amount of numbers generated
// can be changed if neccessary, but it you will get a segmentation
// violation error if it's to big
long n = 10000000;
// variable to store generated number
uint16_t test_value;
// generating those numbers
for (long i = 0; i < n; i++) {
test_value = xor_shift.gen_rdm_16_bit();
}
// stops the timer and calculates the difference between start and end
time1 += clock() - tstart;
// prints out the time
std::cout << "time needed to generate " << n
<< " 16 bit numbers: " << time1 / CLOCKS_PER_SEC << " s"
<< std::endl;
CHECK(time1 / CLOCKS_PER_SEC < 10.0);
}
// This test calculates the time needed to generate a certain amount of 16
// bit ints with rand() allowowing a comparison with xorShift
SECTION("TestTime16Rand", "[]") {
double time1 = 0.0, tstart;
tstart = clock();
long n = 10000000;
uint16_t test_value;
for (long i = 0; i < n; i++) {
test_value = rand();
}
time1 += clock() - tstart;
std::cout << "time needed to generate " << n
<< " 16 bit numbers with rand(): " << time1 / CLOCKS_PER_SEC
<< " s" << std::endl;
CHECK(time1 / CLOCKS_PER_SEC < 10.0);
}
// the same test for 32 bit numbers
SECTION("TestTime32", "[]") {
double time1 = 0.0, tstart;
tstart = clock();
RandomNumberGenerator xor_shift;
long n = 10000000;
uint32_t test_value;
for (long i = 0; i < n; i++) {
test_value = xor_shift.gen_rdm_32_bit();
}
time1 += clock() - tstart;
std::cout << "time needed to generate " << n
<< " 32 bit numbers: " << time1 / CLOCKS_PER_SEC << " s"
<< std::endl;
CHECK(time1 / CLOCKS_PER_SEC < 1.0);
}
// true 32 bit numbers with shifting and rand() for comparison to the
// section above
SECTION("TestTime32Rand", "[]") {
double time1 = 0.0, tstart;
tstart = clock();
long n = 10000000;
uint32_t test_value;
for (long i = 0; i < n; i++) {
test_value = (uint16_t)rand();
test_value |= (uint16_t)rand() << 16;
}
time1 += clock() - tstart;
std::cout << "time needed to generate " << n
<< " 32 bit numbers with rand() and shifting: "
<< time1 / CLOCKS_PER_SEC << " s" << std::endl;
CHECK(time1 / CLOCKS_PER_SEC < 1.0);
}
// the same test for 64 bit numbers
SECTION("TestTime64", "[]") {
double time1 = 0.0, tstart;
tstart = clock();
RandomNumberGenerator xor_shift;
long n = 10000000;
uint64_t test_value;
for (long i = 0; i < n; i++) {
test_value = xor_shift.gen_rdm_64_bit();
}
time1 += clock() - tstart;
std::cout << "time needed to generate " << n
<< " 64 bit numbers: " << time1 / CLOCKS_PER_SEC << " s"
<< std::endl;
CHECK(time1 / CLOCKS_PER_SEC < 1.0);
}
// true 64 bit numbers with shifting and rand() for comparison to the
// section above
SECTION("TestTime64Rand", "[]") {
double time1 = 0.0, tstart;
tstart = clock();
long n = 10000000;
uint64_t test_value = 0;
for (long i = 0; i < n; i++) {
// the following lines have been copied from
// Treatment::create_cookie_secret()
u_int64_t value1 = (uint16_t)rand();
value1 = (value1 << 48);
test_value |= value1;
u_int64_t value2 = (uint16_t)rand();
value2 = (value2 << 32);
test_value |= value2;
u_int64_t value3 = (uint16_t)rand();
test_value |= value3;
}
time1 += clock() - tstart;
std::cout << "time needed to generate " << n
<< " 64 bit numbers with rand() and shifting: "
<< time1 / CLOCKS_PER_SEC << " s" << std::endl;
CHECK(time1 / CLOCKS_PER_SEC < 1.0);
}
}

1301
test/Treatment_test.cpp Normal file

File diff suppressed because it is too large Load Diff

14
test/dpdk_dummy_test.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <catch2/catch.hpp>
#include <cstdio>
#include <iostream>
#include <rte_mbuf.h>
#include <rte_mempool.h>
TEST_CASE("rte_mbuf", "[]") {
struct rte_mbuf* mbuf;
struct rte_mempool* mempool = nullptr;
mbuf = rte_pktmbuf_alloc(mempool);
CHECK(mbuf != nullptr);
}

View File

@ -1,15 +1,18 @@
# ..each entry will be a test executable
# ..the structure of the test directory should be equivalent
# ..to the structure in source/ or include/
# each entry will be a test executable.
# the structure of the test directory should be equivalent
# to the structure in source/ or include/
# Syntax:
# 'Name of the Test (e.g. class name)' : 'Path/to/source/file'
# Please keep all alphabetically sorted
test_sources_dict = {
#'PacketInfo' : 'test/PacketDissection/PacketInfo_test.cpp',
'PacketContainer' : 'test/PacketDissection/PacketContainer_test.cpp',
'Configurator' : 'test/ConfigurationManagement/Configurator_test.cpp',
'libdpdk_dummy' : 'test/libdpdk_dummy/libdpdk_dummy_test.cpp',
'Treatment' : 'test/Treatment/Treatment_test.cpp',
'RandomNumberGenerator' : 'test/RandomNumberGenerator/RandomNumberGenerator_test.cpp',
#'Attacker' : 'test/Attacker_test.cpp',
'Configurator' : 'test/Configurator_test.cpp',
#'Initializer' : 'test/Initializer_test.cpp',
#'Inspection' : 'test/Inspection_test.cpp',
'DPDK_dummy' : 'test/dpdk_dummy_test.cpp',
'PacketContainer' : 'test/PacketContainer_test.cpp',
#'PacketInfo' : 'test/PacketInfo_test.cpp',
'RandomNumberGenerator' : 'test/RandomNumberGenerator_test.cpp',
#'Treatment' : 'test/Treatment_test.cpp'
}