1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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);
}
|