virconftest.c 10.4 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
static int testConfParseInt(const void *opaque ATTRIBUTE_UNUSED)
{
    const char *srcdata =                       \
        "int = -1729\n"                         \
        "uint = 1729\n"                         \
        "llong = -6963472309248\n"              \
        "ullong = 6963472309248\n"              \
        "size_t = 87539319\n"                   \
        "ssize_t = -87539319\n"                 \
        "string = \"foo\"\n";

    int ret = -1;
    virConfPtr conf = virConfReadMem(srcdata, strlen(srcdata), 0);
    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") !=
104
        VIR_CONF_LLONG) {
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        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") !=
124
        VIR_CONF_ULLONG) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        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") !=
145
        VIR_CONF_LLONG) {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        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") !=
166
        VIR_CONF_ULLONG) {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        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") !=
187
        VIR_CONF_ULLONG) {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        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") !=
208
        VIR_CONF_LLONG) {
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        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)
{
    const char *srcdata =                         \
        "false = 0\n"                             \
        "true = 1\n"                              \
        "int = 6963472309248\n"                   \
        "string = \"foo\"\n";

    int ret = -1;
    virConfPtr conf = virConfReadMem(srcdata, strlen(srcdata), 0);
    bool f = true;
    bool t = false;

    if (!conf)
        return -1;

    if (virConfGetValueType(conf, "false") !=
249
        VIR_CONF_ULLONG) {
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        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") !=
265
        VIR_CONF_ULLONG) {
266 267 268 269 270 271 272 273 274 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 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 366 367 368 369 370 371 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
        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)
{
    const char *srcdata =                         \
        "int = 6963472309248\n"                   \
        "string = \"foo\"\n";

    int ret = -1;
    virConfPtr conf = virConfReadMem(srcdata, strlen(srcdata), 0);
    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)
{
    const char *srcdata =                         \
        "string_list = [\"foo\", \"bar\"]\n"      \
        "string = \"foo\"\n";

    int ret = -1;
    virConfPtr conf = virConfReadMem(srcdata, strlen(srcdata), 0);
    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:
397
    virStringListFree(str);
398 399 400 401 402
    virConfFree(conf);
    return ret;
}


403 404 405 406 407 408 409 410 411 412 413 414 415 416
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;

417 418 419 420 421 422 423 424 425 426 427 428
    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;

429 430 431 432
    return ret;
}


433
VIR_TEST_MAIN(mymain)