root/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. empty_input
  2. no_specials
  3. single_dash
  4. double_dash
  5. special_args
  6. special_arg_at_end
  7. long_arg
  8. negative_score
  9. negative_score_2
  10. string_arg_with_dash
  11. string_arg_with_dash_2
  12. string_arg_with_dash_3
  13. main

   1 /*
   2  * Copyright 2020-2022 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>
  11 #include <crm/common/cmdline_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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  29     assert_null(pcmk__cmdline_preproc(NULL, ""));
  30 }
  31 
  32 static void
  33 no_specials(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  34     const char *argv[] = { "-a", "-b", "-c", "-d", "-1", NULL };
  35     const gchar *expected[] = { "-a", "-b", "-c", "-d", "-1", 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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 }

/* [previous][next][first][last][top][bottom][index][help] */