xref: /aosp_15_r20/external/wayland/tests/socket-test.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique  * Permission is hereby granted, free of charge, to any person obtaining
3*84e872a0SLloyd Pique  * a copy of this software and associated documentation files (the
4*84e872a0SLloyd Pique  * "Software"), to deal in the Software without restriction, including
5*84e872a0SLloyd Pique  * without limitation the rights to use, copy, modify, merge, publish,
6*84e872a0SLloyd Pique  * distribute, sublicense, and/or sell copies of the Software, and to
7*84e872a0SLloyd Pique  * permit persons to whom the Software is furnished to do so, subject to
8*84e872a0SLloyd Pique  * the following conditions:
9*84e872a0SLloyd Pique  *
10*84e872a0SLloyd Pique  * The above copyright notice and this permission notice (including the
11*84e872a0SLloyd Pique  * next paragraph) shall be included in all copies or substantial
12*84e872a0SLloyd Pique  * portions of the Software.
13*84e872a0SLloyd Pique  *
14*84e872a0SLloyd Pique  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15*84e872a0SLloyd Pique  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16*84e872a0SLloyd Pique  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17*84e872a0SLloyd Pique  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18*84e872a0SLloyd Pique  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19*84e872a0SLloyd Pique  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20*84e872a0SLloyd Pique  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21*84e872a0SLloyd Pique  * SOFTWARE.
22*84e872a0SLloyd Pique  */
23*84e872a0SLloyd Pique 
24*84e872a0SLloyd Pique #include <stdlib.h>
25*84e872a0SLloyd Pique #include <assert.h>
26*84e872a0SLloyd Pique #include <errno.h>
27*84e872a0SLloyd Pique #include <string.h>
28*84e872a0SLloyd Pique #include <stdio.h>
29*84e872a0SLloyd Pique #include <sys/socket.h>
30*84e872a0SLloyd Pique #include <sys/types.h>
31*84e872a0SLloyd Pique #include <sys/wait.h>
32*84e872a0SLloyd Pique #include <sys/un.h>
33*84e872a0SLloyd Pique #include <unistd.h>
34*84e872a0SLloyd Pique 
35*84e872a0SLloyd Pique #include "wayland-client.h"
36*84e872a0SLloyd Pique #include "wayland-os.h"
37*84e872a0SLloyd Pique #include "wayland-server.h"
38*84e872a0SLloyd Pique #include "test-runner.h"
39*84e872a0SLloyd Pique 
40*84e872a0SLloyd Pique /* Paths longer than what the .sun_path array can contain must be rejected.
41*84e872a0SLloyd Pique  * This is a hard limitation of assigning a name to AF_UNIX/AF_LOCAL sockets.
42*84e872a0SLloyd Pique  * See `man 7 unix`.
43*84e872a0SLloyd Pique  */
44*84e872a0SLloyd Pique 
45*84e872a0SLloyd Pique static struct sockaddr_un example_sockaddr_un;
46*84e872a0SLloyd Pique 
47*84e872a0SLloyd Pique #define TOO_LONG (1 + sizeof example_sockaddr_un.sun_path)
48*84e872a0SLloyd Pique 
49*84e872a0SLloyd Pique /* Ensure the connection doesn't fail due to lack of XDG_RUNTIME_DIR. */
50*84e872a0SLloyd Pique static const char *
require_xdg_runtime_dir(void)51*84e872a0SLloyd Pique require_xdg_runtime_dir(void)
52*84e872a0SLloyd Pique {
53*84e872a0SLloyd Pique 	char *val = getenv("XDG_RUNTIME_DIR");
54*84e872a0SLloyd Pique 	assert(val && val[0] == '/' && "set $XDG_RUNTIME_DIR to run this test");
55*84e872a0SLloyd Pique 
56*84e872a0SLloyd Pique 	return val;
57*84e872a0SLloyd Pique }
58*84e872a0SLloyd Pique 
TEST(socket_path_overflow_client_connect)59*84e872a0SLloyd Pique TEST(socket_path_overflow_client_connect)
60*84e872a0SLloyd Pique {
61*84e872a0SLloyd Pique 	char path[TOO_LONG];
62*84e872a0SLloyd Pique 	struct wl_display *d;
63*84e872a0SLloyd Pique 
64*84e872a0SLloyd Pique 	require_xdg_runtime_dir();
65*84e872a0SLloyd Pique 
66*84e872a0SLloyd Pique 	memset(path, 'a', sizeof path);
67*84e872a0SLloyd Pique 	path[sizeof path - 1] = '\0';
68*84e872a0SLloyd Pique 
69*84e872a0SLloyd Pique 	d = wl_display_connect(path);
70*84e872a0SLloyd Pique 	assert(d == NULL);
71*84e872a0SLloyd Pique 	assert(errno == ENAMETOOLONG);
72*84e872a0SLloyd Pique 
73*84e872a0SLloyd Pique 	/* This is useless, but prevents a warning about example_sockaddr_un
74*84e872a0SLloyd Pique 	 * being discarded from the compilation unit. */
75*84e872a0SLloyd Pique 	strcpy(example_sockaddr_un.sun_path, "happy now clang?");
76*84e872a0SLloyd Pique 	assert(example_sockaddr_un.sun_path[0] != '\0');
77*84e872a0SLloyd Pique }
78*84e872a0SLloyd Pique 
TEST(socket_path_overflow_server_create)79*84e872a0SLloyd Pique TEST(socket_path_overflow_server_create)
80*84e872a0SLloyd Pique {
81*84e872a0SLloyd Pique 	char path[TOO_LONG];
82*84e872a0SLloyd Pique 	struct wl_display *d;
83*84e872a0SLloyd Pique 	int ret;
84*84e872a0SLloyd Pique 
85*84e872a0SLloyd Pique 	require_xdg_runtime_dir();
86*84e872a0SLloyd Pique 
87*84e872a0SLloyd Pique 	memset(path, 'a', sizeof path);
88*84e872a0SLloyd Pique 	path[sizeof path - 1] = '\0';
89*84e872a0SLloyd Pique 
90*84e872a0SLloyd Pique 	d = wl_display_create();
91*84e872a0SLloyd Pique 	assert(d != NULL);
92*84e872a0SLloyd Pique 
93*84e872a0SLloyd Pique 	ret = wl_display_add_socket(d, path);
94*84e872a0SLloyd Pique 	assert(ret < 0);
95*84e872a0SLloyd Pique 	assert(errno == ENAMETOOLONG);
96*84e872a0SLloyd Pique 
97*84e872a0SLloyd Pique 	wl_display_destroy(d);
98*84e872a0SLloyd Pique }
99*84e872a0SLloyd Pique 
TEST(add_existing_socket)100*84e872a0SLloyd Pique TEST(add_existing_socket)
101*84e872a0SLloyd Pique {
102*84e872a0SLloyd Pique 	char path[sizeof example_sockaddr_un.sun_path];
103*84e872a0SLloyd Pique 	const char *name = "wayland-test-0";
104*84e872a0SLloyd Pique 	const char *xdg_runtime_dir;
105*84e872a0SLloyd Pique 	struct wl_display *d;
106*84e872a0SLloyd Pique 	int ret;
107*84e872a0SLloyd Pique 	size_t len;
108*84e872a0SLloyd Pique 
109*84e872a0SLloyd Pique 	xdg_runtime_dir = require_xdg_runtime_dir();
110*84e872a0SLloyd Pique 
111*84e872a0SLloyd Pique 	d = wl_display_create();
112*84e872a0SLloyd Pique 	assert(d != NULL);
113*84e872a0SLloyd Pique 
114*84e872a0SLloyd Pique 	/* this one should be OK */
115*84e872a0SLloyd Pique 	ret = wl_display_add_socket(d, name);
116*84e872a0SLloyd Pique 	assert(ret == 0);
117*84e872a0SLloyd Pique 
118*84e872a0SLloyd Pique 	/* this one should fail */
119*84e872a0SLloyd Pique 	ret = wl_display_add_socket(d, name);
120*84e872a0SLloyd Pique 	assert(ret < 0);
121*84e872a0SLloyd Pique 
122*84e872a0SLloyd Pique 	/* the original socket should still exist.
123*84e872a0SLloyd Pique 	 * this was a bug introduced in e2c0d47b0c77f18cd90e9c6eabb358c4d89681c8 */
124*84e872a0SLloyd Pique 	len = snprintf(path, sizeof example_sockaddr_un.sun_path, "%s/%s",
125*84e872a0SLloyd Pique 		       xdg_runtime_dir, name);
126*84e872a0SLloyd Pique 	assert(len < sizeof example_sockaddr_un.sun_path
127*84e872a0SLloyd Pique 	       && "Bug in test. Path too long");
128*84e872a0SLloyd Pique 
129*84e872a0SLloyd Pique 	assert(access(path, F_OK) != -1);
130*84e872a0SLloyd Pique 
131*84e872a0SLloyd Pique 	/* the original socket should still exist */
132*84e872a0SLloyd Pique 	ret = wl_display_add_socket(d, name);
133*84e872a0SLloyd Pique 	assert(ret < 0);
134*84e872a0SLloyd Pique 
135*84e872a0SLloyd Pique 	wl_display_destroy(d);
136*84e872a0SLloyd Pique }
137*84e872a0SLloyd Pique 
TEST(add_socket_auto)138*84e872a0SLloyd Pique TEST(add_socket_auto)
139*84e872a0SLloyd Pique {
140*84e872a0SLloyd Pique 	/* the number of auto sockets is currently 32,
141*84e872a0SLloyd Pique 	 * set in wayland-server.c.
142*84e872a0SLloyd Pique 	 */
143*84e872a0SLloyd Pique 	const int MAX_SOCKETS = 32;
144*84e872a0SLloyd Pique 
145*84e872a0SLloyd Pique 	char path[sizeof example_sockaddr_un.sun_path];
146*84e872a0SLloyd Pique 	const char *name;
147*84e872a0SLloyd Pique 	const char *xdg_runtime_dir;
148*84e872a0SLloyd Pique 	struct wl_display *d;
149*84e872a0SLloyd Pique 	int i;
150*84e872a0SLloyd Pique 	size_t len;
151*84e872a0SLloyd Pique 
152*84e872a0SLloyd Pique 	xdg_runtime_dir = require_xdg_runtime_dir();
153*84e872a0SLloyd Pique 
154*84e872a0SLloyd Pique 	d = wl_display_create();
155*84e872a0SLloyd Pique 	assert(d != NULL);
156*84e872a0SLloyd Pique 
157*84e872a0SLloyd Pique 	for (i = 0; i <= MAX_SOCKETS; ++i) {
158*84e872a0SLloyd Pique 		name = wl_display_add_socket_auto(d);
159*84e872a0SLloyd Pique 		assert(name != NULL);
160*84e872a0SLloyd Pique 
161*84e872a0SLloyd Pique 		len = snprintf(path, sizeof example_sockaddr_un.sun_path,
162*84e872a0SLloyd Pique 			       "%s/%s", xdg_runtime_dir, name);
163*84e872a0SLloyd Pique 		assert(len < sizeof example_sockaddr_un.sun_path
164*84e872a0SLloyd Pique 		       && "Bug in test. Path too long");
165*84e872a0SLloyd Pique 
166*84e872a0SLloyd Pique 		/* was the socket created correctly? */
167*84e872a0SLloyd Pique 		assert(access(path, F_OK) != -1);
168*84e872a0SLloyd Pique 
169*84e872a0SLloyd Pique 		/* is the name sequential? */
170*84e872a0SLloyd Pique 		len = snprintf(path, sizeof example_sockaddr_un.sun_path,
171*84e872a0SLloyd Pique 			       "wayland-%d", i);
172*84e872a0SLloyd Pique 		assert(strcmp(name, path) == 0);
173*84e872a0SLloyd Pique 	}
174*84e872a0SLloyd Pique 
175*84e872a0SLloyd Pique 	/* next addition should return NULL */
176*84e872a0SLloyd Pique 	name = wl_display_add_socket_auto(d);
177*84e872a0SLloyd Pique 	assert(name == NULL);
178*84e872a0SLloyd Pique 
179*84e872a0SLloyd Pique 	/* check if the socket was not deleted the last time */
180*84e872a0SLloyd Pique 	name = wl_display_add_socket_auto(d);
181*84e872a0SLloyd Pique 	assert(name == NULL);
182*84e872a0SLloyd Pique 
183*84e872a0SLloyd Pique 	wl_display_destroy(d);
184*84e872a0SLloyd Pique }
185*84e872a0SLloyd Pique 
186*84e872a0SLloyd Pique struct client_create_listener {
187*84e872a0SLloyd Pique 	struct wl_listener listener;
188*84e872a0SLloyd Pique 	struct wl_display *display;
189*84e872a0SLloyd Pique };
190*84e872a0SLloyd Pique 
191*84e872a0SLloyd Pique struct client_destroy_listener {
192*84e872a0SLloyd Pique 	struct wl_listener listener;
193*84e872a0SLloyd Pique 	struct wl_display *display;
194*84e872a0SLloyd Pique };
195*84e872a0SLloyd Pique 
196*84e872a0SLloyd Pique static void
client_destroy_notify(struct wl_listener * l,void * data)197*84e872a0SLloyd Pique client_destroy_notify(struct wl_listener *l, void *data)
198*84e872a0SLloyd Pique {
199*84e872a0SLloyd Pique 	struct client_destroy_listener *listener =
200*84e872a0SLloyd Pique 		wl_container_of(l, listener, listener);
201*84e872a0SLloyd Pique 	wl_display_terminate(listener->display);
202*84e872a0SLloyd Pique 	free(listener);
203*84e872a0SLloyd Pique }
204*84e872a0SLloyd Pique 
205*84e872a0SLloyd Pique static void
client_create_notify(struct wl_listener * l,void * data)206*84e872a0SLloyd Pique client_create_notify(struct wl_listener *l, void *data)
207*84e872a0SLloyd Pique {
208*84e872a0SLloyd Pique 	struct wl_client *client = data;
209*84e872a0SLloyd Pique 	struct client_create_listener *listener =
210*84e872a0SLloyd Pique 		wl_container_of(l, listener, listener);
211*84e872a0SLloyd Pique 	struct client_destroy_listener *destroy_listener = (struct client_destroy_listener *)malloc(sizeof *destroy_listener);
212*84e872a0SLloyd Pique 	assert(destroy_listener != NULL);
213*84e872a0SLloyd Pique 	destroy_listener->display = listener->display;
214*84e872a0SLloyd Pique 	destroy_listener->listener.notify = client_destroy_notify;
215*84e872a0SLloyd Pique 	wl_client_add_destroy_listener(client, &destroy_listener->listener);
216*84e872a0SLloyd Pique }
217*84e872a0SLloyd Pique 
TEST(absolute_socket_path)218*84e872a0SLloyd Pique TEST(absolute_socket_path)
219*84e872a0SLloyd Pique {
220*84e872a0SLloyd Pique 	struct wl_display *display;
221*84e872a0SLloyd Pique 	struct client_create_listener client_create_listener;
222*84e872a0SLloyd Pique 	struct sockaddr_un addr;
223*84e872a0SLloyd Pique 	int fd;
224*84e872a0SLloyd Pique 	socklen_t size;
225*84e872a0SLloyd Pique 	const char *xdg_runtime_dir;
226*84e872a0SLloyd Pique 	size_t len;
227*84e872a0SLloyd Pique 	int ret;
228*84e872a0SLloyd Pique 	pid_t pid;
229*84e872a0SLloyd Pique 
230*84e872a0SLloyd Pique 	/* It's a little weird that this test about absolute socket paths
231*84e872a0SLloyd Pique 	 * uses XDG_RUNTIME_DIR, but that's the only location guaranteed
232*84e872a0SLloyd Pique 	 * by test-runner to be both writable and unique. This isn't
233*84e872a0SLloyd Pique 	 * really a problem; we'll just take care that the leaf-level
234*84e872a0SLloyd Pique 	 * filename used for the socket isn't anything that would
235*84e872a0SLloyd Pique 	 * accidentally be generated by a default usage of wl_display_connect(). */
236*84e872a0SLloyd Pique 	xdg_runtime_dir = require_xdg_runtime_dir();
237*84e872a0SLloyd Pique 	memset(&addr, 0, sizeof addr);
238*84e872a0SLloyd Pique 	len = snprintf(addr.sun_path, sizeof addr.sun_path,
239*84e872a0SLloyd Pique 		       "%s/%s", xdg_runtime_dir, "wayland-absolute-0");
240*84e872a0SLloyd Pique 	assert(len < sizeof addr.sun_path
241*84e872a0SLloyd Pique 	       && "Bug in test. Path too long");
242*84e872a0SLloyd Pique 
243*84e872a0SLloyd Pique 	/* The path must not exist prior to binding. */
244*84e872a0SLloyd Pique 	assert(access(addr.sun_path, F_OK) == -1);
245*84e872a0SLloyd Pique 
246*84e872a0SLloyd Pique 	size = offsetof (struct sockaddr_un, sun_path) + strlen(addr.sun_path);
247*84e872a0SLloyd Pique 	addr.sun_family = AF_LOCAL;
248*84e872a0SLloyd Pique 	fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
249*84e872a0SLloyd Pique 	assert(fd >= 0 );
250*84e872a0SLloyd Pique 	ret = bind(fd, (struct sockaddr *) &addr, size);
251*84e872a0SLloyd Pique 	assert(ret >= 0);
252*84e872a0SLloyd Pique 	ret = listen(fd, 128);
253*84e872a0SLloyd Pique 	assert(ret >= 0);
254*84e872a0SLloyd Pique 
255*84e872a0SLloyd Pique 	/* Start server display. Be careful (by avoiding wl_display_add_socket_auto()
256*84e872a0SLloyd Pique 	 * to offer only the absolutely qualified socket made above. */
257*84e872a0SLloyd Pique 	display = wl_display_create();
258*84e872a0SLloyd Pique 	assert(display != NULL);
259*84e872a0SLloyd Pique 	client_create_listener.listener.notify = client_create_notify;
260*84e872a0SLloyd Pique 	client_create_listener.display = display;
261*84e872a0SLloyd Pique 	wl_display_add_client_created_listener(display, &client_create_listener.listener);
262*84e872a0SLloyd Pique 	ret = wl_display_add_socket_fd(display, fd);
263*84e872a0SLloyd Pique 	assert(ret == 0);
264*84e872a0SLloyd Pique 
265*84e872a0SLloyd Pique 	/* Execute client that connects to the absolutely qualified server socket path. */
266*84e872a0SLloyd Pique 	pid = fork();
267*84e872a0SLloyd Pique 	assert(pid != -1);
268*84e872a0SLloyd Pique 
269*84e872a0SLloyd Pique 	if (pid == 0) {
270*84e872a0SLloyd Pique 		ret = setenv("WAYLAND_DISPLAY", addr.sun_path, 1);
271*84e872a0SLloyd Pique 		assert(ret == 0);
272*84e872a0SLloyd Pique 		struct wl_display *client_display = wl_display_connect(NULL);
273*84e872a0SLloyd Pique 		assert(client_display != NULL);
274*84e872a0SLloyd Pique 		ret = wl_display_roundtrip(client_display);
275*84e872a0SLloyd Pique 		assert(ret != -1);
276*84e872a0SLloyd Pique 		wl_display_disconnect(client_display);
277*84e872a0SLloyd Pique 		exit(0);
278*84e872a0SLloyd Pique 		assert(false);
279*84e872a0SLloyd Pique 	}
280*84e872a0SLloyd Pique 
281*84e872a0SLloyd Pique 	wl_display_run(display);
282*84e872a0SLloyd Pique 	ret = waitpid(pid, NULL, 0);
283*84e872a0SLloyd Pique 	assert(ret == pid);
284*84e872a0SLloyd Pique 
285*84e872a0SLloyd Pique 	wl_display_destroy(display);
286*84e872a0SLloyd Pique 
287*84e872a0SLloyd Pique 	ret = unlink(addr.sun_path);
288*84e872a0SLloyd Pique 	assert(ret == 0);
289*84e872a0SLloyd Pique }
290