1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#include <stdlib.h>
#include <string.h>
#include "strlist.h"
#include "junitxml.h"
static void testcase_result_state_free(struct JUNIT_Testcase **testcase) {
struct JUNIT_Testcase *tc = (*testcase);
if (tc->tc_result_state_type == JUNIT_RESULT_STATE_FAILURE) {
guard_free(tc->result_state.failure->message);
guard_free(tc->result_state.failure);
} else if (tc->tc_result_state_type == JUNIT_RESULT_STATE_ERROR) {
guard_free(tc->result_state.error->message);
guard_free(tc->result_state.error);
} else if (tc->tc_result_state_type == JUNIT_RESULT_STATE_SKIPPED) {
guard_free(tc->result_state.skipped->message);
guard_free(tc->result_state.skipped);
}
}
static void testcase_free(struct JUNIT_Testcase **testcase) {
struct JUNIT_Testcase *tc = (*testcase);
guard_free(tc->name);
guard_free(tc->message);
guard_free(tc->classname);
testcase_result_state_free(&tc);
guard_free(tc);
}
void junitxml_testsuite_free(struct JUNIT_Testsuite **testsuite) {
struct JUNIT_Testsuite *suite = (*testsuite);
guard_free(suite->name);
guard_free(suite->hostname);
guard_free(suite->timestamp);
for (size_t i = 0; i < suite->_tc_alloc; i++) {
testcase_free(&suite->testcase[i]);
}
guard_free(suite->testcase);
guard_free(suite);
}
static int testsuite_append_testcase(struct JUNIT_Testsuite **testsuite, struct JUNIT_Testcase *testcase) {
struct JUNIT_Testsuite *suite = (*testsuite);
struct JUNIT_Testcase **tmp = realloc(suite->testcase, (suite->_tc_alloc + 1 ) * sizeof(*testcase));
if (tmp == NULL) {
return -1;
} else {
suite->testcase = tmp;
}
suite->testcase[suite->_tc_inuse] = testcase;
suite->_tc_inuse++;
suite->_tc_alloc++;
return 0;
}
static struct JUNIT_Failure *testcase_failure_from_attributes(struct StrList *attrs) {
struct JUNIT_Failure *result = calloc(1, sizeof(*result));
if(!result) {
return NULL;
}
for (size_t x = 0; x < strlist_count(attrs); x += 2) {
char *attr_name = strlist_item(attrs, x);
char *attr_value = strlist_item(attrs, x + 1);
if (!strcmp(attr_name, "message")) {
result->message = strdup(attr_value);
}
}
return result;
}
static struct JUNIT_Error *testcase_error_from_attributes(struct StrList *attrs) {
struct JUNIT_Error *result = calloc(1, sizeof(*result));
if(!result) {
return NULL;
}
for (size_t x = 0; x < strlist_count(attrs); x += 2) {
char *attr_name = strlist_item(attrs, x);
char *attr_value = strlist_item(attrs, x + 1);
if (!strcmp(attr_name, "message")) {
result->message = strdup(attr_value);
}
}
return result;
}
static struct JUNIT_Skipped *testcase_skipped_from_attributes(struct StrList *attrs) {
struct JUNIT_Skipped *result = calloc(1, sizeof(*result));
if(!result) {
return NULL;
}
for (size_t x = 0; x < strlist_count(attrs); x += 2) {
char *attr_name = strlist_item(attrs, x);
char *attr_value = strlist_item(attrs, x + 1);
if (!strcmp(attr_name, "message")) {
result->message = strdup(attr_value);
}
}
return result;
}
static struct JUNIT_Testcase *testcase_from_attributes(struct StrList *attrs) {
struct JUNIT_Testcase *result = calloc(1, sizeof(*result));
if(!result) {
return NULL;
}
for (size_t x = 0; x < strlist_count(attrs); x += 2) {
char *attr_name = strlist_item(attrs, x);
char *attr_value = strlist_item(attrs, x + 1);
if (!strcmp(attr_name, "name")) {
result->name = strdup(attr_value);
} else if (!strcmp(attr_name, "classname")) {
result->classname = strdup(attr_value);
} else if (!strcmp(attr_name, "time")) {
result->time = strtof(attr_value, NULL);
} else if (!strcmp(attr_name, "message")) {
result->message = strdup(attr_value);
}
}
return result;
}
static struct StrList *attributes_to_strlist(xmlTextReaderPtr reader) {
struct StrList *list;
xmlNodePtr node = xmlTextReaderCurrentNode(reader);
if (!node) {
return NULL;
}
list = strlist_init();
if (xmlTextReaderNodeType(reader) == 1 && node->properties) {
xmlAttr *attr = node->properties;
while (attr && attr->name && attr->children) {
char *attr_name = (char *) attr->name;
char *attr_value = (char *) xmlNodeListGetString(node->doc, attr->children, 1);
strlist_append(&list, attr_name ? attr_name : "");
strlist_append(&list, attr_value ? attr_value : "");
xmlFree((xmlChar *) attr_value);
attr = attr->next;
}
}
return list;
}
static int read_xml_data(xmlTextReaderPtr reader, struct JUNIT_Testsuite **testsuite) {
//const xmlChar *value;
const xmlChar *name = xmlTextReaderConstName(reader);
if (!name) {
// name could not be converted to string
name = BAD_CAST "--";
}
//value = xmlTextReaderConstValue(reader);
const char *node_name = (char *) name;
//const char *node_value = (char *) value;
struct StrList *attrs = attributes_to_strlist(reader);
if (attrs && strlist_count(attrs)) {
if (!strcmp(node_name, "testsuite")) {
for (size_t x = 0; x < strlist_count(attrs); x += 2) {
char *attr_name = strlist_item(attrs, x);
char *attr_value = strlist_item(attrs, x + 1);
if (!strcmp(attr_name, "name")) {
(*testsuite)->name = strdup(attr_value);
} else if (!strcmp(attr_name, "errors")) {
(*testsuite)->errors = (int) strtol(attr_value, NULL, 10);
} else if (!strcmp(attr_name, "failures")) {
(*testsuite)->failures = (int) strtol(attr_value, NULL, 0);
} else if (!strcmp(attr_name, "skipped")) {
(*testsuite)->skipped = (int) strtol(attr_value, NULL, 0);
} else if (!strcmp(attr_name, "tests")) {
(*testsuite)->tests = (int) strtol(attr_value, NULL, 0);
} else if (!strcmp(attr_name, "time")) {
(*testsuite)->time = strtof(attr_value, NULL);
} else if (!strcmp(attr_name, "timestamp")) {
(*testsuite)->timestamp = strdup(attr_value);
} else if (!strcmp(attr_name, "hostname")) {
(*testsuite)->hostname = strdup(attr_value);
}
}
} else if (!strcmp(node_name, "testcase")) {
struct JUNIT_Testcase *testcase = testcase_from_attributes(attrs);
testsuite_append_testcase(testsuite, testcase);
} else if (!strcmp(node_name, "failure")) {
size_t cur_tc = (*testsuite)->_tc_inuse > 0 ? (*testsuite)->_tc_inuse - 1 : (*testsuite)->_tc_inuse;
struct JUNIT_Failure *failure = testcase_failure_from_attributes(attrs);
(*testsuite)->testcase[cur_tc]->tc_result_state_type = JUNIT_RESULT_STATE_FAILURE;
(*testsuite)->testcase[cur_tc]->result_state.failure = failure;
} else if (!strcmp(node_name, "error")) {
size_t cur_tc = (*testsuite)->_tc_inuse > 0 ? (*testsuite)->_tc_inuse - 1 : (*testsuite)->_tc_inuse;
struct JUNIT_Error *error = testcase_error_from_attributes(attrs);
(*testsuite)->testcase[cur_tc]->tc_result_state_type = JUNIT_RESULT_STATE_ERROR;
(*testsuite)->testcase[cur_tc]->result_state.error = error;
} else if (!strcmp(node_name, "skipped")) {
size_t cur_tc = (*testsuite)->_tc_inuse > 0 ? (*testsuite)->_tc_inuse - 1 : (*testsuite)->_tc_inuse;
struct JUNIT_Skipped *skipped = testcase_skipped_from_attributes(attrs);
(*testsuite)->testcase[cur_tc]->tc_result_state_type = JUNIT_RESULT_STATE_SKIPPED;
(*testsuite)->testcase[cur_tc]->result_state.skipped = skipped;
}
}
(*testsuite)->passed = (*testsuite)->tests - (*testsuite)->failures - (*testsuite)->errors - (*testsuite)->skipped;
guard_strlist_free(&attrs);
return 0;
}
static int read_xml_file(const char *filename, struct JUNIT_Testsuite **testsuite) {
xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0);
if (!reader) {
return -1;
}
int result = xmlTextReaderRead(reader);
while (result == 1) {
read_xml_data(reader, testsuite);
result = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
return 0;
}
struct JUNIT_Testsuite *junitxml_testsuite_read(const char *filename) {
struct JUNIT_Testsuite *result;
if (access(filename, F_OK)) {
return NULL;
}
result = calloc(1, sizeof(*result));
if (!result) {
return NULL;
}
read_xml_file(filename, &result);
return result;
}
|