| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "errno-util.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "memfd-util.h" |
| 8 |
#include "tests.h" |
| 9 |
|
| 10 |
TEST(memfd_get_sealed) { |
| 11 |
#define TEST_TEXT "this is some random test text we are going to write to a memfd" |
| 12 |
_cleanup_close_ int fd = memfd_new_full("test-memfd-get-sealed", MFD_ALLOW_SEALING); |
| 13 |
if (fd < 0) { |
| 14 |
ASSERT_TRUE(ERRNO_IS_NOT_SUPPORTED(fd)); |
| 15 |
return (void) log_tests_skipped_errno(fd, "Failed to create new memfd"); |
| 16 |
} |
| 17 |
ASSERT_OK_EQ_ERRNO(write(fd, TEST_TEXT, strlen(TEST_TEXT)), (ssize_t) strlen(TEST_TEXT)); |
| 18 |
/* we'll leave the read offset at the end of the memfd, the fdopen_independent() descriptors should |
| 19 |
* start at the beginning anyway */ |
| 20 |
|
| 21 |
uint64_t size, new_size; |
| 22 |
ASSERT_OK(memfd_get_size(fd, &size)); |
| 23 |
ASSERT_GE(size, (uint64_t) strlen(TEST_TEXT)); |
| 24 |
|
| 25 |
ASSERT_OK(memfd_set_size(fd, size * 2)); |
| 26 |
ASSERT_OK(memfd_get_size(fd, &new_size)); |
| 27 |
ASSERT_EQ(new_size, size * 2); |
| 28 |
|
| 29 |
ASSERT_OK(memfd_set_size(fd, new_size / 2)); |
| 30 |
ASSERT_OK(memfd_get_size(fd, &size)); |
| 31 |
ASSERT_EQ(size, new_size / 2); |
| 32 |
|
| 33 |
ASSERT_OK_ZERO(memfd_get_sealed(fd)); |
| 34 |
ASSERT_OK(memfd_set_sealed(fd)); |
| 35 |
ASSERT_OK_POSITIVE(memfd_get_sealed(fd)); |
| 36 |
} |
| 37 |
|
| 38 |
DEFINE_TEST_MAIN(LOG_DEBUG); |