|
| 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 | +} |
0 commit comments