]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/tests/containertests.c
drop broken lxc-test-fuzzers
[mirror_lxc.git] / src / tests / containertests.c
index 8d1658fab4e7d8f1c9adff24cd5e2075107f4ebc..6da622f77d04bcbf7854fb91c02f55a3cda05ca3 100644 (file)
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
-#include "../lxc/lxccontainer.h"
+
+#include "config.h"
+
+#include <lxc/lxccontainer.h>
 
 #include <unistd.h>
 #include <signal.h>
@@ -25,7 +28,8 @@
 #include <sys/wait.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <lxc/state.h>
+#include <string.h>
+#include "state.h"
 
 #define MYNAME "lxctest1"
 
@@ -38,26 +42,29 @@ static int destroy_busybox(void)
                perror("fork");
                return -1;
        }
+
        if (pid == 0) {
-               ret = execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
-               // Should not return
-               perror("execl");
-               exit(1);
+               execlp("lxc-destroy", "lxc-destroy", "-f", "-n", MYNAME, NULL);
+               exit(EXIT_FAILURE);
        }
+
 again:
        ret = waitpid(pid, &status, 0);
        if (ret == -1) {
-               if (errno == -EINTR)
+               if (errno == EINTR)
                        goto again;
                perror("waitpid");
                return -1;
        }
+
        if (ret != pid)
                goto again;
+
        if (!WIFEXITED(status))  { // did not exit normally
                fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
                return -1;
        }
+
        return WEXITSTATUS(status);
 }
 
@@ -70,26 +77,29 @@ static int create_busybox(void)
                perror("fork");
                return -1;
        }
+
        if (pid == 0) {
-               ret = execlp("lxc-create", "lxc-create", "-t", "busybox", "-f", LXC_DEFAULT_CONFIG, "-n", MYNAME, NULL);
-               // Should not return
-               perror("execl");
-               exit(1);
+               execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL);
+               exit(EXIT_FAILURE);
        }
+
 again:
        ret = waitpid(pid, &status, 0);
        if (ret == -1) {
-               if (errno == -EINTR)
+               if (errno == EINTR)
                        goto again;
                perror("waitpid");
                return -1;
        }
+
        if (ret != pid)
                goto again;
+
        if (!WIFEXITED(status))  { // did not exit normally
                fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__);
                return -1;
        }
+
        return WEXITSTATUS(status);
 }
 
@@ -102,47 +112,63 @@ int main(int argc, char *argv[])
        char *str;
 
        ret = 1;
+
        /* test refcounting */
        c = lxc_container_new(MYNAME, NULL);
        if (!c) {
                fprintf(stderr, "%d: error creating lxc_container %s\n", __LINE__, MYNAME);
                goto out;
        }
+
        if (!lxc_container_get(c)) {
                fprintf(stderr, "%d: error getting refcount\n", __LINE__);
                goto out;
        }
+
        /* peek in, inappropriately, make sure refcount is a we'd like */
        if (c->numthreads != 2) {
                fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 2);
                goto out;
        }
+
        if (strcmp(c->name, MYNAME) != 0) {
                fprintf(stderr, "%d: container has wrong name (%s not %s)\n", __LINE__, c->name, MYNAME);
                goto out;
        }
+
        str = c->config_file_name(c);
 #define CONFIGFNAM LXCPATH "/" MYNAME "/config"
-       if (!str || strcmp(str, CONFIGFNAM)) {
+       if (str && strcmp(str, CONFIGFNAM)) {
                fprintf(stderr, "%d: got wrong config file name (%s, not %s)\n", __LINE__, str, CONFIGFNAM);
                goto out;
        }
        free(str);
        free(c->configfile);
        c->configfile = NULL;
+
        str = c->config_file_name(c);
        if (str) {
                fprintf(stderr, "%d: config file name was not NULL as it should have been\n", __LINE__);
                goto out;
        }
-       if (lxc_container_put(c) != 0) {
+
+       ret = lxc_container_put(c);
+       if (ret < 0) {
+               fprintf(stderr, "%d: c is invalid pointer\n", __LINE__);
+               ret = 1;
+               goto out;
+       }
+       else if (ret == 1) {
                fprintf(stderr, "%d: c was freed on non-final put\n", __LINE__);
+               c = NULL;
                goto out;
        }
+
        if (c->numthreads != 1) {
                fprintf(stderr, "%d: refcount is %d, not %d\n", __LINE__, c->numthreads, 1);
                goto out;
        }
+
        if (lxc_container_put(c) != 1) {
                fprintf(stderr, "%d: c was not freed on final put\n", __LINE__);
                goto out;
@@ -156,11 +182,6 @@ int main(int argc, char *argv[])
                goto out;
        }
 
-       if (c->lxc_conf != NULL) {
-               fprintf(stderr, "%d: lxc_conf is not NULL as it should be\n", __LINE__);
-               ret = 1;
-               goto out;
-       }
        b = c->is_defined(c);
        if (b) {
                fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
@@ -210,22 +231,18 @@ int main(int argc, char *argv[])
                fprintf(stderr, "%d: lxc_get_wait_states gave %d not %d\n", __LINE__, numstates, MAX_STATE);
                goto out;
        }
+
        const char **sstr = malloc(numstates * sizeof(const char *));
        numstates = lxc_get_wait_states(sstr);
        int i;
+
        for (i=0; i<numstates; i++) {
                fprintf(stderr, "got state %d %s\n", i, sstr[i]);
        }
        free(sstr);
 
-       printf("hit return to start container");
-       char mychar;
-       ret = scanf("%c", &mychar);
-       if (ret < 0)
-               goto out;
-
        /* non-daemonized is tested in 'startone' */
-       c->want_daemonize(c);
+       c->want_daemonize(c, true);
        if (!c->startl(c, 0, NULL, NULL)) {
                fprintf(stderr, "%d: %s failed to start daemonized\n", __LINE__, c->name);
                goto out;
@@ -243,12 +260,6 @@ int main(int argc, char *argv[])
                goto out;
        }
 
-       printf("hit return to finish");
-       ret = scanf("%c", &mychar);
-       if (ret < 0)
-               goto out;
-
-
        fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
        ret = 0;
 
@@ -256,7 +267,8 @@ out:
        if (c) {
                c->stop(c);
                destroy_busybox();
+               lxc_container_put(c);
        }
-       lxc_container_put(c);
+
        exit(ret);
 }