Blame test/run-tests.c

Packit b5b901
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#include <errno.h>
Packit b5b901
#include <stdio.h>
Packit b5b901
#include <string.h>
Packit b5b901
Packit b5b901
#ifdef _WIN32
Packit b5b901
# include <io.h>
Packit b5b901
#else
Packit b5b901
# include <unistd.h>
Packit b5b901
#endif
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "runner.h"
Packit b5b901
#include "task.h"
Packit b5b901
Packit b5b901
/* Actual tests and helpers are defined in test-list.h */
Packit b5b901
#include "test-list.h"
Packit b5b901
Packit b5b901
int ipc_helper(int listen_after_write);
Packit b5b901
int ipc_helper_heavy_traffic_deadlock_bug(void);
Packit b5b901
int ipc_helper_tcp_connection(void);
Packit b5b901
int ipc_helper_closed_handle(void);
Packit b5b901
int ipc_send_recv_helper(void);
Packit b5b901
int ipc_helper_bind_twice(void);
Packit Service e08953
int ipc_helper_send_zero(void);
Packit b5b901
int stdio_over_pipes_helper(void);
Packit Service e08953
void spawn_stdin_stdout(void);
Packit Service e08953
void process_title_big_argv(void);
Packit b5b901
int spawn_tcp_server_helper(void);
Packit b5b901
Packit b5b901
static int maybe_run_test(int argc, char **argv);
Packit b5b901
Packit b5b901
Packit b5b901
int main(int argc, char **argv) {
Packit Service e08953
#ifndef _WIN32
Packit Service e08953
  if (0 == geteuid() && NULL == getenv("UV_RUN_AS_ROOT")) {
Packit Service e08953
    fprintf(stderr, "The libuv test suite cannot be run as root.\n");
Packit b5b901
    return EXIT_FAILURE;
Packit Service e08953
  }
Packit Service e08953
#endif
Packit b5b901
Packit Service e08953
  platform_init(argc, argv);
Packit b5b901
  argv = uv_setup_args(argc, argv);
Packit b5b901
Packit b5b901
  switch (argc) {
Packit b5b901
  case 1: return run_tests(0);
Packit b5b901
  case 2: return maybe_run_test(argc, argv);
Packit b5b901
  case 3: return run_test_part(argv[1], argv[2]);
Packit b5b901
  case 4: return maybe_run_test(argc, argv);
Packit b5b901
  default:
Packit b5b901
    fprintf(stderr, "Too many arguments.\n");
Packit b5b901
    fflush(stderr);
Packit b5b901
    return EXIT_FAILURE;
Packit b5b901
  }
Packit b5b901
Packit Service e08953
#ifndef __SUNPRO_C
Packit b5b901
  return EXIT_SUCCESS;
Packit Service e08953
#endif
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static int maybe_run_test(int argc, char **argv) {
Packit b5b901
  if (strcmp(argv[1], "--list") == 0) {
Packit b5b901
    print_tests(stdout);
Packit b5b901
    return 0;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "ipc_helper_listen_before_write") == 0) {
Packit b5b901
    return ipc_helper(0);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "ipc_helper_listen_after_write") == 0) {
Packit b5b901
    return ipc_helper(1);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "ipc_helper_heavy_traffic_deadlock_bug") == 0) {
Packit b5b901
    return ipc_helper_heavy_traffic_deadlock_bug();
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "ipc_send_recv_helper") == 0) {
Packit b5b901
    return ipc_send_recv_helper();
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "ipc_helper_tcp_connection") == 0) {
Packit b5b901
    return ipc_helper_tcp_connection();
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "ipc_helper_closed_handle") == 0) {
Packit b5b901
    return ipc_helper_closed_handle();
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "ipc_helper_bind_twice") == 0) {
Packit b5b901
    return ipc_helper_bind_twice();
Packit b5b901
  }
Packit b5b901
Packit Service e08953
  if (strcmp(argv[1], "ipc_helper_send_zero") == 0) {
Packit Service e08953
    return ipc_helper_send_zero();
Packit Service e08953
  }
