summaryrefslogtreecommitdiffstats
path: root/tests/c-api
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-10-15 16:35:13 +0100
committerRichard W.M. Jones <rjones@redhat.com>2012-10-15 16:35:13 +0100
commitc164cc9bea89aa627dad4338469c26a1a70b61f5 (patch)
tree53bd350652d7d2adf4b2ed897ff02d9b062ea384 /tests/c-api
parentec1d2398722fbebd6dd3daa205d0dd3ca653fe8d (diff)
downloadlibguestfs-c164cc9bea89aa627dad4338469c26a1a70b61f5.tar.gz
libguestfs-c164cc9bea89aa627dad4338469c26a1a70b61f5.tar.xz
libguestfs-c164cc9bea89aa627dad4338469c26a1a70b61f5.zip
lib: Add a test for environment parsing functions.
This updates commit 9466060201600db47016133d80af22eb38091a49.
Diffstat (limited to 'tests/c-api')
-rw-r--r--tests/c-api/Makefile.am16
-rw-r--r--tests/c-api/test-environment.c93
2 files changed, 107 insertions, 2 deletions
diff --git a/tests/c-api/Makefile.am b/tests/c-api/Makefile.am
index 65fc9f24..7adedeb7 100644
--- a/tests/c-api/Makefile.am
+++ b/tests/c-api/Makefile.am
@@ -33,7 +33,8 @@ check_PROGRAMS = \
test-last-errno \
test-private-data \
test-user-cancel \
- test-debug-to-file
+ test-debug-to-file \
+ test-environment
TESTS = \
tests \
@@ -44,7 +45,8 @@ TESTS = \
test-last-errno \
test-private-data \
test-user-cancel \
- test-debug-to-file
+ test-debug-to-file \
+ test-environment
if HAVE_CXX
check_PROGRAMS += test-just-header-cxx
@@ -147,6 +149,16 @@ test_debug_to_file_LDADD = \
$(top_builddir)/src/libguestfs.la \
$(top_builddir)/gnulib/lib/libgnu.la
+test_environment_SOURCES = test-environment.c
+test_environment_CFLAGS = \
+ -I$(top_srcdir)/src -I$(top_builddir)/src \
+ -I$(top_srcdir)/gnulib/lib \
+ -I$(top_builddir)/gnulib/lib \
+ $(WARN_CFLAGS) $(WERROR_CFLAGS)
+test_environment_LDADD = \
+ $(top_builddir)/src/libguestfs.la \
+ $(top_builddir)/gnulib/lib/libgnu.la
+
#if HAVE_LIBVIRT
#test_add_libvirt_dom_SOURCES = test-add-libvirt-dom.c
#test_add_libvirt_dom_CFLAGS = \
diff --git a/tests/c-api/test-environment.c b/tests/c-api/test-environment.c
new file mode 100644
index 00000000..3c3ed8a4
--- /dev/null
+++ b/tests/c-api/test-environment.c
@@ -0,0 +1,93 @@
+/* libguestfs
+ * Copyright (C) 2012 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/* Test how libguestfs uses the environment, guestfs_create_flags,
+ * guestfs_parse_environment, guestfs_parse_environment_list.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <error.h>
+#include <assert.h>
+
+#include "guestfs.h"
+
+int
+main (int argc, char *argv[])
+{
+ guestfs_h *g;
+ int r, default_memsize;
+
+ /* What's the default memsize? */
+ g = guestfs_create ();
+ if (!g) error (EXIT_FAILURE, errno, "guestfs_create");
+ default_memsize = guestfs_get_memsize (g);
+ if (default_memsize == -1) exit (EXIT_FAILURE);
+ guestfs_close (g);
+
+ /* Check that guestfs_create parses the environment. */
+ setenv ("LIBGUESTFS_MEMSIZE", "799", 1);
+ g = guestfs_create ();
+ if (!g) error (EXIT_FAILURE, errno, "guestfs_create");
+ assert (guestfs_get_memsize (g) == 799);
+ guestfs_close (g);
+
+ /* Check that guestfs_create_flags with no flags parses the environment. */
+ setenv ("LIBGUESTFS_MEMSIZE", "798", 1);
+ g = guestfs_create_flags (0);
+ if (!g) error (EXIT_FAILURE, errno, "guestfs_create_flags");
+ assert (guestfs_get_memsize (g) == 798);
+ guestfs_close (g);
+
+ /* Check guestfs_create_flags + explicit guestfs_parse_environment. */
+ setenv ("LIBGUESTFS_MEMSIZE", "797", 1);
+ g = guestfs_create_flags (GUESTFS_CREATE_NO_ENVIRONMENT);
+ assert (guestfs_get_memsize (g) == default_memsize);
+ if (!g) error (EXIT_FAILURE, errno, "guestfs_create_flags");
+ setenv ("LIBGUESTFS_MEMSIZE", "796", 1);
+ r = guestfs_parse_environment (g);
+ if (r == -1) exit (EXIT_FAILURE);
+ assert (guestfs_get_memsize (g) == 796);
+ guestfs_close (g);
+
+ /* Check guestfs_parse_environment_list. */
+ setenv ("LIBGUESTFS_MEMSIZE", "795", 1);
+ g = guestfs_create_flags (GUESTFS_CREATE_NO_ENVIRONMENT);
+ assert (guestfs_get_memsize (g) == default_memsize);
+ if (!g) error (EXIT_FAILURE, errno, "guestfs_create_flags");
+ setenv ("LIBGUESTFS_MEMSIZE", "794", 1);
+ const char *local_environment[] = {
+ "LIBGUESTFS_MEMSIZE=793",
+ "LIBGUESTFS_MEMSIZE_NOT_REALLY_A_VARIABLE=1",
+ "FOO=bar",
+ "HOME=/homes",
+ "BLAH",
+ NULL
+ };
+ r = guestfs_parse_environment_list (g, (char **) local_environment);
+ if (r == -1) exit (EXIT_FAILURE);
+ assert (guestfs_get_memsize (g) == 793);
+ guestfs_close (g);
+
+ exit (EXIT_SUCCESS);
+}