virconftest.c 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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/>.
 *
 */

22
#include <config.h>
J
Jim Meyering 已提交
23

D
Daniel Veillard 已提交
24 25 26
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
27 28
#include <string.h>
#include <errno.h>
29
#include "virconf.h"
30
#include "viralloc.h"
31 32 33 34
#include "testutils.h"


#define VIR_FROM_THIS VIR_FROM_NONE
D
Daniel Veillard 已提交
35

36
static int testConfRoundTrip(const void *opaque)
37
{
38 39
    const char *name = opaque;
    int ret = -1;
40
    virConfPtr conf = NULL;
D
Daniel Veillard 已提交
41
    int len = 10000;
42
    char *buffer = NULL;
43 44
    char *srcfile = NULL;
    char *dstfile = NULL;
D
Daniel Veillard 已提交
45

46 47 48 49
    if (virAsprintf(&srcfile, "%s/virconfdata/%s.conf",
                    abs_srcdir, name) < 0 ||
        virAsprintf(&dstfile, "%s/virconfdata/%s.out",
                    abs_srcdir, name) < 0)
50
        goto cleanup;
D
Daniel Veillard 已提交
51

52
    if (VIR_ALLOC_N_QUIET(buffer, len) < 0) {
53 54 55
        fprintf(stderr, "out of memory\n");
        goto cleanup;
    }
56
    conf = virConfReadFile(srcfile, 0);
D
Daniel Veillard 已提交
57
    if (conf == NULL) {
58
        fprintf(stderr, "Failed to process %s\n", srcfile);
59
        goto cleanup;
D
Daniel Veillard 已提交
60
    }
61
    ret = virConfWriteMem(buffer, &len, conf);
D
Daniel Veillard 已提交
62
    if (ret < 0) {
63
        fprintf(stderr, "Failed to serialize %s back\n", srcfile);
64
        goto cleanup;
65
    }
66

67 68
    if (virTestCompareToFile(buffer, dstfile) < 0)
        goto cleanup;
69

70
    ret = 0;
71
 cleanup:
72 73
    VIR_FREE(srcfile);
    VIR_FREE(dstfile);
74
    VIR_FREE(buffer);
75
    virConfFree(conf);
76
    return ret;
D
Daniel Veillard 已提交
77
}
78 79


80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static int testConfMemoryNoNewline(const void *opaque ATTRIBUTE_UNUSED)
{
    const char *srcdata = \
        "ullong = '123456789'\n" \
        "string = 'foo'\n" \
        "uint = 12345";

    virConfPtr conf = virConfReadString(srcdata, 0);
    int ret = -1;
    virConfValuePtr val;
    unsigned long long llvalue;
    char *str = NULL;
    int uintvalue;

    if (!conf)
        return -1;

    if (!(val = virConfGetValue(conf, "ullong")))
        goto cleanup;

    if (val->type != VIR_CONF_STRING)
        goto cleanup;

    if (virStrToLong_ull(val->str, NULL, 10, &llvalue) < 0)
        goto cleanup;

    if (llvalue != 123456789) {
        fprintf(stderr, "Expected '123' got '%llu'\n", llvalue);
        goto cleanup;
    }

    if (virConfGetValueType(conf, "string") !=
        VIR_CONF_STRING) {
        fprintf(stderr, "expected a string for 'string'\n");
        goto cleanup;
    }

    if (virConfGetValueString(conf, "string", &str) < 0)
        goto cleanup;

    if (STRNEQ_NULLABLE(str, "foo")) {
        fprintf(stderr, "Expected 'foo' got '%s'\n", str);
        goto cleanup;
    }

    if (virConfGetValueType(conf, "uint") != VIR_CONF_ULLONG) {
        fprintf(stderr, "expected an unsigned long for 'uint'\n");
        goto cleanup;
    }

    if (virConfGetValueInt(conf, "uint", &uintvalue) < 0)
        goto cleanup;

    if (uintvalue != 12345) {
        fprintf(stderr, "Expected 12345 got %ud\n", uintvalue);
        goto cleanup;
    }

    ret = 0;
 cleanup:
    VIR_FREE(str);
    virConfFree(conf);
    return ret;
}


