virbuftest.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "internal.h"
#include "testutils.h"
9
#include "virbuffer.h"
10
#include "viralloc.h"
11
#include "virstring.h"
12

13 14
#define VIR_FROM_THIS VIR_FROM_NONE

15 16 17 18 19 20 21 22 23 24
#define TEST_ERROR(...)                             \
    do {                                            \
        if (virTestGetDebug())                      \
            fprintf(stderr, __VA_ARGS__);           \
    } while (0)

struct testInfo {
    int doEscape;
};

E
Eric Blake 已提交
25
static int testBufInfiniteLoop(const void *data)
26 27 28 29 30 31 32 33 34 35
{
    virBuffer bufinit = VIR_BUFFER_INITIALIZER;
    virBufferPtr buf = &bufinit;
    char *addstr = NULL, *bufret = NULL;
    int ret = -1;
    const struct testInfo *info = data;

    virBufferAddChar(buf, 'a');

    /*
E
Eric Blake 已提交
36
     * Infinite loop used to trigger if:
37
     * (strlen + 1 > 1000) && (strlen == buf-size - buf-use - 1)
E
Eric Blake 已提交
38 39
     * which was the case after the above addchar at the time of the bug.
     * This test is a bit fragile, since it relies on virBuffer internals.
40
     */
41
    if (virAsprintf(&addstr, "%*s", buf->a - buf->b - 1, "a") < 0)
42 43 44 45 46
        goto out;

    if (info->doEscape)
        virBufferEscapeString(buf, "%s", addstr);
    else
47
        virBufferAsprintf(buf, "%s", addstr);
48 49

    ret = 0;
50
 out:
51 52 53 54 55 56 57 58 59 60 61
    bufret = virBufferContentAndReset(buf);
    if (!bufret) {
        TEST_ERROR("Buffer had error set");
        ret = -1;
    }

    VIR_FREE(addstr);
    VIR_FREE(bufret);
    return ret;
}

E
Eric Blake 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static int testBufAutoIndent(const void *data ATTRIBUTE_UNUSED)
{
    virBuffer bufinit = VIR_BUFFER_INITIALIZER;
    virBufferPtr buf = &bufinit;
    const char expected[] =
        "  1\n  2\n  3\n  4\n  5\n  6\n  7\n  &amp;\n  8\n  9\n  10\n  ' 11'\n";
    char *result = NULL;
    int ret = 0;

    if (virBufferGetIndent(buf, false) != 0 ||
        virBufferGetIndent(buf, true) != 0) {
        TEST_ERROR("Wrong indentation");
        ret = -1;
    }
    virBufferAdjustIndent(buf, 3);
77 78 79 80
    if (STRNEQ(virBufferCurrentContent(buf), "")) {
        TEST_ERROR("Wrong content");
        ret = -1;
    }
E
Eric Blake 已提交
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
    if (virBufferGetIndent(buf, false) != 3 ||
        virBufferGetIndent(buf, true) != 3 ||
        virBufferError(buf)) {
        TEST_ERROR("Wrong indentation");
        ret = -1;
    }
    virBufferAdjustIndent(buf, -2);
    if (virBufferGetIndent(buf, false) != 1 ||
        virBufferGetIndent(buf, true) != 1 ||
        virBufferError(buf)) {
        TEST_ERROR("Wrong indentation");
        ret = -1;
    }
    virBufferAdjustIndent(buf, -3);
    if (virBufferGetIndent(buf, false) != -1 ||
        virBufferGetIndent(buf, true) != -1 ||
        virBufferError(buf) != -1) {
        TEST_ERROR("Usage error not flagged");
        ret = -1;
    }
    virBufferFreeAndReset(buf);
    if (virBufferGetIndent(buf, false) != 0 ||
        virBufferGetIndent(buf, true) != 0 ||
        virBufferError(buf)) {
        TEST_ERROR("Reset didn't clear indentation");
        ret = -1;
    }
    virBufferAdjustIndent(buf, 2);
    virBufferAddLit(buf, "1");
110 111 112 113
    if (virBufferError(buf)) {
        TEST_ERROR("Buffer had error");
        return -1;
    }
114 115 116 117
    if (STRNEQ(virBufferCurrentContent(buf), "  1")) {
        TEST_ERROR("Wrong content");
        ret = -1;
    }
E
Eric Blake 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    if (virBufferGetIndent(buf, false) != 2 ||
        virBufferGetIndent(buf, true) != 0) {
        TEST_ERROR("Wrong indentation");
        ret = -1;
    }
    virBufferAddLit(buf, "\n");
    virBufferAdd(buf, "" "2\n", -1); /* Extra "" appeases syntax-check */
    virBufferAddChar(buf, '3');
    virBufferAddChar(buf, '\n');
    virBufferAsprintf(buf, "%d", 4);
    virBufferAsprintf(buf, "%c", '\n');
    virBufferStrcat(buf, "5", "\n", "6\n", NULL);
    virBufferEscapeString(buf, "%s\n", "7");
    virBufferEscapeString(buf, "%s\n", "&");
    virBufferEscapeSexpr(buf, "%s", "8\n");
    virBufferURIEncodeString(buf, "9");
    virBufferAddChar(buf, '\n');
    virBufferEscapeShell(buf, "10");
    virBufferAddChar(buf, '\n');
    virBufferEscapeShell(buf, " 11");
    virBufferAddChar(buf, '\n');

140 141 142 143 144
    if (virBufferError(buf)) {
        TEST_ERROR("Buffer had error");
        return -1;
    }

E
Eric Blake 已提交
145 146 147 148 149 150 151 152 153
    result = virBufferContentAndReset(buf);
    if (!result || STRNEQ(result, expected)) {
        virtTestDifference(stderr, expected, result);
        ret = -1;
    }
    VIR_FREE(result);
    return ret;
}

