2016-07-07 22:39:00 +08:00
|
|
|
/*
|
|
|
|
* virconftest.c: Test the config file API
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2016 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-01-30 02:15:54 +08:00
|
|
|
#include <config.h>
|
2007-12-06 05:40:15 +08:00
|
|
|
|
2006-08-30 06:27:07 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2007-11-13 06:16:25 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2012-12-13 00:35:35 +08:00
|
|
|
#include "virconf.h"
|
2012-12-13 02:06:53 +08:00
|
|
|
#include "viralloc.h"
|
2016-07-07 22:39:00 +08:00
|
|
|
#include "testutils.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
2006-08-30 06:27:07 +08:00
|
|
|
|
2016-07-07 22:39:00 +08:00
|
|
|
static int testConfRoundTrip(const void *opaque)
|
2011-04-25 06:25:10 +08:00
|
|
|
{
|
2016-07-07 22:39:00 +08:00
|
|
|
const char *name = opaque;
|
|
|
|
int ret = -1;
|
2014-09-01 20:08:04 +08:00
|
|
|
virConfPtr conf = NULL;
|
2006-08-30 06:27:07 +08:00
|
|
|
int len = 10000;
|
2011-04-25 06:25:10 +08:00
|
|
|
char *buffer = NULL;
|
2016-07-07 22:39:00 +08:00
|
|
|
char *srcfile = NULL;
|
|
|
|
char *dstfile = NULL;
|
2006-08-30 06:27:07 +08:00
|
|
|
|
2016-07-07 22:39:00 +08:00
|
|
|
if (virAsprintf(&srcfile, "%s/virconfdata/%s.conf",
|
|
|
|
abs_srcdir, name) < 0 ||
|
|
|
|
virAsprintf(&dstfile, "%s/virconfdata/%s.out",
|
|
|
|
abs_srcdir, name) < 0)
|
2011-04-25 06:25:10 +08:00
|
|
|
goto cleanup;
|
2006-08-30 06:27:07 +08:00
|
|
|
|
2013-06-07 16:37:25 +08:00
|
|
|
if (VIR_ALLOC_N_QUIET(buffer, len) < 0) {
|
2011-04-25 06:25:10 +08:00
|
|
|
fprintf(stderr, "out of memory\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-07-07 22:39:00 +08:00
|
|
|
conf = virConfReadFile(srcfile, 0);
|
2006-08-30 06:27:07 +08:00
|
|
|
if (conf == NULL) {
|
2016-07-07 22:39:00 +08:00
|
|
|
fprintf(stderr, "Failed to process %s\n", srcfile);
|
2011-04-25 06:25:10 +08:00
|
|
|
goto cleanup;
|
2006-08-30 06:27:07 +08:00
|
|
|
}
|
2011-04-25 06:25:10 +08:00
|
|
|
ret = virConfWriteMem(buffer, &len, conf);
|
2006-08-30 06:27:07 +08:00
|
|
|
if (ret < 0) {
|
2016-07-07 22:39:00 +08:00
|
|
|
fprintf(stderr, "Failed to serialize %s back\n", srcfile);
|
2011-04-25 06:25:10 +08:00
|
|
|
goto cleanup;
|
2007-10-19 16:29:13 +08:00
|
|
|
}
|
2011-04-25 06:25:10 +08:00
|
|
|
|
2016-07-07 22:39:00 +08:00
|
|
|
if (virTestCompareToFile(buffer, dstfile) < 0)
|
|
|
|
goto cleanup;
|
2011-04-25 06:25:10 +08:00
|
|
|
|
2016-07-07 22:39:00 +08:00
|
|
|
ret = 0;
|
2014-03-25 14:53:44 +08:00
|
|
|
cleanup:
|
2016-07-07 22:39:00 +08:00
|
|
|
VIR_FREE(srcfile);
|
|
|
|
VIR_FREE(dstfile);
|
2011-04-25 06:25:10 +08:00
|
|
|
VIR_FREE(buffer);
|
2014-09-01 20:08:04 +08:00
|
|
|
virConfFree(conf);
|
2016-07-07 22:39:00 +08:00
|
|
|
return ret;
|
2006-08-30 06:27:07 +08:00
|
|
|
}
|
2016-07-07 22:39:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (virTestRun("fc4", testConfRoundTrip, "fc4") < 0)
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
if (virTestRun("libvirtd", testConfRoundTrip, "libvirtd") < 0)
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
if (virTestRun("no-newline", testConfRoundTrip, "no-newline") < 0)
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VIRT_TEST_MAIN(mymain)
|