/test/run-tests.c

https://github.com/mmalecki/libuv · C · 172 lines · 114 code · 35 blank · 23 comment · 40 complexity · b2a4a6a8ffd4d6322fe3926c892713cc MD5 · raw file

  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #ifdef _WIN32
  25. # include <io.h>
  26. #else
  27. # include <unistd.h>
  28. #endif
  29. #include "uv.h"
  30. #include "runner.h"
  31. #include "task.h"
  32. /* Actual tests and helpers are defined in test-list.h */
  33. #include "test-list.h"
  34. int ipc_helper(int listen_after_write);
  35. int ipc_helper_tcp_connection(void);
  36. int ipc_send_recv_helper(void);
  37. int ipc_helper_bind_twice(void);
  38. int stdio_over_pipes_helper(void);
  39. static int maybe_run_test(int argc, char **argv);
  40. int main(int argc, char **argv) {
  41. platform_init(argc, argv);
  42. argv = uv_setup_args(argc, argv);
  43. switch (argc) {
  44. case 1: return run_tests(0);
  45. case 2: return maybe_run_test(argc, argv);
  46. case 3: return run_test_part(argv[1], argv[2]);
  47. default:
  48. LOGF("Too many arguments.\n");
  49. return 1;
  50. }
  51. }
  52. static int maybe_run_test(int argc, char **argv) {
  53. if (strcmp(argv[1], "--list") == 0) {
  54. print_tests(stdout);
  55. return 0;
  56. }
  57. if (strcmp(argv[1], "ipc_helper_listen_before_write") == 0) {
  58. return ipc_helper(0);
  59. }
  60. if (strcmp(argv[1], "ipc_helper_listen_after_write") == 0) {
  61. return ipc_helper(1);
  62. }
  63. if (strcmp(argv[1], "ipc_send_recv_helper") == 0) {
  64. return ipc_send_recv_helper();
  65. }
  66. if (strcmp(argv[1], "ipc_helper_tcp_connection") == 0) {
  67. return ipc_helper_tcp_connection();
  68. }
  69. if (strcmp(argv[1], "ipc_helper_bind_twice") == 0) {
  70. return ipc_helper_bind_twice();
  71. }
  72. if (strcmp(argv[1], "stdio_over_pipes_helper") == 0) {
  73. return stdio_over_pipes_helper();
  74. }
  75. if (strcmp(argv[1], "spawn_helper1") == 0) {
  76. return 1;
  77. }
  78. if (strcmp(argv[1], "spawn_helper2") == 0) {
  79. printf("hello world\n");
  80. return 1;
  81. }
  82. if (strcmp(argv[1], "spawn_helper3") == 0) {
  83. char buffer[256];
  84. ASSERT(buffer == fgets(buffer, sizeof(buffer) - 1, stdin));
  85. buffer[sizeof(buffer) - 1] = '\0';
  86. fputs(buffer, stdout);
  87. return 1;
  88. }
  89. if (strcmp(argv[1], "spawn_helper4") == 0) {
  90. /* Never surrender, never return! */
  91. while (1) uv_sleep(10000);
  92. }
  93. if (strcmp(argv[1], "spawn_helper5") == 0) {
  94. const char out[] = "fourth stdio!\n";
  95. #ifdef _WIN32
  96. DWORD bytes;
  97. WriteFile((HANDLE) _get_osfhandle(3), out, sizeof(out) - 1, &bytes, NULL);
  98. #else
  99. {
  100. ssize_t r;
  101. do
  102. r = write(3, out, sizeof(out) - 1);
  103. while (r == -1 && errno == EINTR);
  104. fsync(3);
  105. }
  106. #endif
  107. return 1;
  108. }
  109. if (strcmp(argv[1], "spawn_helper6") == 0) {
  110. int r;
  111. r = fprintf(stdout, "hello world\n");
  112. ASSERT(r > 0);
  113. r = fprintf(stderr, "hello errworld\n");
  114. ASSERT(r > 0);
  115. return 1;
  116. }
  117. if (strcmp(argv[1], "spawn_helper7") == 0) {
  118. int r;
  119. char *test;
  120. /* Test if the test value from the parent is still set */
  121. test = getenv("ENV_TEST");
  122. ASSERT(test != NULL);
  123. r = fprintf(stdout, "%s", test);
  124. ASSERT(r > 0);
  125. return 1;
  126. }
  127. #ifndef _WIN32
  128. if (strcmp(argv[1], "spawn_helper8") == 0) {
  129. int fd;
  130. ASSERT(sizeof(fd) == read(0, &fd, sizeof(fd)));
  131. ASSERT(fd > 2);
  132. ASSERT(-1 == write(fd, "x", 1));
  133. return 1;
  134. }
  135. #endif /* !_WIN32 */
  136. return run_test(argv[1], 0, 1);
  137. }