diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2011-03-14 13:19:47 +0000 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2011-03-15 12:16:50 +0000 |
commit | 6d6b7edd1102f8383643866bf358e494e0d518ef (patch) | |
tree | 9c889d805e3ab70655071336899d9f628cb577f9 /capitests | |
parent | d1f1f74e5f45fa5b94ebb096fa65fa33ecc23d09 (diff) | |
download | libguestfs-6d6b7edd1102f8383643866bf358e494e0d518ef.tar.gz libguestfs-6d6b7edd1102f8383643866bf358e494e0d518ef.tar.xz libguestfs-6d6b7edd1102f8383643866bf358e494e0d518ef.zip |
New APIs: guestfs_first_private, guestfs_next_private to walk over
the private data area.
This commit adds new APIs for walking over the keys and pointers in
the private data area associated with each handle (note this is only
applicable to the C API).
Diffstat (limited to 'capitests')
-rw-r--r-- | capitests/Makefile.am | 13 | ||||
-rw-r--r-- | capitests/test-private-data.c | 85 |
2 files changed, 96 insertions, 2 deletions
diff --git a/capitests/Makefile.am b/capitests/Makefile.am index 542c4fb9..83e62c8e 100644 --- a/capitests/Makefile.am +++ b/capitests/Makefile.am @@ -30,7 +30,8 @@ check_PROGRAMS = \ test-create-handle \ test-config \ test-add-drive-opts \ - test-last-errno + test-last-errno \ + test-private-data TESTS = \ tests \ @@ -38,7 +39,8 @@ TESTS = \ test-create-handle \ test-config \ test-add-drive-opts \ - test-last-errno + test-last-errno \ + test-private-data # The API behind this test is not baked yet. #if HAVE_LIBVIRT @@ -103,6 +105,13 @@ test_last_errno_CFLAGS = \ test_last_errno_LDADD = \ $(top_builddir)/src/libguestfs.la +test_private_data_SOURCES = test-private-data.c +test_private_data_CFLAGS = \ + -I$(top_srcdir)/src -I$(top_builddir)/src \ + $(WARN_CFLAGS) $(WERROR_CFLAGS) +test_private_data_LDADD = \ + $(top_builddir)/src/libguestfs.la + #if HAVE_LIBVIRT #test_add_libvirt_dom_SOURCES = test-add-libvirt-dom.c #test_add_libvirt_dom_CFLAGS = \ diff --git a/capitests/test-private-data.c b/capitests/test-private-data.c new file mode 100644 index 00000000..fad88b58 --- /dev/null +++ b/capitests/test-private-data.c @@ -0,0 +1,85 @@ +/* libguestfs + * Copyright (C) 2011 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 aspects of the private data area API. */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <assert.h> + +#include "guestfs.h" + +#define PREFIX "test_" + +int +main (int argc, char *argv[]) +{ + guestfs_h *g; + const char *key; + void *data; + size_t count; + + g = guestfs_create (); + if (g == NULL) { + fprintf (stderr, "failed to create handle\n"); + exit (EXIT_FAILURE); + } + + guestfs_set_private (g, PREFIX "a", (void *) 1); + guestfs_set_private (g, PREFIX "b", (void *) 2); + guestfs_set_private (g, PREFIX "c", (void *) 3); + guestfs_set_private (g, PREFIX "a", (void *) 4); /* overwrites previous */ + + /* Check we can fetch keys. */ + assert (guestfs_get_private (g, PREFIX "a") == (void *) 4); + assert (guestfs_get_private (g, PREFIX "b") == (void *) 2); + assert (guestfs_get_private (g, PREFIX "c") == (void *) 3); + assert (guestfs_get_private (g, PREFIX "d") == NULL); + + /* Check we can count keys by iterating. */ + count = 0; + data = guestfs_first_private (g, &key); + while (data != NULL) { + if (strncmp (key, PREFIX, strlen (PREFIX)) == 0) + count++; + data = guestfs_next_private (g, &key); + } + assert (count == 3); + + /* Delete some keys. */ + guestfs_set_private (g, PREFIX "a", NULL); + guestfs_set_private (g, PREFIX "b", NULL); + + /* Count them again. */ + count = 0; + data = guestfs_first_private (g, &key); + while (data != NULL) { + if (strncmp (key, PREFIX, strlen (PREFIX)) == 0) + count++; + data = guestfs_next_private (g, &key); + } + assert (count == 1); + + guestfs_close (g); + + exit (EXIT_SUCCESS); +} |