core: add OpenFile setting

This commit is contained in:
Richard Phibel
2023-01-10 15:16:26 +01:00
committed by Lennart Poettering
parent 81315baa68
commit cd48e23f6a
27 changed files with 914 additions and 6 deletions
+2
View File
@@ -695,6 +695,8 @@ tests += [
[files('test-hmac.c')],
[files('test-sha256.c')],
[files('test-open-file.c')],
]
############################################################
+45
View File
@@ -22,6 +22,7 @@
#include "load-fragment.h"
#include "macro.h"
#include "memory-util.h"
#include "open-file.h"
#include "pcre2-util.h"
#include "rm-rf.h"
#include "specifier.h"
@@ -1048,6 +1049,50 @@ TEST(config_parse_log_filter_patterns) {
}
}
TEST(config_parse_open_file) {
_cleanup_(manager_freep) Manager *m = NULL;
_cleanup_(unit_freep) Unit *u = NULL;
_cleanup_(open_file_freep) OpenFile *of = NULL;
int r;
r = manager_new(LOOKUP_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
if (manager_errno_skip_test(r)) {
log_notice_errno(r, "Skipping test: manager_new: %m");
return;
}
assert_se(r >= 0);
assert_se(manager_startup(m, NULL, NULL, NULL) >= 0);
assert_se(u = unit_new(m, sizeof(Service)));
assert_se(unit_add_name(u, "foobar.service") == 0);
r = config_parse_open_file(NULL, "fake", 1, "section", 1,
"OpenFile", 0, "/proc/1/ns/mnt:host-mount-namespace:read-only",
&of, u);
assert_se(r >= 0);
assert_se(of);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "host-mount-namespace"));
assert_se(of->flags == OPENFILE_READ_ONLY);
of = open_file_free(of);
r = config_parse_open_file(NULL, "fake", 1, "section", 1,
"OpenFile", 0, "/proc/1/ns/mnt::read-only",
&of, u);
assert_se(r >= 0);
assert_se(of);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "mnt"));
assert_se(of->flags == OPENFILE_READ_ONLY);
r = config_parse_open_file(NULL, "fake", 1, "section", 1,
"OpenFile", 0, "",
&of, u);
assert_se(r >= 0);
assert_se(!of);
}
static int intro(void) {
if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)
return log_tests_skipped("cgroupfs not available");
+185
View File
@@ -0,0 +1,185 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "open-file.h"
#include "string-util.h"
#include "tests.h"
TEST(open_file_parse) {
_cleanup_(open_file_freep) OpenFile *of = NULL;
int r;
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only", &of);
assert_se(r >= 0);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "host-mount-namespace"));
assert_se(of->flags == OPENFILE_READ_ONLY);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt", &of);
assert_se(r >= 0);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "mnt"));
assert_se(of->flags == 0);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace", &of);
assert_se(r >= 0);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "host-mount-namespace"));
assert_se(of->flags == 0);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt::read-only", &of);
assert_se(r >= 0);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "mnt"));
assert_se(of->flags == OPENFILE_READ_ONLY);
of = open_file_free(of);
r = open_file_parse("../file.dat:file:read-only", &of);
assert_se(r == -EINVAL);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:rw", &of);
assert_se(r == -EINVAL);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:append", &of);
assert_se(r >= 0);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "host-mount-namespace"));
assert_se(of->flags == OPENFILE_APPEND);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:truncate", &of);
assert_se(r >= 0);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "host-mount-namespace"));
assert_se(of->flags == OPENFILE_TRUNCATE);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,append", &of);
assert_se(r == -EINVAL);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,truncate", &of);
assert_se(r == -EINVAL);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:append,truncate", &of);
assert_se(r == -EINVAL);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,read-only", &of);
assert_se(r == -EINVAL);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:graceful", &of);
assert_se(r >= 0);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "host-mount-namespace"));
assert_se(of->flags == OPENFILE_GRACEFUL);
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only,graceful", &of);
assert_se(r >= 0);
assert_se(streq(of->path, "/proc/1/ns/mnt"));
assert_se(streq(of->fdname, "host-mount-namespace"));
assert_se(of->flags == (OPENFILE_READ_ONLY | OPENFILE_GRACEFUL));
of = open_file_free(of);
r = open_file_parse("/proc/1/ns/mnt:host-mount-namespace:read-only:other", &of);
assert_se(r == -EINVAL);
}
TEST(open_file_to_string) {
_cleanup_free_ char *s = NULL;
_cleanup_(open_file_freep) OpenFile *of = NULL;
int r;
assert_se(of = new (OpenFile, 1));
*of = (OpenFile){ .path = strdup("/proc/1/ns/mnt"),
.fdname = strdup("host-mount-namespace"),
.flags = OPENFILE_READ_ONLY };
r = open_file_to_string(of, &s);
assert_se(r >= 0);
assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:read-only"));
s = mfree(s);
of->flags = OPENFILE_APPEND;
r = open_file_to_string(of, &s);
assert_se(r >= 0);
assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:append"));
s = mfree(s);
of->flags = OPENFILE_TRUNCATE;
r = open_file_to_string(of, &s);
assert_se(r >= 0);
assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:truncate"));
s = mfree(s);
of->flags = OPENFILE_GRACEFUL;
r = open_file_to_string(of, &s);
assert_se(r >= 0);
assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:graceful"));
s = mfree(s);
of->flags = OPENFILE_READ_ONLY | OPENFILE_GRACEFUL;
r = open_file_to_string(of, &s);
assert_se(r >= 0);
assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace:read-only,graceful"));
s = mfree(s);
of->flags = 0;
r = open_file_to_string(of, &s);
assert_se(r >= 0);
assert_se(streq(s, "/proc/1/ns/mnt:host-mount-namespace"));
s = mfree(s);
assert_se(free_and_strdup(&of->fdname, "mnt"));
of->flags = OPENFILE_READ_ONLY;
r = open_file_to_string(of, &s);
assert_se(r >= 0);
assert_se(streq(s, "/proc/1/ns/mnt::read-only"));
s = mfree(s);
assert_se(free_and_strdup(&of->path, "/path:with:colon"));
assert_se(free_and_strdup(&of->fdname, "path:with:colon"));
of->flags = 0;
r = open_file_to_string(of, &s);
assert_se(r >= 0);
assert_se(streq(s, "/path\\:with\\:colon"));
}
DEFINE_TEST_MAIN(LOG_INFO);