resources-test.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright © 2013 Marek Chalupa
  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 <assert.h>
  26. #include <sys/socket.h>
  27. #include <unistd.h>
  28. #include <stdint.h>
  29. #include "wayland-os.h"
  30. #include "wayland-server.h"
  31. #include "test-runner.h"
  32. TEST(create_resource_tst)
  33. {
  34. struct wl_display *display;
  35. struct wl_client *client;
  36. struct wl_resource *res;
  37. struct wl_list *link;
  38. int s[2];
  39. uint32_t id;
  40. assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
  41. display = wl_display_create();
  42. assert(display);
  43. client = wl_client_create(display, s[0]);
  44. assert(client);
  45. res = wl_resource_create(client, &wl_seat_interface, 4, 0);
  46. assert(res);
  47. /* setters/getters */
  48. assert(wl_resource_get_version(res) == 4);
  49. assert(client == wl_resource_get_client(res));
  50. id = wl_resource_get_id(res);
  51. assert(wl_client_get_object(client, id) == res);
  52. link = wl_resource_get_link(res);
  53. assert(link);
  54. assert(wl_resource_from_link(link) == res);
  55. wl_resource_set_user_data(res, (void *) 0xbee);
  56. assert(wl_resource_get_user_data(res) == (void *) 0xbee);
  57. wl_resource_destroy(res);
  58. wl_client_destroy(client);
  59. wl_display_destroy(display);
  60. close(s[1]);
  61. }
  62. static void
  63. res_destroy_func(struct wl_resource *res)
  64. {
  65. assert(res);
  66. _Bool *destr = wl_resource_get_user_data(res);
  67. *destr = 1;
  68. }
  69. static _Bool notify_called = 0;
  70. static void
  71. destroy_notify(struct wl_listener *l, void *data)
  72. {
  73. assert(l && data);
  74. notify_called = 1;
  75. /* In real code it's common to free the structure holding the
  76. * listener at this point, but not to remove it from the list.
  77. *
  78. * That's fine since this is a destruction notification and
  79. * it's the last time this signal can fire. We set these
  80. * to NULL so we can check them later to ensure no write after
  81. * "free" occurred.
  82. */
  83. l->link.prev = NULL;
  84. l->link.next = NULL;
  85. }
  86. TEST(destroy_res_tst)
  87. {
  88. struct wl_display *display;
  89. struct wl_client *client;
  90. struct wl_resource *res;
  91. int s[2];
  92. unsigned id;
  93. struct wl_list *link;
  94. _Bool destroyed = 0;
  95. struct wl_listener destroy_listener = {
  96. .notify = &destroy_notify
  97. };
  98. assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
  99. display = wl_display_create();
  100. assert(display);
  101. client = wl_client_create(display, s[0]);
  102. assert(client);
  103. res = wl_resource_create(client, &wl_seat_interface, 4, 0);
  104. assert(res);
  105. wl_resource_set_implementation(res, NULL, &destroyed, res_destroy_func);
  106. wl_resource_add_destroy_listener(res, &destroy_listener);
  107. id = wl_resource_get_id(res);
  108. link = wl_resource_get_link(res);
  109. assert(link);
  110. wl_resource_destroy(res);
  111. assert(destroyed);
  112. assert(notify_called); /* check if signal was emitted */
  113. assert(wl_client_get_object(client, id) == NULL);
  114. assert(destroy_listener.link.prev == NULL);
  115. assert(destroy_listener.link.next == NULL);
  116. res = wl_resource_create(client, &wl_seat_interface, 2, 0);
  117. assert(res);
  118. destroyed = 0;
  119. notify_called = 0;
  120. wl_resource_set_destructor(res, res_destroy_func);
  121. wl_resource_set_user_data(res, &destroyed);
  122. wl_resource_add_destroy_listener(res, &destroy_listener);
  123. /* client should destroy the resource upon its destruction */
  124. wl_client_destroy(client);
  125. assert(destroyed);
  126. assert(notify_called);
  127. assert(destroy_listener.link.prev == NULL);
  128. assert(destroy_listener.link.next == NULL);
  129. wl_display_destroy(display);
  130. close(s[1]);
  131. }
  132. TEST(create_resource_with_same_id)
  133. {
  134. struct wl_display *display;
  135. struct wl_client *client;
  136. struct wl_resource *res, *res2;
  137. int s[2];
  138. uint32_t id;
  139. assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
  140. display = wl_display_create();
  141. assert(display);
  142. client = wl_client_create(display, s[0]);
  143. assert(client);
  144. res = wl_resource_create(client, &wl_seat_interface, 2, 0);
  145. assert(res);
  146. id = wl_resource_get_id(res);
  147. assert(wl_client_get_object(client, id) == res);
  148. /* this one should replace the old one */
  149. res2 = wl_resource_create(client, &wl_seat_interface, 1, id);
  150. assert(res2 != NULL);
  151. assert(wl_client_get_object(client, id) == res2);
  152. wl_resource_destroy(res2);
  153. wl_resource_destroy(res);
  154. wl_client_destroy(client);
  155. wl_display_destroy(display);
  156. close(s[1]);
  157. }
  158. static void
  159. display_destroy_notify(struct wl_listener *l, void *data)
  160. {
  161. l->link.prev = l->link.next = NULL;
  162. }
  163. TEST(free_without_remove)
  164. {
  165. struct wl_display *display;
  166. struct wl_listener a, b;
  167. display = wl_display_create();
  168. a.notify = display_destroy_notify;
  169. b.notify = display_destroy_notify;
  170. wl_display_add_destroy_listener(display, &a);
  171. wl_display_add_destroy_listener(display, &b);
  172. wl_display_destroy(display);
  173. assert(a.link.next == a.link.prev && a.link.next == NULL);
  174. assert(b.link.next == b.link.prev && b.link.next == NULL);
  175. }
  176. static enum wl_iterator_result
  177. client_resource_check(struct wl_resource* resource, void* data)
  178. {
  179. /* Ensure there is no iteration over already freed resources. */
  180. assert(!wl_resource_get_user_data(resource));
  181. return WL_ITERATOR_CONTINUE;
  182. }
  183. static void
  184. resource_destroy_notify(struct wl_listener *l, void *data)
  185. {
  186. struct wl_resource* resource = data;
  187. struct wl_client* client = resource->client;
  188. wl_client_for_each_resource(client, client_resource_check, NULL);
  189. /* Set user data to flag the resource has been deleted. The resource should
  190. * not be accessible from this point forward. */
  191. wl_resource_set_user_data(resource, client);
  192. }
  193. TEST(resource_destroy_iteration)
  194. {
  195. struct wl_display *display;
  196. struct wl_client *client;
  197. struct wl_resource *resource1;
  198. struct wl_resource *resource2;
  199. int s[2];
  200. struct wl_listener destroy_listener1 = {
  201. .notify = &resource_destroy_notify
  202. };
  203. struct wl_listener destroy_listener2 = {
  204. .notify = &resource_destroy_notify
  205. };
  206. assert(wl_os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, s) == 0);
  207. display = wl_display_create();
  208. assert(display);
  209. client = wl_client_create(display, s[0]);
  210. assert(client);
  211. resource1 = wl_resource_create(client, &wl_callback_interface, 1, 0);
  212. resource2 = wl_resource_create(client, &wl_callback_interface, 1, 0);
  213. assert(resource1);
  214. assert(resource2);
  215. wl_resource_add_destroy_listener(resource1, &destroy_listener1);
  216. wl_resource_add_destroy_listener(resource2, &destroy_listener2);
  217. wl_client_destroy(client);
  218. close(s[0]);
  219. close(s[1]);
  220. wl_display_destroy(display);
  221. }