From 8c955bc4bd1d0dbe4b2823f4327ad52bf9856fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 6 Apr 2020 20:58:31 +0200 Subject: [PATCH] Native tests --- src/libraries/Native/Unix/CMakeLists.txt | 2 + .../Native/Unix/System.Native/CMakeLists.txt | 31 +++++++ .../Native/Unix/System.Native/tests.c | 86 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 src/libraries/Native/Unix/System.Native/tests.c diff --git a/src/libraries/Native/Unix/CMakeLists.txt b/src/libraries/Native/Unix/CMakeLists.txt index b3c9613a0e10b..0769eaf19d73c 100644 --- a/src/libraries/Native/Unix/CMakeLists.txt +++ b/src/libraries/Native/Unix/CMakeLists.txt @@ -210,3 +210,5 @@ endif() if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_IOS) add_subdirectory(System.Security.Cryptography.Native.Apple) endif() + +enable_testing() diff --git a/src/libraries/Native/Unix/System.Native/CMakeLists.txt b/src/libraries/Native/Unix/System.Native/CMakeLists.txt index bf2e3787607fc..aae0a19eef445 100644 --- a/src/libraries/Native/Unix/System.Native/CMakeLists.txt +++ b/src/libraries/Native/Unix/System.Native/CMakeLists.txt @@ -80,3 +80,34 @@ endif () set_target_properties(System.Native-Static PROPERTIES OUTPUT_NAME System.Native CLEAN_DIRECT_OUTPUT 1) install (TARGETS System.Native-Static DESTINATION .) + +if (HAVE_GETADDRINFO_A) + add_executable(tests + tests.c + ) + + target_link_libraries(tests + System.Native + pthread + ) + + enable_testing() + + add_test(no_args tests) + set_tests_properties(no_args PROPERTIES WILL_FAIL TRUE) + set_tests_properties(no_args PROPERTIES TIMEOUT 1) + + add_test(hostname_local tests localhost) + set_tests_properties(hostname_local PROPERTIES TIMEOUT 1) + + add_test(hostname_ok tests google.at) + set_tests_properties(hostname_ok PROPERTIES TIMEOUT 1) + + add_test(hostname_invalid tests abc.test01) + set_tests_properties(hostname_invalid PROPERTIES WILL_FAIL TRUE) + set_tests_properties(hostname_invalid PROPERTIES TIMEOUT 1) + + add_test(hostname_null tests null) + set_tests_properties(hostname_null PROPERTIES WILL_FAIL TRUE) + set_tests_properties(hostname_null PROPERTIES TIMEOUT 1) +endif () diff --git a/src/libraries/Native/Unix/System.Native/tests.c b/src/libraries/Native/Unix/System.Native/tests.c new file mode 100644 index 0000000000000..e555cadd9c143 --- /dev/null +++ b/src/libraries/Native/Unix/System.Native/tests.c @@ -0,0 +1,86 @@ +#include "pal_networking.h" + +#include +#include +#include +#include +#include +#include +#include + +struct state +{ + HostEntry entry; + char* hostName; + int errorCode; + sem_t semaphore; +}; + +static void callback(HostEntry* entry, int errorCode) +{ + printf("(%lu) handler: enter, errorCode: %d\n", pthread_self(), errorCode); + + struct state* state = (struct state*)entry; + + if (errorCode == 0) + { + printf("(%lu) callback: # of addresses = %d\n", pthread_self(), entry->IPAddressCount); + for (int i = 0; i < entry->IPAddressCount; ++i) + { + IPAddress ipAddress = entry->IPAddressList[i]; + + char buffer[256]; + inet_ntop(ipAddress.IsIPv6 ? AF_INET6 : AF_INET, ipAddress.Address, buffer, sizeof(buffer)); + printf("(%lu) ip for %s: %s\n", pthread_self(), entry->CanonicalName, buffer); + } + } + + state->errorCode = errorCode; + + sem_post(&state->semaphore); +} + +int main(int argc, char** argv) +{ + if (argc != 2) + { + printf("hostname must be given as argument\n"); + return EXIT_FAILURE; + } + + if (!SystemNative_PlatformSupportsGetAddrInfoAsync()) + { + printf("platform support not available\n"); + return EXIT_FAILURE; + } + + printf("platform support available\n"); + + char* hostName = argv[1]; + printf("(%lu) hostname: %s\n", pthread_self(), hostName); + + if (strcmp(hostName, "null")==0) + { + hostName = NULL; + } + + struct state state; + sem_init(&state.semaphore, 0, 0); + + int error = SystemNative_GetHostEntryForNameAsync((uint8_t*)hostName, &state.entry, callback); + + if (error != 0) + { + printf("(%lu) OS call failed with error %d\n", pthread_self(), error); + return EXIT_FAILURE; + } + + printf("(%lu) main: waiting for semaphore\n", pthread_self()); + + sem_wait(&state.semaphore); + sem_destroy(&state.semaphore); + + printf("(%lu) main: exit, errorCode: %d\n", pthread_self(), state.errorCode); + + return state.errorCode; +}