root/lib/common/tests/rules/pcmk__unpack_duration_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. null_invalid
  2. nonnull_end_invalid
  3. no_id
  4. years_invalid
  5. all_valid

   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 <glib.h>
  13 
  14 #include <crm/common/unittest_internal.h>
  15 
  16 #include <crm/common/iso8601.h>
  17 #include <crm/common/xml.h>
  18 #include "../../crmcommon_private.h"
  19 
  20 #define MONTHS_TO_SECONDS "months=\"2\" weeks=\"3\" days=\"-1\" "           \
  21                           "hours=\"1\" minutes=\"1\" seconds=\"1\" />"
  22 
  23 #define ALL_VALID "<duration id=\"duration1\" years=\"1\" " MONTHS_TO_SECONDS
  24 
  25 #define NO_ID     "<duration years=\"1\" " MONTHS_TO_SECONDS
  26 
  27 #define YEARS_INVALID "<duration id=\"duration1\" years=\"not-a-number\" "  \
  28                       MONTHS_TO_SECONDS
  29 
  30 static void
  31 null_invalid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33     xmlNode *duration = pcmk__xml_parse(ALL_VALID);
  34     crm_time_t *start = crm_time_new("2024-01-01 15:00:00");
  35     crm_time_t *end = NULL;
  36 
  37     assert_int_equal(pcmk__unpack_duration(NULL, NULL, NULL), EINVAL);
  38     assert_int_equal(pcmk__unpack_duration(duration, NULL, NULL), EINVAL);
  39     assert_int_equal(pcmk__unpack_duration(duration, start, NULL), EINVAL);
  40     assert_int_equal(pcmk__unpack_duration(duration, NULL, &end), EINVAL);
  41     assert_int_equal(pcmk__unpack_duration(NULL, start, NULL), EINVAL);
  42     assert_int_equal(pcmk__unpack_duration(NULL, start, &end), EINVAL);
  43     assert_int_equal(pcmk__unpack_duration(NULL, NULL, &end), EINVAL);
  44 
  45     crm_time_free(start);
  46     pcmk__xml_free(duration);
  47 }
  48 
  49 static void
  50 nonnull_end_invalid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52     xmlNode *duration = pcmk__xml_parse(ALL_VALID);
  53     crm_time_t *start = crm_time_new("2024-01-01 15:00:00");
  54     crm_time_t *end = crm_time_new("2024-01-01 15:00:01");
  55 
  56     assert_int_equal(pcmk__unpack_duration(duration, start, &end), EINVAL);
  57 
  58     crm_time_free(start);
  59     crm_time_free(end);
  60     pcmk__xml_free(duration);
  61 }
  62 
  63 static void
  64 no_id(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66     xmlNode *duration = pcmk__xml_parse(NO_ID);
  67     crm_time_t *start = crm_time_new("2024-01-01 15:00:00");
  68     crm_time_t *end = NULL;
  69 
  70     assert_int_equal(pcmk__unpack_duration(duration, start, &end),
  71                      pcmk_rc_unpack_error);
  72     assert_null(end);
  73 
  74     crm_time_free(start);
  75     pcmk__xml_free(duration);
  76 }
  77 
  78 static void
  79 years_invalid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81     xmlNode *duration = pcmk__xml_parse(YEARS_INVALID);
  82     crm_time_t *start = crm_time_new("2024-01-01 15:00:00");
  83     crm_time_t *end = NULL;
  84 
  85     assert_int_equal(pcmk__unpack_duration(duration, start, &end),
  86                      pcmk_rc_unpack_error);
  87     assert_null(end);
  88 
  89     crm_time_free(start);
  90     pcmk__xml_free(duration);
  91 }
  92 
  93 static void
  94 all_valid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96     xmlNode *duration = pcmk__xml_parse(ALL_VALID);
  97     crm_time_t *start = crm_time_new("2024-01-01 15:00:00");
  98     crm_time_t *end = NULL;
  99     crm_time_t *reference = crm_time_new("2025-03-21 16:01:01");
 100 
 101     assert_int_equal(pcmk__unpack_duration(duration, start, &end), pcmk_rc_ok);
 102     assert_int_equal(crm_time_compare(end, reference), 0);
 103 
 104     crm_time_free(start);
 105     crm_time_free(end);
 106     crm_time_free(reference);
 107     pcmk__xml_free(duration);
 108 }
 109 
 110 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 111                 cmocka_unit_test(null_invalid),
 112                 cmocka_unit_test(nonnull_end_invalid),
 113                 cmocka_unit_test(no_id),
 114                 cmocka_unit_test(years_invalid),
 115                 cmocka_unit_test(all_valid))

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