2 This file is part of systemd.
4 Copyright 2014 systemd developers
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 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 License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 static int noop_dispatcher(int argc, char *argv[], void *userdata) {
28 #define test_dispatch_one(argv, verbs, expected) \
30 assert_se(dispatch_verb(strv_length(argv), argv, verbs, NULL) == expected);
32 static void test_verbs(void) {
33 static const Verb verbs[] = {
34 { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher },
35 { "list-images", VERB_ANY, 1, 0, noop_dispatcher },
36 { "list", VERB_ANY, 2, VERB_DEFAULT, noop_dispatcher },
37 { "status", 2, VERB_ANY, 0, noop_dispatcher },
38 { "show", VERB_ANY, VERB_ANY, 0, noop_dispatcher },
39 { "terminate", 2, VERB_ANY, 0, noop_dispatcher },
40 { "login", 2, 2, 0, noop_dispatcher },
41 { "copy-to", 3, 4, 0, noop_dispatcher },
46 test_dispatch_one(STRV_MAKE("command-not-found"), verbs, -EINVAL);
49 test_dispatch_one(STRV_MAKE("show"), verbs, 0);
51 /* found, too few args */
52 test_dispatch_one(STRV_MAKE("copy-to", "foo"), verbs, -EINVAL);
54 /* found, meets min args */
55 test_dispatch_one(STRV_MAKE("status", "foo", "bar"), verbs, 0);
57 /* found, too many args */
58 test_dispatch_one(STRV_MAKE("copy-to", "foo", "bar", "baz", "quux", "qaax"), verbs, -EINVAL);
60 /* no verb, but a default is set */
61 test_dispatch_one(STRV_MAKE_EMPTY, verbs, 0);
64 static void test_verbs_no_default(void) {
65 static const Verb verbs[] = {
66 { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher },
70 test_dispatch_one(STRV_MAKE(NULL), verbs, -EINVAL);
73 int main(int argc, char *argv[]) {
75 test_verbs_no_default();