tests: Rename caution -> tests/qemu.
[libguestfs.git] / capitests / test-private-data.c
1 /* libguestfs
2  * Copyright (C) 2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /* Test aspects of the private data area API. */
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <assert.h>
28
29 #include "guestfs.h"
30
31 #define PREFIX "test_"
32
33 static size_t close_callback_called = 0;
34
35 /* This callback deletes all test keys in the handle. */
36 static void
37 close_callback (guestfs_h *g,
38                 void *opaque,
39                 uint64_t event,
40                 int event_handle,
41                 int flags,
42                 const char *buf, size_t buf_len,
43                 const uint64_t *array, size_t array_len)
44 {
45   const char *key;
46   void *data;
47
48   close_callback_called++;
49
50  again:
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);
55       goto again;
56     }
57     data = guestfs_next_private (g, &key);
58   }
59 }
60
61 int
62 main (int argc, char *argv[])
63 {
64   guestfs_h *g;
65   const char *key;
66   void *data;
67   size_t count;
68
69   g = guestfs_create ();
70   if (g == NULL) {
71     fprintf (stderr, "failed to create handle\n");
72     exit (EXIT_FAILURE);
73   }
74
75   if (guestfs_set_event_callback (g, close_callback, GUESTFS_EVENT_CLOSE,
76                                   0, NULL) == -1)
77     exit (EXIT_FAILURE);
78
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 */
83
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);
89
90   /* Check we can count keys by iterating. */
91   count = 0;
92   data = guestfs_first_private (g, &key);
93   while (data != NULL) {
94     if (strncmp (key, PREFIX, strlen (PREFIX)) == 0)
95       count++;
96     data = guestfs_next_private (g, &key);
97   }
98   assert (count == 3);
99
100   /* Delete some keys. */
101   guestfs_set_private (g, PREFIX "a", NULL);
102   guestfs_set_private (g, PREFIX "b", NULL);
103
104   /* Count them again. */
105   count = 0;
106   data = guestfs_first_private (g, &key);
107   while (data != NULL) {
108     if (strncmp (key, PREFIX, strlen (PREFIX)) == 0)
109       count++;
110     data = guestfs_next_private (g, &key);
111   }
112   assert (count == 1);
113
114   /* Closing should implicitly call the close_callback function. */
115   guestfs_close (g);
116
117   assert (close_callback_called == 1);
118
119   exit (EXIT_SUCCESS);
120 }