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