xmconfigtest.c 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * xmconfigtest.c: Test backend for xm_internal config file handling
 *
 * Copyright (C) 2007 Red Hat
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 *
 */

24
#include <config.h>
25

26 27 28
#include <stdio.h>
#include <string.h>

29
#ifdef WITH_XEN
30 31

#include "internal.h"
32
#include "xen_unified.h"
33 34 35 36 37
#include "xm_internal.h"
#include "testutils.h"
#include "conf.h"

static char *progname;
38
static char *abs_top_srcdir;
39 40 41

#define MAX_FILE 4096

42 43
static int testCompareParseXML(const char *xmcfg_rel, const char *xml_rel,
                               int xendConfigVersion) {
44 45 46 47 48 49 50 51 52 53
    char xmlData[MAX_FILE];
    char xmcfgData[MAX_FILE];
    char gotxmcfgData[MAX_FILE];
    char *xmlPtr = &(xmlData[0]);
    char *xmcfgPtr = &(xmcfgData[0]);
    char *gotxmcfgPtr = &(gotxmcfgData[0]);
    virConfPtr conf = NULL;
    int ret = -1;
    virConnectPtr conn;
    int wrote = MAX_FILE;
54
    void *old_priv = NULL;
55
    struct _xenUnifiedPrivate priv;
56 57 58 59 60
    char xmcfg[PATH_MAX];
    char xml[PATH_MAX];

    snprintf(xmcfg, sizeof xmcfg - 1, "%s/tests/%s", abs_top_srcdir, xmcfg_rel);
    snprintf(xml, sizeof xml - 1, "%s/tests/%s", abs_top_srcdir, xml_rel);
61

62
    conn = virConnectOpenReadOnly("test:///default");
63 64
    if (!conn) goto fail;
    old_priv = conn->privateData;
65 66 67 68 69 70 71

    if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
        goto fail;

    if (virtTestLoadFile(xmcfg, &xmcfgPtr, MAX_FILE) < 0)
        goto fail;

72 73 74
    /* Many puppies died to bring you this code. */
    priv.xendConfigVersion = xendConfigVersion;
    conn->privateData = &priv;
75 76 77 78 79 80 81 82

    if (!(conf = xenXMParseXMLToConfig(conn, xmlPtr)))
        goto fail;

    if (virConfWriteMem(gotxmcfgPtr, &wrote, conf) < 0)
        goto fail;
    gotxmcfgPtr[wrote] = '\0';

83 84 85 86 87
    if (strcmp(xmcfgData, gotxmcfgData)) {
        if (getenv("DEBUG_TESTS")) {
            printf("Expect %d '%s'\n", (int)strlen(xmcfgData), xmcfgData);
            printf("Actual %d '%s'\n", (int)strlen(gotxmcfgData), gotxmcfgData);
        }
88
        goto fail;
89
    }
90 91 92 93 94 95 96

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);

97 98 99 100 101
    if (conn) {
        conn->privateData = old_priv;
        virConnectClose(conn);
    }

102 103 104
    return ret;
}

105 106
static int testCompareFormatXML(const char *xmcfg_rel, const char *xml_rel,
                                int xendConfigVersion) {
107 108 109 110 111 112 113 114
    char xmlData[MAX_FILE];
    char xmcfgData[MAX_FILE];
    char *xmlPtr = &(xmlData[0]);
    char *xmcfgPtr = &(xmcfgData[0]);
    char *gotxml = NULL;
    virConfPtr conf = NULL;
    int ret = -1;
    virConnectPtr conn;
115 116
    void *old_priv;
    struct _xenUnifiedPrivate priv;
117 118 119 120 121
    char xmcfg[PATH_MAX];
    char xml[PATH_MAX];

    snprintf(xmcfg, sizeof xmcfg - 1, "%s/tests/%s", abs_top_srcdir, xmcfg_rel);
    snprintf(xml, sizeof xml - 1, "%s/tests/%s", abs_top_srcdir, xml_rel);
122

123
    conn = virConnectOpenReadOnly("test:///default");
124 125
    if (!conn) goto fail;
    old_priv = conn->privateData;
126 127 128 129 130 131 132

    if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
        goto fail;

    if (virtTestLoadFile(xmcfg, &xmcfgPtr, MAX_FILE) < 0)
        goto fail;

133 134 135
    /* Many puppies died to bring you this code. */
    priv.xendConfigVersion = xendConfigVersion;
    conn->privateData = &priv;
136 137 138 139 140 141 142

    if (!(conf = virConfReadMem(xmcfgPtr, strlen(xmcfgPtr))))
        goto fail;

    if (!(gotxml = xenXMDomainFormatXML(conn, conf)))
        goto fail;

143 144 145 146 147
    if (strcmp(xmlData, gotxml)) {
        if (getenv("DEBUG_TESTS")) {
            printf("Expect %d '%s'\n", (int)strlen(xmlData), xmlData);
            printf("Actual %d '%s'\n", (int)strlen(gotxml), gotxml);
        }
148
        goto fail;
149
    }
150 151 152 153 154 155

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);
156
    free(gotxml);
