pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__xe_get_datetime_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 <errno.h>
13 #include <libxml/tree.h>
14 
16 
17 #include <crm/common/iso8601.h>
18 #include <crm/common/xml.h>
20 
21 #define REFERENCE_ISO8601 "2024-001"
22 #define ATTR_PRESENT "start"
23 #define ATTR_MISSING "end"
24 #define REFERENCE_XML "<date_expression id=\"id1\" " \
25  ATTR_PRESENT "=\"" REFERENCE_ISO8601 "\"" \
26  " operation=\"gt\">"
27 #define BAD_XML "<date_expression id=\"id1\" " \
28  ATTR_PRESENT "=\"not_a_time\"" \
29  " operation=\"gt\">"
30 
31 static void
32 null_invalid(void **state)
33 {
34  xmlNode *xml = pcmk__xml_parse(REFERENCE_XML);
35  crm_time_t *t = NULL;
36 
37  assert_int_equal(pcmk__xe_get_datetime(NULL, NULL, NULL), EINVAL);
38  assert_int_equal(pcmk__xe_get_datetime(xml, NULL, NULL), EINVAL);
39  assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_PRESENT, NULL), EINVAL);
40  assert_int_equal(pcmk__xe_get_datetime(xml, NULL, &t), EINVAL);
41  assert_null(t);
42  assert_int_equal(pcmk__xe_get_datetime(NULL, ATTR_PRESENT, NULL), EINVAL);
43  assert_int_equal(pcmk__xe_get_datetime(NULL, ATTR_PRESENT, &t), EINVAL);
44  assert_null(t);
45  assert_int_equal(pcmk__xe_get_datetime(NULL, NULL, &t), EINVAL);
46  assert_null(t);
47 
48  free_xml(xml);
49 }
50 
51 static void
52 nonnull_time_invalid(void **state)
53 {
54  xmlNode *xml = pcmk__xml_parse(REFERENCE_XML);
56 
57  assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_PRESENT, &t), EINVAL);
58 
59  crm_time_free(t);
60  free_xml(xml);
61 }
62 
63 static void
64 attr_missing(void **state)
65 {
66  xmlNode *xml = pcmk__xml_parse(REFERENCE_XML);
67  crm_time_t *t = NULL;
68 
69  assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_MISSING, &t), pcmk_rc_ok);
70  assert_null(t);
71 
72  free_xml(xml);
73 }
74 
75 static void
76 attr_valid(void **state)
77 {
78  xmlNode *xml = pcmk__xml_parse(REFERENCE_XML);
79  crm_time_t *t = NULL;
81 
82  assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_PRESENT, &t), pcmk_rc_ok);
83  assert_int_equal(crm_time_compare(t, reference), 0);
84 
85  crm_time_free(t);
86  crm_time_free(reference);
87  free_xml(xml);
88 }
89 
90 static void
91 attr_invalid(void **state)
92 {
93  xmlNode *xml = pcmk__xml_parse(BAD_XML);
94  crm_time_t *t = NULL;
95 
96  assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_PRESENT, &t),
98  assert_null(t);
99 
100  free_xml(xml);
101 }
102 
104  cmocka_unit_test(null_invalid),
105  cmocka_unit_test(nonnull_time_invalid),
106  cmocka_unit_test(attr_missing),
107  cmocka_unit_test(attr_valid),
108  cmocka_unit_test(attr_invalid))
crm_time_t * crm_time_new_undefined(void)
Allocate memory for an uninitialized time object.
Definition: iso8601.c:129
int pcmk__xe_get_datetime(const xmlNode *xml, const char *attr, crm_time_t **t)
Definition: nvpair.c:728
struct crm_time_s crm_time_t
Definition: iso8601.h:32
#define ATTR_PRESENT
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int pcmk__xml_test_setup_group(void **state)
Definition: unittest.c:85
Wrappers for and extensions to libxml2.
ISO_8601 Date handling.
#define REFERENCE_XML
void free_xml(xmlNode *child)
Definition: xml.c:958
#define REFERENCE_ISO8601
int pcmk__xml_test_teardown_group(void **state)
Definition: unittest.c:104
xmlNode * pcmk__xml_parse(const char *input)
Definition: xml_io.c:245
int crm_time_compare(const crm_time_t *a, const crm_time_t *b)
Definition: iso8601.c:1736
#define BAD_XML
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:112
#define ATTR_MISSING
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:150