backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virtypedparamtest.c
blobec389c65aa4397e9210c83e670a81f1384f4e4a4
1 /*
2 * virtypedparamtest.c: Test typed param functions
4 * Copyright (C) 2015 Mirantis, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #include <config.h>
24 #include <virtypedparam.h>
26 #include "testutils.h"
28 #define VIR_FROM_THIS VIR_FROM_NONE
30 typedef struct _TypedParameterTest {
31 /* Test name for logging */
32 const char *name;
33 /* Flags of the "foobar" parameter check */
34 int foobar_flags;
35 /* Parameters to validate */
36 virTypedParameterPtr params;
37 /* Amount of parameters */
38 int nparams;
40 /* Expected error code */
41 int expected_errcode;
42 /* Expected error message */
43 const char *expected_errmessage;
44 } TypedParameterTest;
46 static int
47 testTypedParamsValidate(const void *opaque)
49 int rv;
50 TypedParameterTest *test = (TypedParameterTest *)opaque;
51 virErrorPtr errptr;
53 rv = virTypedParamsValidate(
54 test->params, test->nparams,
55 "foobar", VIR_TYPED_PARAM_STRING | test->foobar_flags,
56 "foo", VIR_TYPED_PARAM_INT,
57 "bar", VIR_TYPED_PARAM_UINT,
58 "zzz", VIR_TYPED_PARAM_UINT,
59 NULL);
61 if (test->expected_errcode) {
62 errptr = virGetLastError();
64 rv = (errptr == NULL) || ((rv < 0) &&
65 !(errptr->code == test->expected_errcode));
66 if (errptr && test->expected_errmessage) {
67 rv = STRNEQ(test->expected_errmessage, errptr->message);
68 if (rv)
69 printf("%s\n", errptr->message);
73 return rv;
76 #define PARAMS_ARRAY(...) ((virTypedParameter[]){ __VA_ARGS__ })
77 #define PARAMS_SIZE(...) ARRAY_CARDINALITY(PARAMS_ARRAY(__VA_ARGS__))
79 #define PARAMS(...) \
80 .params = PARAMS_ARRAY(__VA_ARGS__), \
81 .nparams = PARAMS_SIZE(__VA_ARGS__),
83 static int
84 testTypedParamsFilter(const void *opaque ATTRIBUTE_UNUSED)
86 size_t i, nfiltered;
87 int rv = -1;
88 virTypedParameter params[] = {
89 { .field = "bar", .type = VIR_TYPED_PARAM_UINT },
90 { .field = "foo", .type = VIR_TYPED_PARAM_INT },
91 { .field = "bar", .type = VIR_TYPED_PARAM_UINT },
92 { .field = "foo", .type = VIR_TYPED_PARAM_INT },
93 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
94 { .field = "foo", .type = VIR_TYPED_PARAM_INT }
96 virTypedParameterPtr *filtered = NULL;
99 nfiltered = virTypedParamsFilter(params, ARRAY_CARDINALITY(params),
100 "foo", &filtered);
101 if (nfiltered != 3)
102 goto cleanup;
104 for (i = 0; i < nfiltered; i++) {
105 if (filtered[i] != &params[1 + i * 2])
106 goto cleanup;
108 VIR_FREE(filtered);
109 filtered = NULL;
111 nfiltered = virTypedParamsFilter(params, ARRAY_CARDINALITY(params),
112 "bar", &filtered);
114 if (nfiltered != 2)
115 goto cleanup;
117 for (i = 0; i < nfiltered; i++) {
118 if (filtered[i] != &params[i * 2])
119 goto cleanup;
122 rv = 0;
123 cleanup:
124 VIR_FREE(filtered);
125 return rv;
128 static int
129 testTypedParamsAddStringList(const void *opaque ATTRIBUTE_UNUSED)
131 int rv = 0;
132 virTypedParameterPtr params = NULL;
133 int nparams = 0, maxparams = 0, i;
135 const char *values[] = {
136 "foo", "bar", "foobar", NULL
139 rv = virTypedParamsAddStringList(&params, &nparams, &maxparams, "param",
140 values);
142 for (i = 0; i < nparams; i++) {
143 if (STRNEQ(params[i].field, "param") ||
144 STRNEQ(params[i].value.s, values[i]) ||
145 params[i].type != VIR_TYPED_PARAM_STRING)
146 rv = -1;
149 virTypedParamsFree(params, nparams);
150 return rv;
153 static int
154 testTypedParamsGetStringList(const void *opaque ATTRIBUTE_UNUSED)
156 size_t i;
157 int picked;
158 int rv = -1;
159 char l = '1';
160 const char **strings = NULL;
162 virTypedParameter params[] = {
163 { .field = "bar", .type = VIR_TYPED_PARAM_STRING,
164 .value = { .s = (char*)"bar1"} },
165 { .field = "foo", .type = VIR_TYPED_PARAM_INT },
166 { .field = "bar", .type = VIR_TYPED_PARAM_STRING,
167 .value = { .s = (char*)"bar2"} },
168 { .field = "foo", .type = VIR_TYPED_PARAM_INT },
169 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
170 { .field = "bar", .type = VIR_TYPED_PARAM_STRING,
171 .value = { .s = NULL } },
172 { .field = "foo", .type = VIR_TYPED_PARAM_INT },
173 { .field = "bar", .type = VIR_TYPED_PARAM_STRING,
174 .value = { .s = (char*)"bar3"} }
177 picked = virTypedParamsGetStringList(params,
178 ARRAY_CARDINALITY(params),
179 "bar",
180 &strings);
182 if (picked < 0)
183 goto cleanup;
185 for (i = 0; i < picked; i++) {
186 if (i == 2) {
187 if (strings[i] != NULL)
188 goto cleanup;
189 continue;
191 if (STRNEQLEN(strings[i], "bar", 3))
192 goto cleanup;
193 if (strings[i][3] != l++)
194 goto cleanup;
197 rv = 0;
198 cleanup:
199 VIR_FREE(strings);
200 return rv;
203 static int
204 testTypedParamsValidator(void)
206 size_t i;
207 int rv = 0;
209 TypedParameterTest test[] = {
211 .name = "Invalid arg type",
212 .foobar_flags = 0,
213 PARAMS({ .field = "foobar", .type = VIR_TYPED_PARAM_INT })
214 .expected_errcode = VIR_ERR_INVALID_ARG,
215 .expected_errmessage =
216 "invalid argument: invalid type 'int' for parameter "
217 "'foobar', expected 'string'"
220 .name = "Extra arg",
221 .foobar_flags = 0,
222 PARAMS({ .field = "f", .type = VIR_TYPED_PARAM_INT })
223 .expected_errcode = VIR_ERR_INVALID_ARG,
224 .expected_errmessage =
225 "argument unsupported: parameter 'f' not supported"
228 .name = "Valid parameters",
229 .foobar_flags = 0,
230 PARAMS(
231 { .field = "bar", .type = VIR_TYPED_PARAM_UINT },
232 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
233 { .field = "foo", .type = VIR_TYPED_PARAM_INT }
235 .expected_errcode = 0, .expected_errmessage = NULL,
238 .name = "Duplicates incorrect",
239 .foobar_flags = 0,
240 PARAMS(
241 { .field = "bar", .type = VIR_TYPED_PARAM_UINT },
242 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
243 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
244 { .field = "foo", .type = VIR_TYPED_PARAM_INT }
246 .expected_errcode = VIR_ERR_INVALID_ARG,
247 .expected_errmessage =
248 "invalid argument: parameter 'foobar' occurs multiple times"
251 .name = "Duplicates OK for marked",
252 .foobar_flags = VIR_TYPED_PARAM_MULTIPLE,
253 PARAMS(
254 { .field = "bar", .type = VIR_TYPED_PARAM_UINT },
255 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
256 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
257 { .field = "foo", .type = VIR_TYPED_PARAM_INT }
259 .expected_errcode = 0, .expected_errmessage = NULL,
262 .name = "BUG, non-duplicate marked as duplicate",
263 .foobar_flags = VIR_TYPED_PARAM_MULTIPLE,
264 PARAMS(
265 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
266 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
267 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
268 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
269 { .field = "foobar", .type = VIR_TYPED_PARAM_STRING },
270 { .field = "zzz", .type = VIR_TYPED_PARAM_UINT },
272 .expected_errcode = 0, .expected_errmessage = NULL,
275 .name = NULL
279 for (i = 0; test[i].name; ++i) {
280 if (virTestRun(test[i].name, testTypedParamsValidate, &test[i]) < 0)
281 rv = -1;
284 return rv;
287 static int
288 mymain(void)
290 int rv = 0;
292 if (testTypedParamsValidator() < 0)
293 rv = -1;
295 if (virTestRun("Filtering", testTypedParamsFilter, NULL) < 0)
296 rv = -1;
298 if (virTestRun("Get All Strings", testTypedParamsGetStringList, NULL) < 0)
299 rv = -1;
301 if (virTestRun("Add string list", testTypedParamsAddStringList, NULL) < 0)
302 rv = -1;
304 if (rv < 0)
305 return EXIT_FAILURE;
306 return EXIT_SUCCESS;
309 VIR_TEST_MAIN(mymain)