root/lib/common/tests/nvpair/pcmk__xe_get_flags_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. assert_flags
  2. null_name_invalid
  3. null_xml_default
  4. no_attr_default
  5. invalid_attr_default
  6. valid_attr

   1 /*
   2  * Copyright 2024 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 <stdint.h>         // UINT32_MAX
  13 #include <libxml/tree.h>
  14 
  15 #include <crm/common/unittest_internal.h>
  16 
  17 #include <crm/common/iso8601.h>
  18 #include <crm/common/xml.h>
  19 #include <crm/common/nvpair_internal.h>
  20 
  21 #define DEFAULT_VALUE 0xfff
  22 
  23 static void
  24 assert_flags(const char *value, int expected_rc, unsigned int expected_flags)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26     int rc = pcmk_rc_ok;
  27     uint32_t flags = 0U;
  28     xmlNode *xml = pcmk__xe_create(NULL, "element");
  29 
  30     assert_non_null(xml);
  31     crm_xml_add(xml, "attribute", value);
  32 
  33     // Without output argument
  34     assert_int_equal(pcmk__xe_get_flags(xml, "attribute", NULL, DEFAULT_VALUE),
  35                      expected_rc);
  36 
  37     // With output argument
  38     rc = pcmk__xe_get_flags(xml, "attribute", &flags, DEFAULT_VALUE);
  39     assert_int_equal(rc, expected_rc);
  40     assert_true(flags == expected_flags);
  41 
  42     free_xml(xml);
  43 }
  44 
  45 static void
  46 null_name_invalid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48     int rc = pcmk_rc_ok;
  49     uint32_t flags = 0U;
  50     xmlNode *xml = pcmk__xe_create(NULL, "element");
  51 
  52     assert_non_null(xml);
  53 
  54     assert_int_equal(pcmk__xe_get_flags(NULL, NULL, NULL, DEFAULT_VALUE),
  55                      EINVAL);
  56 
  57     assert_int_equal(pcmk__xe_get_flags(xml, NULL, NULL, DEFAULT_VALUE),
  58                      EINVAL);
  59 
  60     rc = pcmk__xe_get_flags(xml, NULL, &flags, DEFAULT_VALUE);
  61     assert_int_equal(rc, EINVAL);
  62     assert_true(flags == DEFAULT_VALUE);
  63 
  64     flags = 0U;
  65     rc = pcmk__xe_get_flags(NULL, NULL, &flags, DEFAULT_VALUE);
  66     assert_int_equal(rc, EINVAL);
  67     assert_true(flags == DEFAULT_VALUE);
  68 
  69     free_xml(xml);
  70 }
  71 
  72 static void
  73 null_xml_default(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  74 {
  75     int rc = pcmk_rc_ok;
  76     uint32_t flags = 0U;
  77 
  78     assert_int_equal(pcmk__xe_get_flags(NULL, "attribute", NULL, DEFAULT_VALUE),
  79                      pcmk_rc_ok);
  80 
  81     rc = pcmk__xe_get_flags(NULL, "attribute", &flags, DEFAULT_VALUE);
  82     assert_int_equal(rc, pcmk_rc_ok);
  83     assert_true(flags == DEFAULT_VALUE);
  84 }
  85 
  86 static void
  87 no_attr_default(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89     assert_flags(NULL, pcmk_rc_ok, DEFAULT_VALUE);
  90 }
  91 
  92 static void
  93 invalid_attr_default(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  94 {
  95     char *too_big = crm_strdup_printf("%lld", UINT32_MAX + 1LL);
  96 
  97     assert_flags("x", EINVAL, DEFAULT_VALUE);
  98     assert_flags("-1", pcmk_rc_bad_input, DEFAULT_VALUE);
  99     assert_flags(too_big, pcmk_rc_bad_input, DEFAULT_VALUE);
 100     free(too_big);
 101 }
 102 
 103 static void
 104 valid_attr(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106     assert_flags("0", pcmk_rc_ok, 0x0);
 107     assert_flags("15", pcmk_rc_ok, 0x0f);
 108     assert_flags("61462", pcmk_rc_ok, 0xf016);
 109     assert_flags("4294967295", pcmk_rc_ok, 0xffffffff);
 110 }
 111 
 112 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 113                 cmocka_unit_test(null_name_invalid),
 114                 cmocka_unit_test(null_xml_default),
 115                 cmocka_unit_test(no_attr_default),
 116                 cmocka_unit_test(invalid_attr_default),
 117                 cmocka_unit_test(valid_attr))

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