| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "strv.h" |
| 4 |
#include "tests.h" |
| 5 |
#include "verbs.h" |
| 6 |
|
| 7 |
static int noop_dispatcher(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 8 |
return 0; |
| 9 |
} |
| 10 |
|
| 11 |
#define test_dispatch_one(argv, verbs, expected) \ |
| 12 |
assert_se(_dispatch_verb(argv, verbs, verbs + ELEMENTSOF(verbs) - 1, NULL) == expected); |
| 13 |
|
| 14 |
TEST(verbs) { |
| 15 |
static const Verb verbs[] = { |
| 16 |
{ "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, |
| 17 |
{ "list-images", VERB_ANY, 1, 0, noop_dispatcher }, |
| 18 |
{ "list", VERB_ANY, 2, VERB_DEFAULT, noop_dispatcher }, |
| 19 |
{ "status", 2, VERB_ANY, 0, noop_dispatcher }, |
| 20 |
{ "Group2", VERB_ANY, VERB_ANY, VERB_GROUP_MARKER, NULL }, |
| 21 |
{ "show", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, |
| 22 |
{ "terminate", 2, VERB_ANY, 0, noop_dispatcher }, |
| 23 |
{ "Group3", 0, 0, VERB_GROUP_MARKER, NULL }, |
| 24 |
{ "login", 2, 2, 0, noop_dispatcher }, |
| 25 |
{ "copy-to", 3, 4, 0, noop_dispatcher }, |
| 26 |
{} |
| 27 |
}; |
| 28 |
|
| 29 |
/* not found */ |
| 30 |
test_dispatch_one(STRV_MAKE("command-not-found"), verbs, -EINVAL); |
| 31 |
|
| 32 |
/* found */ |
| 33 |
test_dispatch_one(STRV_MAKE("show"), verbs, 0); |
| 34 |
|
| 35 |
/* found, too few args */ |
| 36 |
test_dispatch_one(STRV_MAKE("copy-to", "foo"), verbs, -EINVAL); |
| 37 |
|
| 38 |
/* found, meets min args */ |
| 39 |
test_dispatch_one(STRV_MAKE("status", "foo", "bar"), verbs, 0); |
| 40 |
|
| 41 |
/* found, too many args */ |
| 42 |
test_dispatch_one(STRV_MAKE("copy-to", "foo", "bar", "baz", "quux", "qaax"), verbs, -EINVAL); |
| 43 |
|
| 44 |
/* no verb, but a default is set */ |
| 45 |
test_dispatch_one(STRV_EMPTY, verbs, 0); |
| 46 |
|
| 47 |
/* the group entry shall not be found */ |
| 48 |
test_dispatch_one(STRV_MAKE("Group2"), verbs, -EINVAL); |
| 49 |
|
| 50 |
/* the group entry shall not be found */ |
| 51 |
test_dispatch_one(STRV_MAKE("Group3"), verbs, -EINVAL); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(verbs_no_default) { |
| 55 |
static const Verb verbs[] = { |
| 56 |
{ "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, |
| 57 |
{}, |
| 58 |
}; |
| 59 |
|
| 60 |
test_dispatch_one(STRV_MAKE(NULL), verbs, -EINVAL); |
| 61 |
test_dispatch_one(STRV_MAKE("hel"), verbs, -EINVAL); |
| 62 |
test_dispatch_one(STRV_MAKE("helpp"), verbs, -EINVAL); |
| 63 |
test_dispatch_one(STRV_MAKE("hgrejgoraoiosafso"), verbs, -EINVAL); |
| 64 |
} |
| 65 |
|
| 66 |
TEST(verbs_no_default_many) { |
| 67 |
static const Verb verbs[] = { |
| 68 |
{ "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, |
| 69 |
{ "list-images", VERB_ANY, 1, 0, noop_dispatcher }, |
| 70 |
{ "list", VERB_ANY, 2, 0, noop_dispatcher }, |
| 71 |
{ "status", 2, VERB_ANY, 0, noop_dispatcher }, |
| 72 |
{ "Specials", VERB_ANY, VERB_ANY, VERB_GROUP_MARKER, NULL }, |
| 73 |
{ "show", VERB_ANY, VERB_ANY, 0, noop_dispatcher }, |
| 74 |
{ "terminate", 2, VERB_ANY, 0, noop_dispatcher }, |
| 75 |
{ "login", 2, 2, 0, noop_dispatcher }, |
| 76 |
{ "copy-to", 3, 4, 0, noop_dispatcher }, |
| 77 |
{} |
| 78 |
}; |
| 79 |
|
| 80 |
test_dispatch_one(STRV_MAKE(NULL), verbs, -EINVAL); |
| 81 |
test_dispatch_one(STRV_MAKE("hel"), verbs, -EINVAL); |
| 82 |
test_dispatch_one(STRV_MAKE("helpp"), verbs, -EINVAL); |
| 83 |
test_dispatch_one(STRV_MAKE("hgrejgoraoiosafso"), verbs, -EINVAL); |
| 84 |
test_dispatch_one(STRV_MAKE("Specials"), verbs, -EINVAL); |
| 85 |
} |
| 86 |
|
| 87 |
DEFINE_TEST_MAIN(LOG_INFO); |