Home | History | Annotate | Line # | Download | only in test
run-tests.c revision 1.1
      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  christos #else
     29  1.1  christos # include <unistd.h>
     30  1.1  christos #endif
     31  1.1  christos 
     32  1.1  christos #include "uv.h"
     33  1.1  christos #include "runner.h"
     34  1.1  christos #include "task.h"
     35  1.1  christos 
     36  1.1  christos /* Actual tests and helpers are defined in test-list.h */
     37  1.1  christos #include "test-list.h"
     38  1.1  christos 
     39  1.1  christos int ipc_helper(int listen_after_write);
     40  1.1  christos int ipc_helper_heavy_traffic_deadlock_bug(void);
     41  1.1  christos int ipc_helper_tcp_connection(void);
     42  1.1  christos int ipc_helper_closed_handle(void);
     43  1.1  christos int ipc_send_recv_helper(void);
     44  1.1  christos int ipc_helper_bind_twice(void);
     45  1.1  christos int ipc_helper_send_zero(void);
     46  1.1  christos int stdio_over_pipes_helper(void);
     47  1.1  christos void spawn_stdin_stdout(void);
     48  1.1  christos void process_title_big_argv(void);
     49  1.1  christos int spawn_tcp_server_helper(void);
     50  1.1  christos 
     51  1.1  christos static int maybe_run_test(int argc, char **argv);
     52  1.1  christos 
     53  1.1  christos 
     54  1.1  christos int main(int argc, char **argv) {
     55  1.1  christos #ifndef _WIN32
     56  1.1  christos   if (0 == geteuid() && NULL == getenv("UV_RUN_AS_ROOT")) {
     57  1.1  christos     fprintf(stderr, "The libuv test suite cannot be run as root.\n");
     58  1.1  christos     return EXIT_FAILURE;
     59  1.1  christos   }
     60  1.1  christos #endif
     61  1.1  christos 
     62  1.1  christos   platform_init(argc, argv);
     63  1.1  christos   argv = uv_setup_args(argc, argv);
     64  1.1  christos 
     65  1.1  christos   switch (argc) {
     66  1.1  christos   case 1: return run_tests(0);
     67  1.1  christos   case 2: return maybe_run_test(argc, argv);
     68  1.1  christos   case 3: return run_test_part(argv[1], argv[2]);
     69  1.1  christos   case 4: return maybe_run_test(argc, argv);
     70  1.1  christos   default:
     71  1.1  christos     fprintf(stderr, "Too many arguments.\n");
     72  1.1  christos     fflush(stderr);
     73  1.1  christos     return EXIT_FAILURE;
     74  1.1  christos   }
     75  1.1  christos 
     76  1.1  christos #ifndef __SUNPRO_C
     77  1.1  christos   return EXIT_SUCCESS;
     78  1.1  christos #endif
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos 
     82  1.1  christos static int maybe_run_test(int argc, char **argv) {
     83  1.1  christos   if (strcmp(argv[1], "--list") == 0) {
     84  1.1  christos     print_tests(stdout);
     85  1.1  christos     return 0;
     86  1.1  christos   }
     87  1.1  christos 
     88  1.1  christos   if (strcmp(argv[1], "ipc_helper_listen_before_write") == 0) {
     89  1.1  christos     return ipc_helper(0);
     90  1.1  christos   }
     91  1.1  christos 
     92  1.1  christos   if (strcmp(argv[1], "ipc_helper_listen_after_write") == 0) {
     93  1.1  christos     return ipc_helper(1);
     94  1.1  christos   }
     95  1.1  christos 
     96  1.1  christos   if (strcmp(argv[1], "ipc_helper_heavy_traffic_deadlock_bug") == 0) {
     97  1.1  christos     return ipc_helper_heavy_traffic_deadlock_bug();
     98  1.1  christos   }
     99  1.1  christos 
    100  1.1  christos   if (strcmp(argv[1], "ipc_send_recv_helper") == 0) {
    101  1.1  christos     return ipc_send_recv_helper();
    102  1.1  christos   }
    103  1.1  christos 
    104  1.1  christos   if (strcmp(argv[1], "ipc_helper_tcp_connection") == 0) {
    105  1.1  christos     return ipc_helper_tcp_connection();
    106  1.1  christos   }
    107  1.1  christos 
    108  1.1  christos   if (strcmp(argv[1], "ipc_helper_closed_handle") == 0) {
    109  1.1  christos     return ipc_helper_closed_handle();
    110  1.1  christos   }
    111  1.1  christos 
    112  1.1  christos   if (strcmp(argv[1], "ipc_helper_bind_twice") == 0) {
    113  1.1  christos     return ipc_helper_bind_twice();
    114  1.1  christos   }
    115  1.1  christos 
    116  1.1  christos   if (strcmp(argv[1], "ipc_helper_send_zero") == 0) {
    117  1.1  christos     return ipc_helper_send_zero();
    118  1.1  christos   }
    119  1.1  christos 
    120  1.1  christos   if (strcmp(argv[1], "stdio_over_pipes_helper") == 0) {
    121  1.1  christos     return stdio_over_pipes_helper();
    122  1.1  christos   }
    123  1.1  christos 
    124  1.1  christos   if (strcmp(argv[1], "spawn_helper1") == 0) {
    125  1.1  christos     notify_parent_process();
    126  1.1  christos     return 1;
    127  1.1  christos   }
    128  1.1  christos 
    129  1.1  christos   if (strcmp(argv[1], "spawn_helper2") == 0) {
    130  1.1  christos     notify_parent_process();
    131  1.1  christos     printf("hello world\n");
    132  1.1  christos     return 1;
    133  1.1  christos   }
    134  1.1  christos 
    135  1.1  christos   if (strcmp(argv[1], "spawn_tcp_server_helper") == 0) {
    136  1.1  christos     notify_parent_process();
    137  1.1  christos     return spawn_tcp_server_helper();
    138  1.1  christos   }
    139  1.1  christos 
    140  1.1  christos   if (strcmp(argv[1], "spawn_helper3") == 0) {
    141  1.1  christos     char buffer[256];
    142  1.1  christos     notify_parent_process();
    143  1.1  christos     ASSERT(buffer == fgets(buffer, sizeof(buffer) - 1, stdin));
    144  1.1  christos     buffer[sizeof(buffer) - 1] = '\0';
    145  1.1  christos     fputs(buffer, stdout);
    146  1.1  christos     return 1;
    147  1.1  christos   }
    148  1.1  christos 
    149  1.1  christos   if (strcmp(argv[1], "spawn_helper4") == 0) {
    150  1.1  christos     notify_parent_process();
    151  1.1  christos     /* Never surrender, never return! */
    152  1.1  christos     while (1) uv_sleep(10000);
    153  1.1  christos   }
    154  1.1  christos 
    155  1.1  christos   if (strcmp(argv[1], "spawn_helper5") == 0) {
    156  1.1  christos     const char out[] = "fourth stdio!\n";
    157  1.1  christos     notify_parent_process();
    158  1.1  christos     {
    159  1.1  christos #ifdef _WIN32
    160  1.1  christos       DWORD bytes;
    161  1.1  christos       WriteFile((HANDLE) _get_osfhandle(3), out, sizeof(out) - 1, &bytes, NULL);
    162  1.1  christos #else
    163  1.1  christos       ssize_t r;
    164  1.1  christos 
    165  1.1  christos       do
    166  1.1  christos         r = write(3, out, sizeof(out) - 1);
    167  1.1  christos       while (r == -1 && errno == EINTR);
    168  1.1  christos 
    169  1.1  christos       fsync(3);
    170  1.1  christos #endif
    171  1.1  christos     }
    172  1.1  christos     return 1;
    173  1.1  christos   }
    174  1.1  christos 
    175  1.1  christos   if (strcmp(argv[1], "spawn_helper6") == 0) {
    176  1.1  christos     int r;
    177  1.1  christos 
    178  1.1  christos     notify_parent_process();
    179  1.1  christos 
    180  1.1  christos     r = fprintf(stdout, "hello world\n");
    181  1.1  christos     ASSERT(r > 0);
    182  1.1  christos 
    183  1.1  christos     r = fprintf(stderr, "hello errworld\n");
    184  1.1  christos     ASSERT(r > 0);
    185  1.1  christos 
    186  1.1  christos     return 1;
    187  1.1  christos   }
    188  1.1  christos 
    189  1.1  christos   if (strcmp(argv[1], "spawn_helper7") == 0) {
    190  1.1  christos     int r;
    191  1.1  christos     char *test;
    192  1.1  christos 
    193  1.1  christos     notify_parent_process();
    194  1.1  christos 
    195  1.1  christos     /* Test if the test value from the parent is still set */
    196  1.1  christos     test = getenv("ENV_TEST");
    197  1.1  christos     ASSERT(test != NULL);
    198  1.1  christos 
    199  1.1  christos     r = fprintf(stdout, "%s", test);
    200  1.1  christos     ASSERT(r > 0);
    201  1.1  christos 
    202  1.1  christos     return 1;
    203  1.1  christos   }
    204  1.1  christos 
    205  1.1  christos #ifndef _WIN32
    206  1.1  christos   if (strcmp(argv[1], "spawn_helper8") == 0) {
    207  1.1  christos     int fd;
    208  1.1  christos 
    209  1.1  christos     notify_parent_process();
    210  1.1  christos     ASSERT(sizeof(fd) == read(0, &fd, sizeof(fd)));
    211  1.1  christos     ASSERT(fd > 2);
    212  1.1  christos # if defined(__PASE__)  /* On IBMi PASE, write() returns 1 */
    213  1.1  christos     ASSERT(1 == write(fd, "x", 1));
    214  1.1  christos # else
    215  1.1  christos     ASSERT(-1 == write(fd, "x", 1));
    216  1.1  christos # endif  /* !__PASE__ */
    217  1.1  christos 
    218  1.1  christos     return 1;
    219  1.1  christos   }
    220  1.1  christos #endif  /* !_WIN32 */
    221  1.1  christos 
    222  1.1  christos   if (strcmp(argv[1], "spawn_helper9") == 0) {
    223  1.1  christos     notify_parent_process();
    224  1.1  christos     spawn_stdin_stdout();
    225  1.1  christos     return 1;
    226  1.1  christos   }
    227  1.1  christos 
    228  1.1  christos #ifndef _WIN32
    229  1.1  christos   if (strcmp(argv[1], "spawn_helper_setuid_setgid") == 0) {
    230  1.1  christos     uv_uid_t uid = atoi(argv[2]);
    231  1.1  christos     uv_gid_t gid = atoi(argv[3]);
    232  1.1  christos 
    233  1.1  christos     ASSERT(uid == getuid());
    234  1.1  christos     ASSERT(gid == getgid());
    235  1.1  christos     notify_parent_process();
    236  1.1  christos 
    237  1.1  christos     return 1;
    238  1.1  christos   }
    239  1.1  christos #endif  /* !_WIN32 */
    240  1.1  christos 
    241  1.1  christos   if (strcmp(argv[1], "process_title_big_argv_helper") == 0) {
    242  1.1  christos     notify_parent_process();
    243  1.1  christos     process_title_big_argv();
    244  1.1  christos     return 0;
    245  1.1  christos   }
    246  1.1  christos 
    247  1.1  christos   return run_test(argv[1], 0, 1);
    248  1.1  christos }
    249