summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Ã…dahl <jadahl@gmail.com>2022-06-10 23:19:03 +0200
committerMarge Bot <marge-bot@gnome.org>2023-03-01 23:57:32 +0000
commitfb622974b9200708c589f7c2e1df575a23794822 (patch)
tree823e2035a66855009971797b52e1a85bc6626eeb
parent73fb64cbb6d65709f4c47b7c2850bb6d66a8b3d8 (diff)
downloadmutter-fb622974b9200708c589f7c2e1df575a23794822.tar.gz
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: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2854>
-rw-r--r--src/tests/drm-mock/drm-mock.c108
-rw-r--r--src/tests/drm-mock/drm-mock.h44
-rw-r--r--src/tests/drm-mock/meson.build14
-rw-r--r--src/tests/meson.build1
4 files changed, 167 insertions, 0 deletions
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 <dlfcn.h>
+#include <glib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <xf86drmMode.h>
+
+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