tests: Use the new DO_TEST_CAPS_*() macros
[libvirt/ericb.git] / tests / virconftest.c
blobcc2b26687c1ef0856a225b481de200294dccfdab
1 /*
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/>.
22 #include <config.h>
24 #include <unistd.h>
25 #include "virconf.h"
26 #include "viralloc.h"
27 #include "testutils.h"
30 #define VIR_FROM_THIS VIR_FROM_NONE
32 static int testConfRoundTrip(const void *opaque)
34 const char *name = opaque;
35 int ret = -1;
36 virConfPtr conf = NULL;
37 int len = 10000;
38 char *buffer = NULL;
39 char *srcfile = NULL;
40 char *dstfile = 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)
46 goto cleanup;
48 if (VIR_ALLOC_N_QUIET(buffer, len) < 0) {
49 fprintf(stderr, "out of memory\n");
50 goto cleanup;
52 conf = virConfReadFile(srcfile, 0);
53 if (conf == NULL) {
54 fprintf(stderr, "Failed to process %s\n", srcfile);
55 goto cleanup;
57 ret = virConfWriteMem(buffer, &len, conf);
58 if (ret < 0) {
59 fprintf(stderr, "Failed to serialize %s back\n", srcfile);
60 goto cleanup;
63 if (virTestCompareToFile(buffer, dstfile) < 0)
64 goto cleanup;
66 ret = 0;
67 cleanup:
68 VIR_FREE(srcfile);
69 VIR_FREE(dstfile);
70 VIR_FREE(buffer);
71 virConfFree(conf);
72 return ret;
76 static int testConfMemoryNoNewline(const void *opaque ATTRIBUTE_UNUSED)
78 const char *srcdata = \
79 "ullong = '123456789'\n" \
80 "string = 'foo'\n" \
81 "uint = 12345";
83 virConfPtr conf = virConfReadString(srcdata, 0);
84 int ret = -1;
85 virConfValuePtr val;
86 unsigned long long llvalue;
87 char *str = NULL;
88 int uintvalue;
90 if (!conf)
91 return -1;
93 if (!(val = virConfGetValue(conf, "ullong")))
94 goto cleanup;
96 if (val->type != VIR_CONF_STRING)
97 goto cleanup;
99 if (virStrToLong_ull(val->str, NULL, 10, &llvalue) < 0)
100 goto cleanup;
102 if (llvalue != 123456789) {
103 fprintf(stderr, "Expected '123' got '%llu'\n", llvalue);
104 goto cleanup;
107 if (virConfGetValueType(conf, "string") !=
108 VIR_CONF_STRING) {
109 fprintf(stderr, "expected a string for 'string'\n");
110 goto cleanup;
113 if (virConfGetValueString(conf, "string", &str) < 0)
114 goto cleanup;
116 if (STRNEQ_NULLABLE(str, "foo")) {
117 fprintf(stderr, "Expected 'foo' got '%s'\n", str);
118 goto cleanup;
121 if (virConfGetValueType(conf, "uint") != VIR_CONF_ULLONG) {
122 fprintf(stderr, "expected an unsigned long for 'uint'\n");
123 goto cleanup;
126 if (virConfGetValueInt(conf, "uint", &uintvalue) < 0)
127 goto cleanup;
129 if (uintvalue != 12345) {
130 fprintf(stderr, "Expected 12345 got %ud\n", uintvalue);
131 goto cleanup;
134 ret = 0;
135 cleanup:
136 VIR_FREE(str);
137 virConfFree(conf);
138 return ret;
142 static int testConfParseInt(const void *opaque ATTRIBUTE_UNUSED)
144 const char *srcdata = \
145 "int = -1729\n" \
146 "uint = 1729\n" \
147 "llong = -6963472309248\n" \
148 "ullong = 6963472309248\n" \
149 "size_t = 87539319\n" \
150 "ssize_t = -87539319\n" \
151 "string = \"foo\"\n";
153 int ret = -1;
154 virConfPtr conf = virConfReadString(srcdata, 0);
155 int iv;
156 unsigned int ui;
157 size_t s;
158 ssize_t ss;
159 long long l;
160 unsigned long long ul;
162 if (!conf)
163 return -1;
165 if (virConfGetValueType(conf, "int") !=
166 VIR_CONF_LLONG) {
167 fprintf(stderr, "expected a long for 'int'\n");
168 goto cleanup;
171 if (virConfGetValueInt(conf, "int", &iv) < 0)
172 goto cleanup;
174 if (iv != -1729) {
175 fprintf(stderr, "Expected -1729 got %d\n", iv);
176 goto cleanup;
179 if (virConfGetValueInt(conf, "string", &iv) != -1) {
180 fprintf(stderr, "Expected error for 'string' param\n");
181 goto cleanup;
185 if (virConfGetValueType(conf, "uint") !=
186 VIR_CONF_ULLONG) {
187 fprintf(stderr, "expected a unsigned long for 'uint'\n");
188 goto cleanup;
191 if (virConfGetValueUInt(conf, "uint", &ui) < 0)
192 goto cleanup;
194 if (ui != 1729) {
195 fprintf(stderr, "Expected 1729 got %u\n", ui);
196 goto cleanup;
199 if (virConfGetValueUInt(conf, "string", &ui) != -1) {
200 fprintf(stderr, "Expected error for 'string' param\n");
201 goto cleanup;
206 if (virConfGetValueType(conf, "llong") !=
207 VIR_CONF_LLONG) {
208 fprintf(stderr, "expected a long for 'llong'\n");
209 goto cleanup;
212 if (virConfGetValueLLong(conf, "llong", &l) < 0)
213 goto cleanup;
215 if (l != -6963472309248) {
216 fprintf(stderr, "Expected -6963472309248 got %lld\n", l);
217 goto cleanup;
220 if (virConfGetValueLLong(conf, "string", &l) != -1) {
221 fprintf(stderr, "Expected error for 'string' param\n");
222 goto cleanup;
227 if (virConfGetValueType(conf, "ullong") !=
228 VIR_CONF_ULLONG) {
229 fprintf(stderr, "expected a unsigned long for 'ullong'\n");
230 goto cleanup;
233 if (virConfGetValueULLong(conf, "ullong", &ul) < 0)
234 goto cleanup;
236 if (ul != 6963472309248) {
237 fprintf(stderr, "Expected 6963472309248 got %llu\n", ul);
238 goto cleanup;
241 if (virConfGetValueULLong(conf, "string", &ul) != -1) {
242 fprintf(stderr, "Expected error for 'string' param\n");
243 goto cleanup;
248 if (virConfGetValueType(conf, "size_t") !=
249 VIR_CONF_ULLONG) {
250 fprintf(stderr, "expected a unsigned long for 'size_T'\n");
251 goto cleanup;
254 if (virConfGetValueSizeT(conf, "size_t", &s) < 0)
255 goto cleanup;
257 if (s != 87539319) {
258 fprintf(stderr, "Expected 87539319 got %zu\n", s);
259 goto cleanup;
262 if (virConfGetValueSizeT(conf, "string", &s) != -1) {
263 fprintf(stderr, "Expected error for 'string' param\n");
264 goto cleanup;
269 if (virConfGetValueType(conf, "ssize_t") !=
270 VIR_CONF_LLONG) {
271 fprintf(stderr, "expected a unsigned long for 'ssize_t'\n");
272 goto cleanup;
275 if (virConfGetValueSSizeT(conf, "ssize_t", &ss) < 0)
276 goto cleanup;
278 if (ss != -87539319) {
279 fprintf(stderr, "Expected -87539319 got %zd\n", ss);
280 goto cleanup;
283 if (virConfGetValueSSizeT(conf, "string", &ss) != -1) {
284 fprintf(stderr, "Expected error for 'string' param\n");
285 goto cleanup;
288 ret = 0;
289 cleanup:
290 virConfFree(conf);
291 return ret;
294 static int testConfParseBool(const void *opaque ATTRIBUTE_UNUSED)
296 const char *srcdata = \
297 "false = 0\n" \
298 "true = 1\n" \
299 "int = 6963472309248\n" \
300 "string = \"foo\"\n";
302 int ret = -1;
303 virConfPtr conf = virConfReadString(srcdata, 0);
304 bool f = true;
305 bool t = false;
307 if (!conf)
308 return -1;
310 if (virConfGetValueType(conf, "false") !=
311 VIR_CONF_ULLONG) {
312 fprintf(stderr, "expected a long for 'false'\n");
313 goto cleanup;
316 if (virConfGetValueBool(conf, "false", &f) < 0)
317 goto cleanup;
319 if (f != false) {
320 fprintf(stderr, "Expected 0 got %d\n", f);
321 goto cleanup;
326 if (virConfGetValueType(conf, "true") !=
327 VIR_CONF_ULLONG) {
328 fprintf(stderr, "expected a long for 'true'\n");
329 goto cleanup;
332 if (virConfGetValueBool(conf, "true", &t) < 0)
333 goto cleanup;
335 if (t != true) {
336 fprintf(stderr, "Expected 1 got %d\n", t);
337 goto cleanup;
342 if (virConfGetValueBool(conf, "int", &t) != -1) {
343 fprintf(stderr, "Expected error for 'string' param\n");
344 goto cleanup;
347 if (virConfGetValueBool(conf, "string", &t) != -1) {
348 fprintf(stderr, "Expected error for 'string' param\n");
349 goto cleanup;
353 ret = 0;
354 cleanup:
355 virConfFree(conf);
356 return ret;
360 static int testConfParseString(const void *opaque ATTRIBUTE_UNUSED)
362 const char *srcdata = \
363 "int = 6963472309248\n" \
364 "string = \"foo\"\n";
366 int ret = -1;
367 virConfPtr conf = virConfReadString(srcdata, 0);
368 char *str = NULL;
370 if (!conf)
371 return -1;
373 if (virConfGetValueType(conf, "string") !=
374 VIR_CONF_STRING) {
375 fprintf(stderr, "expected a string for 'string'\n");
376 goto cleanup;
379 if (virConfGetValueString(conf, "string", &str) < 0)
380 goto cleanup;
382 if (STRNEQ_NULLABLE(str, "foo")) {
383 fprintf(stderr, "Expected 'foo' got '%s'\n", str);
384 goto cleanup;
387 if (virConfGetValueString(conf, "int", &str) != -1) {
388 fprintf(stderr, "Expected error for 'int'\n");
389 goto cleanup;
392 ret = 0;
393 cleanup:
394 VIR_FREE(str);
395 virConfFree(conf);
396 return ret;
400 static int testConfParseStringList(const void *opaque ATTRIBUTE_UNUSED)
402 const char *srcdata = \
403 "string_list = [\"foo\", \"bar\"]\n" \
404 "string = \"foo\"\n";
406 int ret = -1;
407 virConfPtr conf = virConfReadString(srcdata, 0);
408 char **str = NULL;
410 if (!conf)
411 return -1;
413 if (virConfGetValueType(conf, "string_list") !=
414 VIR_CONF_LIST) {
415 fprintf(stderr, "expected a list for 'string_list'\n");
416 goto cleanup;
419 if (virConfGetValueStringList(conf, "string_list", false, &str) < 0)
420 goto cleanup;
422 if (virStringListLength((const char *const*)str) != 2) {
423 fprintf(stderr, "expected a 2 element list\n");
424 goto cleanup;
427 if (STRNEQ_NULLABLE(str[0], "foo")) {
428 fprintf(stderr, "Expected 'foo' got '%s'\n", str[0]);
429 goto cleanup;
432 if (STRNEQ_NULLABLE(str[1], "bar")) {
433 fprintf(stderr, "Expected 'bar' got '%s'\n", str[1]);
434 goto cleanup;
438 if (virConfGetValueStringList(conf, "string", false, &str) != -1) {
439 fprintf(stderr, "Expected error for 'string'\n");
440 goto cleanup;
443 if (virConfGetValueStringList(conf, "string", true, &str) < 0)
444 goto cleanup;
446 if (virStringListLength((const char *const*)str) != 1) {
447 fprintf(stderr, "expected a 1 element list\n");
448 goto cleanup;
451 if (STRNEQ_NULLABLE(str[0], "foo")) {
452 fprintf(stderr, "Expected 'foo' got '%s'\n", str[0]);
453 goto cleanup;
457 ret = 0;
458 cleanup:
459 virStringListFree(str);
460 virConfFree(conf);
461 return ret;
465 static int
466 mymain(void)
468 int ret = 0;
470 if (virTestRun("fc4", testConfRoundTrip, "fc4") < 0)
471 ret = -1;
473 if (virTestRun("libvirtd", testConfRoundTrip, "libvirtd") < 0)
474 ret = -1;
476 if (virTestRun("no-newline", testConfRoundTrip, "no-newline") < 0)
477 ret = -1;
479 if (virTestRun("memory-no-newline", testConfMemoryNoNewline, NULL) < 0)
480 ret = -1;
482 if (virTestRun("int", testConfParseInt, NULL) < 0)
483 ret = -1;
485 if (virTestRun("bool", testConfParseBool, NULL) < 0)
486 ret = -1;
488 if (virTestRun("string", testConfParseString, NULL) < 0)
489 ret = -1;
491 if (virTestRun("string-list", testConfParseStringList, NULL) < 0)
492 ret = -1;
494 return ret;
498 VIR_TEST_MAIN(mymain)