]>
Commit | Line | Data |
---|---|---|
d2f0e78f DR |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 systemd developers | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. | |
18 | ***/ | |
19 | ||
20 | #include "macro.h" | |
21 | #include "strv.h" | |
22 | #include "verbs.h" | |
23 | ||
24 | static int noop_dispatcher(int argc, char *argv[], void *userdata) { | |
25 | return 0; | |
26 | } | |
27 | ||
28 | #define test_dispatch_one(argv, verbs, expected) \ | |
29 | optind = 0; \ | |
30 | assert_se(dispatch_verb(strv_length(argv), argv, verbs, NULL) == expected); | |
31 | ||
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 }, | |
42 | {} | |
43 | }; | |
44 | ||
45 | /* not found */ | |
46 | test_dispatch_one(STRV_MAKE("command-not-found"), verbs, -EINVAL); | |
47 | ||
48 | /* found */ | |
49 | test_dispatch_one(STRV_MAKE("show"), verbs, 0); | |
50 | ||
51 | /* found, too few args */ | |
52 | test_dispatch_one(STRV_MAKE("copy-to", "foo"), verbs, -EINVAL); | |
53 | ||
54 | /* found, meets min args */ | |
55 | test_dispatch_one(STRV_MAKE("status", "foo", "bar"), verbs, 0); | |
56 | ||
57 | /* found, too many args */ | |
58 | test_dispatch_one(STRV_MAKE("copy-to", "foo", "bar", "baz", "quux", "qaax"), verbs, -EINVAL); | |
59 | ||
60 | /* no verb, but a default is set */ | |
61 | test_dispatch_one(STRV_MAKE_EMPTY, verbs, 0); | |
62 | } | |
63 | ||
64 | static void test_verbs_no_default(void) { | |
65 | static const Verb verbs[] = { | |
66 | { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, | |
67 | {}, | |
68 | }; | |
69 | ||
70 | test_dispatch_one(STRV_MAKE(NULL), verbs, -EINVAL); | |
71 | } | |
72 | ||
73 | int main(int argc, char *argv[]) { | |
74 | test_verbs(); | |
75 | test_verbs_no_default(); | |
76 | ||
77 | return 0; | |
78 | } |