Skip to content

Commit ff38877

Browse files
authored
Add internal tests for socket apis (#1900)
1 parent 128c0ea commit ff38877

6 files changed

Lines changed: 277 additions & 1 deletion

File tree

.github/workflows/compilation_on_android_ubuntu.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ jobs:
560560
run: WASI_SYSROOT=../../../../../core/deps/wasi-libc/sysroot bash build.sh
561561
working-directory: ./core/iwasm/libraries/lib-wasi-threads/test/
562562

563+
- name: build socket api tests
564+
if: matrix.test_option == '$WASI_TEST_OPTIONS'
565+
run: WASI_SYSROOT=../../../../../core/deps/wasi-libc/sysroot bash build.sh
566+
working-directory: ./core/iwasm/libraries/lib-socket/test/
567+
563568
- name: run tests
564569
timeout-minutes: 10
565570
run: ./test_wamr.sh ${{ matrix.test_option }} -t ${{ matrix.running_mode }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ core/deps/**
1414
core/shared/mem-alloc/tlsf
1515
core/app-framework/wgl
1616
core/iwasm/libraries/lib-wasi-threads/test/*.wasm
17+
core/iwasm/libraries/lib-socket/test/*.wasm
1718

1819
wamr-sdk/out/
1920
wamr-sdk/runtime/build_runtime_sdk/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
set -ueo pipefail
7+
CC="${CC:=/opt/wasi-sdk/bin/clang}"
8+
files=("tcp_udp.c" "nslookup.c")
9+
WASI_SYSROOT=${WASI_SYSROOT:=~/dev/wasi-libc/sysroot}
10+
11+
for file in "${files[@]}"
12+
do
13+
echo $file
14+
$CC \
15+
--target=wasm32-wasi-threads \
16+
-I../inc \
17+
--sysroot $WASI_SYSROOT \
18+
../src/wasi/wasi_socket_ext.c -pthread -ftls-model=local-exec \
19+
-Wl,--allow-undefined \
20+
-Wl,--strip-all,--no-entry \
21+
-Wl,--export=__heap_base \
22+
-Wl,--export=__data_end \
23+
-Wl,--shared-memory,--max-memory=10485760 \
24+
-Wl,--export=malloc \
25+
-Wl,--export=free \
26+
-o "${file%.*}.wasm" "$file"
27+
done
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include <assert.h>
7+
#include <string.h>
8+
#ifdef __wasi__
9+
#include <wasi/api.h>
10+
#include <sys/socket.h>
11+
#include <netinet/in.h>
12+
#include <wasi_socket_ext.h>
13+
#else
14+
#include <netdb.h>
15+
#endif
16+
17+
void
18+
test_nslookup(int af)
19+
{
20+
struct addrinfo *res;
21+
int count = 0;
22+
struct addrinfo hints;
23+
char *url = "google-public-dns-a.google.com";
24+
25+
memset(&hints, 0, sizeof(hints));
26+
hints.ai_family = af;
27+
hints.ai_socktype = SOCK_STREAM;
28+
int ret = getaddrinfo(url, 0, &hints, &res);
29+
assert(ret == 0);
30+
struct addrinfo *address = res;
31+
while (address) {
32+
assert(address->ai_family == af);
33+
assert(address->ai_socktype == SOCK_STREAM);
34+
count++;
35+
address = address->ai_next;
36+
}
37+
38+
assert(count > 0);
39+
freeaddrinfo(res);
40+
}
41+
42+
int
43+
main()
44+
{
45+
test_nslookup(AF_INET); /* for ipv4 */
46+
test_nslookup(AF_INET6); /* for ipv6 */
47+
48+
return 0;
49+
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
#include <unistd.h>
6+
#include <string.h>
7+
#include <assert.h>
8+
#ifdef __wasi__
9+
#include <wasi/api.h>
10+
#include <sys/socket.h>
11+
#include <wasi_socket_ext.h>
12+
#endif
13+
#include <arpa/inet.h>
14+
#include <pthread.h>
15+
#define SERVER_MSG "Message from server."
16+
#define PORT 8989
17+
pthread_mutex_t mut;
18+
pthread_cond_t cond;
19+
int server_init_complete = 0;
20+
char buffer[sizeof(SERVER_MSG) + 1];
21+
22+
struct socket_info {
23+
union {
24+
struct sockaddr_in addr_ipv4;
25+
struct sockaddr_in6 addr_ipv6;
26+
} addr;
27+
int sock;
28+
};
29+
30+
struct thread_args {
31+
int family;
32+
int protocol;
33+
};
34+
35+
struct socket_info
36+
init_socket_addr(int family, int protocol)
37+
{
38+
int sock = socket(family, protocol, 0);
39+
assert(sock != -1);
40+
41+
struct socket_info info;
42+
if (family == AF_INET) {
43+
struct sockaddr_in addr;
44+
memset(&addr, 0, sizeof(addr));
45+
addr.sin_family = AF_INET;
46+
addr.sin_port = htons(PORT);
47+
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
48+
info.addr.addr_ipv4 = addr;
49+
}
50+
else if (family == AF_INET6) {
51+
struct sockaddr_in6 addr;
52+
memset(&addr, 0, sizeof(addr));
53+
addr.sin6_family = AF_INET6;
54+
addr.sin6_port = htons(PORT);
55+
addr.sin6_addr = in6addr_loopback;
56+
info.addr.addr_ipv6 = addr;
57+
}
58+
info.sock = sock;
59+
return info;
60+
}
61+
62+
void
63+
assert_thread_args(struct thread_args *args)
64+
{
65+
assert(args->family == AF_INET || args->family == AF_INET6);
66+
assert(args->protocol == SOCK_STREAM || args->protocol == SOCK_DGRAM);
67+
}
68+
69+
void *
70+
server(void *arg)
71+
{
72+
server_init_complete = 0;
73+
struct thread_args *args = (struct thread_args *)arg;
74+
assert_thread_args(args);
75+
76+
struct socket_info init_server_sock =
77+
init_socket_addr(args->family, args->protocol);
78+
79+
int server_sock = init_server_sock.sock;
80+
socklen_t addr_size;
81+
struct sockaddr_storage client_addr;
82+
strcpy(buffer, SERVER_MSG);
83+
84+
struct sockaddr *server_addr = (struct sockaddr *)&init_server_sock.addr;
85+
int ret = bind(server_sock, server_addr,
86+
args->family == AF_INET ? sizeof(struct sockaddr_in)
87+
: sizeof(struct sockaddr_in6));
88+
assert(ret == 0);
89+
90+
(args->protocol == SOCK_STREAM) && listen(server_sock, 1);
91+
pthread_mutex_lock(&mut);
92+
server_init_complete = 1;
93+
pthread_mutex_unlock(&mut);
94+
pthread_cond_signal(&cond);
95+
96+
addr_size = sizeof(client_addr);
97+
if (args->protocol == SOCK_STREAM) {
98+
int client_sock =
99+
accept(server_sock, (struct sockaddr *)&client_addr, &addr_size);
100+
assert(client_sock >= 0);
101+
sendto(client_sock, buffer, strlen(buffer), 0,
102+
(struct sockaddr *)&client_addr, addr_size);
103+
104+
assert(close(client_sock) == 0);
105+
}
106+
else {
107+
recvfrom(server_sock, buffer, sizeof(buffer), 0,
108+
(struct sockaddr *)&client_addr, &addr_size);
109+
sendto(server_sock, buffer, strlen(buffer), 0,
110+
(struct sockaddr *)&client_addr, addr_size);
111+
112+
assert(close(server_sock) == 0);
113+
}
114+
115+
return NULL;
116+
}
117+
118+
void *
119+
client(void *arg)
120+
{
121+
struct thread_args *args = (struct thread_args *)arg;
122+
assert_thread_args(args);
123+
124+
pthread_mutex_lock(&mut);
125+
126+
while (server_init_complete == 0) {
127+
pthread_cond_wait(&cond, &mut);
128+
}
129+
130+
struct socket_info init_client_sock =
131+
init_socket_addr(args->family, args->protocol);
132+
int sock = init_client_sock.sock;
133+
pthread_mutex_unlock(&mut);
134+
135+
if (args->family == AF_INET) {
136+
struct sockaddr_in addr = init_client_sock.addr.addr_ipv4;
137+
if (args->protocol == SOCK_STREAM) {
138+
assert(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) != -1);
139+
}
140+
else {
141+
assert(sendto(sock, buffer, strlen(buffer), 0,
142+
(struct sockaddr *)&addr, sizeof(addr))
143+
!= -1);
144+
}
145+
}
146+
else {
147+
struct sockaddr_in6 addr = init_client_sock.addr.addr_ipv6;
148+
if (args->protocol == SOCK_STREAM) {
149+
assert(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) != -1);
150+
}
151+
else {
152+
assert(sendto(sock, buffer, strlen(buffer), 0,
153+
(struct sockaddr *)&addr, sizeof(addr))
154+
!= -1);
155+
}
156+
}
157+
158+
recv(sock, buffer, sizeof(buffer), 0);
159+
assert(strcmp(buffer, SERVER_MSG) == 0);
160+
assert(close(sock) == 0);
161+
return NULL;
162+
}
163+
164+
void
165+
test_protocol(int family, int protocol)
166+
{
167+
pthread_t server_thread, client_thread;
168+
assert(pthread_cond_init(&cond, NULL) == 0);
169+
assert(pthread_mutex_init(&mut, NULL) == 0);
170+
171+
struct thread_args args = { family, protocol };
172+
assert(pthread_create(&server_thread, NULL, server, (void *)&args) == 0);
173+
assert(pthread_create(&client_thread, NULL, client, (void *)&args) == 0);
174+
assert(pthread_join(server_thread, NULL) == 0);
175+
assert(pthread_join(client_thread, NULL) == 0);
176+
177+
assert(pthread_mutex_destroy(&mut) == 0);
178+
assert(pthread_cond_destroy(&cond) == 0);
179+
}
180+
181+
int
182+
main(int argc, char **argv)
183+
{
184+
/* test tcp with ipv4 and ipv6 */
185+
test_protocol(AF_INET, SOCK_STREAM);
186+
test_protocol(AF_INET6, SOCK_STREAM);
187+
188+
/* test udp with ipv4 and ipv6 */
189+
test_protocol(AF_INET, SOCK_DGRAM);
190+
test_protocol(AF_INET6, SOCK_DGRAM);
191+
192+
return 0;
193+
}

tests/wamr-test-suites/wasi-test-script/run_wasi_tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ if [[ $MODE != "aot" ]];then
2323
tests/c/testsuite/ \
2424
tests/assemblyscript/testsuite/ \
2525
tests/proposals/wasi-threads/ \
26-
${WAMR_DIR}/core/iwasm/libraries/lib-wasi-threads/test/
26+
${WAMR_DIR}/core/iwasm/libraries/lib-wasi-threads/test/ \
27+
${WAMR_DIR}/core/iwasm/libraries/lib-socket/test/ \
2728
exit_code=${PIPESTATUS[0]}
2829
deactivate
2930
else

0 commit comments

Comments
 (0)