From 6d2f2182d31361fa496bbc8abb92f2ac4f3eea48 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 25 Jan 2025 12:38:40 +0100 Subject: [PATCH] Add UDP socket test --- src/samples/test-sample/CMakeLists.txt | 4 +++ src/samples/test-sample/test.cpp | 39 +++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/samples/test-sample/CMakeLists.txt b/src/samples/test-sample/CMakeLists.txt index 6a131eff..bd5f2878 100644 --- a/src/samples/test-sample/CMakeLists.txt +++ b/src/samples/test-sample/CMakeLists.txt @@ -9,3 +9,7 @@ list(SORT SRC_FILES) add_executable(test-sample ${SRC_FILES}) momo_assign_source_group(${SRC_FILES}) + +target_link_libraries(test-sample PRIVATE + emulator-common +) diff --git a/src/samples/test-sample/test.cpp b/src/samples/test-sample/test.cpp index 83282f81..2e64c255 100644 --- a/src/samples/test-sample/test.cpp +++ b/src/samples/test-sample/test.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include using namespace std::literals; @@ -195,7 +195,7 @@ std::optional read_registry_string(const HKEY root, const char* pat return ""; } - return {std::string(data, min(length - 1, sizeof(data)))}; + return {std::string(data, std::min(static_cast(length - 1), sizeof(data)))}; } bool test_registry() @@ -231,6 +231,36 @@ bool test_exceptions() } } +bool test_socket() +{ + network::udp_socket receiver{AF_INET}; + const network::udp_socket sender{AF_INET}; + const network::address destination{"127.0.0.1:28970", AF_INET}; + constexpr std::string_view send_data = "Hello World"; + + if (!receiver.bind(destination)) + { + puts("Failed to bind socket!"); + return false; + } + + if (!sender.send(destination, send_data)) + { + puts("Failed to send data!"); + return false; + } + + const auto response = receiver.receive(); + + if (!response) + { + puts("Failed to recieve data!"); + return false; + } + + return send_data == response->second; +} + void throw_access_violation() { if (do_the_task) @@ -256,7 +286,7 @@ bool test_ud2_exception(void* address) { __try { - static_cast(address)(); + reinterpret_cast(address)(); return false; } __except (EXCEPTION_EXECUTE_HANDLER) @@ -301,7 +331,7 @@ void print_time() puts(res ? "Success" : "Fail"); \ } -int main(int argc, const char* argv[]) +int main(const int argc, const char* argv[]) { if (argc == 2 && argv[1] == "-time"sv) { @@ -319,6 +349,7 @@ int main(int argc, const char* argv[]) RUN_TEST(test_exceptions, "Exceptions") RUN_TEST(test_native_exceptions, "Native Exceptions") RUN_TEST(test_tls, "TLS") + RUN_TEST(test_socket, "Socket") return valid ? 0 : 1; }