root/lib/common/tests/iso8601/pcmk__add_time_from_xml_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. null_time_invalid
  2. null_xml_ok
  3. invalid_component
  4. missing_attr
  5. invalid_attr
  6. out_of_range_attr
  7. add_years
  8. add_months
  9. add_weeks
  10. add_days
  11. add_hours
  12. add_minutes
  13. add_seconds

   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 <libxml/tree.h>    // xmlNode
  13 
  14 #include <crm/common/unittest_internal.h>
  15 
  16 #include <crm/common/iso8601.h>
  17 #include <crm/common/iso8601_internal.h>
  18 #include <crm/common/xml.h>
  19 #include "../../crmcommon_private.h"
  20 
  21 #define ALL_VALID "<duration id=\"duration1\" years=\"1\" months=\"2\" "   \
  22                    "weeks=\"3\" days=\"-1\" hours=\"1\" minutes=\"1\" "      \
  23                    "seconds=\"1\" />"
  24 
  25 #define YEARS_INVALID "<duration id=\"duration1\" years=\"not-a-number\" />"
  26 
  27 #define YEARS_TOO_BIG "<duration id=\"duration1\" years=\"2222222222\" />"
  28 
  29 #define YEARS_TOO_SMALL "<duration id=\"duration1\" years=\"-2222222222\" />"
  30 
  31 static void
  32 null_time_invalid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
  35 
  36     assert_int_equal(pcmk__add_time_from_xml(NULL, pcmk__time_years, xml),
  37                      EINVAL);
  38     free_xml(xml);
  39 }
  40 
  41 static void
  42 null_xml_ok(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
  45     crm_time_t *reference = pcmk_copy_time(t);
  46 
  47     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, NULL),
  48                      pcmk_rc_ok);
  49     assert_int_equal(crm_time_compare(t, reference), 0);
  50 
  51     crm_time_free(t);
  52     crm_time_free(reference);
  53 }
  54 
  55 static void
  56 invalid_component(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
  59 
  60     assert_int_equal(pcmk__add_time_from_xml(NULL, pcmk__time_unknown, xml),
  61                      EINVAL);
  62     free_xml(xml);
  63 }
  64 
  65 static void
  66 missing_attr(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  67 {
  68     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
  69     crm_time_t *reference = pcmk_copy_time(t);
  70     xmlNode *xml = pcmk__xml_parse(YEARS_INVALID);
  71 
  72     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_months, xml),
  73                      pcmk_rc_ok);
  74     assert_int_equal(crm_time_compare(t, reference), 0);
  75 
  76     crm_time_free(t);
  77     crm_time_free(reference);
  78     free_xml(xml);
  79 }
  80 
  81 static void
  82 invalid_attr(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
  85     crm_time_t *reference = pcmk_copy_time(t);
  86     xmlNode *xml = pcmk__xml_parse(YEARS_INVALID);
  87 
  88     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, xml),
  89                      pcmk_rc_unpack_error);
  90     assert_int_equal(crm_time_compare(t, reference), 0);
  91 
  92     crm_time_free(t);
  93     crm_time_free(reference);
  94     free_xml(xml);
  95 }
  96 
  97 static void
  98 out_of_range_attr(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
 101     crm_time_t *reference = pcmk_copy_time(t);
 102     xmlNode *xml = NULL;
 103 
 104     xml = pcmk__xml_parse(YEARS_TOO_BIG);
 105     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, xml), ERANGE);
 106     assert_int_equal(crm_time_compare(t, reference), 0);
 107     free_xml(xml);
 108 
 109     xml = pcmk__xml_parse(YEARS_TOO_SMALL);
 110     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, xml), ERANGE);
 111     assert_int_equal(crm_time_compare(t, reference), 0);
 112     free_xml(xml);
 113 
 114     crm_time_free(t);
 115     crm_time_free(reference);
 116 }
 117 
 118 static void
 119 add_years(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
 122     crm_time_t *reference = crm_time_new("2025-01-01 15:00:00");
 123     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
 124 
 125     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_years, xml),
 126                      pcmk_rc_ok);
 127     assert_int_equal(crm_time_compare(t, reference), 0);
 128 
 129     crm_time_free(t);
 130     crm_time_free(reference);
 131     free_xml(xml);
 132 }
 133 
 134 static void
 135 add_months(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 136 {
 137     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
 138     crm_time_t *reference = crm_time_new("2024-03-01 15:00:00");
 139     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
 140 
 141     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_months, xml),
 142                      pcmk_rc_ok);
 143     assert_int_equal(crm_time_compare(t, reference), 0);
 144 
 145     crm_time_free(t);
 146     crm_time_free(reference);
 147     free_xml(xml);
 148 }
 149 
 150 static void
 151 add_weeks(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 152 {
 153     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
 154     crm_time_t *reference = crm_time_new("2024-01-22 15:00:00");
 155     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
 156 
 157     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_weeks, xml),
 158                      pcmk_rc_ok);
 159     assert_int_equal(crm_time_compare(t, reference), 0);
 160 
 161     crm_time_free(t);
 162     crm_time_free(reference);
 163     free_xml(xml);
 164 }
 165 
 166 static void
 167 add_days(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 {
 169     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
 170     crm_time_t *reference = crm_time_new("2023-12-31 15:00:00");
 171     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
 172 
 173     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_days, xml),
 174                      pcmk_rc_ok);
 175     assert_int_equal(crm_time_compare(t, reference), 0);
 176 
 177     crm_time_free(t);
 178     crm_time_free(reference);
 179     free_xml(xml);
 180 }
 181 
 182 static void
 183 add_hours(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 184 {
 185     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
 186     crm_time_t *reference = crm_time_new("2024-01-01 16:00:00");
 187     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
 188 
 189     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_hours, xml),
 190                      pcmk_rc_ok);
 191     assert_int_equal(crm_time_compare(t, reference), 0);
 192 
 193     crm_time_free(t);
 194     crm_time_free(reference);
 195     free_xml(xml);
 196 }
 197 
 198 static void
 199 add_minutes(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
 202     crm_time_t *reference = crm_time_new("2024-01-01 15:01:00");
 203     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
 204 
 205     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_minutes, xml),
 206                      pcmk_rc_ok);
 207     assert_int_equal(crm_time_compare(t, reference), 0);
 208 
 209     crm_time_free(t);
 210     crm_time_free(reference);
 211     free_xml(xml);
 212 }
 213 
 214 static void
 215 add_seconds(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 216 {
 217     crm_time_t *t = crm_time_new("2024-01-01 15:00:00");
 218     crm_time_t *reference = crm_time_new("2024-01-01 15:00:01");
 219     xmlNode *xml = pcmk__xml_parse(ALL_VALID);
 220 
 221     assert_int_equal(pcmk__add_time_from_xml(t, pcmk__time_seconds, xml),
 222                      pcmk_rc_ok);
 223     assert_int_equal(crm_time_compare(t, reference), 0);
 224 
 225     crm_time_free(t);
 226     crm_time_free(reference);
 227     free_xml(xml);
 228 }
 229 
 230 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 231                 cmocka_unit_test(null_time_invalid),
 232                 cmocka_unit_test(null_xml_ok),
 233                 cmocka_unit_test(invalid_component),
 234                 cmocka_unit_test(missing_attr),
 235                 cmocka_unit_test(invalid_attr),
 236                 cmocka_unit_test(out_of_range_attr),
 237                 cmocka_unit_test(add_years),
 238                 cmocka_unit_test(add_months),
 239                 cmocka_unit_test(add_weeks),
 240                 cmocka_unit_test(add_days),
 241                 cmocka_unit_test(add_hours),
 242                 cmocka_unit_test(add_minutes),
 243                 cmocka_unit_test(add_seconds));

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