xref: /openbmc/qemu/tests/unit/test-xs-node.c (revision 3ef7ff83caa27d8b3bfc76805cd47bc97d23b7d7)
1 /*
2  * QEMU XenStore XsNode testing
3  *
4  * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "qemu/module.h"
13 
14 static int nr_xs_nodes;
15 static GList *xs_node_list;
16 
17 #define XS_NODE_UNIT_TEST
18 
19 /*
20  * We don't need the core Xen definitions. And we *do* want to be able
21  * to run the unit tests even on architectures that Xen doesn't support
22  * (because life's too short to bother doing otherwise, and test coverage
23  * doesn't hurt).
24  */
25 #define __XEN_PUBLIC_XEN_H__
26 
27 #include "hw/i386/kvm/xenstore_impl.c"
28 
29 #define DOMID_QEMU 0
30 #define DOMID_GUEST 1
31 
32 /* This doesn't happen in qemu but we want to make valgrind happy */
33 static void xs_impl_delete(XenstoreImplState *s)
34 {
35     int err;
36 
37     err = xs_impl_rm(s, DOMID_QEMU, XBT_NULL, "/local");
38     g_assert(!err);
39     g_assert(s->nr_nodes == 1);
40 
41     xs_node_unref(s->root);
42     g_free(s);
43 
44     if (xs_node_list) {
45         GList *l;
46         for (l = xs_node_list; l; l = l->next) {
47             XsNode *n = l->data;
48             printf("Remaining node at %p name %s ref %u\n", n, n->name,
49                    n->ref);
50         }
51     }
52     g_assert(!nr_xs_nodes);
53 }
54 
55 static int write_str(XenstoreImplState *s, unsigned int dom_id,
56                           unsigned int tx_id, const char *path,
57                           const char *content)
58 {
59     GByteArray *d = g_byte_array_new();
60     int err;
61 
62     g_byte_array_append(d, (void *)content, strlen(content));
63     err = xs_impl_write(s, dom_id, tx_id, path, d);
64     g_byte_array_unref(d);
65     return err;
66 }
67 
68 static XenstoreImplState *setup(void)
69 {
70    XenstoreImplState *s = xs_impl_create();
71    char *abspath;
72    int err;
73 
74    abspath = g_strdup_printf("/local/domain/%u", DOMID_GUEST);
75 
76    err = write_str(s, DOMID_QEMU, XBT_NULL, abspath, "");
77    g_assert(!err);
78 
79    g_free(abspath);
80 
81    abspath = g_strdup_printf("/local/domain/%u/some", DOMID_GUEST);
82 
83    err = write_str(s, DOMID_QEMU, XBT_NULL, abspath, "");
84    g_assert(!err);
85    g_assert(s->nr_nodes == 5);
86 
87    g_free(abspath);
88 
89    return s;
90 }
91 
92 static void test_xs_node_simple(void)
93 {
94     GByteArray *data = g_byte_array_new();
95     XenstoreImplState *s = setup();
96     GList *items = NULL;
97     XsNode *old_root;
98     uint64_t gencnt;
99     int err;
100 
101     g_assert(s);
102 
103     /* Read gives ENOENT when it should */
104     err = xs_impl_read(s, DOMID_GUEST, XBT_NULL, "foo", data);
105     g_assert(err == ENOENT);
106 
107     /* Write works */
108     err = write_str(s, DOMID_GUEST, XBT_NULL, "some/relative/path",
109                     "something");
110     g_assert(s->nr_nodes == 7);
111     g_assert(!err);
112 
113     /* Read gives back what we wrote */
114     err = xs_impl_read(s, DOMID_GUEST, XBT_NULL, "some/relative/path", data);
115     g_assert(!err);
116     g_assert(data->len == strlen("something"));
117     g_assert(!memcmp(data->data, "something", data->len));
118 
119     /* Even if we use an abolute path */
120     g_byte_array_set_size(data, 0);
121     err = xs_impl_read(s, DOMID_GUEST, XBT_NULL,
122                        "/local/domain/1/some/relative/path", data);
123     g_assert(!err);
124     g_assert(data->len == strlen("something"));
125 
126     /* Keep a copy, to force COW mode */
127     old_root = xs_node_ref(s->root);
128 
129     /* Write works again */
130     err = write_str(s, DOMID_GUEST, XBT_NULL,
131                     "/local/domain/1/some/relative/path2",
132                     "something else");
133     g_assert(!err);
134     g_assert(s->nr_nodes == 8);
135 
136     /* Overwrite an existing node */
137     err = write_str(s, DOMID_GUEST, XBT_NULL, "some/relative/path",
138                     "another thing");
139     g_assert(!err);
140     g_assert(s->nr_nodes == 8);
141 
142     /* We can list the two files we wrote */
143     err = xs_impl_directory(s, DOMID_GUEST, XBT_NULL, "some/relative", &gencnt,
144                             &items);
145     g_assert(!err);
146     g_assert(items);
147     g_assert(gencnt == 2);
148     g_assert(!strcmp(items->data, "path"));
149     g_assert(items->next);
150     g_assert(!strcmp(items->next->data, "path2"));
151     g_assert(!items->next->next);
152     g_list_free_full(items, g_free);
153 
154     /* Write somewhere else which already existed */
155     err = write_str(s, DOMID_GUEST, XBT_NULL, "some/relative", "moredata");
156     g_assert(!err);
157 
158     g_byte_array_set_size(data, 0);
159     err = xs_impl_read(s, DOMID_GUEST, XBT_NULL, "some/relative", data);
160     g_assert(!err);
161     g_assert(data->len == strlen("moredata"));
162     g_assert(!memcmp(data->data, "moredata", data->len));
163 
164     /* Overwrite existing data */
165     err = write_str(s, DOMID_GUEST, XBT_NULL, "some/relative", "otherdata");
166     g_assert(!err);
167 
168     g_byte_array_set_size(data, 0);
169     err = xs_impl_read(s, DOMID_GUEST, XBT_NULL, "some/relative", data);
170     g_assert(!err);
171     g_assert(data->len == strlen("otherdata"));
172     g_assert(!memcmp(data->data, "otherdata", data->len));
173 
174     /* Remove the subtree */
175     err = xs_impl_rm(s, DOMID_GUEST, XBT_NULL, "some/relative");
176     g_assert(!err);
177     g_assert(s->nr_nodes == 5);
178 
179     g_byte_array_set_size(data, 0);
180     err = xs_impl_read(s, DOMID_GUEST, XBT_NULL, "some/relative", data);
181     g_assert(err == ENOENT);
182     g_byte_array_unref(data);
183 
184     xs_node_unref(old_root);
185     xs_impl_delete(s);
186 }
187 
188 
189 int main(int argc, char **argv)
190 {
191     g_test_init(&argc, &argv, NULL);
192     module_call_init(MODULE_INIT_QOM);
193 
194     g_test_add_func("/xs_node/simple", test_xs_node_simple);
195 
196     return g_test_run();
197 }
198