aboutsummaryrefslogtreecommitdiff
path: root/tests/test-keyval.c
blob: 27f66258c98443316c0142f85dd1d1c1fa1e74af (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
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
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
/*
 * Unit tests for parsing of KEY=VALUE,... strings
 *
 * Copyright (C) 2017 Red Hat Inc.
 *
 * Authors:
 *  Markus Armbruster <armbru@redhat.com>,
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/option.h"

static void test_keyval_parse(void)
{
    Error *err = NULL;
    QDict *qdict, *sub_qdict;
    char long_key[129];
    char *params;

    /* Nothing */
    qdict = keyval_parse("", NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 0);
    QDECREF(qdict);

    /* Empty key (qemu_opts_parse() accepts this) */
    qdict = keyval_parse("=val", NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Empty key fragment */
    qdict = keyval_parse(".", NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);
    qdict = keyval_parse("key.", NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Overlong key */
    memset(long_key, 'a', 127);
    long_key[127] = 'z';
    long_key[128] = 0;
    params = g_strdup_printf("k.%s=v", long_key);
    qdict = keyval_parse(params + 2, NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Overlong key fragment */
    qdict = keyval_parse(params, NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);
    g_free(params);

    /* Long key (qemu_opts_parse() accepts and truncates silently) */
    params = g_strdup_printf("k.%s=v", long_key + 1);
    qdict = keyval_parse(params + 2, NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 1);
    g_assert_cmpstr(qdict_get_try_str(qdict, long_key + 1), ==, "v");
    QDECREF(qdict);

    /* Long key fragment */
    qdict = keyval_parse(params, NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 1);
    sub_qdict = qdict_get_qdict(qdict, "k");
    g_assert(sub_qdict);
    g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
    g_assert_cmpstr(qdict_get_try_str(sub_qdict, long_key + 1), ==, "v");
    QDECREF(qdict);
    g_free(params);

    /* Multiple keys, last one wins */
    qdict = keyval_parse("a=1,b=2,,x,a=3", NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 2);
    g_assert_cmpstr(qdict_get_try_str(qdict, "a"), ==, "3");
    g_assert_cmpstr(qdict_get_try_str(qdict, "b"), ==, "2,x");
    QDECREF(qdict);

    /* Even when it doesn't in qemu_opts_parse() */
    qdict = keyval_parse("id=foo,id=bar", NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 1);
    g_assert_cmpstr(qdict_get_try_str(qdict, "id"), ==, "bar");
    QDECREF(qdict);

    /* Dotted keys */
    qdict = keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 2);
    sub_qdict = qdict_get_qdict(qdict, "a");
    g_assert(sub_qdict);
    g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
    sub_qdict = qdict_get_qdict(sub_qdict, "b");
    g_assert(sub_qdict);
    g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
    g_assert_cmpstr(qdict_get_try_str(sub_qdict, "c"), ==, "2");
    g_assert_cmpstr(qdict_get_try_str(qdict, "d"), ==, "3");
    QDECREF(qdict);

    /* Inconsistent dotted keys */
    qdict = keyval_parse("a.b=1,a=2", NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);
    qdict = keyval_parse("a.b=1,a.b.c=2", NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Trailing comma is ignored */
    qdict = keyval_parse("x=y,", NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 1);
    g_assert_cmpstr(qdict_get_try_str(qdict, "x"), ==, "y");
    QDECREF(qdict);

    /* Except when it isn't */
    qdict = keyval_parse(",", NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Value containing ,id= not misinterpreted as qemu_opts_parse() does */
    qdict = keyval_parse("x=,,id=bar", NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 1);
    g_assert_cmpstr(qdict_get_try_str(qdict, "x"), ==, ",id=bar");
    QDECREF(qdict);

    /* Anti-social ID is left to caller (qemu_opts_parse() rejects it) */
    qdict = keyval_parse("id=666", NULL, &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 1);
    g_assert_cmpstr(qdict_get_try_str(qdict, "id"), ==, "666");
    QDECREF(qdict);

    /* Implied value not supported (unlike qemu_opts_parse()) */
    qdict = keyval_parse("an,noaus,noaus=", NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Implied value, key "no" (qemu_opts_parse(): negated empty key) */
    qdict = keyval_parse("no", NULL, &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Implied key */
    qdict = keyval_parse("an,aus=off,noaus=", "implied", &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 3);
    g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "an");
    g_assert_cmpstr(qdict_get_try_str(qdict, "aus"), ==, "off");
    g_assert_cmpstr(qdict_get_try_str(qdict, "noaus"), ==, "");
    QDECREF(qdict);

    /* Implied dotted key */
    qdict = keyval_parse("val", "eins.zwei", &error_abort);
    g_assert_cmpuint(qdict_size(qdict), ==, 1);
    sub_qdict = qdict_get_qdict(qdict, "eins");
    g_assert(sub_qdict);
    g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
    g_assert_cmpstr(qdict_get_try_str(sub_qdict, "zwei"), ==, "val");
    QDECREF(qdict);

    /* Implied key with empty value (qemu_opts_parse() accepts this) */
    qdict = keyval_parse(",", "implied", &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Likewise (qemu_opts_parse(): implied key with comma value) */
    qdict = keyval_parse(",,,a=1", "implied", &err);
    error_free_or_abort(&err);
    g_assert(!qdict);

    /* Empty key is not an implied key */
    qdict = keyval_parse("=val", "implied", &err);
    error_free_or_abort(&err);
    g_assert(!qdict);
}

int main(int argc, char *argv[])
{
    g_test_init(&argc, &argv, NULL);
    g_test_add_func("/keyval/keyval_parse", test_keyval_parse);
    g_test_run();
    return 0;
}