summaryrefslogtreecommitdiff
blob: 82f861b36ba927892dade7d303f9c5d36151100a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
#include <config.h>

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

#ifdef WITH_QEMU

# include "internal.h"
# include "memory.h"
# include "testutils.h"
# include "util.h"
# include "qemu/qemu_monitor.h"

struct testEscapeString
{
    const char *unescaped;
    const char *escaped;
};

static struct testEscapeString escapeStrings[] = {
    { "", "" },
    { " ", " " },
    { "\\", "\\\\" },
    { "\n", "\\n" },
    { "\r", "\\r" },
    { "\"", "\\\"" },
    { "\"\"\"\\\\\n\r\\\\\n\r\"\"\"", "\\\"\\\"\\\"\\\\\\\\\\n\\r\\\\\\\\\\n\\r\\\"\\\"\\\"" },
    { "drive_add dummy file=foo\\", "drive_add dummy file=foo\\\\" },
    { "block info", "block info" },
    { "set_password \":\\\"\"", "set_password \\\":\\\\\\\"\\\"" },
};

static int testEscapeArg(const void *data ATTRIBUTE_UNUSED)
{
    int i;
    char *escaped = NULL;
    for (i = 0; i < ARRAY_CARDINALITY(escapeStrings); ++i) {
        escaped = qemuMonitorEscapeArg(escapeStrings[i].unescaped);
        if (!escaped) {
            if (virTestGetDebug() > 0) {
                fprintf(stderr, "\nUnescaped string [%s]\n",
                        escapeStrings[i].unescaped);
                fprintf(stderr, "Expect result [%s]\n",
                        escapeStrings[i].escaped);
                fprintf(stderr, "Actual result [(null)]\n");
            }
            return -1;
        }
        if (STRNEQ(escapeStrings[i].escaped, escaped)) {
            virtTestDifference(stderr, escapeStrings[i].escaped, escaped);
            VIR_FREE(escaped);
            return -1;
        }
        VIR_FREE(escaped);
    }

    return 0;
}

static int testUnescapeArg(const void *data ATTRIBUTE_UNUSED)
{
    int i;
    char *unescaped = NULL;
    for (i = 0; i < ARRAY_CARDINALITY(escapeStrings); ++i) {
        unescaped = qemuMonitorUnescapeArg(escapeStrings[i].escaped);
        if (!unescaped) {
            if (virTestGetDebug() > 0) {
                fprintf(stderr, "\nEscaped string [%s]\n",
                        escapeStrings[i].escaped);
                fprintf(stderr, "Expect result [%s]\n",
                        escapeStrings[i].unescaped);
                fprintf(stderr, "Actual result [(null)]\n");
            }
            return -1;
        }
        if (STRNEQ(escapeStrings[i].unescaped, unescaped)) {
            virtTestDifference(stderr, escapeStrings[i].unescaped, unescaped);
            VIR_FREE(unescaped);
            return -1;
        }
        VIR_FREE(unescaped);
    }

    return 0;
}

static int
mymain(void)
{
    int result = 0;

# define DO_TEST(_name)                                                 \
    do {                                                                \
        if (virtTestRun("qemu monitor "#_name, 1, test##_name,          \
                        NULL) < 0) {                                    \
            result = -1;                                                \
        }                                                               \
    } while (0)

    DO_TEST(EscapeArg);
    DO_TEST(UnescapeArg);

    return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)

#else
# include "testutils.h"

int main(void)
{
    return EXIT_AM_SKIP;
}

#endif /* WITH_QEMU */