Skip to content

Commit 3e0516c

Browse files
committed
tests: add io redirection
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 0ba2a8b commit 3e0516c

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

tests/system/input_output.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* @copyright Copyright (c) contributors to Project Ocre,
3+
* which has been established as Project Ocre a Series of LF Projects, LLC
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <stdio.h>
9+
#include <string.h>
10+
11+
#include <sys/socket.h>
12+
#include <unistd.h>
13+
14+
#include <unity.h>
15+
#include <ocre/ocre.h>
16+
17+
#define ARG_TEST_STRING "Zirigdum!"
18+
19+
struct ocre_context *context;
20+
21+
void setUp(void)
22+
{
23+
ocre_initialize(NULL);
24+
context = ocre_create_context(NULL);
25+
}
26+
27+
void tearDown(void)
28+
{
29+
ocre_destroy_context(context);
30+
ocre_deinitialize();
31+
}
32+
33+
void test_ocre_container_output_stdout(void)
34+
{
35+
const struct ocre_container_args args = {
36+
.argv =
37+
(const char *[]){
38+
ARG_TEST_STRING,
39+
NULL,
40+
},
41+
};
42+
43+
int stdout_pair[2];
44+
TEST_ASSERT_EQUAL_INT(0, socketpair(AF_UNIX, SOCK_STREAM, 0, stdout_pair));
45+
46+
struct ocre_container *container =
47+
ocre_context_create_container(context, "print_args.wasm", "wamr/wasip1", NULL, true, &args,
48+
STDIN_FILENO, stdout_pair[1], STDERR_FILENO);
49+
50+
TEST_ASSERT_EQUAL_INT(0, ocre_container_start(container));
51+
52+
ocre_container_wait(container, NULL);
53+
54+
char buf[1000];
55+
memset(buf, 0, sizeof(buf));
56+
57+
ssize_t n = read(stdout_pair[0], buf, sizeof(buf) - 1);
58+
59+
TEST_ASSERT_GREATER_THAN_size_t(0, n);
60+
61+
char *second_line = strchr(buf, '\n');
62+
++second_line;
63+
TEST_ASSERT_NOT_NULL(second_line);
64+
65+
TEST_ASSERT_EQUAL_STRING("argv[1]=" ARG_TEST_STRING "\n", second_line);
66+
67+
ocre_container_kill(container);
68+
ocre_container_wait(container, NULL);
69+
70+
ocre_context_remove_container(context, container);
71+
}
72+
73+
void test_ocre_container_input_stdin_output_stdout(void)
74+
{
75+
int stdin_pair[2];
76+
int stdout_pair[2];
77+
TEST_ASSERT_EQUAL_INT(0, socketpair(AF_UNIX, SOCK_STREAM, 0, stdin_pair));
78+
TEST_ASSERT_EQUAL_INT(0, socketpair(AF_UNIX, SOCK_STREAM, 0, stdout_pair));
79+
80+
struct ocre_container *container = ocre_context_create_container(
81+
context, "cat.wasm", "wamr/wasip1", NULL, true, NULL, stdin_pair[1], stdout_pair[1], STDERR_FILENO);
82+
83+
TEST_ASSERT_EQUAL_INT(0, ocre_container_start(container));
84+
85+
char buf[1000];
86+
memset(buf, 0, sizeof(buf));
87+
88+
ssize_t nwrite = write(stdin_pair[0], ARG_TEST_STRING "\n", strlen(ARG_TEST_STRING) + 1);
89+
90+
ssize_t nread = read(stdout_pair[0], buf, nwrite);
91+
92+
TEST_ASSERT_EQUAL(nwrite, nread);
93+
94+
TEST_ASSERT_EQUAL_STRING(ARG_TEST_STRING "\n", buf);
95+
96+
ocre_container_kill(container);
97+
ocre_container_wait(container, NULL);
98+
99+
ocre_context_remove_container(context, container);
100+
}
101+
102+
void test_ocre_container_input_stdin_output_stderr(void)
103+
{
104+
const struct ocre_container_args args = {
105+
.argv =
106+
(const char *[]){
107+
"-e",
108+
NULL,
109+
},
110+
};
111+
112+
int stdin_pair[2];
113+
int stderr_pair[2];
114+
TEST_ASSERT_EQUAL_INT(0, socketpair(AF_UNIX, SOCK_STREAM, 0, stdin_pair));
115+
TEST_ASSERT_EQUAL_INT(0, socketpair(AF_UNIX, SOCK_STREAM, 0, stderr_pair));
116+
117+
struct ocre_container *container = ocre_context_create_container(
118+
context, "cat.wasm", "wamr/wasip1", NULL, true, &args, stdin_pair[1], STDOUT_FILENO, stderr_pair[1]);
119+
120+
TEST_ASSERT_EQUAL_INT(0, ocre_container_start(container));
121+
122+
char buf[1000];
123+
memset(buf, 0, sizeof(buf));
124+
125+
ssize_t nwrite = write(stdin_pair[0], ARG_TEST_STRING "\n", strlen(ARG_TEST_STRING) + 1);
126+
127+
ssize_t nread = read(stderr_pair[0], buf, nwrite);
128+
129+
TEST_ASSERT_EQUAL(nwrite, nread);
130+
131+
TEST_ASSERT_EQUAL_STRING(ARG_TEST_STRING "\n", buf);
132+
133+
ocre_container_kill(container);
134+
ocre_container_wait(container, NULL);
135+
136+
ocre_context_remove_container(context, container);
137+
}
138+
139+
int main(void)
140+
{
141+
UNITY_BEGIN();
142+
RUN_TEST(test_ocre_container_output_stdout);
143+
RUN_TEST(test_ocre_container_input_stdin_output_stdout);
144+
RUN_TEST(test_ocre_container_input_stdin_output_stderr);
145+
return UNITY_END();
146+
}

tests/system/posix/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ list(APPEND OCRE_SDK_PRELOADED_IMAGES
1111
"return0.wasm"
1212
"return1.wasm"
1313
"sleep5_return0.wasm"
14+
"print_args.wasm"
15+
"cat.wasm"
1416
)
1517

1618
if (SANITIZER)
@@ -36,6 +38,7 @@ list(APPEND OCRE_TESTS
3638
ocre
3739
context
3840
container
41+
input_output
3942
)
4043

4144
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/src/ocre/var/lib/ocre/images)
@@ -74,4 +77,5 @@ add_custom_target(run-systests
7477
test_ocre.log
7578
test_context.log
7679
test_container.log
80+
test_input_output.log
7781
)

0 commit comments

Comments
 (0)