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