+#ifdef GLIB_LINUX
+static void
+test_memfd (void)
+{
+ GBytes *bytes;
+ gint fd;
+
+ fd = glib_linux_memfd_create ("", MFD_CLOEXEC);
+ if (fd == -1 && errno == EINVAL)
+ {
+ g_test_skip ("missing kernel memfd support");
+ return;
+ }
+
+ /* We should not be able to seal this one */
+ g_assert (!g_unix_fd_ensure_zero_copy_safe (fd));
+ close (fd);
+
+ /* but this one will work */
+ fd = glib_linux_memfd_create ("", MFD_CLOEXEC | MFD_ALLOW_SEALING);
+ bytes = g_bytes_new_take_zero_copy_fd (fd);
+ g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
+ g_bytes_unref (bytes);
+
+ /* try with real data */
+ fd = glib_linux_memfd_create ("", MFD_CLOEXEC | MFD_ALLOW_SEALING);
+ g_assert_se (write (fd, NYAN, N_NYAN) == N_NYAN);
+ bytes = g_bytes_new_take_zero_copy_fd (fd);
+ g_assert_cmpint (g_bytes_get_size (bytes), ==, N_NYAN);
+ g_assert (memcmp (g_bytes_get_data (bytes, NULL), NYAN, N_NYAN) == 0);
+ g_assert (g_bytes_get_zero_copy_fd (bytes) == fd);
+
+ /* ensure that we cannot modify the fd further */
+ g_assert_se (write (fd, NYAN, N_NYAN) == -1);
+
+ /* that's enough for now */
+ g_bytes_unref (bytes);
+}
+#endif
+