Skip to content

Commit

Permalink
Native tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gfoidl committed Apr 7, 2020
1 parent fc09ca1 commit 8c955bc
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/libraries/Native/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
31 changes: 31 additions & 0 deletions src/libraries/Native/Unix/System.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
86 changes: 86 additions & 0 deletions src/libraries/Native/Unix/System.Native/tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "pal_networking.h"

#include <arpa/inet.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>

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;
}

0 comments on commit 8c955bc

Please sign in to comment.