libmount: add optlist tests

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2022-06-27 19:40:38 +02:00
parent add9d45821
commit 45c1fe0c1b
5 changed files with 173 additions and 8 deletions

View File

@ -173,7 +173,7 @@ static inline void ul_debug_print_masks(
for (d = flagnames; d && d->name; d++) {
if (!d->help)
continue;
fprintf(stderr, " %-8s [0x%04x] : %s\n",
fprintf(stderr, " %-8s [0x%06x] : %s\n",
d->name, d->mask, d->help);
}
}

View File

@ -87,6 +87,7 @@ check_PROGRAMS += \
test_mount_cache \
test_mount_lock \
test_mount_optstr \
test_mount_optlist \
test_mount_tab \
test_mount_tab_diff \
test_mount_tab_update \
@ -134,6 +135,11 @@ test_mount_optstr_CFLAGS = $(libmount_tests_cflags)
test_mount_optstr_LDFLAGS = $(libmount_tests_ldflags)
test_mount_optstr_LDADD = $(libmount_tests_ldadd)
test_mount_optlist_SOURCES = libmount/src/optlist.c
test_mount_optlist_CFLAGS = $(libmount_tests_cflags)
test_mount_optlist_LDFLAGS = $(libmount_tests_ldflags)
test_mount_optlist_LDADD = $(libmount_tests_ldadd)
test_mount_tab_SOURCES = libmount/src/tab.c
test_mount_tab_CFLAGS = $(libmount_tests_cflags)
test_mount_tab_LDFLAGS = $(libmount_tests_ldflags)

View File

@ -33,6 +33,7 @@ UL_DEBUG_DEFINE_MASKNAMES(libmount) =
{ "locks", MNT_DEBUG_LOCKS, "mtab and utab locking" },
{ "loop", MNT_DEBUG_LOOP, "loop devices routines" },
{ "options", MNT_DEBUG_OPTIONS, "mount options parsing" },
{ "optlist", MNT_DEBUG_OPTLIST, "mount options container" },
{ "tab", MNT_DEBUG_TAB, "fstab, mtab, mountinfo routines" },
{ "update", MNT_DEBUG_UPDATE, "mtab, utab updates" },
{ "utils", MNT_DEBUG_UTILS, "misc library utils" },
@ -68,7 +69,7 @@ void mnt_init_debug(int mask)
mnt_get_library_version(&ver);
mnt_get_library_features(&features);
DBG(INIT, ul_debug("library debug mask: 0x%04x", libmount_debug_mask));
DBG(INIT, ul_debug("library debug mask: 0x%06x", libmount_debug_mask));
DBG(INIT, ul_debug("library version: %s", ver));
p = features;
while (p && *p)

View File

@ -51,7 +51,7 @@
#define MNT_DEBUG_HOOK (1 << 15)
#define MNT_DEBUG_OPTLIST (1 << 16)
#define MNT_DEBUG_ALL 0xFFFF
#define MNT_DEBUG_ALL 0xFFFFFF
UL_DEBUG_DECLARE_MASK(libmount);
#define DBG(m, x) __UL_DBG(libmount, MNT_DEBUG_, m, x)

View File

@ -52,7 +52,8 @@ struct libmnt_optlist {
struct libmnt_optlist *mnt_new_optlist(void)
{
struct libmnt_optlist *ls = calloc(1, sizeof(*ls));
if (ls)
if (!ls)
return NULL;
ls->refcount = 1;
@ -114,6 +115,7 @@ int mnt_optlist_remove_opt(struct libmnt_optlist *ls, struct libmnt_opt *opt)
if (!opt)
return -EINVAL;
DBG(OPTLIST, ul_debugobj(ls, " remove %s", opt->name));
if (opt->map && opt->ent
&& opt->map == ls->linux_map && opt->ent->id & MS_PROPAGATION)
ls->propagation &= ~opt->ent->id;
@ -309,7 +311,7 @@ static int optlist_add_flags(struct libmnt_optlist *ls, unsigned long flags,
if (!ls || !map)
return -EINVAL;
if (map && (rc = mnt_optlist_register_map(ls, map)))
if (map && (rc = mnt_optlist_register_map(ls, map)))
return rc;
for (ent = map; ent && ent->name; ent++) {
@ -346,7 +348,10 @@ static int optlist_add_flags(struct libmnt_optlist *ls, unsigned long flags,
int mnt_optlist_append_flags(struct libmnt_optlist *ls, unsigned long flags,
const struct libmnt_optmap *map)
{
DBG(OPTLIST, ul_debugobj(ls, "append %lx", flags));
if (!ls || !map)
return -EINVAL;
DBG(OPTLIST, ul_debugobj(ls, "append 0x%08lx", flags));
return optlist_add_flags(ls, flags, map, NULL);
}
@ -359,7 +364,7 @@ int mnt_optlist_set_flags(struct libmnt_optlist *ls, unsigned long flags,
if (!ls || !map)
return -EINVAL;
DBG(OPTLIST, ul_debugobj(ls, "set %lx", flags));
DBG(OPTLIST, ul_debugobj(ls, "set 0x%08lx", flags));
/* remove all previous options defined by flag */
list_for_each_safe(p, next, &ls->opts) {
@ -384,7 +389,7 @@ int mnt_optlist_insert_flags(struct libmnt_optlist *ls, unsigned long flags,
if (!ls || !map || !after || !after_map)
return -EINVAL;
DBG(OPTLIST, ul_debugobj(ls, "insert %lx (after %s)",
DBG(OPTLIST, ul_debugobj(ls, "insert 0x%08lx (after %s)",
flags, opt->ent ? opt->ent->name : "???"));
opt = mnt_optlist_get_opt(ls, after, after_map);
@ -476,3 +481,156 @@ int mnt_opt_has_value(struct libmnt_opt *opt)
{
return opt && opt->value;
}
#ifdef TEST_PROGRAM
#include "xalloc.h"
static int mk_optlist(struct libmnt_optlist **ol, const char *optstr)
{
int rc = 0;
*ol = mnt_new_optlist();
if (!*ol)
rc = -ENOMEM;
if (!rc)
rc = mnt_optlist_register_map(*ol, mnt_get_builtin_optmap(MNT_LINUX_MAP));
if (!rc)
rc = mnt_optlist_register_map(*ol, mnt_get_builtin_optmap(MNT_USERSPACE_MAP));
if (!rc && optstr)
rc = mnt_optlist_append_optstr(*ol, optstr, NULL);
if (rc) {
mnt_unref_optlist(*ol);
*ol = NULL;
}
return rc;
}
static void dump_optlist(struct libmnt_optlist *ol)
{
struct libmnt_iter itr;
struct libmnt_opt *opt;
int i = 0;
mnt_reset_iter(&itr, MNT_ITER_FORWARD);
while (mnt_optlist_next_opt(ol, &itr, &opt) == 0) {
if (opt->ent)
printf("#%02d [%p:0x%08x] name:'%s',\tvalue:'%s'\n",
++i, opt->map, opt->ent->id, opt->name, opt->value);
else
printf("#%02d [ unknown ] name:'%s',\tvalue:'%s'\n",
++i, opt->name, opt->value);
}
}
static const struct libmnt_optmap *get_map(const char *name)
{
if (name && strcmp(name, "linux") == 0)
return mnt_get_builtin_optmap(MNT_LINUX_MAP);
if (name && strcmp(name, "user") == 0)
return mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
return NULL;
}
static inline unsigned long str2flg(const char *str)
{
return (unsigned long) strtox64_or_err(str, "connt convert string to flags");
}
static int test_append_str(struct libmnt_test *ts, int argc, char *argv[])
{
struct libmnt_optlist *ol;
int rc;
if (argc < 3)
return -EINVAL;
rc = mk_optlist(&ol, argv[1]);
if (!rc)
rc = mnt_optlist_append_optstr(ol, argv[2], get_map(argv[3]));
if (!rc)
dump_optlist(ol);
mnt_unref_optlist(ol);
return rc;
}
static int test_prepend_str(struct libmnt_test *ts, int argc, char *argv[])
{
struct libmnt_optlist *ol;
int rc;
if (argc < 3)
return -EINVAL;
rc = mk_optlist(&ol, argv[1]);
if (!rc)
rc = mnt_optlist_prepend_optstr(ol, argv[2], get_map(argv[3]));
if (!rc)
dump_optlist(ol);
mnt_unref_optlist(ol);
return rc;
}
static int test_set_str(struct libmnt_test *ts, int argc, char *argv[])
{
struct libmnt_optlist *ol;
int rc;
if (argc < 3)
return -EINVAL;
rc = mk_optlist(&ol, argv[1]);
if (!rc)
rc = mnt_optlist_set_optstr(ol, argv[2], get_map(argv[3]));
if (!rc)
dump_optlist(ol);
mnt_unref_optlist(ol);
return rc;
}
static int test_append_flg(struct libmnt_test *ts, int argc, char *argv[])
{
struct libmnt_optlist *ol;
int rc;
if (argc < 4)
return -EINVAL;
rc = mk_optlist(&ol, argv[1]);
if (!rc)
rc = mnt_optlist_append_flags(ol, str2flg(argv[2]), get_map(argv[3]));
if (!rc)
dump_optlist(ol);
mnt_unref_optlist(ol);
return rc;
}
static int test_set_flg(struct libmnt_test *ts, int argc, char *argv[])
{
struct libmnt_optlist *ol;
int rc;
if (argc < 4)
return -EINVAL;
rc = mk_optlist(&ol, argv[1]);
if (!rc)
rc = mnt_optlist_set_flags(ol, str2flg(argv[2]), get_map(argv[3]));
if (!rc)
dump_optlist(ol);
mnt_unref_optlist(ol);
return rc;
}
int main(int argc, char *argv[])
{
struct libmnt_test tss[] = {
{ "--append-str", test_append_str, "<list> <str> [linux|user] append to the list" },
{ "--prepend-str", test_prepend_str, "<list> <str> [linux|user] prepend to the list" },
{ "--set-str", test_set_str, "<list> <str> [linux|user] set to the list" },
{ "--append-flg", test_append_flg, "<list> <flg> linux|user append to the list" },
{ "--set-flg", test_set_flg, "<list> <flg> linux|user set to the list" },
{ NULL }
};
return mnt_run_test(tss, argc, argv);
}
#endif /* TEST_PROGRAM */