pacemaker 2.1.1-77db578727
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk__cmdline_preproc_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2020-2021 the Pacemaker project contributors
3 *
4 * The version control history for this file may have further details.
5 *
6 * This source code is licensed under the GNU Lesser General Public License
7 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
12
13#define LISTS_EQ(a, b) { \
14 g_assert_cmpint(g_strv_length((gchar **) (a)), ==, g_strv_length((gchar **) (b))); \
15 for (int i = 0; i < g_strv_length((a)); i++) { \
16 g_assert_cmpstr((a)[i], ==, (b)[i]); \
17 } \
18}
19
20static void
21empty_input(void) {
22 g_assert_null(pcmk__cmdline_preproc(NULL, ""));
23}
24
25static void
26no_specials(void) {
27 const char *argv[] = { "-a", "-b", "-c", "-d", NULL };
28 const gchar *expected[] = { "-a", "-b", "-c", "-d", NULL };
29
30 gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
31 LISTS_EQ(processed, expected);
32 g_strfreev(processed);
33
34 processed = pcmk__cmdline_preproc((char **) argv, "");
35 LISTS_EQ(processed, expected);
36 g_strfreev(processed);
37}
38
39static void
40single_dash(void) {
41 const char *argv[] = { "-", NULL };
42 const gchar *expected[] = { "-", NULL };
43
44 gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
45 LISTS_EQ(processed, expected);
46 g_strfreev(processed);
47}
48
49static void
50double_dash(void) {
51 const char *argv[] = { "-a", "--", "-bc", NULL };
52 const gchar *expected[] = { "-a", "--", "-bc", NULL };
53
54 gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
55 LISTS_EQ(processed, expected);
56 g_strfreev(processed);
57}
58
59static void
60special_args(void) {
61 const char *argv[] = { "-aX", "-Fval", NULL };
62 const gchar *expected[] = { "-a", "X", "-F", "val", NULL };
63
64 gchar **processed = pcmk__cmdline_preproc((char **) argv, "aF");
65 LISTS_EQ(processed, expected);
66 g_strfreev(processed);
67}
68
69static void
70special_arg_at_end(void) {
71 const char *argv[] = { "-a", NULL };
72 const gchar *expected[] = { "-a", NULL };
73
74 gchar **processed = pcmk__cmdline_preproc((char **) argv, "a");
75 LISTS_EQ(processed, expected);
76 g_strfreev(processed);
77}
78
79static void
80long_arg(void) {
81 const char *argv[] = { "--blah=foo", NULL };
82 const gchar *expected[] = { "--blah=foo", NULL };
83
84 gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
85 LISTS_EQ(processed, expected);
86 g_strfreev(processed);
87}
88
89static void
90negative_score(void) {
91 const char *argv[] = { "-v", "-1000", NULL };
92 const gchar *expected[] = { "-v", "-1000", NULL };
93
94 gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
95 LISTS_EQ(processed, expected);
96 g_strfreev(processed);
97}
98
99static void
100negative_score_2(void) {
101 const char *argv[] = { "-1i3", NULL };
102 const gchar *expected[] = { "-1", "-i", "-3", NULL };
103
104 gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
105 LISTS_EQ(processed, expected);
106 g_strfreev(processed);
107}
108
109static void
110string_arg_with_dash(void) {
111 const char *argv[] = { "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
112 const gchar *expected[] = { "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
113
114 gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
115 LISTS_EQ(processed, expected);
116 g_strfreev(processed);
117}
118
119static void
120string_arg_with_dash_2(void) {
121 const char *argv[] = { "-n", "crm_mon_options", "-v", "-1i3", NULL };
122 const gchar *expected[] = { "-n", "crm_mon_options", "-v", "-1i3", NULL };
123
124 gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
125 LISTS_EQ(processed, expected);
126 g_strfreev(processed);
127}
128
129static void
130string_arg_with_dash_3(void) {
131 const char *argv[] = { "-abc", "-1i3", NULL };
132 const gchar *expected[] = { "-a", "-b", "-c", "-1i3", NULL };
133
134 gchar **processed = pcmk__cmdline_preproc((char **) argv, "c");
135 LISTS_EQ(processed, expected);
136 g_strfreev(processed);
137}
138
139int
140main(int argc, char **argv)
141{
142 g_test_init(&argc, &argv, NULL);
143
144 g_test_add_func("/common/cmdline/preproc/empty_input", empty_input);
145 g_test_add_func("/common/cmdline/preproc/no_specials", no_specials);
146 g_test_add_func("/common/cmdline/preproc/single_dash", single_dash);
147 g_test_add_func("/common/cmdline/preproc/double_dash", double_dash);
148 g_test_add_func("/common/cmdline/preproc/special_args", special_args);
149 g_test_add_func("/common/cmdline/preproc/special_arg_at_end", special_arg_at_end);
150 g_test_add_func("/common/cmdline/preproc/long_arg", long_arg);
151 g_test_add_func("/common/cmdline/preproc/negative_score", negative_score);
152 g_test_add_func("/common/cmdline/preproc/negative_score_2", negative_score_2);
153 g_test_add_func("/common/cmdline/preproc/string_arg_with_dash", string_arg_with_dash);
154 g_test_add_func("/common/cmdline/preproc/string_arg_with_dash_2", string_arg_with_dash_2);
155 g_test_add_func("/common/cmdline/preproc/string_arg_with_dash_3", string_arg_with_dash_3);
156 return g_test_run();
157}
gchar ** pcmk__cmdline_preproc(char **argv, const char *special)
Definition cmdline.c:146
#define LISTS_EQ(a, b)
int main(int argc, char **argv)