Menu

[r1477]: / advanced / src / test / parse_xml.c  Maximize  Restore  History

Download this file

522 lines (407 with data), 14.8 kB

  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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include <limits.h>
#include <stdlib.h>
#include "xmlrpc_config.h"
#include "girstring.h"
#include "casprintf.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/xmlparser.h"
#include "test.h"
#include "xml_data.h"
#include "parse_xml.h"
static void test_expat (void)
{
xmlrpc_env env;
xml_element *elem, *array, *data, *value1, *i4;
char *cdata;
size_t size;
xmlrpc_env_init(&env);
/* Parse a moderately complex XML document. */
xml_parse(&env, expat_data, strlen(expat_data), &elem);
TEST_NO_FAULT(&env);
TEST(elem != NULL);
/* Verify our results. */
TEST(streq(xml_element_name(elem), "value"));
TEST(xml_element_children_size(elem) == 1);
array = xml_element_children(elem)[0];
TEST(streq(xml_element_name(array), "array"));
TEST(xml_element_children_size(array) == 1);
data = xml_element_children(array)[0];
TEST(streq(xml_element_name(data), "data"));
TEST(xml_element_children_size(data) > 1);
value1 = xml_element_children(data)[0];
TEST(streq(xml_element_name(value1), "value"));
TEST(xml_element_children_size(value1) == 1);
i4 = xml_element_children(value1)[0];
TEST(streq(xml_element_name(i4), "i4"));
TEST(xml_element_children_size(i4) == 0);
cdata = xml_element_cdata(i4);
size = xml_element_cdata_size(i4);
TEST(size == strlen("2147483647"));
TEST(memcmp(cdata, "2147483647", strlen("2147483647")) == 0);
/* Test cleanup code (w/memprof). */
xml_element_free(elem);
/* Test broken XML */
xml_parse(&env, expat_error_data, strlen(expat_error_data), &elem);
TEST(env.fault_occurred);
xmlrpc_env_clean(&env);
}
static void
testParseNumberValue(void) {
char const xmldata[] =
XML_PROLOGUE
"<methodCall>\r\n"
"<methodName>test</methodName>\r\n"
"<params>\r\n"
"<param><value><int>2147483647</int></value></param>\r\n" \
"<param><value><int>-2147483648</int></value></param>\r\n" \
"<param><value><i1>10</i1></value></param>\r\n"
"<param><value><i2>10</i2></value></param>\r\n"
"<param><value><i4>10</i4></value></param>\r\n"
"<param><value><i8>10</i8></value></param>\r\n"
"<param><value><ex:i8>10</ex:i8></value></param>\r\n"
"<param><value><double>10</double></value></param>\r\n"
"<param><value><double>10.1</double></value></param>\r\n"
"<param><value><double>-10.1</double></value></param>\r\n"
"</params>\r\n"
"</methodCall>\r\n";
xmlrpc_env env;
xmlrpc_value * paramArrayP;
const char * methodName;
int arraySize;
xmlrpc_int int_max, int_min;
xmlrpc_int32 i_i1, i_i2, i_i4;
xmlrpc_int64 i_i8, i_ex_i8;
double d1, d2, d3;
xmlrpc_env_init(&env);
xmlrpc_parse_call(&env, xmldata, strlen(xmldata),
&methodName, &paramArrayP);
TEST_NO_FAULT(&env);
arraySize = xmlrpc_array_size(&env, paramArrayP);
TEST_NO_FAULT(&env);
TEST(arraySize == 10);
xmlrpc_decompose_value(
&env, paramArrayP, "(iiiiiIIddd)",
&int_max, &int_min, &i_i1, &i_i2, &i_i4, &i_i8, &i_ex_i8,
&d1, &d2, &d3);
TEST_NO_FAULT(&env);
TEST(int_max == INT_MAX);
TEST(int_min == INT_MIN);
TEST(i_i1 == 10);
TEST(i_i2 == 10);
TEST(i_i4 == 10);
TEST(i_i8 == 10);
TEST(i_ex_i8 == 10);
TEST(d1 == 10.0);
TEST(d2 == 10.1);
TEST(d3 == -10.1);
xmlrpc_DECREF(paramArrayP);
strfree(methodName);
xmlrpc_env_clean(&env);
}
static void
testParseMiscSimpleValue(void) {
char const xmldata[] =
XML_PROLOGUE
"<methodCall>\r\n"
"<methodName>test</methodName>\r\n"
"<params>\r\n"
"<param><value><string>hello</string></value></param>\r\n"
"<param><value><boolean>0</boolean></value></param>\r\n"
"<param><value><boolean>1</boolean></value></param>\r\n"
"<param><value><dateTime.iso8601>19980717T14:08:55</dateTime.iso8601>"
"</value></param>\r\n"
"<param><value><base64>YmFzZTY0IGRhdGE=</base64></value></param>\r\n"
"<param><value><nil/></value></param>\r\n"
"<param><value><ex:nil/></value></param>\r\n"
"</params>\r\n"
"</methodCall>\r\n";
xmlrpc_env env;
xmlrpc_value * paramArrayP;
const char * methodName;
int arraySize;
const char * str_hello;
xmlrpc_bool b_false, b_true;
const char * datetime;
unsigned char * b64_data;
size_t b64_len;
xmlrpc_env_init(&env);
xmlrpc_parse_call(&env, xmldata, strlen(xmldata),
&methodName, &paramArrayP);
TEST_NO_FAULT(&env);
arraySize = xmlrpc_array_size(&env, paramArrayP);
TEST_NO_FAULT(&env);
TEST(arraySize == 7);
xmlrpc_decompose_value(
&env, paramArrayP, "(sbb86nn)",
&str_hello, &b_false, &b_true, &datetime, &b64_data, &b64_len);
TEST_NO_FAULT(&env);
TEST(streq(str_hello, "hello"));
TEST(!b_false);
TEST(b_true);
TEST(streq(datetime, "19980717T14:08:55"));
TEST(b64_len == 11);
TEST(memcmp(b64_data, "base64 data", b64_len) == 0);
free(b64_data);
strfree(str_hello);
strfree(datetime);
xmlrpc_DECREF(paramArrayP);
strfree(methodName);
xmlrpc_env_clean(&env);
}
static void
validateParseResponseResult(xmlrpc_value * const valueP) {
xmlrpc_env env;
xmlrpc_value * s;
xmlrpc_int32 int_max;
xmlrpc_int32 int_min;
xmlrpc_int32 int_one;
xmlrpc_bool bool_false;
xmlrpc_bool bool_true;
char * str_hello;
char * str_untagged;
char * datetime;
unsigned char * b64_data;
size_t b64_len;
double negone;
double zero;
double one;
xmlrpc_env_init(&env);
xmlrpc_decompose_value(
&env, valueP, "((iibbs68())idddSs)",
&int_max, &int_min,
&bool_false, &bool_true, &str_hello,
&b64_data, &b64_len, &datetime,
&int_one, &negone, &zero, &one, &s, &str_untagged);
TEST_NO_FAULT(&env);
TEST(int_max == INT_MAX);
TEST(int_min == INT_MIN);
TEST(!bool_false);
TEST(bool_true);
TEST(strlen(str_hello) == strlen("Hello, world! <&>"));
TEST(streq(str_hello, "Hello, world! <&>"));
TEST(b64_len == 11);
TEST(memcmp(b64_data, "base64 data", b64_len) == 0);
TEST(streq(datetime, "19980717T14:08:55"));
TEST(int_one == 1);
TEST(negone == -1.0);
TEST(zero == 0.0);
TEST(one == 1.0);
TEST(streq(str_untagged, "Untagged string"));
free(str_hello);
free(b64_data);
free(datetime);
free(str_untagged);
{
/* Analyze the contents of our struct. */
xmlrpc_value * sval;
int size, sval_int;
TEST(s != NULL);
size = xmlrpc_struct_size(&env, s);
TEST_NO_FAULT(&env);
TEST(size == 2);
sval = xmlrpc_struct_get_value(&env, s, "ten <&>");
TEST_NO_FAULT(&env);
xmlrpc_decompose_value(&env, sval, "i", &sval_int);
TEST_NO_FAULT(&env);
TEST(sval_int == 10);
sval = xmlrpc_struct_get_value(&env, s, "twenty");
TEST_NO_FAULT(&env);
xmlrpc_decompose_value(&env, sval, "i", &sval_int);
TEST_NO_FAULT(&env);
TEST(sval_int == 20);
xmlrpc_DECREF(s);
}
xmlrpc_env_clean(&env);
}
static void
testParseGoodResponse(void) {
xmlrpc_env env;
xmlrpc_value * valueP;
int faultCode;
const char * faultString;
xmlrpc_env_init(&env);
xmlrpc_parse_response2(&env, good_response_xml, strlen(good_response_xml),
&valueP, &faultCode, &faultString);
TEST_NO_FAULT(&env);
TEST(faultString == NULL);
validateParseResponseResult(valueP);
xmlrpc_DECREF(valueP);
/* Try it again with old interface */
valueP = xmlrpc_parse_response(&env,
good_response_xml,
strlen(good_response_xml));
TEST_NO_FAULT(&env);
TEST(valueP != NULL);
validateParseResponseResult(valueP);
xmlrpc_DECREF(valueP);
xmlrpc_env_clean(&env);
}
static void
testParseBadResponse(void) {
/*----------------------------------------------------------------------------
Test parsing of data that is supposed to be a response, but is not
valid. Either not valid XML or not valid XML-RPC.
-----------------------------------------------------------------------------*/
unsigned int i;
{
xmlrpc_env env;
xmlrpc_value * valueP;
int faultCode;
const char * faultString;
/* First, test some poorly-formed XML data. */
xmlrpc_env_init(&env);
xmlrpc_parse_response2(&env,
unparseable_value, strlen(unparseable_value),
&valueP, &faultCode, &faultString);
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
xmlrpc_env_clean(&env);
xmlrpc_env_init(&env);
/* And again with the old interface */
valueP = xmlrpc_parse_response(&env, unparseable_value,
strlen(unparseable_value));
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
xmlrpc_env_clean(&env);
TEST(valueP == NULL);
}
/* Try with good XML, but bad XML-RPC. For this test, we test up to
but not including the <value> in a successful RPC response.
*/
/* Next, check for bogus responses. These are all well-formed XML, but
** they aren't legal XML-RPC. */
for (i = 15; bad_responses[i] != NULL; ++i) {
const char * const bad_resp = bad_responses[i];
xmlrpc_env env;
xmlrpc_value * v;
xml_element *elem;
xmlrpc_env_init(&env);
/* First, check to make sure that our test case is well-formed XML.
** (It's easy to make mistakes when writing the test cases!) */
xml_parse(&env, bad_resp, strlen(bad_resp), &elem);
TEST_NO_FAULT(&env);
xml_element_free(elem);
/* Now, make sure the higher-level routine barfs appropriately. */
v = xmlrpc_parse_response(&env, bad_resp, strlen(bad_resp));
TEST(env.fault_occurred);
TEST(env.fault_code != 0); /* We use 0 as a code in our bad faults. */
TEST(v == NULL);
xmlrpc_env_clean(&env);
}
/* Try with good XML, good XML-RPC response, except that the value
isn't valid XML-RPC
*/
for (i = 0; bad_values[i] != NULL; ++i) {
const char * const bad_resp = bad_values[i];
xmlrpc_env env;
xmlrpc_value * valueP;
xml_element *elem;
int faultCode;
const char * faultString;
xmlrpc_env_init(&env);
/* First, check to make sure that our test case is well-formed XML.
** (It's easy to make mistakes when writing the test cases!) */
xml_parse(&env, bad_resp, strlen(bad_resp), &elem);
TEST_NO_FAULT(&env);
xml_element_free(elem);
/* Now, make sure the higher-level routine barfs appropriately. */
xmlrpc_parse_response2(&env, bad_resp, strlen(bad_resp),
&valueP, &faultCode, &faultString);
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
xmlrpc_env_clean(&env);
xmlrpc_env_init(&env);
/* And again with the old interface */
valueP = xmlrpc_parse_response(&env, bad_resp, strlen(bad_resp));
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
TEST(valueP == NULL);
xmlrpc_env_clean(&env);
}
}
static void
testParseFaultResponse(void) {
/*----------------------------------------------------------------------------
Test parsing of a valid response that indicates the RPC failed.
-----------------------------------------------------------------------------*/
xmlrpc_env env;
xmlrpc_env_init(&env);
{
xmlrpc_value * resultP;
int faultCode;
const char * faultString;
xmlrpc_parse_response2(&env,
serialized_fault, strlen(serialized_fault),
&resultP, &faultCode, &faultString);
TEST_NO_FAULT(&env);
TEST(faultString != NULL);
TEST(faultCode == 6);
TEST(streq(faultString, "A fault occurred"));
strfree(faultString);
}
/* Now with the old interface */
{
xmlrpc_value * valueP;
xmlrpc_env fault;
/* Parse a valid fault. */
xmlrpc_env_init(&fault);
valueP = xmlrpc_parse_response(&fault, serialized_fault,
strlen(serialized_fault));
TEST(fault.fault_occurred);
TEST(fault.fault_code == 6);
TEST(streq(fault.fault_string, "A fault occurred"));
xmlrpc_env_clean(&fault);
}
xmlrpc_env_clean(&env);
}
static void
test_parse_xml_call(void) {
xmlrpc_env env;
const char *method_name;
xmlrpc_value *params;
int i1, i2;
const char **bad_call;
xml_element *elem;
xmlrpc_env_init(&env);
/* Parse a valid call. */
xmlrpc_parse_call(&env, serialized_call, strlen(serialized_call),
&method_name, &params);
TEST_NO_FAULT(&env);
TEST(params != NULL);
xmlrpc_decompose_value(&env, params, "(ii)", &i1, &i2);
xmlrpc_DECREF(params);
TEST_NO_FAULT(&env);
TEST(streq(method_name, "gloom&doom"));
TEST(i1 == 10 && i2 == 20);
strfree(method_name);
/* Test some poorly-formed XML data. */
xmlrpc_parse_call(&env, unparseable_value, strlen(unparseable_value),
&method_name, &params);
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
TEST(method_name == NULL && params == NULL);
/* Next, check for bogus values. These are all well-formed XML, but
they aren't legal XML-RPC.
*/
for (bad_call = bad_calls; *bad_call != NULL; ++bad_call) {
/* First, check to make sure that our test case is well-formed XML.
** (It's easy to make mistakes when writing the test cases!) */
xml_parse(&env, *bad_call, strlen(*bad_call), &elem);
TEST_NO_FAULT(&env);
xml_element_free(elem);
/* Now, make sure the higher-level routine barfs appropriately. */
xmlrpc_parse_call(&env, *bad_call, strlen(*bad_call),
&method_name, &params);
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
TEST(method_name == NULL && params == NULL);
}
xmlrpc_env_clean(&env);
}
void
test_parse_xml(void) {
printf("Running XML parsing tests.\n");
test_expat();
testParseNumberValue();
testParseMiscSimpleValue();
testParseGoodResponse();
testParseFaultResponse();
testParseBadResponse();
test_parse_xml_call();
printf("\n");
printf("XML parsing tests done.\n");
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.