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

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

#include "internal.h"
#include "util.h"
#include "testutils.h"
10
#include "virbuffer.h"
11 12 13 14 15 16 17 18 19 20 21 22
#include "memory.h"

#define TEST_ERROR(...)                             \
    do {                                            \
        if (virTestGetDebug())                      \
            fprintf(stderr, __VA_ARGS__);           \
    } while (0)

struct testInfo {
    int doEscape;
};

E
Eric Blake 已提交
23
static int testBufInfiniteLoop(const void *data)
24 25 26 27 28 29 30 31 32 33
{
    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 已提交
34
     * Infinite loop used to trigger if:
35
     * (strlen + 1 > 1000) && (strlen == buf-size - buf-use - 1)
E
Eric Blake 已提交
36 37
     * 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.
38 39 40 41 42 43 44 45
     */
    if (virAsprintf(&addstr, "%*s", buf->a - buf->b - 1, "a") < 0) {
        goto out;
    }

    if (info->doEscape)
        virBufferEscapeString(buf, "%s", addstr);
    else
46
        virBufferAsprintf(buf, "%s", addstr);
47 48 49 50 51 52 53 54 55 56 57 58 59 60

    ret = 0;
out:
    bufret = virBufferContentAndReset(buf);
    if (!bufret) {
        TEST_ERROR("Buffer had error set");
        ret = -1;
    }

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

E
Eric Blake 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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);
76 77 78 79
    if (STRNEQ(virBufferCurrentContent(buf), "")) {
        TEST_ERROR("Wrong content");
        ret = -1;
    }
E
Eric Blake 已提交
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
    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");
109 110 111 112
    if (STRNEQ(virBufferCurrentContent(buf), "  1")) {
        TEST_ERROR("Wrong content");
        ret = -1;
    }
E
Eric Blake 已提交
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
    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');

    result = virBufferContentAndReset(buf);
    if (!result || STRNEQ(result, expected)) {
        virtTestDifference(stderr, expected, result);
        ret = -1;
    }
    VIR_FREE(result);
    return ret;
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
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;
    int i = 1;

#define ACT(str, len, result) \
    do {                                          \
        if (virBufferTrim(buf, str, len) != result) {   \
            TEST_ERROR("trim %d failed", i);            \
            goto cleanup;                               \
        }                                               \
        i++;                                            \
    } while (0);

    if (virBufferTrim(buf, "", 0) != -1) {
        TEST_ERROR("Wrong failure detection 1");
        goto cleanup;
    }
    buf = &bufinit;
    if (virBufferTrim(buf, NULL, -1) != -1) {
        TEST_ERROR("Wrong failure detection 2");
        goto cleanup;
    }

    virBufferAddLit(buf, "a;");
    ACT("", 0, 1);
    ACT("", -1, 1);
    ACT(NULL, 1, 1);
    ACT(NULL, 5, 0);
    ACT("a", 2, 0);

    virBufferAddLit(buf, ",b,,");
    ACT("b", -1, 0);
    ACT("b,,", 1, 1);
    ACT(",", -1, 1);

    result = virBufferContentAndReset(buf);
    if (!result || STRNEQ(result, expected)) {
        virtTestDifference(stderr, expected, result);
        goto cleanup;
    }

    ret = 0;

cleanup:
    virBufferFreeAndReset(buf);
    VIR_FREE(result);
    return ret;
}


199
static int
E
Eric Blake 已提交
200
mymain(void)
201 202 203 204
{
    int ret = 0;


205
#define DO_TEST(msg, cb, data)                                         \
206 207 208 209 210 211 212 213
    do {                                                               \
        struct testInfo info = { data };                               \
        if (virtTestRun("Buf: " msg, 1, cb, &info) < 0)                 \
            ret = -1;                                                  \
    } while (0)

    DO_TEST("EscapeString infinite loop", testBufInfiniteLoop, 1);
    DO_TEST("VSprintf infinite loop", testBufInfiniteLoop, 0);
E
Eric Blake 已提交
214
    DO_TEST("Auto-indentation", testBufAutoIndent, 0);
215
    DO_TEST("Trim", testBufTrim, 0);
216

217
    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
218 219 220
}

VIRT_TEST_MAIN(mymain)