]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
d2f0e78f DR |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2014 systemd developers | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include "macro.h" | |
22 | #include "strv.h" | |
23 | #include "verbs.h" | |
24 | ||
25 | static int noop_dispatcher(int argc, char *argv[], void *userdata) { | |
26 | return 0; | |
27 | } | |
28 | ||
29 | #define test_dispatch_one(argv, verbs, expected) \ | |
30 | optind = 0; \ | |
31 | assert_se(dispatch_verb(strv_length(argv), argv, verbs, NULL) == expected); | |
32 | ||
33 | static void test_verbs(void) { | |
34 | static const Verb verbs[] = { | |
35 | { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, | |
36 | { "list-images", VERB_ANY, 1, 0, noop_dispatcher }, | |
37 | { "list", VERB_ANY, 2, VERB_DEFAULT, noop_dispatcher }, | |
38 | { "status", 2, VERB_ANY, 0, noop_dispatcher }, | |
39 | { "show", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, | |
40 | { "terminate", 2, VERB_ANY, 0, noop_dispatcher }, | |
41 | { "login", 2, 2, 0, noop_dispatcher }, | |
42 | { "copy-to", 3, 4, 0, noop_dispatcher }, | |
43 | {} | |
44 | }; | |
45 | ||
46 | /* not found */ | |
47 | test_dispatch_one(STRV_MAKE("command-not-found"), verbs, -EINVAL); | |
48 | ||
49 | /* found */ | |
50 | test_dispatch_one(STRV_MAKE("show"), verbs, 0); | |
51 | ||
52 | /* found, too few args */ | |
53 | test_dispatch_one(STRV_MAKE("copy-to", "foo"), verbs, -EINVAL); | |
54 | ||
55 | /* found, meets min args */ | |
56 | test_dispatch_one(STRV_MAKE("status", "foo", "bar"), verbs, 0); | |
57 | ||
58 | /* found, too many args */ | |
59 | test_dispatch_one(STRV_MAKE("copy-to", "foo", "bar", "baz", "quux", "qaax"), verbs, -EINVAL); | |
60 | ||
61 | /* no verb, but a default is set */ | |
62 | test_dispatch_one(STRV_MAKE_EMPTY, verbs, 0); | |
63 | } | |
64 | ||
65 | static void test_verbs_no_default(void) { | |
66 | static const Verb verbs[] = { | |
67 | { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, | |
68 | {}, | |
69 | }; | |
70 | ||
71 | test_dispatch_one(STRV_MAKE(NULL), verbs, -EINVAL); | |
72 | } | |
73 | ||
74 | int main(int argc, char *argv[]) { | |
75 | test_verbs(); | |
76 | test_verbs_no_default(); | |
77 | ||
78 | return 0; | |
79 | } |