]> git.proxmox.com Git - mirror_lxc.git/commitdiff
tests: add unit tests for idmap parser
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 1 Jun 2017 21:43:34 +0000 (23:43 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 2 Jun 2017 22:41:56 +0000 (00:41 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c
src/tests/parse_config_file.c

index c119886672cc2fba99052ede98effdc857d99937..ef0693967d546e58789b8cb3c7e557c407441e78 100644 (file)
@@ -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 "
index 50937061565e6abd47f187e7ea39b09da48f3790..27df87af1c183fe77115beb7c29010560fab12bd 100644 (file)
@@ -28,6 +28,7 @@
 #include <errno.h>
 #include <string.h>
 
+#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);