Add missing libxml2-tools dependency
[archive/platform/upstream/libvirt.git] / tests / xmconfigtest.c
1 /*
2  * xmconfigtest.c: Test backend for xm_internal config file handling
3  *
4  * Copyright (C) 2007, 2010-2011, 2014 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library.  If not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  * Author: Daniel P. Berrange <berrange@redhat.com>
21  *
22  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "internal.h"
31 #include "datatypes.h"
32 #include "xen/xen_driver.h"
33 #include "xen/xm_internal.h"
34 #include "xenxs/xen_xm.h"
35 #include "testutils.h"
36 #include "testutilsxen.h"
37 #include "viralloc.h"
38 #include "virstring.h"
39
40 #define VIR_FROM_THIS VIR_FROM_NONE
41
42 static virCapsPtr caps;
43 static virDomainXMLOptionPtr xmlopt;
44
45 static int
46 testCompareParseXML(const char *xmcfg, const char *xml, int xendConfigVersion)
47 {
48     char *xmlData = NULL;
49     char *xmcfgData = NULL;
50     char *gotxmcfgData = NULL;
51     virConfPtr conf = NULL;
52     int ret = -1;
53     virConnectPtr conn = NULL;
54     int wrote = 4096;
55     struct _xenUnifiedPrivate priv;
56     virDomainDefPtr def = NULL;
57
58     if (VIR_ALLOC_N(gotxmcfgData, wrote) < 0)
59         goto fail;
60
61     conn = virGetConnect();
62     if (!conn) goto fail;
63
64     if (virtTestLoadFile(xml, &xmlData) < 0)
65         goto fail;
66
67     if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
68         goto fail;
69
70     /* Many puppies died to bring you this code. */
71     priv.xendConfigVersion = xendConfigVersion;
72     priv.caps = caps;
73     conn->privateData = &priv;
74
75     if (!(def = virDomainDefParseString(xmlData, caps, xmlopt,
76                                         1 << VIR_DOMAIN_VIRT_XEN,
77                                         VIR_DOMAIN_XML_INACTIVE)))
78         goto fail;
79
80     if (!virDomainDefCheckABIStability(def, def)) {
81         fprintf(stderr, "ABI stability check failed on %s", xml);
82         goto fail;
83     }
84
85     if (!(conf = xenFormatXM(conn, def, xendConfigVersion)))
86         goto fail;
87
88     if (virConfWriteMem(gotxmcfgData, &wrote, conf) < 0)
89         goto fail;
90     gotxmcfgData[wrote] = '\0';
91
92     if (STRNEQ(xmcfgData, gotxmcfgData)) {
93         virtTestDifference(stderr, xmcfgData, gotxmcfgData);
94         goto fail;
95     }
96
97     ret = 0;
98
99  fail:
100     VIR_FREE(xmlData);
101     VIR_FREE(xmcfgData);
102     VIR_FREE(gotxmcfgData);
103     if (conf)
104         virConfFree(conf);
105     virDomainDefFree(def);
106     virObjectUnref(conn);
107
108     return ret;
109 }
110
111 static int
112 testCompareFormatXML(const char *xmcfg, const char *xml, int xendConfigVersion)
113 {
114     char *xmlData = NULL;
115     char *xmcfgData = NULL;
116     char *gotxml = NULL;
117     virConfPtr conf = NULL;
118     int ret = -1;
119     virConnectPtr conn;
120     struct _xenUnifiedPrivate priv;
121     virDomainDefPtr def = NULL;
122
123     conn = virGetConnect();
124     if (!conn) goto fail;
125
126     if (virtTestLoadFile(xml, &xmlData) < 0)
127         goto fail;
128
129     if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
130         goto fail;
131
132     /* Many puppies died to bring you this code. */
133     priv.xendConfigVersion = xendConfigVersion;
134     priv.caps = caps;
135     conn->privateData = &priv;
136
137     if (!(conf = virConfReadMem(xmcfgData, strlen(xmcfgData), 0)))
138         goto fail;
139
140     if (!(def = xenParseXM(conf, priv.xendConfigVersion, priv.caps)))
141         goto fail;
142
143     if (!(gotxml = virDomainDefFormat(def, VIR_DOMAIN_XML_SECURE)))
144         goto fail;
145
146     if (STRNEQ(xmlData, gotxml)) {
147         virtTestDifference(stderr, xmlData, gotxml);
148         goto fail;
149     }
150
151     ret = 0;
152
153  fail:
154     if (conf)
155         virConfFree(conf);
156     VIR_FREE(xmlData);
157     VIR_FREE(xmcfgData);
158     VIR_FREE(gotxml);
159     virDomainDefFree(def);
160     virObjectUnref(conn);
161
162     return ret;
163 }
164
165
166 struct testInfo {
167     const char *name;
168     int version;
169     int mode;
170 };
171
172 static int
173 testCompareHelper(const void *data)
174 {
175     int result = -1;
176     const struct testInfo *info = data;
177     char *xml = NULL;
178     char *cfg = NULL;
179
180     if (virAsprintf(&xml, "%s/xmconfigdata/test-%s.xml",
181                     abs_srcdir, info->name) < 0 ||
182         virAsprintf(&cfg, "%s/xmconfigdata/test-%s.cfg",
183                     abs_srcdir, info->name) < 0)
184         goto cleanup;
185
186     if (info->mode == 0)
187         result = testCompareParseXML(cfg, xml, info->version);
188     else
189         result = testCompareFormatXML(cfg, xml, info->version);
190
191  cleanup:
192     VIR_FREE(xml);
193     VIR_FREE(cfg);
194
195     return result;
196 }
197
198
199 static int
200 mymain(void)
201 {
202     int ret = 0;
203
204     if (!(caps = testXenCapsInit()))
205         return EXIT_FAILURE;
206
207     if (!(xmlopt = xenDomainXMLConfInit()))
208         return EXIT_FAILURE;
209
210 #define DO_TEST(name, version)                                          \
211     do {                                                                \
212         struct testInfo info0 = { name, version, 0 };                   \
213         struct testInfo info1 = { name, version, 1 };                   \
214         if (virtTestRun("Xen XM-2-XML Parse  " name,                    \
215                         testCompareHelper, &info0) < 0)                 \
216             ret = -1;                                                   \
217         if (virtTestRun("Xen XM-2-XML Format " name,                    \
218                         testCompareHelper, &info1) < 0)                 \
219             ret = -1;                                                   \
220     } while (0)
221
222     DO_TEST("paravirt-old-pvfb", 1);
223     DO_TEST("paravirt-old-pvfb-vncdisplay", 1);
224     DO_TEST("paravirt-new-pvfb", 3);
225     DO_TEST("paravirt-new-pvfb-vncdisplay", 3);
226     DO_TEST("paravirt-net-e1000", 3);
227     DO_TEST("paravirt-net-vifname", 3);
228     DO_TEST("paravirt-vcpu", 2);
229     DO_TEST("fullvirt-old-cdrom", 1);
230     DO_TEST("fullvirt-new-cdrom", 2);
231     DO_TEST("fullvirt-utc", 2);
232     DO_TEST("fullvirt-localtime", 2);
233     DO_TEST("fullvirt-usbtablet", 2);
234     DO_TEST("fullvirt-usbmouse", 2);
235     DO_TEST("fullvirt-serial-file", 2);
236     DO_TEST("fullvirt-serial-dev-2-ports", 2);
237     DO_TEST("fullvirt-serial-dev-2nd-port", 2);
238     DO_TEST("fullvirt-serial-null", 2);
239     DO_TEST("fullvirt-serial-pipe", 2);
240     DO_TEST("fullvirt-serial-pty", 2);
241     DO_TEST("fullvirt-serial-stdio", 2);
242     DO_TEST("fullvirt-serial-tcp", 2);
243     DO_TEST("fullvirt-serial-tcp-telnet", 2);
244     DO_TEST("fullvirt-serial-udp", 2);
245     DO_TEST("fullvirt-serial-unix", 2);
246
247     DO_TEST("fullvirt-force-hpet", 2);
248     DO_TEST("fullvirt-force-nohpet", 2);
249
250     DO_TEST("fullvirt-parallel-tcp", 2);
251
252     DO_TEST("fullvirt-sound", 2);
253
254     DO_TEST("fullvirt-net-ioemu", 2);
255     DO_TEST("fullvirt-net-netfront", 2);
256
257     DO_TEST("escape-paths", 2);
258     DO_TEST("no-source-cdrom", 2);
259     DO_TEST("pci-devs", 2);
260
261     virObjectUnref(caps);
262     virObjectUnref(xmlopt);
263
264     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
265 }
266
267 VIRT_TEST_MAIN(mymain)