From: Christian Brauner Date: Thu, 1 Jun 2017 21:43:34 +0000 (+0200) Subject: tests: add unit tests for idmap parser X-Git-Tag: lxc-2.1.1~172^2~2 X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=77803ee7a609491367fcf5608aca059839e00725;p=mirror_lxc.git tests: add unit tests for idmap parser Signed-off-by: Christian Brauner --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index c11988667..ef0693967 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -4903,7 +4903,8 @@ int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data) /* idmap will now keep track of that memory. */ container_root_gid = NULL; - if (lxc_log_get_level() == LXC_LOG_PRIORITY_TRACE) { + if (lxc_log_get_level() == LXC_LOG_PRIORITY_TRACE || + conf->loglevel == LXC_LOG_PRIORITY_TRACE) { lxc_list_for_each(it, idmap) { map = it->elem; TRACE("establishing %cid mapping for \"%d\" in new " diff --git a/src/tests/parse_config_file.c b/src/tests/parse_config_file.c index 509370615..27df87af1 100644 --- a/src/tests/parse_config_file.c +++ b/src/tests/parse_config_file.c @@ -28,6 +28,7 @@ #include #include +#include "confile_utils.h" #include "lxc/state.h" #include "lxctest.h" @@ -103,6 +104,57 @@ static int set_get_compare_clear_save_load(struct lxc_container *c, return 0; } +int test_idmap_parser(void) +{ + size_t i; + struct idmap_check { + bool is_valid; + const char *idmap; + }; + static struct idmap_check idmaps[] = { + /* valid idmaps */ + { true, "u 0 0 1" }, + { true, "g 0 0 1" }, + { true, "u 1 100001 999999999" }, + { true, "g 1 100001 999999999" }, + { true, "u 0 0 0" }, + { true, "g 0 0 0" }, + { true, "u 1000 165536 65536" }, + { true, "g 999 999 1" }, + { true, "u 0 5000 100000" }, + { true, "g 577 789 5" }, + { true, "u 65536 65536 1 " }, + /* invalid idmaps */ + { false, "1u 0 0 0" }, + { false, "1g 0 0 0a" }, + { false, "1 u 0 0 0" }, + { false, "1g 0 0 0 1" }, + { false, "1u a0 b0 c0 d1" }, + { false, "1g 0 b0 0 d1" }, + { false, "1u a0 0 c0 1" }, + { false, "g -1 0 -10" }, + { false, "a 1 0 10" }, + { false, "u 1 1 0 10" }, + { false, "g 1 0 10 z " }, + }; + + for (i = 0; i < sizeof(idmaps) / sizeof(struct idmap_check); i++) { + unsigned long hostid, nsid, range; + char type; + int ret; + ret = parse_idmaps(idmaps[i].idmap, &type, &nsid, &hostid, + &range); + if ((ret < 0 && idmaps[i].is_valid) || + (ret == 0 && !idmaps[i].is_valid)) { + lxc_error("failed to parse idmap \"%s\"\n", + idmaps[i].idmap); + return -1; + } + } + + return 0; +} + int main(int argc, char *argv[]) { struct lxc_container *c; @@ -489,6 +541,11 @@ int main(int argc, char *argv[]) goto non_test_error; } + if (test_idmap_parser() < 0) { + lxc_error("%s\n", "failed to test parser for \"lxc.id_map\""); + goto non_test_error; + } + ret = EXIT_SUCCESS; non_test_error: c->destroy(c);