This source file includes following definitions.
- null_invalid
- nonnull_time_invalid
- attr_missing
- attr_valid
- attr_invalid
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <errno.h>
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 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);
55 crm_time_t *t = crm_time_new_undefined();
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;
80 crm_time_t *reference = crm_time_new(REFERENCE_ISO8601);
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),
97 pcmk_rc_unpack_error);
98 assert_null(t);
99
100 free_xml(xml);
101 }
102
103 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
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))