sanity-test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright © 2012 Collabora, Ltd.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #include <stdlib.h>
  26. #include <assert.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/mman.h>
  30. #include <signal.h>
  31. #include <unistd.h>
  32. #include "test-runner.h"
  33. #include "wayland-util.h"
  34. #include "wayland-private.h"
  35. #include "test-compositor.h"
  36. extern int fd_leak_check_enabled;
  37. TEST(empty)
  38. {
  39. }
  40. TEST(exit_success)
  41. {
  42. exit(EXIT_SUCCESS);
  43. }
  44. FAIL_TEST(exit_failure)
  45. {
  46. exit(EXIT_FAILURE);
  47. }
  48. FAIL_TEST(fail_abort)
  49. {
  50. test_disable_coredumps();
  51. abort();
  52. }
  53. FAIL_TEST(fail_wl_abort)
  54. {
  55. test_disable_coredumps();
  56. wl_abort("Abort the program\n");
  57. }
  58. FAIL_TEST(fail_kill)
  59. {
  60. kill(getpid(), SIGTERM);
  61. }
  62. FAIL_TEST(fail_segv)
  63. {
  64. char * volatile *null = 0;
  65. test_disable_coredumps();
  66. *null = "Goodbye, world";
  67. }
  68. FAIL_TEST(sanity_assert)
  69. {
  70. test_disable_coredumps();
  71. /* must fail */
  72. assert(0);
  73. }
  74. FAIL_TEST(sanity_fd_leak)
  75. {
  76. int fd[2];
  77. assert(fd_leak_check_enabled);
  78. /* leak 2 file descriptors */
  79. if (pipe(fd) < 0)
  80. exit(EXIT_SUCCESS); /* failed to fail */
  81. test_disable_coredumps();
  82. }
  83. FAIL_TEST(sanity_fd_leak_exec)
  84. {
  85. int fd[2];
  86. int nr_fds = count_open_fds();
  87. /* leak 2 file descriptors */
  88. if (pipe(fd) < 0)
  89. exit(EXIT_SUCCESS); /* failed to fail */
  90. test_disable_coredumps();
  91. exec_fd_leak_check(nr_fds);
  92. }
  93. TEST(sanity_fd_exec)
  94. {
  95. int fd[2];
  96. int nr_fds = count_open_fds();
  97. /* create 2 file descriptors, that should pass over exec */
  98. assert(pipe(fd) >= 0);
  99. exec_fd_leak_check(nr_fds + 2);
  100. }
  101. static void
  102. sanity_fd_no_leak(void)
  103. {
  104. int fd[2];
  105. assert(fd_leak_check_enabled);
  106. /* leak 2 file descriptors */
  107. if (pipe(fd) < 0)
  108. exit(EXIT_SUCCESS); /* failed to fail */
  109. close(fd[0]);
  110. close(fd[1]);
  111. }
  112. static void
  113. sanity_client_no_leak(void)
  114. {
  115. struct wl_display *display = wl_display_connect(NULL);
  116. assert(display);
  117. wl_display_disconnect(display);
  118. }
  119. TEST(tc_client_no_fd_leaks)
  120. {
  121. struct display *d = display_create();
  122. /* Client which does not consume the WAYLAND_DISPLAY socket. */
  123. client_create_noarg(d, sanity_fd_no_leak);
  124. display_run(d);
  125. /* Client which does consume the WAYLAND_DISPLAY socket. */
  126. client_create_noarg(d, sanity_client_no_leak);
  127. display_run(d);
  128. display_destroy(d);
  129. }
  130. FAIL_TEST(tc_client_fd_leaks)
  131. {
  132. struct display *d = display_create();
  133. client_create_noarg(d, sanity_fd_leak);
  134. display_run(d);
  135. test_disable_coredumps();
  136. display_destroy(d);
  137. }
  138. FAIL_TEST(tc_client_fd_leaks_exec)
  139. {
  140. struct display *d = display_create();
  141. client_create_noarg(d, sanity_fd_leak_exec);
  142. display_run(d);
  143. test_disable_coredumps();
  144. display_destroy(d);
  145. }
  146. static char *
  147. map_file(int fd, size_t *len)
  148. {
  149. char *data;
  150. *len = lseek(fd, 0, SEEK_END);
  151. data = mmap(0, *len, PROT_READ, MAP_PRIVATE, fd, 0);
  152. assert(data != MAP_FAILED && "Failed to mmap file");
  153. return data;
  154. }
  155. static void
  156. sanity_client_log(void)
  157. {
  158. char *log;
  159. size_t log_len;
  160. char *wayland_socket = strdup(getenv("WAYLAND_SOCKET"));
  161. char *xdg_runtime_dir = strdup(getenv("XDG_RUNTIME_DIR"));
  162. unsetenv("WAYLAND_SOCKET");
  163. unsetenv("XDG_RUNTIME_DIR");
  164. /* Try to connect to the default wayland display, which should fail since
  165. * we have neither WAYLAND_SOCKET nor XDG_RUNTIME_DIR. */
  166. assert(!wl_display_connect(NULL));
  167. /* Check that the client log contains some mention of XDG_RUNTIME_DIR. */
  168. log = map_file(client_log_fd, &log_len);
  169. assert(strstr(log, "XDG_RUNTIME_DIR"));
  170. munmap(log, log_len);
  171. /* Reset the environment variables we unset for the test. The test harness
  172. * leak checker cares about the value of WAYLAND_SOCKET during teardown for
  173. * correct fd accounting. */
  174. setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 0);
  175. setenv("WAYLAND_SOCKET", wayland_socket, 0);
  176. free(xdg_runtime_dir);
  177. free(wayland_socket);
  178. }
  179. TEST(tc_client_log)
  180. {
  181. struct display *d = display_create();
  182. struct client_info *ci;
  183. char *log;
  184. size_t log_len;
  185. ci = client_create_noarg(d, sanity_client_log);
  186. display_run(d);
  187. /* Check that the client log contains some mention of XDG_RUNTIME_DIR. */
  188. log = map_file(ci->log_fd, &log_len);
  189. assert(strstr(log, "XDG_RUNTIME_DIR"));
  190. munmap(log, log_len);
  191. display_destroy(d);
  192. }
  193. FAIL_TEST(timeout_tst)
  194. {
  195. test_set_timeout(1);
  196. test_disable_coredumps();
  197. /* test should reach timeout */
  198. test_sleep(2);
  199. }
  200. TEST(timeout2_tst)
  201. {
  202. /* the test should end before reaching timeout,
  203. * thus it should pass */
  204. test_set_timeout(1);
  205. /* 200 000 microsec = 0.2 sec */
  206. test_usleep(200000);
  207. }
  208. FAIL_TEST(timeout_reset_tst)
  209. {
  210. test_set_timeout(5);
  211. test_set_timeout(10);
  212. test_set_timeout(1);
  213. test_disable_coredumps();
  214. /* test should fail on timeout */
  215. test_sleep(2);
  216. }
  217. TEST(timeout_turnoff)
  218. {
  219. test_set_timeout(1);
  220. test_set_timeout(0);
  221. test_usleep(2);
  222. }
  223. /* test timeouts with test-compositor */
  224. FAIL_TEST(tc_timeout_tst)
  225. {
  226. struct display *d = display_create();
  227. client_create_noarg(d, timeout_tst);
  228. display_run(d);
  229. test_disable_coredumps();
  230. display_destroy(d);
  231. }
  232. FAIL_TEST(tc_timeout2_tst)
  233. {
  234. struct display *d = display_create();
  235. client_create_noarg(d, timeout_reset_tst);
  236. display_run(d);
  237. test_disable_coredumps();
  238. display_destroy(d);
  239. }
  240. TEST(tc_timeout3_tst)
  241. {
  242. struct display *d = display_create();
  243. client_create_noarg(d, timeout2_tst);
  244. display_run(d);
  245. client_create_noarg(d, timeout_turnoff);
  246. display_run(d);
  247. display_destroy(d);
  248. }