From fb622974b9200708c589f7c2e1df575a23794822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Fri, 10 Jun 2022 23:19:03 +0200 Subject: [PATCH] tests: Add static drm-mock library Add a tiny library that sabotages errors in drmMode*() API calls. This will be used to artificially trigger arbitrary errors, e.g. cause the next commit to fail with EBUSY. The three mocked methods are added as they will be used in a future commit. Part-of: --- src/tests/drm-mock/drm-mock.c | 108 +++++++++++++++++++++++++++++++++ src/tests/drm-mock/drm-mock.h | 44 ++++++++++++++ src/tests/drm-mock/meson.build | 14 +++++ src/tests/meson.build | 1 + 4 files changed, 167 insertions(+) create mode 100644 src/tests/drm-mock/drm-mock.c create mode 100644 src/tests/drm-mock/drm-mock.h create mode 100644 src/tests/drm-mock/meson.build diff --git a/src/tests/drm-mock/drm-mock.c b/src/tests/drm-mock/drm-mock.c new file mode 100644 index 000000000..724596189 --- /dev/null +++ b/src/tests/drm-mock/drm-mock.c @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2022 Red Hat Inc. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include "drm-mock.h" + +#include +#include +#include +#include +#include + +static GList *queued_errors[DRM_MOCK_N_CALLS]; + +static int +maybe_mock_error (DrmMockCall call) +{ + if (queued_errors[call]) + { + int error_number = + GPOINTER_TO_INT (queued_errors[call]->data); + + queued_errors[call] = g_list_remove_link (queued_errors[call], + queued_errors[call]); + + errno = error_number; + return -error_number; + } + + return 0; +} + +#define MOCK_FUNCTION(FunctionName, CALL_TYPE, args_type, args) \ +\ +DRM_MOCK_EXPORT int \ +FunctionName args_type \ +{ \ + static int (* real_function) args_type; \ + int ret; \ + if (G_UNLIKELY (!real_function)) \ + real_function = dlsym (RTLD_NEXT, #FunctionName); \ +\ + ret = maybe_mock_error (CALL_TYPE); \ + if (ret != 0) \ + return ret; \ +\ + return real_function args; \ +} + +MOCK_FUNCTION (drmModeAtomicCommit, + DRM_MOCK_CALL_ATOMIC_COMMIT, + (int fd, + drmModeAtomicReqPtr req, + uint32_t flags, + void *user_data), + (fd, req, flags, user_data)) + +MOCK_FUNCTION (drmModePageFlip, + DRM_MOCK_CALL_PAGE_FLIP, + (int fd, + uint32_t crtc_id, + uint32_t fb_id, + uint32_t flags, + void *user_data), + (fd, crtc_id, fb_id, flags, user_data)) + +MOCK_FUNCTION (drmModeSetCrtc, + DRM_MOCK_CALL_SET_CRTC, + (int fd, + uint32_t crtc_id, + uint32_t fb_id, + uint32_t x, + uint32_t y, + uint32_t *connectors, + int count, + drmModeModeInfoPtr mode), + (fd, crtc_id, fb_id, x, y, connectors, count, mode)) + +void +drm_mock_queue_error (DrmMockCall call, + int error_number) +{ + g_return_if_fail (call < DRM_MOCK_N_CALLS); + + queued_errors[call] = g_list_append (queued_errors[call], + GINT_TO_POINTER (error_number)); +} diff --git a/src/tests/drm-mock/drm-mock.h b/src/tests/drm-mock/drm-mock.h new file mode 100644 index 000000000..ab0838c93 --- /dev/null +++ b/src/tests/drm-mock/drm-mock.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2022 Red Hat Inc. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#ifndef DRM_MOCK_H +#define DRM_MOCK_H + +#define DRM_MOCK_EXPORT __attribute__((visibility("default"))) extern + +typedef enum _DrmMockCall +{ + DRM_MOCK_CALL_ATOMIC_COMMIT, + DRM_MOCK_CALL_PAGE_FLIP, + DRM_MOCK_CALL_SET_CRTC, + + DRM_MOCK_N_CALLS +} DrmMockCall; + +DRM_MOCK_EXPORT +void drm_mock_queue_error (DrmMockCall call, + int error_number); + +#endif /* DRM_MOCK_H */ diff --git a/src/tests/drm-mock/meson.build b/src/tests/drm-mock/meson.build new file mode 100644 index 000000000..f3fe22a2d --- /dev/null +++ b/src/tests/drm-mock/meson.build @@ -0,0 +1,14 @@ +drm_mock = shared_library('drm-mock', + sources: [ + 'drm-mock.c', + ], + dependencies: [ + glib_dep, + libdrm_dep, + ], + install: false, +) + +drm_mock_dep = declare_dependency( + link_with: drm_mock, +) diff --git a/src/tests/meson.build b/src/tests/meson.build index 93dd7b309..25d78eaa1 100644 --- a/src/tests/meson.build +++ b/src/tests/meson.build @@ -124,6 +124,7 @@ if have_clutter_tests endif subdir('protocol') +subdir('drm-mock') subdir('wayland-test-clients') if have_installed_tests