display-test.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  1. /*
  2. * Copyright © 2012 Intel Corporation
  3. * Copyright © 2013 Jason Ekstrand
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #define _GNU_SOURCE
  27. #include <stdbool.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <sys/socket.h>
  35. #include <unistd.h>
  36. #include <errno.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/mman.h>
  40. #include <pthread.h>
  41. #include <poll.h>
  42. #include "wayland-private.h"
  43. #include "wayland-server.h"
  44. #include "wayland-client.h"
  45. #include "test-runner.h"
  46. #include "test-compositor.h"
  47. #include "tests-server-protocol.h"
  48. #include "tests-client-protocol.h"
  49. static void
  50. registry_handle_global_noop(void *data,
  51. struct wl_registry *registry,
  52. uint32_t id, const char *interface,
  53. uint32_t ver)
  54. {
  55. }
  56. static void
  57. registry_handle_global_remove_noop(void *data,
  58. struct wl_registry *registry,
  59. uint32_t id)
  60. {
  61. }
  62. static void
  63. fixes_destroy(struct wl_client *client,
  64. struct wl_resource *resource)
  65. {
  66. wl_resource_destroy(resource);
  67. }
  68. static void
  69. fixes_destroy_registry(struct wl_client *client,
  70. struct wl_resource *resource,
  71. struct wl_resource *registry)
  72. {
  73. wl_resource_destroy(registry);
  74. }
  75. static void
  76. fixes_ack_global_remove(struct wl_client *client,
  77. struct wl_resource *resource,
  78. struct wl_resource *registry,
  79. uint32_t global_id)
  80. {
  81. wl_fixes_handle_ack_global_remove(resource, registry, global_id);
  82. }
  83. static const struct wl_fixes_interface fixes_implementation = {
  84. .destroy = fixes_destroy,
  85. .destroy_registry = fixes_destroy_registry,
  86. .ack_global_remove = fixes_ack_global_remove,
  87. };
  88. static void
  89. bind_fixes(struct wl_client *client,
  90. void *data,
  91. uint32_t version,
  92. uint32_t id)
  93. {
  94. struct wl_resource *resource;
  95. resource = wl_resource_create(client, &wl_fixes_interface, version, id);
  96. wl_resource_set_implementation(resource, &fixes_implementation, NULL, NULL);
  97. }
  98. struct display_destroy_listener {
  99. struct wl_listener listener;
  100. int done;
  101. };
  102. static void
  103. display_destroy_notify(struct wl_listener *l, void *data)
  104. {
  105. struct display_destroy_listener *listener;
  106. listener = wl_container_of(l, listener, listener);
  107. listener->done = 1;
  108. }
  109. TEST(display_destroy_listener)
  110. {
  111. struct wl_display *display;
  112. struct display_destroy_listener a, b;
  113. display = wl_display_create();
  114. assert(display);
  115. a.listener.notify = &display_destroy_notify;
  116. a.done = 0;
  117. wl_display_add_destroy_listener(display, &a.listener);
  118. assert(wl_display_get_destroy_listener(display, display_destroy_notify) ==
  119. &a.listener);
  120. b.listener.notify = display_destroy_notify;
  121. b.done = 0;
  122. wl_display_add_destroy_listener(display, &b.listener);
  123. wl_list_remove(&a.listener.link);
  124. wl_display_destroy(display);
  125. assert(!a.done);
  126. assert(b.done);
  127. }
  128. /* Fake 'client' which does not use wl_display_connect, and thus leaves the
  129. * file descriptor passed through WAYLAND_SOCKET intact. This should not
  130. * trigger an assertion in the leak check. */
  131. static void
  132. empty_client(void)
  133. {
  134. return;
  135. }
  136. TEST(tc_leaks_tests)
  137. {
  138. struct display *d = display_create();
  139. client_create_noarg(d, empty_client);
  140. display_run(d);
  141. display_destroy(d);
  142. }
  143. /* This is how pre proxy-version registry binds worked,
  144. * this should create a proxy that shares the display's
  145. * version number: 0 */
  146. static void *
  147. old_registry_bind(struct wl_registry *wl_registry,
  148. uint32_t name,
  149. const struct wl_interface *interface,
  150. uint32_t version)
  151. {
  152. struct wl_proxy *id;
  153. id = wl_proxy_marshal_constructor(
  154. (struct wl_proxy *) wl_registry, WL_REGISTRY_BIND,
  155. interface, name, interface->name, version, NULL);
  156. return (void *) id;
  157. }
  158. struct handler_info {
  159. struct wl_seat *seat;
  160. uint32_t bind_version;
  161. bool use_unversioned;
  162. };
  163. static void
  164. registry_handle_globals(void *data, struct wl_registry *registry,
  165. uint32_t id, const char *intf, uint32_t ver)
  166. {
  167. struct handler_info *hi = data;
  168. /* This is only for the proxy version test */
  169. if (hi->bind_version)
  170. ver = hi->bind_version;
  171. if (strcmp(intf, "wl_seat") == 0) {
  172. if (hi->use_unversioned)
  173. hi->seat = old_registry_bind(registry, id,
  174. &wl_seat_interface, ver);
  175. else
  176. hi->seat = wl_registry_bind(registry, id,
  177. &wl_seat_interface, ver);
  178. assert(hi->seat);
  179. }
  180. }
  181. static const struct wl_registry_listener registry_listener = {
  182. registry_handle_globals,
  183. NULL
  184. };
  185. static struct wl_seat *
  186. client_get_seat_with_info(struct client *c, struct handler_info *hi)
  187. {
  188. struct wl_registry *reg = wl_display_get_registry(c->wl_display);
  189. assert(reg);
  190. assert(hi);
  191. hi->seat = NULL;
  192. wl_registry_add_listener(reg, &registry_listener, hi);
  193. wl_display_roundtrip(c->wl_display);
  194. assert(hi->seat);
  195. wl_registry_destroy(reg);
  196. return hi->seat;
  197. }
  198. static struct wl_seat *
  199. client_get_seat(struct client *c)
  200. {
  201. struct handler_info hi;
  202. hi.use_unversioned = false;
  203. hi.bind_version = 0;
  204. return client_get_seat_with_info(c, &hi);
  205. }
  206. static void
  207. check_pending_error(struct client *c, struct wl_proxy *proxy)
  208. {
  209. uint32_t ec, id;
  210. int err;
  211. const struct wl_interface *intf;
  212. err = wl_display_get_error(c->wl_display);
  213. assert(err == EPROTO);
  214. ec = wl_display_get_protocol_error(c->wl_display, &intf, &id);
  215. assert(ec == 23);
  216. assert(intf == &wl_seat_interface);
  217. assert(id == wl_proxy_get_id(proxy));
  218. }
  219. static void
  220. check_for_error(struct client *c, struct wl_proxy *proxy)
  221. {
  222. /* client should be disconnected */
  223. assert(wl_display_roundtrip(c->wl_display) == -1);
  224. check_pending_error(c, proxy);
  225. }
  226. static struct client_info *
  227. find_client_info(struct display *d, struct wl_client *client)
  228. {
  229. struct client_info *ci;
  230. wl_list_for_each(ci, &d->clients, link) {
  231. if (ci->wl_client == client)
  232. return ci;
  233. }
  234. return NULL;
  235. }
  236. static void
  237. bind_seat(struct wl_client *client, void *data,
  238. uint32_t vers, uint32_t id)
  239. {
  240. struct display *d = data;
  241. struct client_info *ci;
  242. struct wl_resource *res;
  243. ci = find_client_info(d, client);
  244. assert(ci);
  245. res = wl_resource_create(client, &wl_seat_interface, vers, id);
  246. assert(res);
  247. /* save the resource as client's info data,
  248. * so that we can use it later */
  249. ci->data = res;
  250. }
  251. static void
  252. client_disconnect_nocheck(struct client *c)
  253. {
  254. wl_proxy_destroy((struct wl_proxy *) c->tc);
  255. wl_display_disconnect(c->wl_display);
  256. free(c);
  257. }
  258. static void
  259. post_error_main(void)
  260. {
  261. struct client *c = client_connect();
  262. struct wl_seat *seat = client_get_seat(c);
  263. /* stop display so that it can post the error.
  264. * The function should return -1, because of the posted error */
  265. assert(stop_display(c, 1) == -1);
  266. /* display should have posted error, check it! */
  267. check_for_error(c, (struct wl_proxy *) seat);
  268. /* don't call client_disconnect(c), because then the test would be
  269. * aborted due to checks for error in this function */
  270. wl_proxy_destroy((struct wl_proxy *) seat);
  271. client_disconnect_nocheck(c);
  272. }
  273. TEST(post_error_to_one_client)
  274. {
  275. struct display *d = display_create();
  276. struct client_info *cl;
  277. wl_global_create(d->wl_display, &wl_seat_interface,
  278. 1, d, bind_seat);
  279. cl = client_create_noarg(d, post_error_main);
  280. display_run(d);
  281. /* the display was stopped by client, so it can
  282. * proceed in the code and post an error */
  283. assert(cl->data);
  284. wl_resource_post_error((struct wl_resource *) cl->data,
  285. 23, "Dummy error");
  286. /* this one should be ignored */
  287. wl_resource_post_error((struct wl_resource *) cl->data,
  288. 21, "Dummy error (ignore)");
  289. display_resume(d);
  290. display_destroy(d);
  291. }
  292. static void
  293. post_error_main2(void)
  294. {
  295. struct client *c = client_connect();
  296. struct wl_seat *seat = client_get_seat(c);
  297. /* the error should not be posted for this client */
  298. assert(stop_display(c, 2) >= 0);
  299. wl_proxy_destroy((struct wl_proxy *) seat);
  300. client_disconnect(c);
  301. }
  302. static void
  303. post_error_main3(void)
  304. {
  305. struct client *c = client_connect();
  306. struct wl_seat *seat = client_get_seat(c);
  307. assert(stop_display(c, 2) == -1);
  308. check_for_error(c, (struct wl_proxy *) seat);
  309. /* don't call client_disconnect(c), because then the test would be
  310. * aborted due to checks for error in this function */
  311. wl_proxy_destroy((struct wl_proxy *) seat);
  312. client_disconnect_nocheck(c);
  313. }
  314. /* all the testcases could be in one TEST, but splitting it
  315. * apart is better for debugging when the test fails */
  316. TEST(post_error_to_one_from_two_clients)
  317. {
  318. struct display *d = display_create();
  319. struct client_info *cl;
  320. wl_global_create(d->wl_display, &wl_seat_interface,
  321. 1, d, bind_seat);
  322. client_create_noarg(d, post_error_main2);
  323. cl = client_create_noarg(d, post_error_main3);
  324. display_run(d);
  325. /* post error only to the second client */
  326. assert(cl->data);
  327. wl_resource_post_error((struct wl_resource *) cl->data,
  328. 23, "Dummy error");
  329. wl_resource_post_error((struct wl_resource *) cl->data,
  330. 21, "Dummy error (ignore)");
  331. display_resume(d);
  332. display_destroy(d);
  333. }
  334. /* all the testcases could be in one TEST, but splitting it
  335. * apart is better for debugging when the test fails */
  336. TEST(post_error_to_two_clients)
  337. {
  338. struct display *d = display_create();
  339. struct client_info *cl, *cl2;
  340. wl_global_create(d->wl_display, &wl_seat_interface,
  341. 1, d, bind_seat);
  342. cl = client_create_noarg(d, post_error_main3);
  343. cl2 = client_create_noarg(d, post_error_main3);
  344. display_run(d);
  345. /* Try to send the error to both clients */
  346. assert(cl->data && cl2->data);
  347. wl_resource_post_error((struct wl_resource *) cl->data,
  348. 23, "Dummy error");
  349. wl_resource_post_error((struct wl_resource *) cl->data,
  350. 21, "Dummy error (ignore)");
  351. wl_resource_post_error((struct wl_resource *) cl2->data,
  352. 23, "Dummy error");
  353. wl_resource_post_error((struct wl_resource *) cl2->data,
  354. 21, "Dummy error (ignore)");
  355. display_resume(d);
  356. display_destroy(d);
  357. }
  358. static void
  359. post_nomem_main(void)
  360. {
  361. struct client *c = client_connect();
  362. struct wl_seat *seat = client_get_seat(c);
  363. assert(stop_display(c, 1) == -1);
  364. assert(wl_display_get_error(c->wl_display) == ENOMEM);
  365. wl_proxy_destroy((struct wl_proxy *) seat);
  366. client_disconnect_nocheck(c);
  367. }
  368. TEST(post_nomem_tst)
  369. {
  370. struct display *d = display_create();
  371. struct client_info *cl;
  372. wl_global_create(d->wl_display, &wl_seat_interface,
  373. 1, d, bind_seat);
  374. cl = client_create_noarg(d, post_nomem_main);
  375. display_run(d);
  376. assert(cl->data);
  377. wl_resource_post_no_memory((struct wl_resource *) cl->data);
  378. display_resume(d);
  379. /* first client terminated. Run it again,
  380. * but post no memory to client */
  381. cl = client_create_noarg(d, post_nomem_main);
  382. display_run(d);
  383. assert(cl->data);
  384. wl_client_post_no_memory(cl->wl_client);
  385. display_resume(d);
  386. display_destroy(d);
  387. }
  388. static void
  389. post_implementation_error_main(void)
  390. {
  391. struct client *c = client_connect();
  392. struct wl_seat *seat = client_get_seat(c);
  393. uint32_t object_id, protocol_error;
  394. const struct wl_interface *interface;
  395. assert(stop_display(c, 1) == -1);
  396. int err = wl_display_get_error(c->wl_display);
  397. fprintf(stderr, "Err is %i\n", err);
  398. assert(err == EPROTO);
  399. protocol_error = wl_display_get_protocol_error(c->wl_display,
  400. &interface,
  401. &object_id);
  402. assert(protocol_error == WL_DISPLAY_ERROR_IMPLEMENTATION);
  403. assert(interface == &wl_display_interface);
  404. wl_proxy_destroy((struct wl_proxy *) seat);
  405. client_disconnect_nocheck(c);
  406. }
  407. TEST(post_internal_error_tst)
  408. {
  409. struct display *d = display_create();
  410. struct client_info *cl;
  411. wl_global_create(d->wl_display, &wl_seat_interface,
  412. 1, d, bind_seat);
  413. cl = client_create_noarg(d, post_implementation_error_main);
  414. display_run(d);
  415. wl_client_post_implementation_error(cl->wl_client, "Error %i", 20);
  416. display_resume(d);
  417. display_destroy(d);
  418. }
  419. static void
  420. register_reading(struct wl_display *display)
  421. {
  422. while(wl_display_prepare_read(display) != 0 && errno == EAGAIN)
  423. assert(wl_display_dispatch_pending(display) >= 0);
  424. assert(wl_display_flush(display) >= 0);
  425. }
  426. /* create thread that will call prepare+read so that
  427. * it will block */
  428. static pthread_t
  429. create_thread(struct client *c, void *(*func)(void*))
  430. {
  431. pthread_t thread;
  432. c->display_stopped = 0;
  433. /* func must set display->stopped to 1 before sleeping */
  434. assert(pthread_create(&thread, NULL, func, c) == 0);
  435. /* make sure the thread is sleeping. It's a little bit racy
  436. * (setting display_stopped to 1 and calling wl_display_read_events)
  437. * so call usleep once again after the loop ends - it should
  438. * be sufficient... */
  439. while (c->display_stopped == 0)
  440. test_usleep(500);
  441. test_usleep(10000);
  442. return thread;
  443. }
  444. static void *
  445. thread_read_error(void *data)
  446. {
  447. struct client *c = data;
  448. register_reading(c->wl_display);
  449. /*
  450. * Calling the read right now will block this thread
  451. * until the other thread will read the data.
  452. * However, after invoking an error, this
  453. * thread should be woken up or it will block indefinitely.
  454. */
  455. c->display_stopped = 1;
  456. assert(wl_display_read_events(c->wl_display) == -1);
  457. assert(wl_display_dispatch_pending(c->wl_display) == -1);
  458. assert(wl_display_get_error(c->wl_display));
  459. pthread_exit(NULL);
  460. }
  461. /* test posting an error in multi-threaded environment. */
  462. static void
  463. threading_post_err(void)
  464. {
  465. DISABLE_LEAK_CHECKS;
  466. struct client *c = client_connect();
  467. pthread_t thread;
  468. /* register read intention */
  469. register_reading(c->wl_display);
  470. /* use this var as an indicator that thread is sleeping */
  471. c->display_stopped = 0;
  472. /* create new thread that will register its intention too */
  473. thread = create_thread(c, thread_read_error);
  474. /* so now we have sleeping thread waiting for a pthread_cond signal.
  475. * The main thread must call wl_display_read_events().
  476. * If this call fails, then it won't call broadcast at the
  477. * end of the function and the sleeping thread will block indefinitely.
  478. * Make the call fail and watch if libwayland will unblock the thread! */
  479. /* create error on fd, so that wl_display_read_events will fail.
  480. * The same can happen when server hangs up */
  481. close(wl_display_get_fd(c->wl_display));
  482. /* this read events will fail and will
  483. * post an error that should wake the sleeping thread
  484. * and dispatch the incoming events */
  485. assert(wl_display_read_events(c->wl_display) == -1);
  486. /* kill test in 3 seconds. This should be enough time for the
  487. * thread to exit if it's not blocking. If everything is OK, than
  488. * the thread was woken up and the test will end before the SIGALRM */
  489. test_set_timeout(3);
  490. pthread_join(thread, NULL);
  491. client_disconnect_nocheck(c);
  492. }
  493. TEST(threading_errors_tst)
  494. {
  495. struct display *d = display_create();
  496. client_create_noarg(d, threading_post_err);
  497. display_run(d);
  498. display_destroy(d);
  499. }
  500. static void *
  501. thread_prepare_and_read(void *data)
  502. {
  503. struct client *c = data;
  504. register_reading(c->wl_display);
  505. c->display_stopped = 1;
  506. assert(wl_display_read_events(c->wl_display) == 0);
  507. assert(wl_display_dispatch_pending(c->wl_display) == 0);
  508. pthread_exit(NULL);
  509. }
  510. /* test cancel read*/
  511. static void
  512. threading_cancel_read(void)
  513. {
  514. DISABLE_LEAK_CHECKS;
  515. struct client *c = client_connect();
  516. pthread_t th1, th2, th3;
  517. register_reading(c->wl_display);
  518. th1 = create_thread(c, thread_prepare_and_read);
  519. th2 = create_thread(c, thread_prepare_and_read);
  520. th3 = create_thread(c, thread_prepare_and_read);
  521. /* all the threads are sleeping, waiting until read or cancel
  522. * is called. Cancel the read and let the threads proceed */
  523. wl_display_cancel_read(c->wl_display);
  524. /* kill test in 3 seconds. This should be enough time for the
  525. * thread to exit if it's not blocking. If everything is OK, than
  526. * the thread was woken up and the test will end before the SIGALRM */
  527. test_set_timeout(3);
  528. pthread_join(th1, NULL);
  529. pthread_join(th2, NULL);
  530. pthread_join(th3, NULL);
  531. client_disconnect(c);
  532. }
  533. TEST(threading_cancel_read_tst)
  534. {
  535. struct display *d = display_create();
  536. client_create_noarg(d, threading_cancel_read);
  537. display_run(d);
  538. display_destroy(d);
  539. }
  540. static void
  541. threading_read_eagain(void)
  542. {
  543. DISABLE_LEAK_CHECKS;
  544. struct client *c = client_connect();
  545. pthread_t th1, th2, th3;
  546. register_reading(c->wl_display);
  547. th1 = create_thread(c, thread_prepare_and_read);
  548. th2 = create_thread(c, thread_prepare_and_read);
  549. th3 = create_thread(c, thread_prepare_and_read);
  550. /* All the threads are sleeping, waiting until read or cancel
  551. * is called. Since we have no data on socket waiting,
  552. * the wl_connection_read should end up with error and set errno
  553. * to EAGAIN. Check if the threads are woken up in this case. */
  554. assert(wl_display_read_events(c->wl_display) == 0);
  555. /* errno should be still set to EAGAIN if wl_connection_read
  556. * set it - check if we're testing the right case */
  557. assert(errno == EAGAIN);
  558. test_set_timeout(3);
  559. pthread_join(th1, NULL);
  560. pthread_join(th2, NULL);
  561. pthread_join(th3, NULL);
  562. client_disconnect(c);
  563. }
  564. TEST(threading_read_eagain_tst)
  565. {
  566. struct display *d = display_create();
  567. client_create_noarg(d, threading_read_eagain);
  568. display_run(d);
  569. display_destroy(d);
  570. }
  571. static void *
  572. thread_prepare_and_read2(void *data)
  573. {
  574. struct client *c = data;
  575. while(wl_display_prepare_read(c->wl_display) != 0 && errno == EAGAIN)
  576. assert(wl_display_dispatch_pending(c->wl_display) == -1);
  577. assert(wl_display_flush(c->wl_display) == -1);
  578. c->display_stopped = 1;
  579. assert(wl_display_read_events(c->wl_display) == -1);
  580. assert(wl_display_dispatch_pending(c->wl_display) == -1);
  581. pthread_exit(NULL);
  582. }
  583. static void
  584. threading_read_after_error(void)
  585. {
  586. DISABLE_LEAK_CHECKS;
  587. struct client *c = client_connect();
  588. pthread_t thread;
  589. /* create an error */
  590. close(wl_display_get_fd(c->wl_display));
  591. assert(wl_display_dispatch(c->wl_display) == -1);
  592. /* try to prepare for reading */
  593. while(wl_display_prepare_read(c->wl_display) != 0 && errno == EAGAIN)
  594. assert(wl_display_dispatch_pending(c->wl_display) == -1);
  595. assert(wl_display_flush(c->wl_display) == -1);
  596. assert(pthread_create(&thread, NULL,
  597. thread_prepare_and_read2, c) == 0);
  598. /* make sure thread is sleeping */
  599. while (c->display_stopped == 0)
  600. test_usleep(500);
  601. test_usleep(10000);
  602. assert(wl_display_read_events(c->wl_display) == -1);
  603. /* kill test in 3 seconds */
  604. test_set_timeout(3);
  605. pthread_join(thread, NULL);
  606. client_disconnect_nocheck(c);
  607. }
  608. TEST(threading_read_after_error_tst)
  609. {
  610. struct display *d = display_create();
  611. client_create_noarg(d, threading_read_after_error);
  612. display_run(d);
  613. display_destroy(d);
  614. }
  615. static void
  616. wait_for_error_using_dispatch(struct client *c, struct wl_proxy *proxy)
  617. {
  618. int ret;
  619. while (true) {
  620. /* Dispatching should eventually hit the protocol error before
  621. * any other error. */
  622. ret = wl_display_dispatch(c->wl_display);
  623. if (ret == 0) {
  624. continue;
  625. } else {
  626. assert(errno == EPROTO);
  627. break;
  628. }
  629. }
  630. check_pending_error(c, proxy);
  631. }
  632. static void
  633. wait_for_error_using_prepare_read(struct client *c, struct wl_proxy *proxy)
  634. {
  635. int ret = 0;
  636. struct pollfd pfd[2];
  637. while (true) {
  638. while (wl_display_prepare_read(c->wl_display) != 0 &&
  639. errno == EAGAIN) {
  640. assert(wl_display_dispatch_pending(c->wl_display) >= 0);
  641. }
  642. /* Flush may fail due to EPIPE if the connection is broken, but
  643. * this must not set a fatal display error because that would
  644. * result in it being impossible to read a potential protocol
  645. * error. */
  646. do {
  647. ret = wl_display_flush(c->wl_display);
  648. } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
  649. assert(ret >= 0 || errno == EPIPE);
  650. assert(wl_display_get_error(c->wl_display) == 0);
  651. pfd[0].fd = wl_display_get_fd(c->wl_display);
  652. pfd[0].events = POLLIN;
  653. do {
  654. ret = poll(pfd, 1, -1);
  655. } while (ret == -1 && errno == EINTR);
  656. assert(ret != -1);
  657. /* We should always manage to read the error before the EPIPE
  658. * comes this way. */
  659. assert(wl_display_read_events(c->wl_display) == 0);
  660. /* Dispatching should eventually hit the protocol error before
  661. * any other error. */
  662. ret = wl_display_dispatch_pending(c->wl_display);
  663. if (ret == 0) {
  664. continue;
  665. } else {
  666. assert(errno == EPROTO);
  667. break;
  668. }
  669. }
  670. check_pending_error(c, proxy);
  671. }
  672. static void
  673. check_error_after_epipe(void *data)
  674. {
  675. bool use_dispatch_helpers = *(bool *) data;
  676. struct client *client;
  677. struct wl_seat *seat;
  678. struct wl_callback *callback;
  679. client = client_connect();
  680. /* This will, according to the implementation below, cause the server
  681. * to post an error. */
  682. seat = client_get_seat(client);
  683. wl_display_flush(client->wl_display);
  684. /* The server will not actually destroy the client until it receives
  685. * input, so send something to trigger the client destruction. */
  686. callback = wl_display_sync(client->wl_display);
  687. wl_callback_destroy(callback);
  688. /* Sleep some to give the server a chance to react and destroy the
  689. * client. */
  690. test_usleep(200000);
  691. /* Wait for the protocol error and check that we reached it before
  692. * EPIPE. */
  693. if (use_dispatch_helpers) {
  694. wait_for_error_using_dispatch(client, (struct wl_proxy *) seat);
  695. } else {
  696. wait_for_error_using_prepare_read(client,
  697. (struct wl_proxy *) seat);
  698. }
  699. wl_seat_destroy(seat);
  700. client_disconnect_nocheck(client);
  701. }
  702. static void
  703. bind_seat_and_post_error(struct wl_client *client, void *data,
  704. uint32_t version, uint32_t id)
  705. {
  706. struct display *d = data;
  707. struct client_info *ci;
  708. struct wl_resource *resource;
  709. ci = find_client_info(d, client);
  710. assert(ci);
  711. resource = wl_resource_create(client, &wl_seat_interface, version, id);
  712. assert(resource);
  713. ci->data = resource;
  714. wl_resource_post_error(ci->data, 23, "Dummy error");
  715. }
  716. TEST(error_code_after_epipe)
  717. {
  718. struct display *d = display_create();
  719. bool use_dispatch_helpers;
  720. wl_global_create(d->wl_display, &wl_seat_interface,
  721. 1, d, bind_seat_and_post_error);
  722. use_dispatch_helpers = true;
  723. client_create(d, check_error_after_epipe, &use_dispatch_helpers);
  724. display_run(d);
  725. use_dispatch_helpers = false;
  726. client_create(d, check_error_after_epipe, &use_dispatch_helpers);
  727. display_run(d);
  728. display_destroy(d);
  729. }
  730. static void
  731. check_seat_versions(struct wl_seat *seat, uint32_t ev)
  732. {
  733. struct wl_pointer *pointer;
  734. assert(wl_proxy_get_version((struct wl_proxy *) seat) == ev);
  735. assert(wl_seat_get_version(seat) == ev);
  736. pointer = wl_seat_get_pointer(seat);
  737. assert(wl_pointer_get_version(pointer) == ev);
  738. assert(wl_proxy_get_version((struct wl_proxy *) pointer) == ev);
  739. wl_proxy_destroy((struct wl_proxy *) pointer);
  740. }
  741. /* Normal client with proxy versions available. */
  742. static void
  743. seat_version(void *data)
  744. {
  745. struct handler_info *hi = data;
  746. struct client *c = client_connect();
  747. struct wl_seat *seat;
  748. /* display proxy should always be version 0 */
  749. assert(wl_proxy_get_version((struct wl_proxy *) c->wl_display) == 0);
  750. seat = client_get_seat_with_info(c, hi);
  751. if (hi->use_unversioned)
  752. check_seat_versions(seat, 0);
  753. else
  754. check_seat_versions(seat, hi->bind_version);
  755. wl_proxy_destroy((struct wl_proxy *) seat);
  756. client_disconnect_nocheck(c);
  757. }
  758. TEST(versions)
  759. {
  760. struct display *d = display_create();
  761. struct wl_global *global;
  762. int i;
  763. global = wl_global_create(d->wl_display, &wl_seat_interface,
  764. 5, d, bind_seat);
  765. for (i = 1; i <= 5; i++) {
  766. struct handler_info hi;
  767. hi.bind_version = i;
  768. hi.use_unversioned = false;
  769. client_create(d, seat_version, &hi);
  770. hi.use_unversioned = true;
  771. client_create(d, seat_version, &hi);
  772. }
  773. display_run(d);
  774. wl_global_destroy(global);
  775. display_destroy(d);
  776. }
  777. static void
  778. check_error_on_destroyed_object(void)
  779. {
  780. struct client *c;
  781. struct wl_seat *seat;
  782. uint32_t id;
  783. const struct wl_interface *intf;
  784. c = client_connect();
  785. seat = client_get_seat(c);
  786. /* destroy the seat proxy. The display won't know
  787. * about it yet, so it will post the error as usual */
  788. wl_proxy_destroy((struct wl_proxy *) seat);
  789. /* let display post the error. The error will
  790. * be caught in stop_display while dispatching */
  791. assert(stop_display(c, 1) == -1);
  792. /* check the returned error. Since the object was destroyed,
  793. * we don't know the interface and id */
  794. assert(wl_display_get_error(c->wl_display) == EPROTO);
  795. assert(wl_display_get_protocol_error(c->wl_display, &intf, &id) == 23);
  796. assert(intf == NULL);
  797. assert(id == 0);
  798. client_disconnect_nocheck(c);
  799. }
  800. TEST(error_on_destroyed_object)
  801. {
  802. struct client_info *cl;
  803. struct display *d = display_create();
  804. wl_global_create(d->wl_display, &wl_seat_interface,
  805. 1, d, bind_seat);
  806. cl = client_create_noarg(d, check_error_on_destroyed_object);
  807. display_run(d);
  808. /* did client bind to the seat? */
  809. assert(cl->data);
  810. /* post error on the destroyed object */
  811. wl_resource_post_error((struct wl_resource *) cl->data,
  812. 23, "Dummy error");
  813. display_resume(d);
  814. display_destroy(d);
  815. }
  816. static bool
  817. global_filter(const struct wl_client *client,
  818. const struct wl_global *global,
  819. void *data)
  820. {
  821. /* Hide the wl_data_offer interface if no data was provided */
  822. if (wl_global_get_interface(global) == &wl_data_offer_interface)
  823. return data != NULL;
  824. /* Show all the others */
  825. return true;
  826. }
  827. static void
  828. bind_data_offer(struct wl_client *client, void *data,
  829. uint32_t vers, uint32_t id)
  830. {
  831. /* Client should not be able to bind to this interface! */
  832. assert(false);
  833. }
  834. static void
  835. registry_handle_filtered(void *data, struct wl_registry *registry,
  836. uint32_t id, const char *intf, uint32_t ver)
  837. {
  838. uint32_t *name = data;
  839. if (strcmp (intf, "wl_data_offer") == 0) {
  840. assert(name);
  841. *name = id;
  842. }
  843. }
  844. static void
  845. registry_handle_remove_filtered(void *data, struct wl_registry *registry,
  846. uint32_t id)
  847. {
  848. assert(false);
  849. }
  850. static const struct wl_registry_listener registry_listener_filtered = {
  851. registry_handle_filtered,
  852. registry_handle_remove_filtered,
  853. };
  854. static void
  855. get_globals(void *data)
  856. {
  857. struct client *c = client_connect();
  858. struct wl_registry *registry;
  859. registry = wl_display_get_registry(c->wl_display);
  860. wl_registry_add_listener(registry, &registry_listener_filtered, data);
  861. wl_display_roundtrip(c->wl_display);
  862. wl_registry_destroy(registry);
  863. client_disconnect_nocheck(c);
  864. }
  865. TEST(filtered_global_is_hidden)
  866. {
  867. struct display *d;
  868. struct wl_global *g;
  869. d = display_create();
  870. g = wl_global_create(d->wl_display, &wl_data_offer_interface,
  871. 1, d, bind_data_offer);
  872. wl_display_set_global_filter(d->wl_display, global_filter, NULL);
  873. client_create(d, get_globals, NULL);
  874. display_run(d);
  875. wl_global_destroy(g);
  876. display_destroy(d);
  877. }
  878. static void
  879. get_dynamic_globals(void)
  880. {
  881. struct client *c = client_connect();
  882. struct wl_registry *registry;
  883. registry = wl_display_get_registry(c->wl_display);
  884. wl_registry_add_listener(registry, &registry_listener_filtered, NULL);
  885. wl_display_roundtrip(c->wl_display);
  886. /* Wait for the server to create a new global */
  887. assert(stop_display(c, 1) >= 0);
  888. /* Check that we don't see it */
  889. wl_display_roundtrip(c->wl_display);
  890. /* Wait for the server to remove that global */
  891. assert(stop_display(c, 1) >= 0);
  892. /* Check that we don't get a global_remove event */
  893. wl_display_roundtrip(c->wl_display);
  894. wl_registry_destroy(registry);
  895. client_disconnect_nocheck(c);
  896. }
  897. TEST(filtered_dynamic_global_is_hidden)
  898. {
  899. struct display *d;
  900. struct wl_global *g;
  901. d = display_create();
  902. wl_display_set_global_filter(d->wl_display, global_filter, NULL);
  903. /* Create a client and let it enumerate the globals */
  904. client_create_noarg(d, get_dynamic_globals);
  905. display_run(d);
  906. /* Dynamically create a new global */
  907. g = wl_global_create(d->wl_display, &wl_data_offer_interface,
  908. 1, d, bind_data_offer);
  909. display_resume(d);
  910. /* Dynamically remove the global */
  911. wl_global_destroy(g);
  912. display_resume(d);
  913. display_destroy(d);
  914. }
  915. static void
  916. check_bind_error(struct client *c)
  917. {
  918. uint32_t errorcode, id;
  919. int err;
  920. const struct wl_interface *intf;
  921. err = wl_display_get_error(c->wl_display);
  922. assert(err == EPROTO);
  923. errorcode = wl_display_get_protocol_error(c->wl_display, &intf, &id);
  924. assert(errorcode == WL_DISPLAY_ERROR_INVALID_OBJECT);
  925. }
  926. static void
  927. force_bind(void *data)
  928. {
  929. struct client *c = client_connect();
  930. struct wl_registry *registry;
  931. void *ptr;
  932. uint32_t *name = data;
  933. registry = wl_display_get_registry(c->wl_display);
  934. ptr = wl_registry_bind (registry, *name, &wl_data_offer_interface, 1);
  935. wl_display_roundtrip(c->wl_display);
  936. check_bind_error(c);
  937. wl_proxy_destroy((struct wl_proxy *) ptr);
  938. wl_registry_destroy(registry);
  939. client_disconnect_nocheck(c);
  940. }
  941. TEST(bind_fails_on_filtered_global)
  942. {
  943. struct display *d;
  944. struct wl_global *g;
  945. uint32_t *name;
  946. /* Create a anonymous shared memory to pass the interface name */
  947. name = mmap(NULL, sizeof(uint32_t),
  948. PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  949. d = display_create();
  950. g = wl_global_create(d->wl_display, &wl_data_offer_interface,
  951. 1, d, bind_data_offer);
  952. wl_display_set_global_filter(d->wl_display, global_filter, name);
  953. client_create(d, get_globals, name);
  954. *name = 0;
  955. display_run(d);
  956. /* wl_data_offer should be 2 */
  957. assert(*name == 2);
  958. wl_display_set_global_filter(d->wl_display, global_filter, NULL);
  959. /* Try to bind to the interface name when a global filter is in place */
  960. client_create(d, force_bind, name);
  961. display_run(d);
  962. wl_global_destroy(g);
  963. display_destroy(d);
  964. }
  965. static void
  966. pre_fd(void *data, struct fd_passer *fdp)
  967. {
  968. fd_passer_destroy(fdp);
  969. }
  970. static void
  971. fd(void *data, struct fd_passer *fdp, int32_t fd)
  972. {
  973. /* We destroyed the resource before this event */
  974. assert(false);
  975. }
  976. struct fd_passer_listener fd_passer_listener = {
  977. pre_fd,
  978. fd,
  979. };
  980. static void
  981. zombie_fd_handle_globals(void *data, struct wl_registry *registry,
  982. uint32_t id, const char *intf, uint32_t ver)
  983. {
  984. struct fd_passer *fdp;
  985. if (!strcmp(intf, "fd_passer")) {
  986. fdp = wl_registry_bind(registry, id, &fd_passer_interface, 1);
  987. fd_passer_add_listener(fdp, &fd_passer_listener, NULL);
  988. }
  989. }
  990. static const struct wl_registry_listener zombie_fd_registry_listener = {
  991. zombie_fd_handle_globals,
  992. NULL
  993. };
  994. static void
  995. zombie_client(void)
  996. {
  997. struct client *c = client_connect();
  998. struct wl_registry *registry;
  999. registry = wl_display_get_registry(c->wl_display);
  1000. wl_registry_add_listener(registry, &zombie_fd_registry_listener, NULL);
  1001. /* Gets the registry */
  1002. wl_display_roundtrip(c->wl_display);
  1003. /* push out the fd_passer bind */
  1004. wl_display_roundtrip(c->wl_display);
  1005. /* push out our fd_passer.destroy */
  1006. wl_display_roundtrip(c->wl_display);
  1007. wl_registry_destroy(registry);
  1008. client_disconnect_nocheck(c);
  1009. }
  1010. struct passer_data {
  1011. struct wl_resource *conjoined_passer;
  1012. };
  1013. static void
  1014. feed_pipe(int fd, char tosend)
  1015. {
  1016. int count;
  1017. do {
  1018. count = write(fd, &tosend, 1);
  1019. } while (count != 1 && errno == EAGAIN);
  1020. assert(count == 1);
  1021. close(fd);
  1022. }
  1023. static void
  1024. fd_passer_clobber(struct wl_client *client, struct wl_resource *res)
  1025. {
  1026. struct passer_data *pdata = wl_resource_get_user_data(res);
  1027. int pipes1[2], pipes2[2], ret;
  1028. if (pdata->conjoined_passer) {
  1029. ret = pipe(pipes1);
  1030. assert(ret == 0);
  1031. ret = pipe(pipes2);
  1032. assert(ret == 0);
  1033. wl_resource_queue_event(res, FD_PASSER_FD, pipes1[0]);
  1034. fd_passer_send_fd(pdata->conjoined_passer, pipes2[0]);
  1035. feed_pipe(pipes1[1], '1');
  1036. feed_pipe(pipes2[1], '2');
  1037. close(pipes1[0]);
  1038. close(pipes2[0]);
  1039. }
  1040. wl_resource_destroy(res);
  1041. }
  1042. static void
  1043. fd_passer_twin(struct wl_client *client, struct wl_resource *res, struct wl_resource *passer)
  1044. {
  1045. struct passer_data *pdata = wl_resource_get_user_data(res);
  1046. pdata->conjoined_passer = passer;
  1047. }
  1048. static const struct fd_passer_interface fdp_interface = {
  1049. fd_passer_clobber,
  1050. fd_passer_twin
  1051. };
  1052. static void
  1053. pdata_destroy(struct wl_resource *res)
  1054. {
  1055. struct passer_data *pdata = wl_resource_get_user_data(res);
  1056. free(pdata);
  1057. }
  1058. static void
  1059. bind_fd_passer(struct wl_client *client, void *data,
  1060. uint32_t vers, uint32_t id)
  1061. {
  1062. struct wl_resource *res;
  1063. struct passer_data *pdata;
  1064. pdata = malloc(sizeof(*pdata));
  1065. assert(pdata);
  1066. pdata->conjoined_passer = NULL;
  1067. res = wl_resource_create(client, &fd_passer_interface, vers, id);
  1068. wl_resource_set_implementation(res, &fdp_interface, pdata, pdata_destroy);
  1069. assert(res);
  1070. if (vers == 1) {
  1071. fd_passer_send_pre_fd(res);
  1072. fd_passer_send_fd(res, fileno(stdin));
  1073. }
  1074. }
  1075. TEST(zombie_fd)
  1076. {
  1077. struct display *d;
  1078. struct wl_global *g;
  1079. d = display_create();
  1080. g = wl_global_create(d->wl_display, &fd_passer_interface,
  1081. 1, d, bind_fd_passer);
  1082. client_create_noarg(d, zombie_client);
  1083. display_run(d);
  1084. wl_global_destroy(g);
  1085. display_destroy(d);
  1086. }
  1087. static void
  1088. double_pre_fd(void *data, struct fd_passer *fdp)
  1089. {
  1090. assert(false);
  1091. }
  1092. static void
  1093. double_fd(void *data, struct fd_passer *fdp, int32_t fd)
  1094. {
  1095. char buf;
  1096. int count;
  1097. do {
  1098. count = read(fd, &buf, 1);
  1099. } while (count != 1 && errno == EAGAIN);
  1100. assert(count == 1);
  1101. close(fd);
  1102. fd_passer_destroy(fdp);
  1103. assert(buf == '2');
  1104. }
  1105. struct fd_passer_listener double_fd_passer_listener = {
  1106. double_pre_fd,
  1107. double_fd,
  1108. };
  1109. static void
  1110. double_zombie_fd_handle_globals(void *data, struct wl_registry *registry,
  1111. uint32_t id, const char *intf, uint32_t ver)
  1112. {
  1113. struct fd_passer *fdp1, *fdp2;
  1114. if (!strcmp(intf, "fd_passer")) {
  1115. fdp1 = wl_registry_bind(registry, id, &fd_passer_interface, 2);
  1116. fd_passer_add_listener(fdp1, &double_fd_passer_listener, NULL);
  1117. fdp2 = wl_registry_bind(registry, id, &fd_passer_interface, 2);
  1118. fd_passer_add_listener(fdp2, &double_fd_passer_listener, NULL);
  1119. fd_passer_conjoin(fdp1, fdp2);
  1120. fd_passer_destroy(fdp1);
  1121. }
  1122. }
  1123. static const struct wl_registry_listener double_zombie_fd_registry_listener = {
  1124. double_zombie_fd_handle_globals,
  1125. NULL
  1126. };
  1127. static void
  1128. double_zombie_client(void)
  1129. {
  1130. struct client *c = client_connect();
  1131. struct wl_registry *registry;
  1132. registry = wl_display_get_registry(c->wl_display);
  1133. wl_registry_add_listener(registry, &double_zombie_fd_registry_listener, NULL);
  1134. /* Gets the registry */
  1135. wl_display_roundtrip(c->wl_display);
  1136. /* One more so server can respond to conjoin+destroy */
  1137. wl_display_roundtrip(c->wl_display);
  1138. /* And finally push out our last fd_passer.destroy */
  1139. wl_display_roundtrip(c->wl_display);
  1140. wl_registry_destroy(registry);
  1141. client_disconnect_nocheck(c);
  1142. }
  1143. TEST(zombie_fd_errant_consumption)
  1144. {
  1145. struct display *d;
  1146. struct wl_global *g;
  1147. d = display_create();
  1148. g = wl_global_create(d->wl_display, &fd_passer_interface,
  1149. 2, d, bind_fd_passer);
  1150. client_create_noarg(d, double_zombie_client);
  1151. display_run(d);
  1152. wl_global_destroy(g);
  1153. display_destroy(d);
  1154. }
  1155. static void
  1156. registry_bind_interface_mismatch_handle_global(void *data,
  1157. struct wl_registry *registry,
  1158. uint32_t id, const char *intf,
  1159. uint32_t ver)
  1160. {
  1161. uint32_t *seat_id_ptr = data;
  1162. if (strcmp(intf, wl_seat_interface.name) == 0) {
  1163. *seat_id_ptr = id;
  1164. }
  1165. }
  1166. static const struct wl_registry_listener bind_interface_mismatch_registry_listener = {
  1167. registry_bind_interface_mismatch_handle_global,
  1168. NULL
  1169. };
  1170. static void
  1171. registry_bind_interface_mismatch_client(void)
  1172. {
  1173. struct client *c = client_connect();
  1174. struct wl_registry *registry;
  1175. uint32_t seat_id = 0;
  1176. void *ptr;
  1177. int ret;
  1178. registry = wl_display_get_registry(c->wl_display);
  1179. wl_registry_add_listener(registry,
  1180. &bind_interface_mismatch_registry_listener,
  1181. &seat_id);
  1182. ret = wl_display_roundtrip(c->wl_display);
  1183. assert(ret >= 0);
  1184. assert(seat_id != 0);
  1185. /* Bind with a different interface */
  1186. ptr = wl_registry_bind(registry, seat_id, &wl_output_interface, 1);
  1187. ret = wl_display_roundtrip(c->wl_display);
  1188. assert(ret < 0);
  1189. check_bind_error(c);
  1190. wl_proxy_destroy((struct wl_proxy *) ptr);
  1191. wl_registry_destroy(registry);
  1192. client_disconnect_nocheck(c);
  1193. }
  1194. TEST(registry_bind_interface_mismatch)
  1195. {
  1196. struct display *d;
  1197. struct wl_global *seat_global;
  1198. d = display_create();
  1199. seat_global = wl_global_create(d->wl_display, &wl_seat_interface,
  1200. 1, NULL, NULL);
  1201. client_create_noarg(d, registry_bind_interface_mismatch_client);
  1202. display_run(d);
  1203. wl_global_destroy(seat_global);
  1204. display_destroy(d);
  1205. }
  1206. static void
  1207. send_overflow_client(void *data)
  1208. {
  1209. struct client *c = client_connect();
  1210. int i, err = 0;
  1211. int *pipes = data;
  1212. char tmp = '\0';
  1213. int sock, optval = 16384;
  1214. /* By default, client buffers are now unbounded, set a limit to cause
  1215. * an overflow, otherwise the client buffers will grow indefinitely. */
  1216. wl_display_set_max_buffer_size(c->wl_display, 4096);
  1217. /* Limit the send buffer size for the display socket to guarantee
  1218. * that the test will cause an overflow. */
  1219. sock = wl_display_get_fd(c->wl_display);
  1220. assert(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof(optval)) == 0);
  1221. /* Request to break out of 'display_run' in the main process */
  1222. assert(stop_display(c, 1) >= 0);
  1223. /* On Linux, the actual socket data + metadata space is twice `optval`;
  1224. * since each noop request requires 8 bytes, the buffer should overflow
  1225. * within <=4096 iterations. */
  1226. for (i = 0; i < 1000000; i++) {
  1227. noop_request(c);
  1228. fprintf(stderr, "Send loop %i\n", i);
  1229. err = wl_display_get_error(c->wl_display);
  1230. if (err)
  1231. break;
  1232. }
  1233. /* Do not close the pipe file descriptors afterwards, because the leak
  1234. * check verifies that the initial/final FD counts are the same */
  1235. assert(write(pipes[1], &tmp, sizeof(tmp)) == (ssize_t)sizeof(tmp));
  1236. /* Expect an error - ring_buffer_ensure_space() returns E2BIG */
  1237. fprintf(stderr, "Send loop failed on try %d, err = %d, %s\n", i, err, strerror(err));
  1238. assert(err == EAGAIN || err == E2BIG);
  1239. client_disconnect_nocheck(c);
  1240. }
  1241. TEST(send_overflow_disconnection)
  1242. {
  1243. struct display *d;
  1244. char tmp;
  1245. int rpipe[2];
  1246. ssize_t ret;
  1247. assert(pipe(rpipe) != -1);
  1248. d = display_create();
  1249. (void) client_create(d, send_overflow_client, &rpipe);
  1250. /* Close write end of the pipe, so that the later read() call gets
  1251. * interrupted if the client dies */
  1252. close(rpipe[1]);
  1253. /* Run the display until the client sends a `stop_display`, then
  1254. * send a resume message but don't actually look at new messages */
  1255. display_run(d);
  1256. display_post_resume_events(d);
  1257. wl_display_flush_clients(d->wl_display);
  1258. /* Wait until all noop requests have been sent (read returns 1), or
  1259. * until client process aborts (read returns 0) */
  1260. do {
  1261. ret = read(rpipe[0], &tmp, sizeof(tmp));
  1262. } while (ret == -1 && errno == EINTR);
  1263. assert(ret != -1);
  1264. close(rpipe[0]);
  1265. /* For a clean shutdown */
  1266. display_run(d);
  1267. display_destroy(d);
  1268. }
  1269. static void
  1270. registry_global_remove_before_handle_global(void *data,
  1271. struct wl_registry *registry,
  1272. uint32_t id, const char *intf,
  1273. uint32_t ver)
  1274. {
  1275. uint32_t *id_ptr = data;
  1276. if (strcmp(intf, wl_seat_interface.name) == 0) {
  1277. assert(*id_ptr == 0);
  1278. *id_ptr = id;
  1279. }
  1280. }
  1281. static void
  1282. registry_global_remove_before_handle_global_remove(void *data,
  1283. struct wl_registry *registry,
  1284. uint32_t id)
  1285. {
  1286. uint32_t *id_ptr = data;
  1287. if (*id_ptr == id) {
  1288. *id_ptr = 0;
  1289. }
  1290. }
  1291. /* This listener expects a uint32_t user data pointer, sets it to the wl_seat
  1292. * global ID when receiving a "global" event, and sets it to zero when receiving
  1293. * a "global_remove" event. */
  1294. static const struct wl_registry_listener global_remove_before_registry_listener = {
  1295. registry_global_remove_before_handle_global,
  1296. registry_global_remove_before_handle_global_remove,
  1297. };
  1298. static void
  1299. global_remove_before_client(void)
  1300. {
  1301. struct client *c = client_connect();
  1302. struct wl_registry *registry;
  1303. uint32_t global_id = 0, saved_global_id;
  1304. struct wl_seat *seat;
  1305. int ret;
  1306. registry = wl_display_get_registry(c->wl_display);
  1307. wl_registry_add_listener(registry,
  1308. &global_remove_before_registry_listener,
  1309. &global_id);
  1310. ret = wl_display_roundtrip(c->wl_display);
  1311. assert(ret >= 0);
  1312. assert(global_id != 0);
  1313. saved_global_id = global_id;
  1314. /* Wait for the compositor to remove the global */
  1315. assert(stop_display(c, 1) >= 0);
  1316. /* Check binding still works after the global has been removed. Also
  1317. * check we get the global_remove event. */
  1318. seat = wl_registry_bind(registry, saved_global_id, &wl_seat_interface, 1);
  1319. ret = wl_display_roundtrip(c->wl_display);
  1320. assert(ret >= 0);
  1321. assert(global_id == 0);
  1322. wl_seat_destroy(seat);
  1323. wl_registry_destroy(registry);
  1324. client_disconnect(c);
  1325. }
  1326. static void
  1327. registry_global_remove_after_handle_global(void *data,
  1328. struct wl_registry *registry,
  1329. uint32_t id, const char *intf,
  1330. uint32_t ver)
  1331. {
  1332. /* Make sure the global isn't advertised anymore after being removed */
  1333. assert(strcmp(intf, wl_seat_interface.name) != 0);
  1334. }
  1335. static const struct wl_registry_listener global_remove_after_registry_listener = {
  1336. registry_global_remove_after_handle_global,
  1337. NULL,
  1338. };
  1339. static void
  1340. global_remove_after_client(void)
  1341. {
  1342. struct client *c = client_connect();
  1343. struct wl_registry *registry;
  1344. uint32_t global_id = 0;
  1345. int ret;
  1346. registry = wl_display_get_registry(c->wl_display);
  1347. wl_registry_add_listener(registry,
  1348. &global_remove_after_registry_listener,
  1349. &global_id);
  1350. ret = wl_display_roundtrip(c->wl_display);
  1351. assert(ret >= 0);
  1352. wl_registry_destroy(registry);
  1353. client_disconnect(c);
  1354. }
  1355. TEST(global_remove)
  1356. {
  1357. struct display *d;
  1358. struct wl_global *global;
  1359. d = display_create();
  1360. global = wl_global_create(d->wl_display, &wl_seat_interface,
  1361. 1, d, bind_seat);
  1362. /* Create a client before removing the global */
  1363. client_create_noarg(d, global_remove_before_client);
  1364. display_run(d);
  1365. wl_global_remove(global);
  1366. /* Create another client after removing the global */
  1367. client_create_noarg(d, global_remove_after_client);
  1368. display_resume(d);
  1369. wl_global_destroy(global);
  1370. display_destroy(d);
  1371. }
  1372. static void
  1373. registry_ack_global_remove_handle_global_remove(void *data,
  1374. struct wl_registry *registry,
  1375. uint32_t id)
  1376. {
  1377. struct client *client = data;
  1378. wl_fixes_ack_global_remove(client->wl_fixes, registry, id);
  1379. }
  1380. static struct wl_registry_listener registry_ack_global_remove_listener = {
  1381. .global = registry_handle_global_noop,
  1382. .global_remove = registry_ack_global_remove_handle_global_remove,
  1383. };
  1384. static void
  1385. ack_global_remove_client(void *data)
  1386. {
  1387. struct client *c = client_connect();
  1388. struct wl_registry *registry;
  1389. struct wl_registry_listener *registry_listener = data;
  1390. int ret;
  1391. registry = wl_display_get_registry(c->wl_display);
  1392. wl_registry_add_listener(registry,
  1393. registry_listener,
  1394. c);
  1395. ret = wl_display_roundtrip(c->wl_display);
  1396. assert(ret >= 0);
  1397. /* yield the control back to the compositor so it can remove
  1398. * the global */
  1399. assert(stop_display(c, 1) >= 0);
  1400. /* check if there are any global_remove events */
  1401. ret = wl_display_roundtrip(c->wl_display);
  1402. assert(ret >= 0);
  1403. /* yield the control back to the compositor so it can check
  1404. * whether the global is withdrawn */
  1405. assert(stop_display(c, 1) >= 0);
  1406. wl_registry_destroy(registry);
  1407. client_disconnect(c);
  1408. }
  1409. static void
  1410. mark_global_withdrawn(struct wl_global *global)
  1411. {
  1412. bool *withdrawn = wl_global_get_user_data(global);
  1413. *withdrawn = true;
  1414. }
  1415. TEST(ack_global_remove)
  1416. {
  1417. struct display *d;
  1418. struct wl_global *seat;
  1419. struct wl_global *fixes;
  1420. bool withdrawn = false;
  1421. d = display_create();
  1422. fixes = wl_global_create(d->wl_display, &wl_fixes_interface,
  1423. 2, NULL, bind_fixes);
  1424. seat = wl_global_create(d->wl_display, &wl_seat_interface,
  1425. 1, &withdrawn, NULL);
  1426. wl_global_set_withdrawn_listener(seat, mark_global_withdrawn);
  1427. client_create(d, ack_global_remove_client,
  1428. &registry_ack_global_remove_listener);
  1429. display_run(d);
  1430. assert(!withdrawn);
  1431. wl_global_remove(seat);
  1432. /* the global will be marked as withdrawn after the client
  1433. * acknowledges the wl_registry.global_remove event */
  1434. display_resume(d);
  1435. assert(withdrawn);
  1436. /* the global will be still marked as withdrawn even after
  1437. * the client disconnects */
  1438. display_resume(d);
  1439. assert(withdrawn);
  1440. wl_global_destroy(seat);
  1441. wl_global_destroy(fixes);
  1442. display_destroy(d);
  1443. }
  1444. static struct wl_registry_listener registry_no_ack_global_remove_listener = {
  1445. .global = registry_handle_global_noop,
  1446. .global_remove = registry_handle_global_remove_noop,
  1447. };
  1448. TEST(no_ack_global_remove)
  1449. {
  1450. struct display *d;
  1451. struct wl_global *seat;
  1452. struct wl_global *fixes;
  1453. bool withdrawn = false;
  1454. d = display_create();
  1455. fixes = wl_global_create(d->wl_display, &wl_fixes_interface,
  1456. 2, NULL, bind_fixes);
  1457. seat = wl_global_create(d->wl_display, &wl_seat_interface,
  1458. 1, &withdrawn, NULL);
  1459. wl_global_set_withdrawn_listener(seat, mark_global_withdrawn);
  1460. client_create(d, ack_global_remove_client,
  1461. &registry_no_ack_global_remove_listener);
  1462. display_run(d);
  1463. assert(!withdrawn);
  1464. wl_global_remove(seat);
  1465. /* if the client does not ack the wl_registry.global_remove event,
  1466. * the global will not be marked as withdrawn */
  1467. display_resume(d);
  1468. assert(!withdrawn);
  1469. /* when the client disconnects, the wl_registry.global_remove will
  1470. * be implicitly acknowledged and the global can be destroyed then */
  1471. display_resume(d);
  1472. assert(withdrawn);
  1473. wl_global_destroy(seat);
  1474. wl_global_destroy(fixes);
  1475. display_destroy(d);
  1476. }
  1477. TEST(global_remove_without_clients)
  1478. {
  1479. struct display *d;
  1480. struct wl_global *global;
  1481. bool withdrawn = false;
  1482. d = display_create();
  1483. global = wl_global_create(d->wl_display, &wl_seat_interface,
  1484. 1, &withdrawn, NULL);
  1485. wl_global_set_withdrawn_listener(global, mark_global_withdrawn);
  1486. /* if there are no clients at all, the global can be destroyed
  1487. * immediately */
  1488. wl_global_remove(global);
  1489. assert(withdrawn);
  1490. wl_global_destroy(global);
  1491. display_destroy(d);
  1492. }
  1493. static void
  1494. dispatch_single_read_events(struct wl_display *d)
  1495. {
  1496. if (wl_display_prepare_read(d) < 0) {
  1497. return;
  1498. }
  1499. int ret = 0;
  1500. do {
  1501. ret = wl_display_flush(d);
  1502. } while (ret < 0 && (errno == EINTR || errno == EAGAIN));
  1503. assert(ret >= 0);
  1504. struct pollfd pfd[1];
  1505. pfd[0].fd = wl_display_get_fd(d);
  1506. pfd[0].events = POLLIN;
  1507. do {
  1508. ret = poll(pfd, 1, -1);
  1509. } while (ret < 0 && errno == EINTR);
  1510. assert(ret > 0);
  1511. wl_display_read_events(d);
  1512. }
  1513. static void
  1514. dispatch_single_client(void)
  1515. {
  1516. struct client *c = client_connect();
  1517. assert(wl_display_dispatch_pending_single(c->wl_display) == 0);
  1518. struct wl_registry *registry = wl_display_get_registry(c->wl_display);
  1519. dispatch_single_read_events(c->wl_display);
  1520. // [1815110.061] {Default Queue} wl_registry#3.global(1, "test", 1)
  1521. assert(wl_display_dispatch_pending_single(c->wl_display) == 1);
  1522. dispatch_single_read_events(c->wl_display);
  1523. // [1815110.067] {Default Queue} wl_registry#3.global(2, "wl_seat", 1)
  1524. assert(wl_display_dispatch_pending_single(c->wl_display) == 1);
  1525. // No more events
  1526. assert(wl_display_dispatch_pending_single(c->wl_display) == 0);
  1527. wl_registry_destroy(registry);
  1528. client_disconnect(c);
  1529. }
  1530. TEST(dispatch_single)
  1531. {
  1532. struct display *d = display_create();
  1533. struct wl_global *global = wl_global_create(d->wl_display,
  1534. &wl_seat_interface,
  1535. 1, d, bind_seat);
  1536. client_create_noarg(d, dispatch_single_client);
  1537. display_run(d);
  1538. wl_global_destroy(global);
  1539. display_destroy(d);
  1540. }
  1541. static void
  1542. terminate_display(void *arg)
  1543. {
  1544. struct wl_display *wl_display = arg;
  1545. wl_display_terminate(wl_display);
  1546. }
  1547. TEST(no_source_terminate)
  1548. {
  1549. struct display *d;
  1550. struct wl_event_loop *loop;
  1551. d = display_create();
  1552. loop = wl_display_get_event_loop(d->wl_display);
  1553. wl_event_loop_add_idle(loop, terminate_display, d->wl_display);
  1554. display_run(d);
  1555. display_destroy(d);
  1556. }