blob: e60e0062f591e9068b3277a718231e596c015037 [file] [log] [blame]
Kristian Høgsberg306e3612012-04-12 12:54:14 -04001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#define _GNU_SOURCE
24
25#include <unistd.h>
26#include <stdio.h>
27#include <stdlib.h>
Kristian Høgsbergdd909212012-04-27 11:15:58 -040028#include <stdarg.h>
Kristian Høgsberg306e3612012-04-12 12:54:14 -040029#include <sys/types.h>
30#include <sys/wait.h>
31#include <string.h>
32#include <assert.h>
Kristian Høgsbergdd909212012-04-27 11:15:58 -040033#include <sys/socket.h>
Kristian Høgsberg306e3612012-04-12 12:54:14 -040034
35#include "test-runner.h"
36
Kristian Høgsbergdd909212012-04-27 11:15:58 -040037static void
38test_client_cleanup(struct weston_process *proc, int status)
39{
40 struct test_client *client =
41 container_of(proc, struct test_client, proc);
42
43 fprintf(stderr, "test client exited, status %d\n", status);
44
45 client->status = status;
46 client->done = 1;
47
Kristian Høgsberg3018b442012-04-27 15:02:56 -040048 assert(client->status == 0);
49
Kristian Høgsbergdd909212012-04-27 11:15:58 -040050 if (client->terminate)
51 wl_display_terminate(client->compositor->wl_display);
52}
53
Kristian Høgsberg3018b442012-04-27 15:02:56 -040054static int
55test_client_data(int fd, uint32_t mask, void *data)
56{
57 struct test_client *client = data;
58 struct wl_event_loop *loop;
59 int len;
60
61 len = read(client->fd, client->buf, sizeof client->buf);
62 assert(len >= 0);
63 fprintf(stderr, "got %.*s from client\n", len - 1, client->buf);
64 assert(client->buf[len - 1] == '\n');
65 client->buf[len - 1] = '\0';
66
67 loop = wl_display_get_event_loop(client->compositor->wl_display);
68 wl_event_loop_add_idle(loop, (void *) client->handle, client);
69
70 return 1;
71}
72
Kristian Høgsbergdd909212012-04-27 11:15:58 -040073struct test_client *
74test_client_launch(struct weston_compositor *compositor)
75{
76 struct test_client *client;
Kristian Høgsberg3018b442012-04-27 15:02:56 -040077 struct wl_event_loop *loop;
Kristian Høgsbergdd909212012-04-27 11:15:58 -040078 int ret, sv[2], client_fd;
79 char buf[256];
80
81 client = malloc(sizeof *client);
82 assert(client);
83 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv);
84 assert(ret == 0);
85
86 client_fd = dup(sv[0]);
87 assert(client_fd >= 0);
88 snprintf(buf, sizeof buf, "%d", client_fd);
89 setenv("TEST_SOCKET", buf, 1);
90 snprintf(buf, sizeof buf, "%s/test-client", getenv("abs_builddir"));
91 fprintf(stderr, "launching %s\n", buf);
92
93 client->terminate = 0;
94 client->compositor = compositor;
95 client->client = weston_client_launch(compositor,
96 &client->proc, buf,
97 test_client_cleanup);
98 assert(client->client);
99 close(sv[0]);
100 client->fd = sv[1];
101
Kristian Høgsberg3018b442012-04-27 15:02:56 -0400102 loop = wl_display_get_event_loop(compositor->wl_display);
103 wl_event_loop_add_fd(loop, client->fd, WL_EVENT_READABLE,
104 test_client_data, client);
105
Kristian Høgsbergdd909212012-04-27 11:15:58 -0400106 return client;
107}
108
109void
110test_client_send(struct test_client *client, const char *fmt, ...)
111{
112 char buf[256];
113 va_list ap;
114 int len;
115
116 va_start(ap, fmt);
117 len = vsnprintf(buf, sizeof buf, fmt, ap);
118 va_end(ap);
119
120 assert(write(client->fd, buf, len) == len);
121}
122
Kristian Høgsberg306e3612012-04-12 12:54:14 -0400123extern const struct test __start_test_section, __stop_test_section;
124
125static void
126run_test(void *data)
127{
128 struct weston_compositor *compositor = data;
129 const struct test *t;
130
131 for (t = &__start_test_section; t < &__stop_test_section; t++)
132 t->run(compositor);
133}
134
135int
136module_init(struct weston_compositor *compositor);
137
138WL_EXPORT int
139module_init(struct weston_compositor *compositor)
140{
141 struct wl_event_loop *loop;
142
143 loop = wl_display_get_event_loop(compositor->wl_display);
144
145 wl_event_loop_add_idle(loop, run_test, compositor);
146
147 return 0;
148}