2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /* Test aspects of the private data area API. */
31 #define PREFIX "test_"
33 static size_t close_callback_called = 0;
35 /* This callback deletes all test keys in the handle. */
37 close_callback (guestfs_h *g,
42 const char *buf, size_t buf_len,
43 const uint64_t *array, size_t array_len)
48 close_callback_called++;
51 data = guestfs_first_private (g, &key);
52 while (data != NULL) {
53 if (strncmp (key, PREFIX, strlen (PREFIX)) == 0) {
54 guestfs_set_private (g, key, NULL);
57 data = guestfs_next_private (g, &key);
62 main (int argc, char *argv[])
69 g = guestfs_create ();
71 fprintf (stderr, "failed to create handle\n");
75 if (guestfs_set_event_callback (g, close_callback, GUESTFS_EVENT_CLOSE,
79 guestfs_set_private (g, PREFIX "a", (void *) 1);
80 guestfs_set_private (g, PREFIX "b", (void *) 2);
81 guestfs_set_private (g, PREFIX "c", (void *) 3);
82 guestfs_set_private (g, PREFIX "a", (void *) 4); /* overwrites previous */
84 /* Check we can fetch keys. */
85 assert (guestfs_get_private (g, PREFIX "a") == (void *) 4);
86 assert (guestfs_get_private (g, PREFIX "b") == (void *) 2);
87 assert (guestfs_get_private (g, PREFIX "c") == (void *) 3);
88 assert (guestfs_get_private (g, PREFIX "d") == NULL);
90 /* Check we can count keys by iterating. */
92 data = guestfs_first_private (g, &key);
93 while (data != NULL) {
94 if (strncmp (key, PREFIX, strlen (PREFIX)) == 0)
96 data = guestfs_next_private (g, &key);
100 /* Delete some keys. */
101 guestfs_set_private (g, PREFIX "a", NULL);
102 guestfs_set_private (g, PREFIX "b", NULL);
104 /* Count them again. */
106 data = guestfs_first_private (g, &key);
107 while (data != NULL) {
108 if (strncmp (key, PREFIX, strlen (PREFIX)) == 0)
110 data = guestfs_next_private (g, &key);
114 /* Closing should implicitly call the close_callback function. */
117 assert (close_callback_called == 1);