157

158 159 160 161 162
    if (conn) {
        conn->privateData = old_priv;
        virConnectClose(conn);
    }

163 164 165
    return ret;
}

166
static int testCompareParavirtOldPVFBFormat(const void *data ATTRIBUTE_UNUSED) {
167 168 169 170
    return testCompareFormatXML("xmconfigdata/test-paravirt-old-pvfb.cfg",
                                "xmconfigdata/test-paravirt-old-pvfb.xml",
                                2);
}
171
static int testCompareParavirtOldPVFBParse(const void *data ATTRIBUTE_UNUSED) {
172 173 174 175 176
    return testCompareParseXML("xmconfigdata/test-paravirt-old-pvfb.cfg",
                               "xmconfigdata/test-paravirt-old-pvfb.xml",
                               2);
}

177
static int testCompareParavirtNewPVFBFormat(const void *data ATTRIBUTE_UNUSED) {
178 179 180 181
    return testCompareFormatXML("xmconfigdata/test-paravirt-new-pvfb.cfg",
                                "xmconfigdata/test-paravirt-new-pvfb.xml",
                                3);
}
182
static int testCompareParavirtNewPVFBParse(const void *data ATTRIBUTE_UNUSED) {
183 184 185 186 187
    return testCompareParseXML("xmconfigdata/test-paravirt-new-pvfb.cfg",
                               "xmconfigdata/test-paravirt-new-pvfb.xml",
                               3);
}

188
static int testCompareFullvirtOldCDROMFormat(const void *data ATTRIBUTE_UNUSED) {
189 190 191 192
    return testCompareFormatXML("xmconfigdata/test-fullvirt-old-cdrom.cfg",
                                "xmconfigdata/test-fullvirt-old-cdrom.xml",
                                1);
}
193
static int testCompareFullvirtOldCDROMParse(const void *data ATTRIBUTE_UNUSED) {
194 195 196 197 198
    return testCompareParseXML("xmconfigdata/test-fullvirt-old-cdrom.cfg",
                               "xmconfigdata/test-fullvirt-old-cdrom.xml",
                               1);
}

199
static int testCompareFullvirtNewCDROMFormat(const void *data ATTRIBUTE_UNUSED) {
200 201 202 203
    return testCompareFormatXML("xmconfigdata/test-fullvirt-new-cdrom.cfg",
                                "xmconfigdata/test-fullvirt-new-cdrom.xml",
                                2);
}
204
static int testCompareFullvirtNewCDROMParse(const void *data ATTRIBUTE_UNUSED) {
205 206 207 208 209
    return testCompareParseXML("xmconfigdata/test-fullvirt-new-cdrom.cfg",
                               "xmconfigdata/test-fullvirt-new-cdrom.xml",
                               2);
}

210
static int testCompareFullvirtClockUTCFormat(const void *data ATTRIBUTE_UNUSED) {
211 212 213 214 215
    return testCompareFormatXML("xmconfigdata/test-fullvirt-utc.cfg",
                                "xmconfigdata/test-fullvirt-utc.xml",
                                2);
}

216
static int testCompareFullvirtClockUTCParse(const void *data ATTRIBUTE_UNUSED) {
217 218 219 220 221
    return testCompareParseXML("xmconfigdata/test-fullvirt-utc.cfg",
                               "xmconfigdata/test-fullvirt-utc.xml",
                               2);
}

222
static int testCompareFullvirtClockLocaltimeFormat(const void *data ATTRIBUTE_UNUSED) {
223 224 225 226
    return testCompareFormatXML("xmconfigdata/test-fullvirt-localtime.cfg",
                                "xmconfigdata/test-fullvirt-localtime.xml",
                                2);
}
227 228