154 155 156 157 158 159 160
static int testBufTrim(const void *data ATTRIBUTE_UNUSED)
{
    virBuffer bufinit = VIR_BUFFER_INITIALIZER;
    virBufferPtr buf = NULL;
    char *result = NULL;
    const char *expected = "a,b";
    int ret = -1;
J
Ján Tomko 已提交
161 162

    virBufferTrim(buf, "", 0);
163 164 165
    buf = &bufinit;

    virBufferAddLit(buf, "a;");
J
Ján Tomko 已提交
166 167 168 169 170
    virBufferTrim(buf, "", 0);
    virBufferTrim(buf, "", -1);
    virBufferTrim(buf, NULL, 1);
    virBufferTrim(buf, NULL, 5);
    virBufferTrim(buf, "a", 2);
171 172

    virBufferAddLit(buf, ",b,,");
J
Ján Tomko 已提交
173 174 175
    virBufferTrim(buf, "b", -1);
    virBufferTrim(buf, "b,,", 1);
    virBufferTrim(buf, ",", -1);
176

177 178 179 180 181
    if (virBufferError(buf)) {
        TEST_ERROR("Buffer had error");
        return -1;
    }

182 183 184 185 186 187
    result = virBufferContentAndReset(buf);
    if (!result || STRNEQ(result, expected)) {
        virtTestDifference(stderr, expected, result);
        goto cleanup;
    }

J
Ján Tomko 已提交
188 189 190 191 192 193
    virBufferTrim(buf, NULL, -1);
    if (virBufferError(buf) != -1) {
        TEST_ERROR("Usage error not flagged");
        goto cleanup;
    }

194 195
    ret = 0;

196
 cleanup:
197 198 199 200 201
    virBufferFreeAndReset(buf);
    VIR_FREE(result);
    return ret;
}

