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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
#include <limits.h>
#include <stdlib.h>
#include "xmlrpc_config.h"
#include "girstring.h"
#include "casprintf.h"
#include "xmlrpc-c/base.h"
#include "testtool.h"
#include "xml_data.h"
#include "parse_xml.h"
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"
"<param><value><double>+10.1</double></value></param>\r\n"
"<param><value><double>0</double></value></param>\r\n"
"<param><value><double>.01</double></value></param>\r\n"
"<param><value><double>5.</double></value></param>\r\n"
"<param><value><double>5.3E6</double></value></param>\r\n"
"<param><value><double> 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, d4, d5, d6, d7, d8, d9;
xmlrpc_env_init(&env);
xmlrpc_parse_call(&env, xmldata, strlen(xmldata),
&methodName, ¶mArrayP);
TEST_NO_FAULT(&env);
arraySize = xmlrpc_array_size(&env, paramArrayP);
TEST_NO_FAULT(&env);
TEST(arraySize == 16);
xmlrpc_decompose_value(
&env, paramArrayP, "(iiiiiIIddddddddd)",
&int_max, &int_min, &i_i1, &i_i2, &i_i4, &i_i8, &i_ex_i8,
&d1, &d2, &d3, &d4, &d5, &d6, &d7, &d8, &d9);
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);
TESTFLOATEQUAL(d1, 10.0);
TESTFLOATEQUAL(d2, 10.1);
TESTFLOATEQUAL(d3, -10.1);
TESTFLOATEQUAL(d4, +10.1);
TESTFLOATEQUAL(d5, 0.0);
TESTFLOATEQUAL(d6, 0.01);
TESTFLOATEQUAL(d7, 5.0);
TESTFLOATEQUAL(d8, 5.3E6);
TESTFLOATEQUAL(d9, 1.0);
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>"
"<dateTime.iso8601>19980717T14:08:55.123456</dateTime.iso8601>"
"</value></param>\r\n"
"<param><value>"
"<dateTime.iso8601>19980717T14:08:55Z</dateTime.iso8601>"
"</value></param>\r\n"
"<param><value>"
"<dateTime.iso8601>19980717T14:08:55.123456Z</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_sec;
const char * datetime_usec;
const char * datetime_secZ;
const char * datetime_usecZ;
unsigned char * b64_data;
size_t b64_len;
xmlrpc_env_init(&env);
xmlrpc_parse_call(&env, xmldata, strlen(xmldata),
&methodName, ¶mArrayP);
TEST_NO_FAULT(&env);
arraySize = xmlrpc_array_size(&env, paramArrayP);
TEST_NO_FAULT(&env);
TEST(arraySize == 10);
xmlrpc_decompose_value(
&env, paramArrayP, "(sbb88886nn)",
&str_hello, &b_false, &b_true,
&datetime_sec, &datetime_usec, &datetime_secZ, &datetime_usecZ,
&b64_data, &b64_len);
TEST_NO_FAULT(&env);
TEST(streq(str_hello, "hello"));
TEST(!b_false);
TEST(b_true);
TEST(streq(datetime_sec, "19980717T14:08:55"));
TEST(streq(datetime_usec, "19980717T14:08:55.123456"));
TEST(streq(datetime_secZ, "19980717T14:08:55"));
TEST(streq(datetime_usecZ, "19980717T14:08:55.123456"));
TEST(b64_len == 11);
TEST(memcmp(b64_data, "base64 data", b64_len) == 0);
free(b64_data);
strfree(str_hello);
strfree(datetime_sec);
strfree(datetime_usec);
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
testParseBadResponseXml(void) {
/*----------------------------------------------------------------------------
Test parsing of data that is supposed to be a response, but in not
even valid XML.
-----------------------------------------------------------------------------*/
xmlrpc_env env;
xmlrpc_value * valueP;
int faultCode;
const char * faultString;
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);
}
static void
testParseBadResponseXmlRpc(void) {
/*----------------------------------------------------------------------------
Test parsing of data that is supposed to be a response, and is valid
XML, but is not valid XML-RPC.
-----------------------------------------------------------------------------*/
unsigned int i;
/* 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;
xmlrpc_env_init(&env);
/* 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);
}
}
static void
testParseBadResult(void) {
/*----------------------------------------------------------------------------
Test parsing of data that is supposed to be a response, but is not
valid. It looks like a valid success response, but the result value
is not valid XML-RPC.
-----------------------------------------------------------------------------*/
unsigned int i;
for (i = 0; bad_values[i] != NULL; ++i) {
const char * const bad_resp = bad_values[i];
xmlrpc_env env;
xmlrpc_value * valueP;
int faultCode;
const char * faultString;
xmlrpc_env_init(&env);
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
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.
-----------------------------------------------------------------------------*/
testParseBadResponseXml();
testParseBadResponseXmlRpc();
testParseBadResult();
}
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_env fault;
/* Parse a valid fault. */
xmlrpc_env_init(&fault);
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
testParseXmlCall(void) {
xmlrpc_env env;
const char *method_name;
xmlrpc_value *params;
int i1, i2;
const char **bad_call;
xmlrpc_env_init(&env);
/* Parse a valid call. */
xmlrpc_parse_call(&env, serialized_call, strlen(serialized_call),
&method_name, ¶ms);
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, ¶ms);
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) {
xmlrpc_parse_call(&env, *bad_call, strlen(*bad_call),
&method_name, ¶ms);
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
TEST(method_name == NULL && params == NULL);
}
xmlrpc_env_clean(&env);
}
static void
testParseXmlValue(void) {
const char * const xmlInt7 = "<value><int>7</int ></value>";
const char * const xmlBadVal1 = "hello";
const char * const xmlBadVal2 = "<junk/>";
xmlrpc_value * valueP;
xmlrpc_env env;
xmlrpc_env_init(&env);
xmlrpc_parse_value_xml(&env, xmlInt7, strlen(xmlInt7), &valueP);
TEST_NO_FAULT(&env);
xmlrpc_DECREF(valueP);
xmlrpc_parse_value_xml(&env, xmlBadVal1, strlen(xmlBadVal1), &valueP);
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
xmlrpc_env_clean(&env);
xmlrpc_env_init(&env);
xmlrpc_parse_value_xml(&env, xmlBadVal2, strlen(xmlBadVal2), &valueP);
TEST_FAULT(&env, XMLRPC_PARSE_ERROR);
xmlrpc_env_clean(&env);
}
void
test_parse_xml(void) {
printf("Running XML parsing tests.\n");
testParseNumberValue();
testParseMiscSimpleValue();
testParseGoodResponse();
testParseFaultResponse();
testParseBadResponse();
testParseXmlCall();
testParseXmlValue();
printf("\n");
printf("XML parsing tests done.\n");
}
|