]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add basic test for memfd_set_sealed() and memfd_get_sealed() 27659/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 16 May 2023 09:59:25 +0000 (18:59 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 16 May 2023 09:59:25 +0000 (18:59 +0900)
src/test/meson.build
src/test/test-memfd-util.c [new file with mode: 0644]

index 8e76df624d53f330e8e9853ac6f70236ce6a1666..7f8de2a2ce20ea7376db421793690e834f73009a 100644 (file)
@@ -112,6 +112,7 @@ simple_tests += files(
         'test-log.c',
         'test-logarithm.c',
         'test-macro.c',
+        'test-memfd-util.c',
         'test-memory-util.c',
         'test-mempool.c',
         'test-mkdir.c',
diff --git a/src/test/test-memfd-util.c b/src/test/test-memfd-util.c
new file mode 100644 (file)
index 0000000..f8e1b46
--- /dev/null
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <unistd.h>
+
+#include "errno-util.h"
+#include "fd-util.h"
+#include "memfd-util.h"
+#include "string-util.h"
+#include "tests.h"
+
+TEST(memfd_get_sealed) {
+#define TEST_TEXT "this is some random test text we are going to write to a memfd"
+        _cleanup_close_ int fd = -EBADF;
+
+        fd = memfd_new("test-memfd-get-sealed");
+        if (fd < 0) {
+                assert_se(ERRNO_IS_NOT_SUPPORTED(fd));
+                return;
+        }
+
+        assert_se(write(fd, TEST_TEXT, strlen(TEST_TEXT)) == strlen(TEST_TEXT));
+        /* we'll leave the read offset at the end of the memfd, the fdopen_independent() descriptors should
+         * start at the beginning anyway */
+
+        assert_se(memfd_get_sealed(fd) == 0);
+        assert_se(memfd_set_sealed(fd) >= 0);
+        assert_se(memfd_get_sealed(fd) > 0);
+}
+
+DEFINE_TEST_MAIN(LOG_DEBUG);