client-test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright © 2012 Intel Corporation
  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 <stdio.h>
  26. #include <stdlib.h>
  27. #include <stdarg.h>
  28. #include <string.h>
  29. #include <assert.h>
  30. #include <sys/socket.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include "wayland-os.h"
  36. #include "wayland-private.h"
  37. #include "wayland-server.h"
  38. #include "test-runner.h"
  39. struct client_destroy_listener {
  40. struct wl_listener listener;
  41. bool done;
  42. struct wl_listener late_listener;
  43. bool late_done;
  44. struct wl_listener resource_listener;
  45. bool resource_done;
  46. };
  47. static void
  48. client_destroy_notify(struct wl_listener *l, void *data)
  49. {
  50. struct client_destroy_listener *listener =
  51. wl_container_of(l, listener, listener);
  52. listener->done = true;
  53. assert(!listener->resource_done);
  54. assert(!listener->late_done);
  55. }
  56. static void
  57. client_resource_destroy_notify(struct wl_listener *l, void *data)
  58. {
  59. struct client_destroy_listener *listener =
  60. wl_container_of(l, listener, resource_listener);
  61. assert(listener->done);
  62. listener->resource_done = true;
  63. assert(!listener->late_done);
  64. }
  65. static void
  66. client_late_destroy_notify(struct wl_listener *l, void *data)
  67. {
  68. struct client_destroy_listener *listener =
  69. wl_container_of(l, listener, late_listener);
  70. assert(listener->done);
  71. assert(listener->resource_done);
  72. listener->late_done = true;
  73. }
  74. static void
  75. client_user_data_destroy(void *data)
  76. {
  77. bool *user_data_destroyed = data;
  78. *user_data_destroyed = true;
  79. }
  80. TEST(client_destroy_listener)
  81. {
  82. struct wl_display *display;
  83. struct wl_client *client;
  84. struct wl_resource *resource;
  85. struct client_destroy_listener a, b;
  86. bool user_data_destroyed = false;
  87. int s[2];
  88. assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
  89. display = wl_display_create();
  90. assert(display);
  91. client = wl_client_create(display, s[0]);
  92. assert(client);
  93. wl_client_set_user_data(client, &user_data_destroyed, client_user_data_destroy);
  94. assert(wl_client_get_user_data(client) == &user_data_destroyed);
  95. resource = wl_resource_create(client, &wl_callback_interface, 1, 0);
  96. assert(resource);
  97. a.listener.notify = client_destroy_notify;
  98. a.done = false;
  99. a.resource_listener.notify = client_resource_destroy_notify;
  100. a.resource_done = false;
  101. a.late_listener.notify = client_late_destroy_notify;
  102. a.late_done = false;
  103. wl_client_add_destroy_listener(client, &a.listener);
  104. wl_resource_add_destroy_listener(resource, &a.resource_listener);
  105. wl_client_add_destroy_late_listener(client, &a.late_listener);
  106. assert(wl_client_get_destroy_listener(client, client_destroy_notify) ==
  107. &a.listener);
  108. assert(wl_resource_get_destroy_listener(resource, client_resource_destroy_notify) ==
  109. &a.resource_listener);
  110. assert(wl_client_get_destroy_late_listener(client, client_late_destroy_notify) ==
  111. &a.late_listener);
  112. b.listener.notify = client_destroy_notify;
  113. b.done = false;
  114. b.resource_listener.notify = client_resource_destroy_notify;
  115. b.resource_done = false;
  116. b.late_listener.notify = client_late_destroy_notify;
  117. b.late_done = false;
  118. wl_client_add_destroy_listener(client, &b.listener);
  119. wl_resource_add_destroy_listener(resource, &b.resource_listener);
  120. wl_client_add_destroy_late_listener(client, &b.late_listener);
  121. wl_list_remove(&a.listener.link);
  122. wl_list_remove(&a.resource_listener.link);
  123. wl_list_remove(&a.late_listener.link);
  124. assert(!user_data_destroyed);
  125. wl_client_destroy(client);
  126. assert(!a.done);
  127. assert(!a.resource_done);
  128. assert(!a.late_done);
  129. assert(b.done);
  130. assert(b.resource_done);
  131. assert(b.late_done);
  132. assert(user_data_destroyed);
  133. close(s[0]);
  134. close(s[1]);
  135. wl_display_destroy(display);
  136. }
  137. static void
  138. client_destroy_remove_link_notify(struct wl_listener *l, void *data)
  139. {
  140. struct wl_client *client = data;
  141. struct client_destroy_listener *listener =
  142. wl_container_of(l, listener, listener);
  143. /* The client destruction signal should not be emitted more than once. */
  144. assert(!listener->done);
  145. listener->done = true;
  146. /* The client should have been removed from the display's list. */
  147. assert(wl_list_empty(wl_client_get_link(client)));
  148. }
  149. /*
  150. * Tests that wl_client_destroy() will remove the client from the display's
  151. * client list to prevent client access during destruction.
  152. */
  153. TEST(client_destroy_removes_link)
  154. {
  155. struct wl_display *display;
  156. struct wl_client *client;
  157. struct client_destroy_listener destroy_listener;
  158. int s[2];
  159. assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
  160. display = wl_display_create();
  161. assert(display);
  162. client = wl_client_create(display, s[0]);
  163. assert(client);
  164. destroy_listener.listener.notify = client_destroy_remove_link_notify;
  165. destroy_listener.done = false;
  166. wl_client_add_destroy_listener(client, &destroy_listener.listener);
  167. assert(wl_client_get_destroy_listener(client,
  168. client_destroy_remove_link_notify) == &destroy_listener.listener);
  169. wl_client_destroy(client);
  170. assert(destroy_listener.done);
  171. close(s[0]);
  172. close(s[1]);
  173. wl_display_destroy(display);
  174. }