Skip to content

Commit 8c955bc

Browse files
committed
Native tests
1 parent fc09ca1 commit 8c955bc

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

src/libraries/Native/Unix/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,5 @@ endif()
210210
if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_IOS)
211211
add_subdirectory(System.Security.Cryptography.Native.Apple)
212212
endif()
213+
214+
enable_testing()

src/libraries/Native/Unix/System.Native/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,34 @@ endif ()
8080
set_target_properties(System.Native-Static PROPERTIES OUTPUT_NAME System.Native CLEAN_DIRECT_OUTPUT 1)
8181

8282
install (TARGETS System.Native-Static DESTINATION .)
83+
84+
if (HAVE_GETADDRINFO_A)
85+
add_executable(tests
86+
tests.c
87+
)
88+
89+
target_link_libraries(tests
90+
System.Native
91+
pthread
92+
)
93+
94+
enable_testing()
95+
96+
add_test(no_args tests)
97+
set_tests_properties(no_args PROPERTIES WILL_FAIL TRUE)
98+
set_tests_properties(no_args PROPERTIES TIMEOUT 1)
99+
100+
add_test(hostname_local tests localhost)
101+
set_tests_properties(hostname_local PROPERTIES TIMEOUT 1)
102+
103+
add_test(hostname_ok tests google.at)
104+
set_tests_properties(hostname_ok PROPERTIES TIMEOUT 1)
105+
106+
add_test(hostname_invalid tests abc.test01)
107+
set_tests_properties(hostname_invalid PROPERTIES WILL_FAIL TRUE)
108+
set_tests_properties(hostname_invalid PROPERTIES TIMEOUT 1)
109+
110+
add_test(hostname_null tests null)
111+
set_tests_properties(hostname_null PROPERTIES WILL_FAIL TRUE)
112+
set_tests_properties(hostname_null PROPERTIES TIMEOUT 1)
113+
endif ()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "pal_networking.h"
2+
3+
#include <arpa/inet.h>
4+
#include <pthread.h>
5+
#include <semaphore.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <sys/socket.h>
9+
#include <string.h>
10+
11+
struct state
12+
{
13+
HostEntry entry;
14+
char* hostName;
15+
int errorCode;
16+
sem_t semaphore;
17+
};
18+
19+
static void callback(HostEntry* entry, int errorCode)
20+
{
21+
printf("(%lu) handler: enter, errorCode: %d\n", pthread_self(), errorCode);
22+
23+
struct state* state = (struct state*)entry;
24+
25+
if (errorCode == 0)
26+
{
27+
printf("(%lu) callback: # of addresses = %d\n", pthread_self(), entry->IPAddressCount);
28+
for (int i = 0; i < entry->IPAddressCount; ++i)
29+
{
30+
IPAddress ipAddress = entry->IPAddressList[i];
31+
32+
char buffer[256];
33+
inet_ntop(ipAddress.IsIPv6 ? AF_INET6 : AF_INET, ipAddress.Address, buffer, sizeof(buffer));
34+
printf("(%lu) ip for %s: %s\n", pthread_self(), entry->CanonicalName, buffer);
35+
}
36+
}
37+
38+
state->errorCode = errorCode;
39+
40+
sem_post(&state->semaphore);
41+
}
42+
43+
int main(int argc, char** argv)
44+
{
45+
if (argc != 2)
46+
{
47+
printf("hostname must be given as argument\n");
48+
return EXIT_FAILURE;
49+
}
50+
51+
if (!SystemNative_PlatformSupportsGetAddrInfoAsync())
52+
{
53+
printf("platform support not available\n");
54+
return EXIT_FAILURE;
55+
}
56+
57+
printf("platform support available\n");
58+
59+
char* hostName = argv[1];
60+
printf("(%lu) hostname: %s\n", pthread_self(), hostName);
61+
62+
if (strcmp(hostName, "null")==0)
63+
{
64+
hostName = NULL;
65+
}
66+
67+
struct state state;
68+
sem_init(&state.semaphore, 0, 0);
69+
70+
int error = SystemNative_GetHostEntryForNameAsync((uint8_t*)hostName, &state.entry, callback);
71+
72+
if (error != 0)
73+
{
74+
printf("(%lu) OS call failed with error %d\n", pthread_self(), error);
75+
return EXIT_FAILURE;
76+
}
77+
78+
printf("(%lu) main: waiting for semaphore\n", pthread_self());
79+
80+
sem_wait(&state.semaphore);
81+
sem_destroy(&state.semaphore);
82+
83+
printf("(%lu) main: exit, errorCode: %d\n", pthread_self(), state.errorCode);
84+
85+
return state.errorCode;
86+
}

0 commit comments

Comments
 (0)