]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-verbs.c
tree-wide: various ubsan zero size memory fixes
[thirdparty/systemd.git] / src / test / test-verbs.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d2f0e78f
DR
2
3#include "macro.h"
4#include "strv.h"
5#include "verbs.h"
6
7static int noop_dispatcher(int argc, char *argv[], void *userdata) {
8 return 0;
9}
10
11#define test_dispatch_one(argv, verbs, expected) \
12 optind = 0; \
13 assert_se(dispatch_verb(strv_length(argv), argv, verbs, NULL) == expected);
14
15static void test_verbs(void) {
16 static const Verb verbs[] = {
17 { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher },
18 { "list-images", VERB_ANY, 1, 0, noop_dispatcher },
19 { "list", VERB_ANY, 2, VERB_DEFAULT, noop_dispatcher },
20 { "status", 2, VERB_ANY, 0, noop_dispatcher },
21 { "show", VERB_ANY, VERB_ANY, 0, noop_dispatcher },
22 { "terminate", 2, VERB_ANY, 0, noop_dispatcher },
23 { "login", 2, 2, 0, noop_dispatcher },
24 { "copy-to", 3, 4, 0, noop_dispatcher },
25 {}
26 };
27
28 /* not found */
29 test_dispatch_one(STRV_MAKE("command-not-found"), verbs, -EINVAL);
30
31 /* found */
32 test_dispatch_one(STRV_MAKE("show"), verbs, 0);
33
34 /* found, too few args */
35 test_dispatch_one(STRV_MAKE("copy-to", "foo"), verbs, -EINVAL);
36
37 /* found, meets min args */
38 test_dispatch_one(STRV_MAKE("status", "foo", "bar"), verbs, 0);
39
40 /* found, too many args */
41 test_dispatch_one(STRV_MAKE("copy-to", "foo", "bar", "baz", "quux", "qaax"), verbs, -EINVAL);
42
43 /* no verb, but a default is set */
44 test_dispatch_one(STRV_MAKE_EMPTY, verbs, 0);
45}
46
47static void test_verbs_no_default(void) {
48 static const Verb verbs[] = {
49 { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher },
50 {},
51 };
52
53 test_dispatch_one(STRV_MAKE(NULL), verbs, -EINVAL);
54}
55
56int main(int argc, char *argv[]) {
57 test_verbs();
58 test_verbs_no_default();
59
60 return 0;
61}