146 147
static int testConfParseInt(const void *opaque ATTRIBUTE_UNUSED)
{
148 149 150 151 152 153 154
    const char *srcdata = \
        "int = -1729\n" \
        "uint = 1729\n" \
        "llong = -6963472309248\n" \
        "ullong = 6963472309248\n" \
        "size_t = 87539319\n" \
        "ssize_t = -87539319\n" \
155 156 157
        "string = \"foo\"\n";

    int ret = -1;
J
Ján Tomko 已提交
158
    virConfPtr conf = virConfReadString(srcdata, 0);
159 160 161 162 163 164 165 166 167 168 169
    int iv;
    unsigned int ui;
    size_t s;
    ssize_t ss;
    long long l;
    unsigned long long ul;

    if (!conf)
        return -1;

    if (virConfGetValueType(conf, "int") !=
170
        VIR_CONF_LLONG) {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        fprintf(stderr, "expected a long for 'int'\n");
        goto cleanup;
    }

    if (virConfGetValueInt(conf, "int", &iv) < 0)
        goto cleanup;

    if (iv != -1729) {
        fprintf(stderr, "Expected -1729 got %d\n", iv);
        goto cleanup;
    }

    if (virConfGetValueInt(conf, "string", &iv) != -1) {
        fprintf(stderr, "Expected error for 'string' param\n");
        goto cleanup;
    }


    if (virConfGetValueType(conf, "uint") !=
190
        VIR_CONF_ULLONG) {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        fprintf(stderr, "expected a unsigned long for 'uint'\n");
        goto cleanup;
    }

    if (virConfGetValueUInt(conf, "uint", &ui) < 0)
        goto cleanup;

    if (ui != 1729) {
        fprintf(stderr, "Expected 1729 got %u\n", ui);
        goto cleanup;
    }

    if (virConfGetValueUInt(conf, "string", &ui) != -1) {
        fprintf(stderr, "Expected error for 'string' param\n");
        goto cleanup;
    }



    if (virConfGetValueType(conf, "llong") !=
211
        VIR_CONF_LLONG) {
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        fprintf(stderr, "expected a long for 'llong'\n");
        goto cleanup;
    }

    if (virConfGetValueLLong(conf, "llong", &l) < 0)
        goto cleanup;

    if (l != -6963472309248) {
        fprintf(stderr, "Expected -6963472309248 got %lld\n", l);
        goto cleanup;
    }

    if (virConfGetValueLLong(conf, "string", &l) != -1) {
        fprintf(stderr, "Expected error for 'string' param\n");
        goto cleanup;
    }



    if (virConfGetValueType(conf, "ullong") !=
232
        VIR_CONF_ULLONG) {
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        fprintf(stderr, "expected a unsigned long for 'ullong'\n");
        goto cleanup;
    }

    if (virConfGetValueULLong(conf, "ullong", &ul) < 0)
        goto cleanup;

    if (ul != 6963472309248) {
        fprintf(stderr, "Expected 6963472309248 got %llu\n", ul);
        goto cleanup;
    }

    if (virConfGetValueULLong(conf, "string", &ul) != -1) {
        fprintf(stderr, "Expected error for 'string' param\n");
        goto cleanup;
    }



    if (virConfGetValueType(conf, "size_t") !=
253
        VIR_CONF_ULLONG) {
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
        fprintf(stderr, "expected a unsigned long for 'size_T'\n");
        goto cleanup;
    }

    if (virConfGetValueSizeT(conf, "size_t", &s) < 0)
        goto cleanup;

    if (s != 87539319) {
        fprintf(stderr, "Expected 87539319 got %zu\n", s);
        goto cleanup;
    }

    if (virConfGetValueSizeT(conf, "string", &s) != -1) {
        fprintf(stderr, "Expected error for 'string' param\n");
        goto cleanup;
    }



    if (virConfGetValueType(conf, "ssize_t") !=
274
        VIR_CONF_LLONG) {
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        fprintf(stderr, "expected a unsigned long for 'ssize_t'\n");
        goto cleanup;
    }

    if (virConfGetValueSSizeT(conf, "ssize_t", &ss) < 0)
        goto cleanup;

    if (ss != -87539319) {
        fprintf(stderr, "Expected -87539319 got %zd\n", ss);
        goto cleanup;
    }

    if (virConfGetValueSSizeT(conf, "string", &ss) != -1) {
        fprintf(stderr, "Expected error for 'string' param\n");
        goto cleanup;
    }

    ret = 0;
 cleanup:
    virConfFree(conf);
    return ret;
}

