2 * virconftest.c: Test the config file API
4 * Copyright (C) 2006-2016 Red Hat, Inc.
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.
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.
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/>.
27 #include "testutils.h"
30 #define VIR_FROM_THIS VIR_FROM_NONE
32 static int testConfRoundTrip(const void *opaque
)
34 const char *name
= opaque
;
36 virConfPtr conf
= NULL
;
42 if (virAsprintf(&srcfile
, "%s/virconfdata/%s.conf",
43 abs_srcdir
, name
) < 0 ||
44 virAsprintf(&dstfile
, "%s/virconfdata/%s.out",
45 abs_srcdir
, name
) < 0)
48 if (VIR_ALLOC_N_QUIET(buffer
, len
) < 0) {
49 fprintf(stderr
, "out of memory\n");
52 conf
= virConfReadFile(srcfile
, 0);
54 fprintf(stderr
, "Failed to process %s\n", srcfile
);
57 ret
= virConfWriteMem(buffer
, &len
, conf
);
59 fprintf(stderr
, "Failed to serialize %s back\n", srcfile
);
63 if (virTestCompareToFile(buffer
, dstfile
) < 0)
76 static int testConfMemoryNoNewline(const void *opaque ATTRIBUTE_UNUSED
)
78 const char *srcdata
= \
79 "ullong = '123456789'\n" \
83 virConfPtr conf
= virConfReadString(srcdata
, 0);
86 unsigned long long llvalue
;
93 if (!(val
= virConfGetValue(conf
, "ullong")))
96 if (val
->type
!= VIR_CONF_STRING
)
99 if (virStrToLong_ull(val
->str
, NULL
, 10, &llvalue
) < 0)
102 if (llvalue
!= 123456789) {
103 fprintf(stderr
, "Expected '123' got '%llu'\n", llvalue
);
107 if (virConfGetValueType(conf
, "string") !=
109 fprintf(stderr
, "expected a string for 'string'\n");
113 if (virConfGetValueString(conf
, "string", &str
) < 0)
116 if (STRNEQ_NULLABLE(str
, "foo")) {
117 fprintf(stderr
, "Expected 'foo' got '%s'\n", str
);
121 if (virConfGetValueType(conf
, "uint") != VIR_CONF_ULLONG
) {
122 fprintf(stderr
, "expected an unsigned long for 'uint'\n");
126 if (virConfGetValueInt(conf
, "uint", &uintvalue
) < 0)
129 if (uintvalue
!= 12345) {
130 fprintf(stderr
, "Expected 12345 got %ud\n", uintvalue
);
142 static int testConfParseInt(const void *opaque ATTRIBUTE_UNUSED
)
144 const char *srcdata
= \
147 "llong = -6963472309248\n" \
148 "ullong = 6963472309248\n" \
149 "size_t = 87539319\n" \
150 "ssize_t = -87539319\n" \
151 "string = \"foo\"\n";
154 virConfPtr conf
= virConfReadString(srcdata
, 0);
160 unsigned long long ul
;
165 if (virConfGetValueType(conf
, "int") !=
167 fprintf(stderr
, "expected a long for 'int'\n");
171 if (virConfGetValueInt(conf
, "int", &iv
) < 0)
175 fprintf(stderr
, "Expected -1729 got %d\n", iv
);
179 if (virConfGetValueInt(conf
, "string", &iv
) != -1) {
180 fprintf(stderr
, "Expected error for 'string' param\n");
185 if (virConfGetValueType(conf
, "uint") !=
187 fprintf(stderr
, "expected a unsigned long for 'uint'\n");
191 if (virConfGetValueUInt(conf
, "uint", &ui
) < 0)
195 fprintf(stderr
, "Expected 1729 got %u\n", ui
);
199 if (virConfGetValueUInt(conf
, "string", &ui
) != -1) {
200 fprintf(stderr
, "Expected error for 'string' param\n");
206 if (virConfGetValueType(conf
, "llong") !=
208 fprintf(stderr
, "expected a long for 'llong'\n");
212 if (virConfGetValueLLong(conf
, "llong", &l
) < 0)
215 if (l
!= -6963472309248) {
216 fprintf(stderr
, "Expected -6963472309248 got %lld\n", l
);
220 if (virConfGetValueLLong(conf
, "string", &l
) != -1) {
221 fprintf(stderr
, "Expected error for 'string' param\n");
227 if (virConfGetValueType(conf
, "ullong") !=
229 fprintf(stderr
, "expected a unsigned long for 'ullong'\n");
233 if (virConfGetValueULLong(conf
, "ullong", &ul
) < 0)
236 if (ul
!= 6963472309248) {
237 fprintf(stderr
, "Expected 6963472309248 got %llu\n", ul
);
241 if (virConfGetValueULLong(conf
, "string", &ul
) != -1) {
242 fprintf(stderr
, "Expected error for 'string' param\n");
248 if (virConfGetValueType(conf
, "size_t") !=
250 fprintf(stderr
, "expected a unsigned long for 'size_T'\n");
254 if (virConfGetValueSizeT(conf
, "size_t", &s
) < 0)
258 fprintf(stderr
, "Expected 87539319 got %zu\n", s
);
262 if (virConfGetValueSizeT(conf
, "string", &s
) != -1) {
263 fprintf(stderr
, "Expected error for 'string' param\n");
269 if (virConfGetValueType(conf
, "ssize_t") !=
271 fprintf(stderr
, "expected a unsigned long for 'ssize_t'\n");
275 if (virConfGetValueSSizeT(conf
, "ssize_t", &ss
) < 0)
278 if (ss
!= -87539319) {
279 fprintf(stderr
, "Expected -87539319 got %zd\n", ss
);
283 if (virConfGetValueSSizeT(conf
, "string", &ss
) != -1) {
284 fprintf(stderr
, "Expected error for 'string' param\n");
294 static int testConfParseBool(const void *opaque ATTRIBUTE_UNUSED
)
296 const char *srcdata
= \
299 "int = 6963472309248\n" \
300 "string = \"foo\"\n";
303 virConfPtr conf
= virConfReadString(srcdata
, 0);
310 if (virConfGetValueType(conf
, "false") !=
312 fprintf(stderr
, "expected a long for 'false'\n");
316 if (virConfGetValueBool(conf
, "false", &f
) < 0)
320 fprintf(stderr
, "Expected 0 got %d\n", f
);
326 if (virConfGetValueType(conf
, "true") !=
328 fprintf(stderr
, "expected a long for 'true'\n");
332 if (virConfGetValueBool(conf
, "true", &t
) < 0)
336 fprintf(stderr
, "Expected 1 got %d\n", t
);
342 if (virConfGetValueBool(conf
, "int", &t
) != -1) {
343 fprintf(stderr
, "Expected error for 'string' param\n");
347 if (virConfGetValueBool(conf
, "string", &t
) != -1) {
348 fprintf(stderr
, "Expected error for 'string' param\n");
360 static int testConfParseString(const void *opaque ATTRIBUTE_UNUSED
)
362 const char *srcdata
= \
363 "int = 6963472309248\n" \
364 "string = \"foo\"\n";
367 virConfPtr conf
= virConfReadString(srcdata
, 0);
373 if (virConfGetValueType(conf
, "string") !=
375 fprintf(stderr
, "expected a string for 'string'\n");
379 if (virConfGetValueString(conf
, "string", &str
) < 0)
382 if (STRNEQ_NULLABLE(str
, "foo")) {
383 fprintf(stderr
, "Expected 'foo' got '%s'\n", str
);
387 if (virConfGetValueString(conf
, "int", &str
) != -1) {
388 fprintf(stderr
, "Expected error for 'int'\n");
400 static int testConfParseStringList(const void *opaque ATTRIBUTE_UNUSED
)
402 const char *srcdata
= \
403 "string_list = [\"foo\", \"bar\"]\n" \
404 "string = \"foo\"\n";
407 virConfPtr conf
= virConfReadString(srcdata
, 0);
413 if (virConfGetValueType(conf
, "string_list") !=
415 fprintf(stderr
, "expected a list for 'string_list'\n");
419 if (virConfGetValueStringList(conf
, "string_list", false, &str
) < 0)
422 if (virStringListLength((const char *const*)str
) != 2) {
423 fprintf(stderr
, "expected a 2 element list\n");
427 if (STRNEQ_NULLABLE(str
[0], "foo")) {
428 fprintf(stderr
, "Expected 'foo' got '%s'\n", str
[0]);
432 if (STRNEQ_NULLABLE(str
[1], "bar")) {
433 fprintf(stderr
, "Expected 'bar' got '%s'\n", str
[1]);
438 if (virConfGetValueStringList(conf
, "string", false, &str
) != -1) {
439 fprintf(stderr
, "Expected error for 'string'\n");
443 if (virConfGetValueStringList(conf
, "string", true, &str
) < 0)
446 if (virStringListLength((const char *const*)str
) != 1) {
447 fprintf(stderr
, "expected a 1 element list\n");
451 if (STRNEQ_NULLABLE(str
[0], "foo")) {
452 fprintf(stderr
, "Expected 'foo' got '%s'\n", str
[0]);
459 virStringListFree(str
);
470 if (virTestRun("fc4", testConfRoundTrip
, "fc4") < 0)
473 if (virTestRun("libvirtd", testConfRoundTrip
, "libvirtd") < 0)
476 if (virTestRun("no-newline", testConfRoundTrip
, "no-newline") < 0)
479 if (virTestRun("memory-no-newline", testConfMemoryNoNewline
, NULL
) < 0)
482 if (virTestRun("int", testConfParseInt
, NULL
) < 0)
485 if (virTestRun("bool", testConfParseBool
, NULL
) < 0)
488 if (virTestRun("string", testConfParseString
, NULL
) < 0)
491 if (virTestRun("string-list", testConfParseStringList
, NULL
) < 0)
498 VIR_TEST_MAIN(mymain
)