Packit Service e08953
Packit b5b901
  if (strcmp(argv[1], "stdio_over_pipes_helper") == 0) {
Packit b5b901
    return stdio_over_pipes_helper();
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_helper1") == 0) {
Packit Service e08953
    notify_parent_process();
Packit b5b901
    return 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_helper2") == 0) {
Packit Service e08953
    notify_parent_process();
Packit b5b901
    printf("hello world\n");
Packit b5b901
    return 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_tcp_server_helper") == 0) {
Packit Service e08953
    notify_parent_process();
Packit b5b901
    return spawn_tcp_server_helper();
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_helper3") == 0) {
Packit b5b901
    char buffer[256];
Packit Service e08953
    notify_parent_process();
Packit b5b901
    ASSERT(buffer == fgets(buffer, sizeof(buffer) - 1, stdin));
Packit b5b901
    buffer[sizeof(buffer) - 1] = '\0';
Packit b5b901
    fputs(buffer, stdout);
Packit b5b901
    return 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_helper4") == 0) {
Packit Service e08953
    notify_parent_process();
Packit b5b901
    /* Never surrender, never return! */
Packit b5b901
    while (1) uv_sleep(10000);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_helper5") == 0) {
Packit b5b901
    const char out[] = "fourth stdio!\n";
Packit Service e08953
    notify_parent_process();
Packit Service e08953
    {
Packit b5b901
#ifdef _WIN32
Packit Service e08953
      DWORD bytes;
Packit Service e08953
      WriteFile((HANDLE) _get_osfhandle(3), out, sizeof(out) - 1, &bytes, NULL);
Packit b5b901
#else
Packit b5b901
      ssize_t r;
Packit b5b901
Packit b5b901
      do
Packit b5b901
        r = write(3, out, sizeof(out) - 1);
Packit b5b901
      while (r == -1 && errno == EINTR);
Packit b5b901
Packit b5b901
      fsync(3);
Packit b5b901
#endif
Packit Service e08953
    }
Packit b5b901
    return 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_helper6") == 0) {
Packit b5b901
    int r;
Packit b5b901
Packit Service e08953
    notify_parent_process();
Packit Service e08953
Packit b5b901
    r = fprintf(stdout, "hello world\n");
Packit b5b901
    ASSERT(r > 0);
Packit b5b901
Packit b5b901
    r = fprintf(stderr, "hello errworld\n");
Packit b5b901
    ASSERT(r > 0);
Packit b5b901
Packit b5b901
    return 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_helper7") == 0) {
Packit b5b901
    int r;
Packit b5b901
    char *test;
Packit Service e08953
Packit Service e08953
    notify_parent_process();
Packit Service e08953
Packit b5b901
    /* Test if the test value from the parent is still set */
Packit b5b901
    test = getenv("ENV_TEST");
Packit b5b901
    ASSERT(test != NULL);
Packit b5b901
Packit b5b901
    r = fprintf(stdout, "%s", test);
Packit b5b901
    ASSERT(r > 0);
Packit b5b901
Packit b5b901
    return 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
#ifndef _WIN32
Packit b5b901
  if (strcmp(argv[1], "spawn_helper8") == 0) {
Packit b5b901
    int fd;
Packit Service e08953
Packit Service e08953
    notify_parent_process();
Packit b5b901
    ASSERT(sizeof(fd) == read(0, &fd, sizeof(fd)));
Packit b5b901
    ASSERT(fd > 2);
Packit Service e08953
# if defined(__PASE__)  /* On IBMi PASE, write() returns 1 */
Packit Service e08953
    ASSERT(1 == write(fd, "x", 1));
Packit Service e08953
# else
Packit b5b901
    ASSERT(-1 == write(fd, "x", 1));
Packit Service e08953
# endif  /* !__PASE__ */
Packit b5b901
Packit b5b901
    return 1;
Packit b5b901
  }
Packit b5b901
#endif  /* !_WIN32 */
Packit b5b901
Packit b5b901
  if (strcmp(argv[1], "spawn_helper9") == 0) {
Packit Service e08953
    notify_parent_process();
Packit Service e08953
    spawn_stdin_stdout();
Packit Service e08953
    return 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
#ifndef _WIN32
Packit b5b901
  if (strcmp(argv[1], "spawn_helper_setuid_setgid") == 0) {
Packit b5b901
    uv_uid_t uid = atoi(argv[2]);
Packit b5b901
    uv_gid_t gid = atoi(argv[3]);
Packit b5b901
Packit b5b901
    ASSERT(uid == getuid());
Packit b5b901
    ASSERT(gid == getgid());
Packit Service e08953
    notify_parent_process();
Packit b5b901
Packit b5b901
    return 1;
Packit b5b901
  }
Packit b5b901
#endif  /* !_WIN32 */
Packit b5b901
Packit Service e08953
  if (strcmp(argv[1], "process_title_big_argv_helper") == 0) {
Packit Service e08953
    notify_parent_process();
Packit Service e08953
    process_title_big_argv();
Packit Service e08953
    return 0;
Packit Service e08953
  }
Packit Service e08953
Packit b5b901
  return run_test(argv[1], 0, 1);
Packit b5b901
}