test: add test for nsresourced

This commit is contained in:
Lennart Poettering
2025-03-11 06:06:40 +01:00
parent 6e09c46a51
commit ef197d6ab2
2 changed files with 33 additions and 0 deletions

View File

@@ -132,6 +132,7 @@ simple_tests += files(
'test-modhex.c',
'test-mountpoint-util.c',
'test-net-naming-scheme.c',
'test-nsresource.c',
'test-nulstr-util.c',
'test-open-file.c',
'test-ordered-set.c',

View File

@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <net/if.h>
#include "fd-util.h"
#include "namespace-util.h"
#include "nsresource.h"
#include "tests.h"
TEST(delegatetap) {
int r;
_cleanup_close_ int userns_fd = userns_acquire_self_root();
if (ERRNO_IS_NEG_PRIVILEGE(userns_fd) || ERRNO_IS_NEG_NOT_SUPPORTED(userns_fd))
return (void) log_tests_skipped_errno(userns_fd, "User namespaces not available");
ASSERT_OK(userns_fd);
r = nsresource_register_userns("foobar", userns_fd);
if (ERRNO_IS_NEG_DISCONNECT(r) || r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r))
return (void) log_tests_skipped_errno(r, "systemd-nsresourced cannot be reached");
ASSERT_OK(r);
_cleanup_free_ char *ifname = NULL;
_cleanup_close_ int tap_fd = nsresource_add_netif_tap(userns_fd, &ifname);
if (ERRNO_IS_NEG_NOT_SUPPORTED(tap_fd))
return (void) log_tests_skipped_errno(tap_fd, "tap device support not available");
ASSERT_OK(tap_fd);
ASSERT_GE(if_nametoindex(ifname), 2U);
}
DEFINE_TEST_MAIN(LOG_DEBUG);