pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk__unpack_duration_test.c
Go to the documentation of this file.
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 
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)
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  free_xml(duration);
47 }
48 
49 static void
50 nonnull_end_invalid(void **state)
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  free_xml(duration);
61 }
62 
63 static void
64 no_id(void **state)
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  crm_time_t *reference = crm_time_new("2025-03-21 16:01:01");
70 
71  assert_int_equal(pcmk__unpack_duration(duration, start, &end), pcmk_rc_ok);
72  assert_int_equal(crm_time_compare(end, reference), 0);
73 
74  crm_time_free(start);
75  crm_time_free(end);
76  crm_time_free(reference);
77  free_xml(duration);
78 }
79 
80 static void
81 years_invalid(void **state)
82 {
83  xmlNode *duration = pcmk__xml_parse(YEARS_INVALID);
84  crm_time_t *start = crm_time_new("2024-01-01 15:00:00");
85  crm_time_t *end = NULL;
86  crm_time_t *reference = crm_time_new("2024-03-21 16:01:01");
87 
88  assert_int_equal(pcmk__unpack_duration(duration, start, &end),
90  assert_int_equal(crm_time_compare(end, reference), 0);
91 
92  crm_time_free(start);
93  crm_time_free(end);
94  crm_time_free(reference);
95  free_xml(duration);
96 }
97 
98 static void
99 all_valid(void **state)
100 {
101  xmlNode *duration = pcmk__xml_parse(ALL_VALID);
102  crm_time_t *start = crm_time_new("2024-01-01 15:00:00");
103  crm_time_t *end = NULL;
104  crm_time_t *reference = crm_time_new("2025-03-21 16:01:01");
105 
106  assert_int_equal(pcmk__unpack_duration(duration, start, &end), pcmk_rc_ok);
107  assert_int_equal(crm_time_compare(end, reference), 0);
108 
109  crm_time_free(start);
110  crm_time_free(end);
111  crm_time_free(reference);
112  free_xml(duration);
113 }
114 
116  cmocka_unit_test(null_invalid),
117  cmocka_unit_test(nonnull_end_invalid),
118  cmocka_unit_test(no_id),
119  cmocka_unit_test(years_invalid),
120  cmocka_unit_test(all_valid))
struct crm_time_s crm_time_t
Definition: iso8601.h:32
#define ALL_VALID
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
#define YEARS_INVALID
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:74
Wrappers for and extensions to libxml2.
ISO_8601 Date handling.
G_GNUC_INTERNAL int pcmk__unpack_duration(const xmlNode *duration, const crm_time_t *start, crm_time_t **end)
Definition: rules.c:292
void free_xml(xmlNode *child)
Definition: xml.c:867
xmlNode * pcmk__xml_parse(const char *input)
Definition: xml_io.c:244
int crm_time_compare(const crm_time_t *a, const crm_time_t *b)
Definition: iso8601.c:1689
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:112
#define NO_ID
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:150