|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | + |
| 3 | +#include <errno.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stdint.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +#define main usbdisplay_daemon_main |
| 12 | +#include "../userspace/usb-displayd.c" |
| 13 | +#undef main |
| 14 | + |
| 15 | +struct capture_context { |
| 16 | + unsigned int expected_frames; |
| 17 | + unsigned int frames; |
| 18 | + struct usbdisplay_frame captured[3]; |
| 19 | + unsigned char pixels[3][4096]; |
| 20 | +}; |
| 21 | + |
| 22 | +static int capture_submit(void *opaque, const struct usbdisplay_frame *frame) |
| 23 | +{ |
| 24 | + struct capture_context *context = opaque; |
| 25 | + unsigned int index = context->frames; |
| 26 | + int result = 0; |
| 27 | + |
| 28 | + if (index >= 3U || frame->bytes > sizeof(context->pixels[index])) { |
| 29 | + result = -EOVERFLOW; |
| 30 | + } else { |
| 31 | + context->captured[index] = *frame; |
| 32 | + memcpy(context->pixels[index], frame->pixels, frame->bytes); |
| 33 | + context->captured[index].pixels = context->pixels[index]; |
| 34 | + context->frames = index + 1U; |
| 35 | + if (context->frames >= context->expected_frames) { |
| 36 | + stop_requested = 1; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +static void capture_close(void *opaque) |
| 44 | +{ |
| 45 | + (void)opaque; |
| 46 | +} |
| 47 | + |
| 48 | +static const struct usbdisplay_backend_v1 capture_backend = { |
| 49 | + .abi_version = USBDISPLAY_BACKEND_ABI_VERSION, |
| 50 | + .struct_size = sizeof(struct usbdisplay_backend_v1), |
| 51 | + .capabilities = USBDISPLAY_BACKEND_CAP_PHYSICAL, |
| 52 | + .name = "test-capture", |
| 53 | + .submit = capture_submit, |
| 54 | + .close = capture_close, |
| 55 | +}; |
| 56 | + |
| 57 | +static const struct usbdisplay_backend_v1 diagnostic_backend = { |
| 58 | + .abi_version = USBDISPLAY_BACKEND_ABI_VERSION, |
| 59 | + .struct_size = sizeof(struct usbdisplay_backend_v1), |
| 60 | + .capabilities = USBDISPLAY_BACKEND_CAP_NONE, |
| 61 | + .name = "test-diagnostic", |
| 62 | + .submit = capture_submit, |
| 63 | + .close = capture_close, |
| 64 | +}; |
| 65 | + |
| 66 | +static void fill_update(struct usbdisplay_update *update, uint32_t source, |
| 67 | + uint64_t sequence) |
| 68 | +{ |
| 69 | + memset(update, 0, sizeof(*update)); |
| 70 | + update->sequence = sequence; |
| 71 | + update->timestamp_ns = sequence; |
| 72 | + update->slot = 0; |
| 73 | + update->width = 32U; |
| 74 | + update->height = 16U; |
| 75 | + update->stride = 32U * 4U; |
| 76 | + update->format = USBDISPLAY_FORMAT_XRGB8888; |
| 77 | + update->source = source; |
| 78 | + update->damage_width = update->width; |
| 79 | + update->damage_height = update->height; |
| 80 | +} |
| 81 | + |
| 82 | +static int write_updates(int descriptor, const struct usbdisplay_update *updates, |
| 83 | + unsigned int count) |
| 84 | +{ |
| 85 | + unsigned int index; |
| 86 | + ssize_t written; |
| 87 | + int result = 0; |
| 88 | + |
| 89 | + for (index = 0; index < count && result == 0; ++index) { |
| 90 | + written = write(descriptor, &updates[index], sizeof(updates[index])); |
| 91 | + if (written != (ssize_t)sizeof(updates[index])) { |
| 92 | + result = -EIO; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return result; |
| 97 | +} |
| 98 | + |
| 99 | +static int run_case(const struct usbdisplay_backend_v1 *backend, |
| 100 | + const struct usbdisplay_update *updates, unsigned int update_count, |
| 101 | + const unsigned char *mapping, size_t mapping_bytes, |
| 102 | + unsigned int expected_frames, struct capture_context *capture) |
| 103 | +{ |
| 104 | + struct usbdisplay_device_info info; |
| 105 | + int descriptors[2] = {-1, -1}; |
| 106 | + int result = 0; |
| 107 | + |
| 108 | + memset(capture, 0, sizeof(*capture)); |
| 109 | + capture->expected_frames = expected_frames; |
| 110 | + memset(&info, 0, sizeof(info)); |
| 111 | + info.abi_version = USBDISPLAY_ABI_VERSION; |
| 112 | + info.width = 32U; |
| 113 | + info.height = 16U; |
| 114 | + info.slot_count = 3U; |
| 115 | + info.slot_bytes = 32U * 16U * 4U; |
| 116 | + info.map_bytes = info.slot_count * info.slot_bytes; |
| 117 | + if (mapping_bytes < info.map_bytes || pipe(descriptors) != 0) { |
| 118 | + result = -EIO; |
| 119 | + } else { |
| 120 | + stop_requested = 0; |
| 121 | + result = write_updates(descriptors[1], updates, update_count); |
| 122 | + if (result == 0) { |
| 123 | + close(descriptors[1]); |
| 124 | + descriptors[1] = -1; |
| 125 | + result = run_loop(descriptors[0], &info, mapping, backend, capture); |
| 126 | + } |
| 127 | + } |
| 128 | + if (descriptors[0] >= 0) { |
| 129 | + close(descriptors[0]); |
| 130 | + } |
| 131 | + if (descriptors[1] >= 0) { |
| 132 | + close(descriptors[1]); |
| 133 | + } |
| 134 | + |
| 135 | + return result; |
| 136 | +} |
| 137 | + |
| 138 | +static bool frame_matches(const struct usbdisplay_frame *frame, |
| 139 | + const unsigned char *expected, size_t bytes) |
| 140 | +{ |
| 141 | + return frame->bytes == bytes && |
| 142 | + memcmp((const unsigned char *)frame->pixels, expected, bytes) == 0; |
| 143 | +} |
| 144 | + |
| 145 | +static int test_cases(void) |
| 146 | +{ |
| 147 | + unsigned char mapping[3U * 32U * 16U * 4U]; |
| 148 | + uint32_t splash[32U * 16U]; |
| 149 | + struct usbdisplay_update updates[2]; |
| 150 | + struct capture_context capture; |
| 151 | + const struct usbdisplay_backend_v1 *backend; |
| 152 | + size_t splash_bytes; |
| 153 | + int result = 0; |
| 154 | + unsigned int index; |
| 155 | + |
| 156 | + for (index = 0; index < sizeof(mapping); ++index) { |
| 157 | + mapping[index] = (unsigned char)((index * 37U + 11U) & 0xffU); |
| 158 | + } |
| 159 | + splash_bytes = usbdisplay_splash_bytes(32U, 16U); |
| 160 | + if (splash_bytes != sizeof(splash) || |
| 161 | + usbdisplay_splash_render((uint32_t *)splash, 32U, 16U, 32U * 4U) != 0) { |
| 162 | + result = -EINVAL; |
| 163 | + } |
| 164 | + |
| 165 | + if (result == 0) { |
| 166 | + fill_update(&updates[0], USBDISPLAY_SOURCE_INITIAL, 1U); |
| 167 | + backend = &capture_backend; |
| 168 | + result = run_case(backend, updates, 1U, mapping, sizeof(mapping), 1U, |
| 169 | + &capture); |
| 170 | + if (result == 0 && (capture.frames != 1U || |
| 171 | + capture.captured[0].source != USBDISPLAY_SOURCE_INITIAL || |
| 172 | + !frame_matches(&capture.captured[0], (const unsigned char *)splash, |
| 173 | + splash_bytes))) { |
| 174 | + result = -EINVAL; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if (result == 0) { |
| 179 | + fill_update(&updates[0], USBDISPLAY_SOURCE_INITIAL, 2U); |
| 180 | + fill_update(&updates[1], USBDISPLAY_SOURCE_FBDEV, 3U); |
| 181 | + result = run_case(&capture_backend, updates, 2U, mapping, |
| 182 | + sizeof(mapping), 2U, &capture); |
| 183 | + if (result == 0 && (capture.frames != 2U || |
| 184 | + capture.captured[1].source != USBDISPLAY_SOURCE_FBDEV || |
| 185 | + !frame_matches(&capture.captured[1], mapping, |
| 186 | + 32U * 16U * 4U))) { |
| 187 | + result = -EINVAL; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if (result == 0) { |
| 192 | + fill_update(&updates[0], USBDISPLAY_SOURCE_DRM, 4U); |
| 193 | + result = run_case(&capture_backend, updates, 1U, mapping, |
| 194 | + sizeof(mapping), 1U, &capture); |
| 195 | + if (result == 0 && (capture.frames != 1U || |
| 196 | + capture.captured[0].source != USBDISPLAY_SOURCE_DRM || |
| 197 | + !frame_matches(&capture.captured[0], mapping, |
| 198 | + 32U * 16U * 4U))) { |
| 199 | + result = -EINVAL; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if (result == 0) { |
| 204 | + fill_update(&updates[0], USBDISPLAY_SOURCE_FBDEV, 5U); |
| 205 | + result = run_case(&capture_backend, updates, 1U, mapping, |
| 206 | + sizeof(mapping), 1U, &capture); |
| 207 | + if (result == 0 && (capture.frames != 1U || |
| 208 | + capture.captured[0].source != USBDISPLAY_SOURCE_FBDEV || |
| 209 | + !frame_matches(&capture.captured[0], mapping, |
| 210 | + 32U * 16U * 4U))) { |
| 211 | + result = -EINVAL; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + if (result == 0) { |
| 216 | + fill_update(&updates[0], USBDISPLAY_SOURCE_INITIAL, 6U); |
| 217 | + result = run_case(&diagnostic_backend, updates, 1U, mapping, |
| 218 | + sizeof(mapping), 1U, &capture); |
| 219 | + if (result == 0 && (capture.frames != 1U || |
| 220 | + !frame_matches(&capture.captured[0], (const unsigned char *)splash, |
| 221 | + splash_bytes))) { |
| 222 | + result = -EINVAL; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + return result; |
| 227 | +} |
| 228 | + |
| 229 | +int main(void) |
| 230 | +{ |
| 231 | + int result = test_cases(); |
| 232 | + |
| 233 | + if (result != 0) { |
| 234 | + fprintf(stderr, "daemon splash runtime test failed: %d\n", result); |
| 235 | + } else { |
| 236 | + printf("daemon splash runtime tests passed\n"); |
| 237 | + } |
| 238 | + |
| 239 | + return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 240 | +} |
0 commit comments