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. negative_score_3
  11. string_arg_with_dash
  12. string_arg_with_dash_2
  13. string_arg_with_dash_3

   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 
  12 #include <crm/common/unittest_internal.h>
  13 #include <crm/common/cmdline_internal.h>
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  27     assert_null(pcmk__cmdline_preproc(NULL, ""));
  28 }
  29 
  30 static void
  31 no_specials(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
  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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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) {
     /* [previous][next][first][last][top][bottom][index][help] */
 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))

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