Kristian Høgsberg | 306e361 | 2012-04-12 12:54:14 -0400 | [diff] [blame] | 1 | /* |
| 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øgsberg | dd90921 | 2012-04-27 11:15:58 -0400 | [diff] [blame] | 28 | #include <stdarg.h> |
Kristian Høgsberg | 306e361 | 2012-04-12 12:54:14 -0400 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <string.h> |
| 32 | #include <assert.h> |
Kristian Høgsberg | dd90921 | 2012-04-27 11:15:58 -0400 | [diff] [blame] | 33 | #include <sys/socket.h> |
Kristian Høgsberg | 306e361 | 2012-04-12 12:54:14 -0400 | [diff] [blame] | 34 | |
| 35 | #include "test-runner.h" |
| 36 | |
Kristian Høgsberg | dd90921 | 2012-04-27 11:15:58 -0400 | [diff] [blame] | 37 | static void |
| 38 | test_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øgsberg | 3018b44 | 2012-04-27 15:02:56 -0400 | [diff] [blame] | 48 | assert(client->status == 0); |
| 49 | |
Kristian Høgsberg | dd90921 | 2012-04-27 11:15:58 -0400 | [diff] [blame] | 50 | if (client->terminate) |
| 51 | wl_display_terminate(client->compositor->wl_display); |
| 52 | } |
| 53 | |
Kristian Høgsberg | 3018b44 | 2012-04-27 15:02:56 -0400 | [diff] [blame] | 54 | static int |
| 55 | test_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øgsberg | dd90921 | 2012-04-27 11:15:58 -0400 | [diff] [blame] | 73 | struct test_client * |
| 74 | test_client_launch(struct weston_compositor *compositor) |
| 75 | { |
| 76 | struct test_client *client; |
Kristian Høgsberg | 3018b44 | 2012-04-27 15:02:56 -0400 | [diff] [blame] | 77 | struct wl_event_loop *loop; |
Kristian Høgsberg | dd90921 | 2012-04-27 11:15:58 -0400 | [diff] [blame] | 78 | 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øgsberg | 3018b44 | 2012-04-27 15:02:56 -0400 | [diff] [blame] | 102 | 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øgsberg | dd90921 | 2012-04-27 11:15:58 -0400 | [diff] [blame] | 106 | return client; |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | test_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øgsberg | 306e361 | 2012-04-12 12:54:14 -0400 | [diff] [blame] | 123 | extern const struct test __start_test_section, __stop_test_section; |
| 124 | |
| 125 | static void |
| 126 | run_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 | |
| 135 | int |
| 136 | module_init(struct weston_compositor *compositor); |
| 137 | |
| 138 | WL_EXPORT int |
| 139 | module_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 | } |