static int testConfParseBool(const void *opaque ATTRIBUTE_UNUSED)
{
300 301 302 303
    const char *srcdata = \
        "false = 0\n" \
        "true = 1\n" \
        "int = 6963472309248\n" \
304 305 306
        "string = \"foo\"\n";

    int ret = -1;
J
Ján Tomko 已提交
307
    virConfPtr conf = virConfReadString(srcdata, 0);
308 309 310 311 312 313 314
    bool f = true;
    bool t = false;

    if (!conf)
        return -1;

    if (virConfGetValueType(conf, "false") !=
315
        VIR_CONF_ULLONG) {
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        fprintf(stderr, "expected a long for 'false'\n");
        goto cleanup;
    }

    if (virConfGetValueBool(conf, "false", &f) < 0)
        goto cleanup;

    if (f != false) {
        fprintf(stderr, "Expected 0 got %d\n", f);
        goto cleanup;
    }



    if (virConfGetValueType(conf, "true") !=
331
        VIR_CONF_ULLONG) {
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
        fprintf(stderr, "expected a long for 'true'\n");
        goto cleanup;
    }

    if (virConfGetValueBool(conf, "true", &t) < 0)
        goto cleanup;

    if (t != true) {
        fprintf(stderr, "Expected 1 got %d\n", t);
        goto cleanup;
    }



    if (virConfGetValueBool(conf, "int", &t) != -1) {
        fprintf(stderr, "Expected error for 'string' param\n");
        goto cleanup;
    }

    if (virConfGetValueBool(conf, "string", &t) != -1) {
        fprintf(stderr, "Expected error for 'string' param\n");
        goto cleanup;
    }


    ret = 0;
 cleanup:
    virConfFree(conf);
    return ret;
}


static int testConfParseString(const void *opaque ATTRIBUTE_UNUSED)
{
366 367
    const char *srcdata = \
        "int = 6963472309248\n" \
368 369 370
        "string = \"foo\"\n";

    int ret = -1;
J
Ján Tomko 已提交
371
    virConfPtr conf = virConfReadString(srcdata, 0);
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    char *str = NULL;

    if (!conf)
        return -1;

    if (virConfGetValueType(conf, "string") !=
        VIR_CONF_STRING) {
        fprintf(stderr, "expected a string for 'string'\n");
        goto cleanup;
    }

    if (virConfGetValueString(conf, "string", &str) < 0)
        goto cleanup;

    if (STRNEQ_NULLABLE(str, "foo")) {
        fprintf(stderr, "Expected 'foo' got '%s'\n", str);
        goto cleanup;
    }

    if (virConfGetValueString(conf, "int", &str) != -1) {
        fprintf(stderr, "Expected error for 'int'\n");
        goto cleanup;
    }

    ret = 0;
 cleanup:
    VIR_FREE(str);
    virConfFree(conf);
    return ret;
}


static int testConfParseStringList(const void *opaque ATTRIBUTE_UNUSED)
{
406 407
    const char *srcdata = \
        "string_list = [\"foo\", \"bar\"]\n" \
408 409 410
        "string = \"foo\"\n";

    int ret = -1;
J
Ján Tomko 已提交
411
    virConfPtr conf = virConfReadString(srcdata, 0);
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    char **str = NULL;

    if (!conf)
        return -1;

    if (virConfGetValueType(conf, "string_list") !=
        VIR_CONF_LIST) {
        fprintf(stderr, "expected a list for 'string_list'\n");
        goto cleanup;
    }

    if (virConfGetValueStringList(conf, "string_list", false, &str) < 0)
        goto cleanup;

    if (virStringListLength((const char *const*)str) != 2) {
        fprintf(stderr, "expected a 2 element list\n");
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(str[0], "foo")) {
        fprintf(stderr, "Expected 'foo' got '%s'\n", str[0]);
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(str[1], "bar")) {
        fprintf(stderr, "Expected 'bar' got '%s'\n", str[1]);
        goto cleanup;
    }


    if (virConfGetValueStringList(conf, "string", false, &str) != -1) {
        fprintf(stderr, "Expected error for 'string'\n");
        goto cleanup;
    }

    if (virConfGetValueStringList(conf, "string", true, &str) < 0)
        goto cleanup;

    if (virStringListLength((const char *const*)str) != 1) {
        fprintf(stderr, "expected a 1 element list\n");
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(str[0], "foo")) {
        fprintf(stderr, "Expected 'foo' got '%s'\n", str[0]);
        goto cleanup;
    }


    ret = 0;
 cleanup:
463
    virStringListFree(str);
464 465 466 467 468
    virConfFree(conf);
    return ret;
}


469 470 471 472 473 474 475 476 477 478 479 480 481 482
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;

483 484 485
    if (virTestRun("memory-no-newline", testConfMemoryNoNewline, NULL) < 0)
        ret = -1;

486 487 488 489 490 491 492 493 494 495 496 497
    if (virTestRun("int", testConfParseInt, NULL) < 0)
        ret = -1;

    if (virTestRun("bool", testConfParseBool, NULL) < 0)
        ret = -1;

    if (virTestRun("string", testConfParseString, NULL) < 0)
        ret = -1;

    if (virTestRun("string-list", testConfParseStringList, NULL) < 0)
        ret = -1;

498 499 500 501
    return ret;
}


502
VIR_TEST_MAIN(mymain)