pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk__cmdline_preproc_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020-2023 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 General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
14 
15 #include <glib.h>
16 #include <stdint.h>
17 
18 #define LISTS_EQ(a, b) { \
19  assert_int_equal(g_strv_length((gchar **) (a)), g_strv_length((gchar **) (b))); \
20  for (int i = 0; i < g_strv_length((a)); i++) { \
21  assert_string_equal((a)[i], (b)[i]); \
22  } \
23 }
24 
25 static void
26 empty_input(void **state) {
27  assert_null(pcmk__cmdline_preproc(NULL, ""));
28 }
29 
30 static void
31 no_specials(void **state) {
32  const char *argv[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL };
33  const gchar *expected[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL };
34 
35  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
36  LISTS_EQ(processed, expected);
37  g_strfreev(processed);
38 
39  processed = pcmk__cmdline_preproc((char **) argv, "");
40  LISTS_EQ(processed, expected);
41  g_strfreev(processed);
42 }
43 
44 static void
45 single_dash(void **state) {
46  const char *argv[] = { "crm_mon", "-", NULL };
47  const gchar *expected[] = { "crm_mon", "-", NULL };
48 
49  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
50  LISTS_EQ(processed, expected);
51  g_strfreev(processed);
52 }
53 
54 static void
55 double_dash(void **state) {
56  const char *argv[] = { "crm_mon", "-a", "--", "-bc", NULL };
57  const gchar *expected[] = { "crm_mon", "-a", "--", "-bc", NULL };
58 
59  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
60  LISTS_EQ(processed, expected);
61  g_strfreev(processed);
62 }
63 
64 static void
65 special_args(void **state) {
66  const char *argv[] = { "crm_mon", "-aX", "-Fval", NULL };
67  const gchar *expected[] = { "crm_mon", "-a", "X", "-F", "val", NULL };
68 
69  gchar **processed = pcmk__cmdline_preproc((char **) argv, "aF");
70  LISTS_EQ(processed, expected);
71  g_strfreev(processed);
72 }
73 
74 static void
75 special_arg_at_end(void **state) {
76  const char *argv[] = { "crm_mon", "-a", NULL };
77  const gchar *expected[] = { "crm_mon", "-a", NULL };
78 
79  gchar **processed = pcmk__cmdline_preproc((char **) argv, "a");
80  LISTS_EQ(processed, expected);
81  g_strfreev(processed);
82 }
83 
84 static void
85 long_arg(void **state) {
86  const char *argv[] = { "crm_mon", "--blah=foo", NULL };
87  const gchar *expected[] = { "crm_mon", "--blah=foo", NULL };
88 
89  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
90  LISTS_EQ(processed, expected);
91  g_strfreev(processed);
92 }
93 
94 static void
95 negative_score(void **state) {
96  const char *argv[] = { "crm_mon", "-v", "-1000", NULL };
97  const gchar *expected[] = { "crm_mon", "-v", "-1000", NULL };
98 
99  gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
100  LISTS_EQ(processed, expected);
101  g_strfreev(processed);
102 }
103 
104 static void
105 negative_score_2(void **state) {
106  const char *argv[] = { "crm_mon", "-1i3", NULL };
107  const gchar *expected[] = { "crm_mon", "-1", "-i", "-3", NULL };
108 
109  gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
110  LISTS_EQ(processed, expected);
111  g_strfreev(processed);
112 }
113 
114 static void
115 negative_score_3(void **state) {
116  const char *argv[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL };
117  const gchar *expected[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL };
118 
119  gchar **processed = pcmk__cmdline_preproc((char **) argv, "pv");
120  LISTS_EQ(processed, expected);
121  g_strfreev(processed);
122 }
123 
124 static void
125 string_arg_with_dash(void **state) {
126  const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
127  const gchar *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
128 
129  gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
130  LISTS_EQ(processed, expected);
131  g_strfreev(processed);
132 }
133 
134 static void
135 string_arg_with_dash_2(void **state) {
136  const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3", NULL };
137  const gchar *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3", NULL };
138 
139  gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
140  LISTS_EQ(processed, expected);
141  g_strfreev(processed);
142 }
143 
144 static void
145 string_arg_with_dash_3(void **state) {
146  const char *argv[] = { "crm_mon", "-abc", "-1i3", NULL };
147  const gchar *expected[] = { "crm_mon", "-a", "-b", "-c", "-1i3", NULL };
148 
149  gchar **processed = pcmk__cmdline_preproc((char **) argv, "c");
150  LISTS_EQ(processed, expected);
151  g_strfreev(processed);
152 }
153 
154 PCMK__UNIT_TEST(NULL, NULL,
155  cmocka_unit_test(empty_input),
156  cmocka_unit_test(no_specials),
157  cmocka_unit_test(single_dash),
158  cmocka_unit_test(double_dash),
159  cmocka_unit_test(special_args),
160  cmocka_unit_test(special_arg_at_end),
161  cmocka_unit_test(long_arg),
162  cmocka_unit_test(negative_score),
163  cmocka_unit_test(negative_score_2),
164  cmocka_unit_test(negative_score_3),
165  cmocka_unit_test(string_arg_with_dash),
166  cmocka_unit_test(string_arg_with_dash_2),
167  cmocka_unit_test(string_arg_with_dash_3))
#define LISTS_EQ(a, b)
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
gchar ** pcmk__cmdline_preproc(char *const *argv, const char *special)
Definition: cmdline.c:213