202 203 204 205 206 207 208 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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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
static int testBufAddBuffer(const void *data ATTRIBUTE_UNUSED)
{
    virBuffer buf1 = VIR_BUFFER_INITIALIZER;
    virBuffer buf2 = VIR_BUFFER_INITIALIZER;
    virBuffer buf3 = VIR_BUFFER_INITIALIZER;
    int ret = -1;
    char *result = NULL;
    const char *expected = \
"  A long time ago, in a galaxy far,\n" \
"  far away...\n"                       \
"    It is a period of civil war.\n"    \
"    Rebel spaceships, striking\n"      \
"    from a hidden base, have won\n"    \
"    their first victory against\n"     \
"    the evil Galactic Empire.\n"       \
"  During the battle, rebel\n"          \
"  spies managed to steal secret\n"     \
"  plans to the Empire's\n"             \
"  ultimate weapon, the DEATH\n"        \
"  STAR, an armored space\n"            \
"  station with enough power to\n"      \
"  destroy an entire planet.\n";

    if (virBufferUse(&buf1)) {
        TEST_ERROR("buf1 already in use");
        goto cleanup;
    }

    if (virBufferUse(&buf2)) {
        TEST_ERROR("buf2 already in use");
        goto cleanup;
    }

    if (virBufferUse(&buf3)) {
        TEST_ERROR("buf3 already in use");
        goto cleanup;
    }

    virBufferAdjustIndent(&buf1, 2);
    virBufferAddLit(&buf1, "A long time ago, in a galaxy far,\n");
    virBufferAddLit(&buf1, "far away...\n");

    virBufferAdjustIndent(&buf2, 4);
    virBufferAddLit(&buf2, "It is a period of civil war.\n");
    virBufferAddLit(&buf2, "Rebel spaceships, striking\n");
    virBufferAddLit(&buf2, "from a hidden base, have won\n");
    virBufferAddLit(&buf2, "their first victory against\n");
    virBufferAddLit(&buf2, "the evil Galactic Empire.\n");

    virBufferAdjustIndent(&buf3, 2);
    virBufferAddLit(&buf3, "During the battle, rebel\n");
    virBufferAddLit(&buf3, "spies managed to steal secret\n");
    virBufferAddLit(&buf3, "plans to the Empire's\n");
    virBufferAddLit(&buf3, "ultimate weapon, the DEATH\n");
    virBufferAddLit(&buf3, "STAR, an armored space\n");
    virBufferAddLit(&buf3, "station with enough power to\n");
    virBufferAddLit(&buf3, "destroy an entire planet.\n");

    if (!virBufferUse(&buf1)) {
        TEST_ERROR("Error adding to buf1");
        goto cleanup;
    }

    if (!virBufferUse(&buf2)) {
        TEST_ERROR("Error adding to buf2");
        goto cleanup;
    }

    if (!virBufferUse(&buf3)) {
        TEST_ERROR("Error adding to buf3");
        goto cleanup;
    }

    virBufferAddBuffer(&buf2, &buf3);

    if (!virBufferUse(&buf2)) {
        TEST_ERROR("buf2 cleared mistakenly");
        goto cleanup;
    }

    if (virBufferUse(&buf3)) {
        TEST_ERROR("buf3 is not clear even though it should be");
        goto cleanup;
    }

    virBufferAddBuffer(&buf1, &buf2);

    if (!virBufferUse(&buf1)) {
        TEST_ERROR("buf1 cleared mistakenly");
        goto cleanup;
    }

    if (virBufferUse(&buf2)) {
        TEST_ERROR("buf2 is not clear even though it should be");
        goto cleanup;
    }

    result = virBufferContentAndReset(&buf1);
    if (STRNEQ_NULLABLE(result, expected)) {
        virtTestDifference(stderr, expected, result);
        goto cleanup;
    }

    ret = 0;
 cleanup:
    virBufferFreeAndReset(&buf1);
    virBufferFreeAndReset(&buf2);
    VIR_FREE(result);
    return ret;
}

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
struct testBufAddStrData {
    const char *data;
    const char *expect;
};

static int
testBufAddStr(const void *opaque ATTRIBUTE_UNUSED)
{
    const struct testBufAddStrData *data = opaque;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *actual;
    int ret = -1;

    virBufferAddLit(&buf, "<c>\n");
    virBufferAdjustIndent(&buf, 2);
    virBufferAddStr(&buf, data->data);
    virBufferAdjustIndent(&buf, -2);
    virBufferAddLit(&buf, "</c>");

    if (!(actual = virBufferContentAndReset(&buf))) {
        TEST_ERROR("buf is empty");
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(actual, data->expect)) {
        TEST_ERROR("testBufAddStr(): Strings don't match:\n");
        virtTestDifference(stderr, data->expect, actual);
        goto cleanup;
    }

    ret = 0;

 cleanup:
    VIR_FREE(actual);
    return ret;
}

350

351
static int
E
Eric Blake 已提交
352
mymain(void)
353 354 355 356
{
    int ret = 0;


357
#define DO_TEST(msg, cb, data)                                         \
358 359
    do {                                                               \
        struct testInfo info = { data };                               \
360
        if (virtTestRun("Buf: " msg, cb, &info) < 0)                   \
361 362 363 364 365
            ret = -1;                                                  \
    } while (0)

    DO_TEST("EscapeString infinite loop", testBufInfiniteLoop, 1);
    DO_TEST("VSprintf infinite loop", testBufInfiniteLoop, 0);
E
Eric Blake 已提交
366
    DO_TEST("Auto-indentation", testBufAutoIndent, 0);
367
    DO_TEST("Trim", testBufTrim, 0);
368
    DO_TEST("AddBuffer", testBufAddBuffer, 0);
369

370 371 372 373 374 375 376 377 378 379 380 381
#define DO_TEST_ADD_STR(DATA, EXPECT)                                  \
    do {                                                               \
        struct testBufAddStrData info = { DATA, EXPECT };              \
        if (virtTestRun("Buf: AddStr", testBufAddStr, &info) < 0)      \
            ret = -1;                                                  \
    } while (0)

    DO_TEST_ADD_STR("", "<c>\n</c>");
    DO_TEST_ADD_STR("<a/>", "<c>\n  <a/></c>");
    DO_TEST_ADD_STR("<a/>\n", "<c>\n  <a/>\n</c>");
    DO_TEST_ADD_STR("<b>\n  <a/>\n</b>\n", "<c>\n  <b>\n    <a/>\n  </b>\n</c>");

382
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
383 384 385
}

VIRT_TEST_MAIN(mymain)