pacemaker  2.1.2-ada5c3b36
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 #include <stdarg.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <setjmp.h>
17 #include <cmocka.h>
18 #include <glib.h>
19 
20 #define LISTS_EQ(a, b) { \
21  assert_int_equal(g_strv_length((gchar **) (a)), g_strv_length((gchar **) (b))); \
22  for (int i = 0; i < g_strv_length((a)); i++) { \
23  assert_string_equal((a)[i], (b)[i]); \
24  } \
25 }
26 
27 static void
28 empty_input(void **state) {
29  assert_null(pcmk__cmdline_preproc(NULL, ""));
30 }
31 
32 static void
33 no_specials(void **state) {
34  const char *argv[] = { "-a", "-b", "-c", "-d", NULL };
35  const gchar *expected[] = { "-a", "-b", "-c", "-d", NULL };
36 
37  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
38  LISTS_EQ(processed, expected);
39  g_strfreev(processed);
40 
41  processed = pcmk__cmdline_preproc((char **) argv, "");
42  LISTS_EQ(processed, expected);
43  g_strfreev(processed);
44 }
45 
46 static void
47 single_dash(void **state) {
48  const char *argv[] = { "-", NULL };
49  const gchar *expected[] = { "-", NULL };
50 
51  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
52  LISTS_EQ(processed, expected);
53  g_strfreev(processed);
54 }
55 
56 static void
57 double_dash(void **state) {
58  const char *argv[] = { "-a", "--", "-bc", NULL };
59  const gchar *expected[] = { "-a", "--", "-bc", NULL };
60 
61  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
62  LISTS_EQ(processed, expected);
63  g_strfreev(processed);
64 }
65 
66 static void
67 special_args(void **state) {
68  const char *argv[] = { "-aX", "-Fval", NULL };
69  const gchar *expected[] = { "-a", "X", "-F", "val", NULL };
70 
71  gchar **processed = pcmk__cmdline_preproc((char **) argv, "aF");
72  LISTS_EQ(processed, expected);
73  g_strfreev(processed);
74 }
75 
76 static void
77 special_arg_at_end(void **state) {
78  const char *argv[] = { "-a", NULL };
79  const gchar *expected[] = { "-a", NULL };
80 
81  gchar **processed = pcmk__cmdline_preproc((char **) argv, "a");
82  LISTS_EQ(processed, expected);
83  g_strfreev(processed);
84 }
85 
86 static void
87 long_arg(void **state) {
88  const char *argv[] = { "--blah=foo", NULL };
89  const gchar *expected[] = { "--blah=foo", NULL };
90 
91  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
92  LISTS_EQ(processed, expected);
93  g_strfreev(processed);
94 }
95 
96 static void
97 negative_score(void **state) {
98  const char *argv[] = { "-v", "-1000", NULL };
99  const gchar *expected[] = { "-v", "-1000", NULL };
100 
101  gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
102  LISTS_EQ(processed, expected);
103  g_strfreev(processed);
104 }
105 
106 static void
107 negative_score_2(void **state) {
108  const char *argv[] = { "-1i3", NULL };
109  const gchar *expected[] = { "-1", "-i", "-3", NULL };
110 
111  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
112  LISTS_EQ(processed, expected);
113  g_strfreev(processed);
114 }
115 
116 static void
117 string_arg_with_dash(void **state) {
118  const char *argv[] = { "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
119  const gchar *expected[] = { "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
120 
121  gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
122  LISTS_EQ(processed, expected);
123  g_strfreev(processed);
124 }
125 
126 static void
127 string_arg_with_dash_2(void **state) {
128  const char *argv[] = { "-n", "crm_mon_options", "-v", "-1i3", NULL };
129  const gchar *expected[] = { "-n", "crm_mon_options", "-v", "-1i3", NULL };
130 
131  gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
132  LISTS_EQ(processed, expected);
133  g_strfreev(processed);
134 }
135 
136 static void
137 string_arg_with_dash_3(void **state) {
138  const char *argv[] = { "-abc", "-1i3", NULL };
139  const gchar *expected[] = { "-a", "-b", "-c", "-1i3", NULL };
140 
141  gchar **processed = pcmk__cmdline_preproc((char **) argv, "c");
142  LISTS_EQ(processed, expected);
143  g_strfreev(processed);
144 }
145 
146 int
147 main(int argc, char **argv)
148 {
149  const struct CMUnitTest tests[] = {
150  cmocka_unit_test(empty_input),
151  cmocka_unit_test(no_specials),
152  cmocka_unit_test(single_dash),
153  cmocka_unit_test(double_dash),
154  cmocka_unit_test(special_args),
155  cmocka_unit_test(special_arg_at_end),
156  cmocka_unit_test(long_arg),
157  cmocka_unit_test(negative_score),
158  cmocka_unit_test(negative_score_2),
159  cmocka_unit_test(string_arg_with_dash),
160  cmocka_unit_test(string_arg_with_dash_2),
161  cmocka_unit_test(string_arg_with_dash_3),
162  };
163 
164  cmocka_set_message_output(CM_OUTPUT_TAP);
165  return cmocka_run_group_tests(tests, NULL, NULL);
166 }
#define LISTS_EQ(a, b)
gchar ** pcmk__cmdline_preproc(char **argv, const char *special)
Definition: cmdline.c:148
int main(int argc, char **argv)