Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
      2      1.1  christos  *
      3      1.1  christos  * Permission is hereby granted, free of charge, to any person obtaining a copy
      4      1.1  christos  * of this software and associated documentation files (the "Software"), to
      5      1.1  christos  * deal in the Software without restriction, including without limitation the
      6      1.1  christos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      7      1.1  christos  * sell copies of the Software, and to permit persons to whom the Software is
      8      1.1  christos  * furnished to do so, subject to the following conditions:
      9      1.1  christos  *
     10      1.1  christos  * The above copyright notice and this permission notice shall be included in
     11      1.1  christos  * all copies or substantial portions of the Software.
     12      1.1  christos  *
     13      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14      1.1  christos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15      1.1  christos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     16      1.1  christos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17      1.1  christos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     18      1.1  christos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     19      1.1  christos  * IN THE SOFTWARE.
     20      1.1  christos  */
     21      1.1  christos 
     22      1.1  christos #include <errno.h>
     23      1.1  christos #include <stdio.h>
     24      1.1  christos #include <string.h>
     25      1.1  christos 
     26      1.1  christos #ifdef _WIN32
     27      1.1  christos # include <io.h>
     28  1.1.1.3  christos # define read _read
     29      1.1  christos #else
     30      1.1  christos # include <unistd.h>
     31      1.1  christos #endif
     32      1.1  christos 
     33      1.1  christos #include "uv.h"
     34      1.1  christos #include "runner.h"
     35      1.1  christos #include "task.h"
     36      1.1  christos 
     37      1.1  christos /* Actual tests and helpers are defined in test-list.h */
     38      1.1  christos #include "test-list.h"
     39      1.1  christos 
     40  1.1.1.2  christos #ifdef __MVS__
     41  1.1.1.2  christos #include "zos-base.h"
     42  1.1.1.2  christos /* Initialize environment and zoslib */
     43  1.1.1.2  christos __attribute__((constructor)) void init() {
     44  1.1.1.2  christos   zoslib_config_t config;
     45  1.1.1.2  christos   init_zoslib_config(&config);
     46  1.1.1.2  christos   init_zoslib(config);
     47  1.1.1.2  christos }
     48  1.1.1.2  christos #endif
     49  1.1.1.2  christos 
     50      1.1  christos int ipc_helper(int listen_after_write);
     51      1.1  christos int ipc_helper_heavy_traffic_deadlock_bug(void);
     52      1.1  christos int ipc_helper_tcp_connection(void);
     53      1.1  christos int ipc_send_recv_helper(void);
     54      1.1  christos int ipc_helper_bind_twice(void);
     55      1.1  christos int ipc_helper_send_zero(void);
     56      1.1  christos int stdio_over_pipes_helper(void);
     57      1.1  christos void spawn_stdin_stdout(void);
     58      1.1  christos void process_title_big_argv(void);
     59      1.1  christos int spawn_tcp_server_helper(void);
     60      1.1  christos 
     61      1.1  christos static int maybe_run_test(int argc, char **argv);
     62      1.1  christos 
     63  1.1.1.2  christos #ifdef _WIN32
     64  1.1.1.2  christos typedef BOOL (WINAPI *sCompareObjectHandles)(_In_ HANDLE, _In_ HANDLE);
     65  1.1.1.2  christos #endif
     66  1.1.1.2  christos 
     67      1.1  christos 
     68      1.1  christos int main(int argc, char **argv) {
     69      1.1  christos #ifndef _WIN32
     70      1.1  christos   if (0 == geteuid() && NULL == getenv("UV_RUN_AS_ROOT")) {
     71      1.1  christos     fprintf(stderr, "The libuv test suite cannot be run as root.\n");
     72      1.1  christos     return EXIT_FAILURE;
     73      1.1  christos   }
     74      1.1  christos #endif
     75      1.1  christos 
     76      1.1  christos   platform_init(argc, argv);
     77      1.1  christos   argv = uv_setup_args(argc, argv);
     78      1.1  christos 
     79      1.1  christos   switch (argc) {
     80      1.1  christos   case 1: return run_tests(0);
     81      1.1  christos   case 2: return maybe_run_test(argc, argv);
     82      1.1  christos   case 3: return run_test_part(argv[1], argv[2]);
     83      1.1  christos   case 4: return maybe_run_test(argc, argv);
     84      1.1  christos   default:
     85      1.1  christos     fprintf(stderr, "Too many arguments.\n");
     86      1.1  christos     fflush(stderr);
     87      1.1  christos     return EXIT_FAILURE;
     88      1.1  christos   }
     89      1.1  christos }
     90      1.1  christos 
     91      1.1  christos 
     92      1.1  christos static int maybe_run_test(int argc, char **argv) {
     93      1.1  christos   if (strcmp(argv[1], "--list") == 0) {
     94      1.1  christos     print_tests(stdout);
     95      1.1  christos     return 0;
     96      1.1  christos   }
     97      1.1  christos 
     98      1.1  christos   if (strcmp(argv[1], "ipc_helper_listen_before_write") == 0) {
     99      1.1  christos     return ipc_helper(0);
    100      1.1  christos   }
    101      1.1  christos 
    102      1.1  christos   if (strcmp(argv[1], "ipc_helper_listen_after_write") == 0) {
    103      1.1  christos     return ipc_helper(1);
    104      1.1  christos   }
    105      1.1  christos 
    106      1.1  christos   if (strcmp(argv[1], "ipc_helper_heavy_traffic_deadlock_bug") == 0) {
    107      1.1  christos     return ipc_helper_heavy_traffic_deadlock_bug();
    108      1.1  christos   }
    109      1.1  christos 
    110      1.1  christos   if (strcmp(argv[1], "ipc_send_recv_helper") == 0) {
    111      1.1  christos     return ipc_send_recv_helper();
    112      1.1  christos   }
    113      1.1  christos 
    114      1.1  christos   if (strcmp(argv[1], "ipc_helper_tcp_connection") == 0) {
    115      1.1  christos     return ipc_helper_tcp_connection();
    116      1.1  christos   }
    117      1.1  christos 
    118      1.1  christos   if (strcmp(argv[1], "ipc_helper_bind_twice") == 0) {
    119      1.1  christos     return ipc_helper_bind_twice();
    120      1.1  christos   }
    121      1.1  christos 
    122      1.1  christos   if (strcmp(argv[1], "ipc_helper_send_zero") == 0) {
    123      1.1  christos     return ipc_helper_send_zero();
    124      1.1  christos   }
    125      1.1  christos 
    126      1.1  christos   if (strcmp(argv[1], "stdio_over_pipes_helper") == 0) {
    127      1.1  christos     return stdio_over_pipes_helper();
    128      1.1  christos   }
    129      1.1  christos 
    130      1.1  christos   if (strcmp(argv[1], "spawn_helper1") == 0) {
    131      1.1  christos     notify_parent_process();
    132      1.1  christos     return 1;
    133      1.1  christos   }
    134      1.1  christos 
    135      1.1  christos   if (strcmp(argv[1], "spawn_helper2") == 0) {
    136      1.1  christos     notify_parent_process();
    137      1.1  christos     printf("hello world\n");
    138      1.1  christos     return 1;
    139      1.1  christos   }
    140      1.1  christos 
    141      1.1  christos   if (strcmp(argv[1], "spawn_tcp_server_helper") == 0) {
    142      1.1  christos     notify_parent_process();
    143      1.1  christos     return spawn_tcp_server_helper();
    144      1.1  christos   }
    145      1.1  christos 
    146      1.1  christos   if (strcmp(argv[1], "spawn_helper3") == 0) {
    147      1.1  christos     char buffer[256];
    148      1.1  christos     notify_parent_process();
    149  1.1.1.3  christos     ASSERT_PTR_EQ(buffer, fgets(buffer, sizeof(buffer) - 1, stdin));
    150      1.1  christos     buffer[sizeof(buffer) - 1] = '\0';
    151      1.1  christos     fputs(buffer, stdout);
    152      1.1  christos     return 1;
    153      1.1  christos   }
    154      1.1  christos 
    155      1.1  christos   if (strcmp(argv[1], "spawn_helper4") == 0) {
    156      1.1  christos     notify_parent_process();
    157      1.1  christos     /* Never surrender, never return! */
    158  1.1.1.2  christos     for (;;) uv_sleep(10000);
    159      1.1  christos   }
    160      1.1  christos 
    161      1.1  christos   if (strcmp(argv[1], "spawn_helper5") == 0) {
    162      1.1  christos     const char out[] = "fourth stdio!\n";
    163      1.1  christos     notify_parent_process();
    164      1.1  christos     {
    165      1.1  christos #ifdef _WIN32
    166      1.1  christos       DWORD bytes;
    167      1.1  christos       WriteFile((HANDLE) _get_osfhandle(3), out, sizeof(out) - 1, &bytes, NULL);
    168      1.1  christos #else
    169      1.1  christos       ssize_t r;
    170      1.1  christos 
    171      1.1  christos       do
    172      1.1  christos         r = write(3, out, sizeof(out) - 1);
    173      1.1  christos       while (r == -1 && errno == EINTR);
    174      1.1  christos 
    175      1.1  christos       fsync(3);
    176      1.1  christos #endif
    177      1.1  christos     }
    178      1.1  christos     return 1;
    179      1.1  christos   }
    180      1.1  christos 
    181      1.1  christos   if (strcmp(argv[1], "spawn_helper6") == 0) {
    182      1.1  christos     int r;
    183      1.1  christos 
    184      1.1  christos     notify_parent_process();
    185      1.1  christos 
    186      1.1  christos     r = fprintf(stdout, "hello world\n");
    187  1.1.1.3  christos     ASSERT_GT(r, 0);
    188      1.1  christos 
    189      1.1  christos     r = fprintf(stderr, "hello errworld\n");
    190  1.1.1.3  christos     ASSERT_GT(r, 0);
    191      1.1  christos 
    192      1.1  christos     return 1;
    193      1.1  christos   }
    194      1.1  christos 
    195      1.1  christos   if (strcmp(argv[1], "spawn_helper7") == 0) {
    196      1.1  christos     int r;
    197      1.1  christos     char *test;
    198      1.1  christos 
    199      1.1  christos     notify_parent_process();
    200      1.1  christos 
    201      1.1  christos     /* Test if the test value from the parent is still set */
    202      1.1  christos     test = getenv("ENV_TEST");
    203  1.1.1.2  christos     ASSERT_NOT_NULL(test);
    204      1.1  christos 
    205      1.1  christos     r = fprintf(stdout, "%s", test);
    206  1.1.1.3  christos     ASSERT_GT(r, 0);
    207      1.1  christos 
    208      1.1  christos     return 1;
    209      1.1  christos   }
    210      1.1  christos 
    211      1.1  christos   if (strcmp(argv[1], "spawn_helper8") == 0) {
    212  1.1.1.2  christos     uv_os_fd_t closed_fd;
    213  1.1.1.2  christos     uv_os_fd_t open_fd;
    214  1.1.1.2  christos #ifdef _WIN32
    215  1.1.1.2  christos     DWORD flags;
    216  1.1.1.2  christos     HMODULE kernelbase_module;
    217  1.1.1.2  christos     sCompareObjectHandles pCompareObjectHandles; /* function introduced in Windows 10 */
    218  1.1.1.2  christos #endif
    219      1.1  christos     notify_parent_process();
    220  1.1.1.3  christos     ASSERT_EQ(sizeof(closed_fd), read(0, &closed_fd, sizeof(closed_fd)));
    221  1.1.1.3  christos     ASSERT_EQ(sizeof(open_fd), read(0, &open_fd, sizeof(open_fd)));
    222  1.1.1.2  christos #ifdef _WIN32
    223  1.1.1.3  christos     ASSERT_GT((intptr_t) closed_fd, 0);
    224  1.1.1.3  christos     ASSERT_GT((intptr_t) open_fd, 0);
    225  1.1.1.3  christos     ASSERT_NE(0, GetHandleInformation(open_fd, &flags));
    226  1.1.1.2  christos     kernelbase_module = GetModuleHandleA("kernelbase.dll");
    227  1.1.1.2  christos     pCompareObjectHandles = (sCompareObjectHandles)
    228  1.1.1.2  christos         GetProcAddress(kernelbase_module, "CompareObjectHandles");
    229  1.1.1.3  christos     ASSERT_NE(pCompareObjectHandles == NULL || \
    230  1.1.1.3  christos               !pCompareObjectHandles(open_fd, closed_fd), 0);
    231  1.1.1.2  christos #else
    232  1.1.1.3  christos     ASSERT_GT(open_fd, 2);
    233  1.1.1.3  christos     ASSERT_GT(closed_fd, 2);
    234      1.1  christos # if defined(__PASE__)  /* On IBMi PASE, write() returns 1 */
    235  1.1.1.3  christos     ASSERT_EQ(1, write(closed_fd, "x", 1));
    236      1.1  christos # else
    237  1.1.1.3  christos     ASSERT_EQ(-1, write(closed_fd, "x", 1));
    238      1.1  christos # endif  /* !__PASE__ */
    239  1.1.1.2  christos #endif
    240      1.1  christos     return 1;
    241      1.1  christos   }
    242      1.1  christos 
    243      1.1  christos   if (strcmp(argv[1], "spawn_helper9") == 0) {
    244      1.1  christos     notify_parent_process();
    245      1.1  christos     spawn_stdin_stdout();
    246      1.1  christos     return 1;
    247      1.1  christos   }
    248      1.1  christos 
    249      1.1  christos #ifndef _WIN32
    250      1.1  christos   if (strcmp(argv[1], "spawn_helper_setuid_setgid") == 0) {
    251      1.1  christos     uv_uid_t uid = atoi(argv[2]);
    252      1.1  christos     uv_gid_t gid = atoi(argv[3]);
    253      1.1  christos 
    254  1.1.1.3  christos     ASSERT_EQ(uid, getuid());
    255  1.1.1.3  christos     ASSERT_EQ(gid, getgid());
    256      1.1  christos     notify_parent_process();
    257      1.1  christos 
    258      1.1  christos     return 1;
    259      1.1  christos   }
    260      1.1  christos #endif  /* !_WIN32 */
    261      1.1  christos 
    262      1.1  christos   if (strcmp(argv[1], "process_title_big_argv_helper") == 0) {
    263      1.1  christos     notify_parent_process();
    264      1.1  christos     process_title_big_argv();
    265      1.1  christos     return 0;
    266      1.1  christos   }
    267      1.1  christos 
    268      1.1  christos   return run_test(argv[1], 0, 1);
    269      1.1  christos }
    270