static int testCompareFullvirtClockLocaltimeParse(const void *data ATTRIBUTE_UNUSED) {
229 230 231 232 233
    return testCompareParseXML("xmconfigdata/test-fullvirt-localtime.cfg",
                               "xmconfigdata/test-fullvirt-localtime.xml",
                               2);
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
static int testCompareFullvirtInputUSBTabletFormat(const void *data ATTRIBUTE_UNUSED) {
    return testCompareFormatXML("xmconfigdata/test-fullvirt-usbtablet.cfg",
                               "xmconfigdata/test-fullvirt-usbtablet.xml",
                               2);
}

static int testCompareFullvirtInputUSBTabletParse(const void *data ATTRIBUTE_UNUSED) {
    return testCompareParseXML("xmconfigdata/test-fullvirt-usbtablet.cfg",
                               "xmconfigdata/test-fullvirt-usbtablet.xml",
                               2);
}

static int testCompareFullvirtInputUSBTabletNoBusParse(const void *data ATTRIBUTE_UNUSED) {
    return testCompareParseXML("xmconfigdata/test-fullvirt-usbtablet.cfg",
                               "xmconfigdata/test-fullvirt-usbtablet-no-bus.xml",
                               2);
}

static int testCompareFullvirtInputUSBMouseFormat(const void *data ATTRIBUTE_UNUSED) {
    return testCompareParseXML("xmconfigdata/test-fullvirt-usbmouse.cfg",
                               "xmconfigdata/test-fullvirt-usbmouse.xml",
                               2);
}

static int testCompareFullvirtInputUSBMouseParse(const void *data ATTRIBUTE_UNUSED) {
    return testCompareParseXML("xmconfigdata/test-fullvirt-usbmouse.cfg",
                               "xmconfigdata/test-fullvirt-usbmouse.xml",
                               2);
}

264 265 266 267 268 269 270 271 272 273 274 275 276

int
main(int argc, char **argv)
{
    int ret = 0;

    progname = argv[0];

    if (argc > 1) {
        fprintf(stderr, "Usage: %s\n", progname);
        exit(EXIT_FAILURE);
    }

277 278 279 280
    abs_top_srcdir = getenv("abs_top_srcdir");
    if (!abs_top_srcdir)
      return 1;

281
    /* Config -> XML */
282 283 284 285 286 287 288 289 290 291 292 293
    if (virtTestRun("Paravirt old PVFB (Format)",
                    1, testCompareParavirtOldPVFBFormat, NULL) != 0)
        ret = -1;
    if (virtTestRun("Paravirt new PVFB (Format)",
                    1, testCompareParavirtNewPVFBFormat, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt old PVFB (Format)",
                    1, testCompareFullvirtOldCDROMFormat, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt new PVFB (Format)",
                    1, testCompareFullvirtNewCDROMFormat, NULL) != 0)
        ret = -1;
294 295 296 297 298 299
    if (virtTestRun("Fullvirt clock Localtime (Format)",
                    1, testCompareFullvirtClockLocaltimeFormat, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt clock UTC (Format)",
                    1, testCompareFullvirtClockUTCFormat, NULL) != 0)
        ret = -1;
300 301 302 303 304 305
    if (virtTestRun("Fullvirt USB mouse (Format)",
                    1, testCompareFullvirtInputUSBMouseFormat, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt USB tablet (Format)",
                    1, testCompareFullvirtInputUSBTabletFormat, NULL) != 0)
        ret = -1;
306

307
    /* XML -> Config */
308 309 310 311 312 313 314 315 316 317 318 319
    if (virtTestRun("Paravirt old PVFB (Parse)",
                    1, testCompareParavirtOldPVFBParse, NULL) != 0)
        ret = -1;
    if (virtTestRun("Paravirt new PVFB (Parse)",
                    1, testCompareParavirtNewPVFBParse, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt old PVFB (Parse)",
                    1, testCompareFullvirtOldCDROMParse, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt new PVFB (Parse)",
                    1, testCompareFullvirtNewCDROMParse, NULL) != 0)
        ret = -1;
320 321 322 323 324 325
    if (virtTestRun("Fullvirt clock Localtime (Parse)",
                    1, testCompareFullvirtClockLocaltimeParse, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt clock UTC (Parse)",
                    1, testCompareFullvirtClockUTCParse, NULL) != 0)
        ret = -1;
326 327 328 329 330 331 332 333 334 335
    if (virtTestRun("Fullvirt USB mouse (Parse)",
                    1, testCompareFullvirtInputUSBMouseParse, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt USB tablet (Parse)",
                    1, testCompareFullvirtInputUSBTabletParse, NULL) != 0)
        ret = -1;
    if (virtTestRun("Fullvirt USB tablet no bus (Parse)",
                    1, testCompareFullvirtInputUSBTabletNoBusParse, NULL) != 0)
        ret = -1;

336 337 338

    exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
339 340 341 342 343
#else /* WITHOUT_XEN */
int
main(void)
{
    fprintf(stderr, "libvirt compiled without Xen support\n");
344
    return(0);
345 346
}
#endif /* WITH_XEN */
347 348 349 350 351 352 353 354 355

/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */