pacemaker  2.1.1-52dc28db4
Scalable High-Availability cluster resource manager
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 
20 static void
21 empty_input(void) {
22  g_assert_null(pcmk__cmdline_preproc(NULL, ""));
23 }
24 
25 static void
26 no_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 
39 static void
40 single_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 
49 static void
50 double_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 
59 static void
60 special_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 
69 static void
70 special_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 
79 static void
80 long_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 
89 static void
90 negative_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 
99 static void
100 negative_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 
109 static void
110 string_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 
119 static void
120 string_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 
129 static void
130 string_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 
139 int
140 main(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 }
#define LISTS_EQ(a, b)
gchar ** pcmk__cmdline_preproc(char **argv, const char *special)
Definition: cmdline.c:146
int main(int argc, char **argv)