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/>.
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 */
33 /* Flags of the "foobar" parameter check */
35 /* Parameters to validate */
36 virTypedParameterPtr params
;
37 /* Amount of parameters */
40 /* Expected error code */
42 /* Expected error message */
43 const char *expected_errmessage
;
47 testTypedParamsValidate(const void *opaque
)
50 TypedParameterTest
*test
= (TypedParameterTest
*)opaque
;
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
,
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
);
69 printf("%s\n", errptr
->message
);
76 #define PARAMS_ARRAY(...) ((virTypedParameter[]){ __VA_ARGS__ })
77 #define PARAMS_SIZE(...) ARRAY_CARDINALITY(PARAMS_ARRAY(__VA_ARGS__))
80 .params = PARAMS_ARRAY(__VA_ARGS__), \
81 .nparams = PARAMS_SIZE(__VA_ARGS__),
84 testTypedParamsFilter(const void *opaque ATTRIBUTE_UNUSED
)
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
),
104 for (i
= 0; i
< nfiltered
; i
++) {
105 if (filtered
[i
] != ¶ms
[1 + i
* 2])
111 nfiltered
= virTypedParamsFilter(params
, ARRAY_CARDINALITY(params
),
117 for (i
= 0; i
< nfiltered
; i
++) {
118 if (filtered
[i
] != ¶ms
[i
* 2])
129 testTypedParamsAddStringList(const void *opaque ATTRIBUTE_UNUSED
)
132 virTypedParameterPtr params
= NULL
;
133 int nparams
= 0, maxparams
= 0, i
;
135 const char *values
[] = {
136 "foo", "bar", "foobar", NULL
139 rv
= virTypedParamsAddStringList(¶ms
, &nparams
, &maxparams
, "param",
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
)
149 virTypedParamsFree(params
, nparams
);
154 testTypedParamsGetStringList(const void *opaque ATTRIBUTE_UNUSED
)
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
),
185 for (i
= 0; i
< picked
; i
++) {
187 if (strings
[i
] != NULL
)
191 if (STRNEQLEN(strings
[i
], "bar", 3))
193 if (strings
[i
][3] != l
++)
204 testTypedParamsValidator(void)
209 TypedParameterTest test
[] = {
211 .name
= "Invalid arg type",
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'"
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",
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",
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
,
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
,
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
,
279 for (i
= 0; test
[i
].name
; ++i
) {
280 if (virTestRun(test
[i
].name
, testTypedParamsValidate
, &test
[i
]) < 0)
292 if (testTypedParamsValidator() < 0)
295 if (virTestRun("Filtering", testTypedParamsFilter
, NULL
) < 0)
298 if (virTestRun("Get All Strings", testTypedParamsGetStringList
, NULL
) < 0)
301 if (virTestRun("Add string list", testTypedParamsAddStringList
, NULL
) < 0)
309 VIR_TEST_MAIN(mymain
)