diff --git a/.github/workflows/run_all_tests.yml b/.github/workflows/run_all_tests.yml index cba7b25..0b8fcc0 100644 --- a/.github/workflows/run_all_tests.yml +++ b/.github/workflows/run_all_tests.yml @@ -22,15 +22,13 @@ jobs: - uses: actions/checkout@v4 - name: build run: | - cmake ../src - make -j4 + ./build_for_testing.sh working-directory: build - - name: build_tests + - name: build_integration_tests run: | - cmake . - make -j4 - working-directory: tests - - name: run_tests + ./build_tests.sh + working-directory: tests/integration_tests/build + - name: run_integration_tests run: | - sudo ./output/run_tests - working-directory: tests + sudo ./run_tests + working-directory: tests/integration_tests/output diff --git a/.gitignore b/.gitignore index 56f6439..4e55331 100644 --- a/.gitignore +++ b/.gitignore @@ -1,17 +1,16 @@ -.DS_Store +*.DS_Store -/build/Makefile -/build/CMakeCache.txt -/build/cmake_install.cmake -/build/CMakeFiles/ +CMakeCache.txt +CMakeFiles/ +cmake_install.cmake +install_manifest.txt +Makefile +*.cmake -/build/output/ +!/build/cmake + +compile_commands.json -/tests/Makefile -/tests/CMakeCache.txt -/tests/cmake_install.cmake -/tests/CMakeFiles/ -/tests/output/ -/tests/run_all_tests +/build/output/ *.dSYM/ diff --git a/build/build_for_release.sh b/build/build_for_release.sh index 0e1d498..ae16c7b 100755 --- a/build/build_for_release.sh +++ b/build/build_for_release.sh @@ -1,2 +1,2 @@ -cmake ../src -DCMAKE_BUILD_TYPE=Release +cmake ../src -DCMAKE_BUILD_TYPE=Release -DSANITIZER=none make -B -j8 diff --git a/build/cmake/deadlightreal-swiftnetConfig.cmake b/build/cmake/deadlightreal-swiftnetConfig.cmake deleted file mode 100644 index 9b12742..0000000 --- a/build/cmake/deadlightreal-swiftnetConfig.cmake +++ /dev/null @@ -1,28 +0,0 @@ - -####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() ####### -####### Any changes to this file will be overwritten by the next CMake run #### -####### The input file was deadlightreal-swiftnetConfig.cmake.in ######## - -get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../" ABSOLUTE) - -macro(set_and_check _var _file) - set(${_var} "${_file}") - if(NOT EXISTS "${_file}") - message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !") - endif() -endmacro() - -macro(check_required_components _NAME) - foreach(comp ${${_NAME}_FIND_COMPONENTS}) - if(NOT ${_NAME}_${comp}_FOUND) - if(${_NAME}_FIND_REQUIRED_${comp}) - set(${_NAME}_FOUND FALSE) - endif() - endif() - endforeach() -endmacro() - -#################################################################################### - -include("${CMAKE_CURRENT_LIST_DIR}/swiftnetTargets.cmake") -set(SWIFTNET_LIBRARIES swiftnet::swiftnet) diff --git a/build/cmake/deadlightreal-swiftnetConfigVersion.cmake b/build/cmake/deadlightreal-swiftnetConfigVersion.cmake deleted file mode 100644 index 6184b17..0000000 --- a/build/cmake/deadlightreal-swiftnetConfigVersion.cmake +++ /dev/null @@ -1,43 +0,0 @@ -# This is a basic version file for the Config-mode of find_package(). -# It is used by write_basic_package_version_file() as input file for configure_file() -# to create a version-file which can be installed along a config.cmake file. -# -# The created file sets PACKAGE_VERSION_EXACT if the current version string and -# the requested version string are exactly the same and it sets -# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version. -# The variable CVF_VERSION must be set before calling configure_file(). - -set(PACKAGE_VERSION "0.2.0") - -if (PACKAGE_FIND_VERSION_RANGE) - # Package version must be in the requested version range - if ((PACKAGE_FIND_VERSION_RANGE_MIN STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION_MIN) - OR ((PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_GREATER PACKAGE_FIND_VERSION_MAX) - OR (PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND PACKAGE_VERSION VERSION_GREATER_EQUAL PACKAGE_FIND_VERSION_MAX))) - set(PACKAGE_VERSION_COMPATIBLE FALSE) - else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - endif() -else() - if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) - set(PACKAGE_VERSION_COMPATIBLE FALSE) - else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION) - set(PACKAGE_VERSION_EXACT TRUE) - endif() - endif() -endif() - - -# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it: -if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "") - return() -endif() - -# check that the installed version has the same 32/64bit-ness as the one which is currently searching: -if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8") - math(EXPR installedBits "8 * 8") - set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)") - set(PACKAGE_VERSION_UNSUITABLE TRUE) -endif() diff --git a/build/cmake/morcules-swiftnetConfig.cmake b/build/cmake/morcules-swiftnetConfig.cmake deleted file mode 100644 index 971545b..0000000 --- a/build/cmake/morcules-swiftnetConfig.cmake +++ /dev/null @@ -1,28 +0,0 @@ - -####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() ####### -####### Any changes to this file will be overwritten by the next CMake run #### -####### The input file was morcules-swiftnetConfig.cmake.in ######## - -get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../" ABSOLUTE) - -macro(set_and_check _var _file) - set(${_var} "${_file}") - if(NOT EXISTS "${_file}") - message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !") - endif() -endmacro() - -macro(check_required_components _NAME) - foreach(comp ${${_NAME}_FIND_COMPONENTS}) - if(NOT ${_NAME}_${comp}_FOUND) - if(${_NAME}_FIND_REQUIRED_${comp}) - set(${_NAME}_FOUND FALSE) - endif() - endif() - endforeach() -endmacro() - -#################################################################################### - -include("${CMAKE_CURRENT_LIST_DIR}/swiftnetTargets.cmake") -set(SWIFTNET_LIBRARIES swiftnet::swiftnet) diff --git a/build/cmake/morcules-swiftnetConfigVersion.cmake b/build/cmake/morcules-swiftnetConfigVersion.cmake deleted file mode 100644 index 6184b17..0000000 --- a/build/cmake/morcules-swiftnetConfigVersion.cmake +++ /dev/null @@ -1,43 +0,0 @@ -# This is a basic version file for the Config-mode of find_package(). -# It is used by write_basic_package_version_file() as input file for configure_file() -# to create a version-file which can be installed along a config.cmake file. -# -# The created file sets PACKAGE_VERSION_EXACT if the current version string and -# the requested version string are exactly the same and it sets -# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version. -# The variable CVF_VERSION must be set before calling configure_file(). - -set(PACKAGE_VERSION "0.2.0") - -if (PACKAGE_FIND_VERSION_RANGE) - # Package version must be in the requested version range - if ((PACKAGE_FIND_VERSION_RANGE_MIN STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION_MIN) - OR ((PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_GREATER PACKAGE_FIND_VERSION_MAX) - OR (PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND PACKAGE_VERSION VERSION_GREATER_EQUAL PACKAGE_FIND_VERSION_MAX))) - set(PACKAGE_VERSION_COMPATIBLE FALSE) - else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - endif() -else() - if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) - set(PACKAGE_VERSION_COMPATIBLE FALSE) - else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION) - set(PACKAGE_VERSION_EXACT TRUE) - endif() - endif() -endif() - - -# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it: -if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "") - return() -endif() - -# check that the installed version has the same 32/64bit-ness as the one which is currently searching: -if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8") - math(EXPR installedBits "8 * 8") - set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)") - set(PACKAGE_VERSION_UNSUITABLE TRUE) -endif() diff --git a/build/cmake/swiftnetConfig.cmake b/build/cmake/swiftnetConfig.cmake deleted file mode 100644 index fbec91e..0000000 --- a/build/cmake/swiftnetConfig.cmake +++ /dev/null @@ -1,28 +0,0 @@ - -####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() ####### -####### Any changes to this file will be overwritten by the next CMake run #### -####### The input file was swiftnetConfig.cmake.in ######## - -get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../" ABSOLUTE) - -macro(set_and_check _var _file) - set(${_var} "${_file}") - if(NOT EXISTS "${_file}") - message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !") - endif() -endmacro() - -macro(check_required_components _NAME) - foreach(comp ${${_NAME}_FIND_COMPONENTS}) - if(NOT ${_NAME}_${comp}_FOUND) - if(${_NAME}_FIND_REQUIRED_${comp}) - set(${_NAME}_FOUND FALSE) - endif() - endif() - endforeach() -endmacro() - -#################################################################################### - -include("${CMAKE_CURRENT_LIST_DIR}/swiftnetTargets.cmake") -set(SWIFTNET_LIBRARIES swiftnet::swift_net) diff --git a/build/cmake/swiftnetConfigVersion.cmake b/build/cmake/swiftnetConfigVersion.cmake deleted file mode 100644 index a445dd1..0000000 --- a/build/cmake/swiftnetConfigVersion.cmake +++ /dev/null @@ -1,43 +0,0 @@ -# This is a basic version file for the Config-mode of find_package(). -# It is used by write_basic_package_version_file() as input file for configure_file() -# to create a version-file which can be installed along a config.cmake file. -# -# The created file sets PACKAGE_VERSION_EXACT if the current version string and -# the requested version string are exactly the same and it sets -# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version. -# The variable CVF_VERSION must be set before calling configure_file(). - -set(PACKAGE_VERSION "1.0.0") - -if (PACKAGE_FIND_VERSION_RANGE) - # Package version must be in the requested version range - if ((PACKAGE_FIND_VERSION_RANGE_MIN STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION_MIN) - OR ((PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_GREATER PACKAGE_FIND_VERSION_MAX) - OR (PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND PACKAGE_VERSION VERSION_GREATER_EQUAL PACKAGE_FIND_VERSION_MAX))) - set(PACKAGE_VERSION_COMPATIBLE FALSE) - else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - endif() -else() - if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) - set(PACKAGE_VERSION_COMPATIBLE FALSE) - else() - set(PACKAGE_VERSION_COMPATIBLE TRUE) - if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION) - set(PACKAGE_VERSION_EXACT TRUE) - endif() - endif() -endif() - - -# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it: -if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "") - return() -endif() - -# check that the installed version has the same 32/64bit-ness as the one which is currently searching: -if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8") - math(EXPR installedBits "8 * 8") - set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)") - set(PACKAGE_VERSION_UNSUITABLE TRUE) -endif() diff --git a/build/install_manifest.txt b/build/install_manifest.txt deleted file mode 100644 index 7bff2fe..0000000 --- a/build/install_manifest.txt +++ /dev/null @@ -1,6 +0,0 @@ -/usr/local/lib/libswift_net.a -/usr/local/include/swift_net.h -/usr/local/lib/cmake/swiftnet/swiftnetTargets.cmake -/usr/local/lib/cmake/swiftnet/swiftnetTargets-debug.cmake -/usr/local/lib/cmake/swiftnet/swiftnetConfig.cmake -/usr/local/lib/cmake/swiftnet/swiftnetConfigVersion.cmake \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 29eef46..e21ce84 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,8 @@ include(GNUInstallDirs) set(CMAKE_C_STANDARD 99) +option(SWIFT_NET_INTERNAL_TESTING "Enable internal testing features" OFF) + set(SOURCE_FILES initialize_swift_net.c send_packet.c @@ -50,11 +52,15 @@ endif() add_library(swiftnet STATIC ${SOURCE_FILES}) if (SWIFT_NET_INTERNAL_TESTING) + message("Internal Testing Enabled") + add_compile_definitions(SWIFT_NET_INTERNAL_TESTING) target_compile_options(swiftnet PRIVATE -O0) target_link_options(swiftnet PRIVATE -O0) else() + message("Internal Testing Disabled") + target_compile_options(swiftnet PRIVATE -O3) target_link_options(swiftnet PRIVATE -O3) endif() diff --git a/src/initialize_swift_net.c b/src/initialize_swift_net.c index 7fe8fef..43493fb 100644 --- a/src/initialize_swift_net.c +++ b/src/initialize_swift_net.c @@ -19,7 +19,7 @@ #endif uint32_t maximum_transmission_unit = 0x00; -struct in_addr private_ip_address; +struct in_addr private_ip_address_testing; uint8_t mac_address[6]; char default_network_interface[SIZEOF_FIELD(struct ifreq, ifr_name)]; @@ -77,7 +77,7 @@ void swiftnet_initialize() { exit(EXIT_FAILURE); } - private_ip_address = ((struct sockaddr_in *)&private_sockaddr)->sin_addr; + private_ip_address_testing = ((struct sockaddr_in *)&private_sockaddr)->sin_addr; const int got_default_interface = get_default_interface_and_mac(default_network_interface, sizeof(default_network_interface), mac_address, temp_socket); if(unlikely(got_default_interface != 0)) { diff --git a/src/internal/internal.h b/src/internal/internal.h index 37d4154..208f727 100644 --- a/src/internal/internal.h +++ b/src/internal/internal.h @@ -137,7 +137,7 @@ extern void* swiftnet_client_process_packets(void* const void_client); extern void* execute_packet_callback_client(void* const void_client); extern void* execute_packet_callback_server(void* const void_server); -extern struct in_addr private_ip_address; +extern struct in_addr private_ip_address_testing; extern uint8_t mac_address[6]; extern char default_network_interface[SIZEOF_FIELD(struct ifreq, ifr_name)]; extern pcap_t* swiftnet_pcap_open(const char* interface); @@ -265,7 +265,7 @@ static struct ip construct_ip_header(struct in_addr destination_addr, const uint .ip_off = htons(0), // Not used .ip_ttl = 64,// Time to live .ip_sum = htons(0), // Checksum - .ip_src = private_ip_address, // Source ip + .ip_src = private_ip_address_testing, // Source ip .ip_dst = destination_addr // Destination ip }; diff --git a/src/swift_net.h b/src/swift_net.h index 1c5a271..5f99b68 100644 --- a/src/swift_net.h +++ b/src/swift_net.h @@ -39,8 +39,8 @@ enum PacketType { SEND_LOST_PACKETS_REQUEST = 0x03, SEND_LOST_PACKETS_RESPONSE = 0x04, SUCCESSFULLY_RECEIVED_PACKET = 0x05, - REQUEST = 0x06, #ifdef SWIFT_NET_REQUESTS + REQUEST = 0x06, RESPONSE = 0x07, #endif }; @@ -397,7 +397,7 @@ extern void swiftnet_server_make_response( #ifdef SWIFT_NET_DEBUG // Adds one or more debug flags to the global debugger state. extern void swiftnet_add_debug_flags(const uint32_t flags); -// Removes one or more debug flags from the global debugger state. + // Removes one or more debug flags from the global debugger state. extern void swiftnet_remove_debug_flags(const uint32_t flags); #endif diff --git a/tests/config.h b/tests/config.h deleted file mode 100644 index af5c5c6..0000000 --- a/tests/config.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#define IP_ADDRESS "127.0.0.1" -#define LOOPBACK true diff --git a/tests/.clangd b/tests/integration_tests/.clangd similarity index 100% rename from tests/.clangd rename to tests/integration_tests/.clangd diff --git a/tests/CMakeLists.txt b/tests/integration_tests/build/CMakeLists.txt similarity index 84% rename from tests/CMakeLists.txt rename to tests/integration_tests/build/CMakeLists.txt index 954d286..7b67bbc 100644 --- a/tests/CMakeLists.txt +++ b/tests/integration_tests/build/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.15) project(SwiftNetTests C) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../output) if(SANITIZER STREQUAL "address") message(STATUS "Using AddressSanitizer") @@ -19,11 +19,11 @@ else() message(STATUS "No sanitizer enabled") endif() -link_directories(${CMAKE_SOURCE_DIR}/../build/output) +link_directories(${CMAKE_SOURCE_DIR}/../../../build/output) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0") -file(GLOB SRC_FILES "${CMAKE_SOURCE_DIR}/src/*.c") +file(GLOB SRC_FILES "${CMAKE_SOURCE_DIR}/../src/*.c") add_executable(run_tests ${SRC_FILES}) diff --git a/tests/build_tests.sh b/tests/integration_tests/build/build_tests.sh similarity index 100% rename from tests/build_tests.sh rename to tests/integration_tests/build/build_tests.sh diff --git a/tests/integration_tests/output/run_tests b/tests/integration_tests/output/run_tests new file mode 100755 index 0000000..52b8d90 Binary files /dev/null and b/tests/integration_tests/output/run_tests differ diff --git a/tests/src/making_request.c b/tests/integration_tests/src/making_request.c similarity index 99% rename from tests/src/making_request.c rename to tests/integration_tests/src/making_request.c index 75ba9b7..f1d3955 100644 --- a/tests/src/making_request.c +++ b/tests/integration_tests/src/making_request.c @@ -1,8 +1,7 @@ -#include "../config.h" #include #include #include -#include "../../src/swift_net.h" +#include "../../../src/swift_net.h" #include #include #include diff --git a/tests/src/run_tests.c b/tests/integration_tests/src/run_tests.c similarity index 79% rename from tests/src/run_tests.c rename to tests/integration_tests/src/run_tests.c index 9b4cc7f..243129d 100644 --- a/tests/src/run_tests.c +++ b/tests/integration_tests/src/run_tests.c @@ -1,5 +1,5 @@ #include "run_tests.h" -#include "../../src/swift_net.h" +#include "../../../src/swift_net.h" #include #include #include @@ -11,51 +11,9 @@ #include #include #include +#include "../../shared.h" -#ifdef __linux__ -#include -#include -#elif defined(__APPLE__) -#include -#endif - -static char private_ip_address[INET_ADDRSTRLEN]; - -int get_private_ip_from_socket() { - const int temp_socket = socket(AF_INET, SOCK_DGRAM, 0); - if (temp_socket < 0) { - PRINT_ERROR("Failed to create temp socket"); - exit(EXIT_FAILURE); - } - - struct sockaddr_in remote = {0}; - remote.sin_family = AF_INET; - remote.sin_port = htons(53); - inet_pton(AF_INET, "8.8.8.8", &remote.sin_addr); - - if (connect(temp_socket, (struct sockaddr *)&remote, sizeof(remote)) < 0) { - PRINT_ERROR("Failed to connect temp socket"); - close(temp_socket); - exit(EXIT_FAILURE); - } - - struct sockaddr private_sockaddr; - socklen_t private_sockaddr_len = sizeof(private_sockaddr); - - if(getsockname(temp_socket, &private_sockaddr, &private_sockaddr_len) == -1) { - PRINT_ERROR("Failed to get private ip address"); - close(temp_socket); - exit(EXIT_FAILURE); - } - - if(!inet_ntop(AF_INET, &((struct sockaddr_in *)&private_sockaddr)->sin_addr, private_ip_address, sizeof(private_ip_address))) { - exit(EXIT_FAILURE); - }; - - printf("Private ip: %s\n", private_ip_address); - - return 0; -} +char private_ip_address_testing[INET_ADDRSTRLEN]; int main() { get_private_ip_from_socket(); @@ -164,7 +122,7 @@ int main() { .args = {.test_sending_packet_args = { .client_data_len = 50, .server_data_len = 50, - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false }}, .test_name = "Test sending small packets" @@ -174,7 +132,7 @@ int main() { .args = {.test_sending_packet_args = { .client_data_len = 0, .server_data_len = 10000, - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false }}, .test_name = "Test client sending large packet" @@ -184,7 +142,7 @@ int main() { .args = {.test_sending_packet_args = { .client_data_len = 10000, .server_data_len = 10, - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false }}, .test_name = "Test server sending large packet" @@ -192,7 +150,7 @@ int main() { { .function = test_making_request, .args = {.test_making_request_args = { - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false, .receiver = Server, .request_data_len = 100, @@ -203,7 +161,7 @@ int main() { { .function = test_making_request, .args = {.test_making_request_args = { - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false, .receiver = Client, .request_data_len = 100, @@ -214,7 +172,7 @@ int main() { { .function = test_making_request, .args = {.test_making_request_args = { - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false, .receiver = Client, .request_data_len = 10000, @@ -225,7 +183,7 @@ int main() { { .function = test_making_request, .args = {.test_making_request_args = { - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false, .receiver = Server, .request_data_len = 10000, @@ -236,7 +194,7 @@ int main() { { .function = test_making_request, .args = {.test_making_request_args = { - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false, .receiver = Client, .request_data_len = 100, @@ -247,7 +205,7 @@ int main() { { .function = test_making_request, .args = {.test_making_request_args = { - .ip_address = private_ip_address, + .ip_address = private_ip_address_testing, .loopback = false, .receiver = Server, .request_data_len = 100, @@ -273,7 +231,7 @@ int main() { return -1; } - printf("\033[32mSuccessfully completed test: %s\033[0m\n", current_test->test_name); + PRINT_SUCCESS("Successfully completed test: %s", current_test->test_name); continue; } diff --git a/tests/src/run_tests.h b/tests/integration_tests/src/run_tests.h similarity index 73% rename from tests/src/run_tests.h rename to tests/integration_tests/src/run_tests.h index 3ce1504..008c691 100644 --- a/tests/src/run_tests.h +++ b/tests/integration_tests/src/run_tests.h @@ -3,14 +3,7 @@ #include #include #include - -#define PRINT_ERROR(error, ...) \ - printf("\033[31m" error "\033[0m\n", ##__VA_ARGS__) - -enum ConnectionType { - Server, - Client -}; +#include "../../shared.h" struct TestSendingPacketArgs { const char* ip_address; @@ -38,5 +31,5 @@ struct Test { const char* test_name; }; -int test_sending_packet(const union Args* args_ptr); -int test_making_request(const union Args* args_ptr); +extern int test_sending_packet(const union Args* args_ptr); +extern int test_making_request(const union Args* args_ptr); diff --git a/tests/src/sending_packet.c b/tests/integration_tests/src/sending_packet.c similarity index 99% rename from tests/src/sending_packet.c rename to tests/integration_tests/src/sending_packet.c index a277282..09ca173 100644 --- a/tests/src/sending_packet.c +++ b/tests/integration_tests/src/sending_packet.c @@ -1,8 +1,7 @@ -#include "../config.h" #include #include #include -#include "../../src/swift_net.h" +#include "../../../src/swift_net.h" #include #include #include diff --git a/tests/performance_tests/build/CMakeLists.txt b/tests/performance_tests/build/CMakeLists.txt new file mode 100644 index 0000000..ea491c3 --- /dev/null +++ b/tests/performance_tests/build/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.15) +project(SwiftNetPerformanceTests C) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../output) + +if(SANITIZER STREQUAL "address") + message(STATUS "Using AddressSanitizer") + add_compile_options(-fsanitize=address -fno-omit-frame-pointer) + add_link_options(-fsanitize=address) +elseif(SANITIZER STREQUAL "thread") + message(STATUS "Using ThreadSanitizer") + add_compile_options(-fsanitize=thread -fno-omit-frame-pointer) + add_link_options(-fsanitize=thread) +elseif(SANITIZER STREQUAL "undefined") + message(STATUS "Using UndefinedBehaviorSanitizer") + add_compile_options(-fsanitize=undefined -fno-omit-frame-pointer) + add_link_options(-fsanitize=undefined) +else() + message(STATUS "No sanitizer enabled") +endif() + +link_directories(${CMAKE_SOURCE_DIR}/../../../build/output) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") + +file(GLOB SRC_FILES "${CMAKE_SOURCE_DIR}/../src/*.c") + +add_executable(run_tests ${SRC_FILES}) + +target_link_libraries(run_tests PRIVATE swiftnet -lpcap) +target_link_options(run_tests PRIVATE -O3) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/tests/performance_tests/build/build_tests.sh b/tests/performance_tests/build/build_tests.sh new file mode 100755 index 0000000..ca8ef91 --- /dev/null +++ b/tests/performance_tests/build/build_tests.sh @@ -0,0 +1,2 @@ +cmake . -DCMAKE_BUILD_TYPE=Release -DSANITIZER=false +make -B -j8 diff --git a/tests/performance_tests/output/run_tests b/tests/performance_tests/output/run_tests new file mode 100755 index 0000000..25c4ad5 Binary files /dev/null and b/tests/performance_tests/output/run_tests differ diff --git a/tests/performance_tests/src/main.c b/tests/performance_tests/src/main.c new file mode 100644 index 0000000..fe99fb7 --- /dev/null +++ b/tests/performance_tests/src/main.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include "../../../src/swift_net.h" +#include "../../shared.h" + + +#define PACKET_SIZE 1000000 // 1 MILLION BYTES +#define PACKETS_TO_SEND 50 // HOW MANY PACKETS TO SEND + +// ********************** // +// SEND 50 MILLION BYTES // +// ********************** // + +char private_ip_address_testing[INET_ADDRSTRLEN]; + +static struct timespec start, end; + +static uint32_t packets_received = 0; +static _Atomic bool finished = false; + +void packet_callback(struct SwiftNetServerPacketData* const packet_data, void* server) { + packets_received++; + + if (packets_received == PACKETS_TO_SEND) { + atomic_store_explicit(&finished, true, memory_order_release); + } + + swiftnet_server_destroy_packet_data(packet_data, server); +} + +void send_large_packets(const bool loopback) { + struct SwiftNetServer* const server = swiftnet_create_server(8080, loopback); + if (server == NULL) { + PRINT_ERROR("FAILED TO INIT SERVER"); + swiftnet_cleanup(); + exit(EXIT_FAILURE); + } + + swiftnet_server_set_message_handler(server, packet_callback, server); + + struct SwiftNetClientConnection* const client = swiftnet_create_client(loopback ? "127.0.0.1" : private_ip_address_testing, 8080, 1000); + if (client == NULL) { + PRINT_ERROR("FAILED TO INIT CLIENT"); + swiftnet_server_cleanup(server); + swiftnet_cleanup(); + exit(EXIT_FAILURE); + } + + struct SwiftNetPacketBuffer buffer = swiftnet_client_create_packet_buffer(PACKET_SIZE); + + uint8_t* const random_data = malloc(PACKET_SIZE); + for (uint32_t i = 0; i < PACKET_SIZE; i++) { + random_data[i] = rand(); + } + + swiftnet_client_append_to_packet(random_data, PACKET_SIZE, &buffer); + + clock_gettime(CLOCK_MONOTONIC, &start);; + + for (uint32_t i = 0; i < PACKETS_TO_SEND; i++) { + swiftnet_client_send_packet(client, &buffer); + } + + while (atomic_load_explicit(&finished, memory_order_acquire) == false) { + continue; + } + + swiftnet_client_destroy_packet_buffer(&buffer); + + clock_gettime(CLOCK_MONOTONIC, &end);; + + usleep(100000); + + double elapsed = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; + + PRINT_SUCCESS("Time to send: %.2f seconds", elapsed); + PRINT_SUCCESS("Bytes per second: %.2f", (PACKETS_TO_SEND * PACKET_SIZE) / elapsed) + + swiftnet_client_cleanup(client); +} + +int main() { + swiftnet_initialize(); + + swiftnet_add_debug_flags(PACKETS_SENDING | PACKETS_RECEIVING | INITIALIZATION | LOST_PACKETS); + + send_large_packets(false); + + swiftnet_cleanup(); + + return 0; +} diff --git a/tests/shared.h b/tests/shared.h new file mode 100644 index 0000000..bb34e97 --- /dev/null +++ b/tests/shared.h @@ -0,0 +1,66 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __linux__ +#include +#include +#elif defined(__APPLE__) +#include +#endif + +#define PRINT_ERROR(error, ...) \ + printf("\033[31m" error "\033[0m\n", ##__VA_ARGS__) + +#define PRINT_SUCCESS(error, ...) \ + printf("\033[32m" error "\033[0m\n", ##__VA_ARGS__); + +enum ConnectionType { + Server, + Client +}; + +extern char private_ip_address_testing[INET_ADDRSTRLEN]; + +static inline int get_private_ip_from_socket() { + const int temp_socket = socket(AF_INET, SOCK_DGRAM, 0); + if (temp_socket < 0) { + PRINT_ERROR("Failed to create temp socket"); + exit(EXIT_FAILURE); + } + + struct sockaddr_in remote = {0}; + remote.sin_family = AF_INET; + remote.sin_port = htons(53); + inet_pton(AF_INET, "8.8.8.8", &remote.sin_addr); + + if (connect(temp_socket, (struct sockaddr *)&remote, sizeof(remote)) < 0) { + PRINT_ERROR("Failed to connect temp socket"); + close(temp_socket); + exit(EXIT_FAILURE); + } + + struct sockaddr private_sockaddr; + socklen_t private_sockaddr_len = sizeof(private_sockaddr); + + if(getsockname(temp_socket, &private_sockaddr, &private_sockaddr_len) == -1) { + PRINT_ERROR("Failed to get private ip address"); + close(temp_socket); + exit(EXIT_FAILURE); + } + + if(!inet_ntop(AF_INET, &((struct sockaddr_in *)&private_sockaddr)->sin_addr, private_ip_address_testing, sizeof(private_ip_address_testing))) { + exit(EXIT_FAILURE); + }; + + printf("Private ip: %s\n", private_ip_address_testing); + + return 0; +}