3 * NB: do not let your IDE fool you. The correct encoding for this file is NOT UTF8.
5 include_once __DIR__ . '/../lib/xmlrpc.inc';
6 include_once __DIR__ . '/../lib/xmlrpcs.inc';
8 include_once __DIR__ . '/parse_args.php';
11 * Tests involving parsing of xml and handling of xmlrpc values
13 class ParsingBugsTests extends PHPUnit_Framework_TestCase
15 public $args = array();
17 protected function setUp()
19 $this->args = argParser::getArgs();
20 if ($this->args['DEBUG'] == 1)
24 protected function tearDown()
26 if ($this->args['DEBUG'] != 1)
28 $out = ob_get_clean();
29 $status = $this->getStatus();
30 if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR
31 || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
36 protected function newMsg($methodName, $params = array())
38 $msg = new xmlrpcmsg($methodName, $params);
39 $msg->setDebug($this->args['DEBUG']);
43 public function testMinusOneString()
45 $v = new xmlrpcval('-1');
46 $u = new xmlrpcval('-1', 'string');
47 $t = new xmlrpcval(-1, 'string');
48 $this->assertEquals($v->scalarval(), $u->scalarval());
49 $this->assertEquals($v->scalarval(), $t->scalarval());
53 * This looks funny, and we might call it a bug. But we strive for 100 backwards compat...
55 public function testMinusOneInt()
58 $v = new xmlrpcval(-1);
59 $this->assertEquals($u->scalarval(), $v->scalarval());
62 public function testUnicodeInMemberName()
64 $str = "G" . chr(252) . "nter, El" . chr(232) . "ne";
65 $v = array($str => new xmlrpcval(1));
66 $r = new xmlrpcresp(new xmlrpcval($v, 'struct'));
68 $m = $this->newMsg('dummy');
69 $r = $m->parseResponse($r);
71 $this->assertEquals(true, $v->structmemexists($str));
74 public function testUnicodeInErrorString()
76 $response = utf8_encode(
77 '<?xml version="1.0"?>
79 <!-- found by G. giunta, covers what happens when lib receives
80 UTF8 chars in response text and comments -->
81 <!-- ' . chr(224) . chr(252) . chr(232) . 'àüè -->
87 <name>faultCode</name>
88 <value><int>888</int></value>
91 <name>faultString</name>
92 <value><string>' . chr(224) . chr(252) . chr(232) . 'àüè</string></value>
98 $m = $this->newMsg('dummy');
99 $r = $m->parseResponse($response);
100 $v = $r->faultString();
101 $this->assertEquals(chr(224) . chr(252) . chr(232) . chr(224) . chr(252) . chr(232), $v);
104 public function testValidNumbers()
106 $m = $this->newMsg('dummy');
108 '<?xml version="1.0"?>
115 <name>integer1</name>
116 <value><int>01</int></value>
120 <value><double>01.10</double></value>
123 <name>integer2</name>
124 <value><int>+1</int></value>
128 <value><double>+1.10</double></value>
132 <value><double>-1.10e2</double></value>
139 $r = $m->parseResponse($fp);
141 $s = $v->structmem('integer1');
142 $t = $v->structmem('float1');
143 $u = $v->structmem('integer2');
144 $w = $v->structmem('float2');
145 $x = $v->structmem('float3');
146 $this->assertEquals(1, $s->scalarval());
147 $this->assertEquals(1.1, $t->scalarval());
148 $this->assertEquals(1, $u->scalarval());
149 $this->assertEquals(1.1, $w->scalarval());
150 $this->assertEquals(-110.0, $x->scalarval());
153 public function testAddScalarToStruct()
155 $v = new xmlrpcval(array('a' => 'b'), 'struct');
156 // use @ operator in case error_log gets on screen
157 $r = @$v->addscalar('c');
158 $this->assertEquals(0, $r);
161 public function testAddStructToStruct()
163 $v = new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
164 $r = $v->addstruct(array('b' => new xmlrpcval('c')));
165 $this->assertEquals(2, $v->structsize());
166 $this->assertEquals(1, $r);
167 $r = $v->addstruct(array('b' => new xmlrpcval('b')));
168 $this->assertEquals(2, $v->structsize());
171 public function testAddArrayToArray()
173 $v = new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
174 $r = $v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
175 $this->assertEquals(4, $v->arraysize());
176 $this->assertEquals(1, $r);
179 public function testEncodeArray()
182 $v = php_xmlrpc_encode($r);
183 $this->assertEquals('array', $v->kindof());
186 public function testEncodeRecursive()
188 $v = php_xmlrpc_encode(php_xmlrpc_encode('a simple string'));
189 $this->assertEquals('scalar', $v->kindof());
192 public function testBrokenRequests()
194 $s = new xmlrpc_server();
195 // omitting the 'params' tag: not tolerated by the lib anymore
196 $f = '<?xml version="1.0"?>
198 <methodName>system.methodHelp</methodName>
200 <value><string>system.methodHelp</string></value>
203 $r = $s->parserequest($f);
204 $this->assertEquals(15, $r->faultCode());
205 // omitting a 'param' tag
206 $f = '<?xml version="1.0"?>
208 <methodName>system.methodHelp</methodName>
210 <value><string>system.methodHelp</string></value>
213 $r = $s->parserequest($f);
214 $this->assertEquals(15, $r->faultCode());
215 // omitting a 'value' tag
216 $f = '<?xml version="1.0"?>
218 <methodName>system.methodHelp</methodName>
220 <param><string>system.methodHelp</string></param>
223 $r = $s->parserequest($f);
224 $this->assertEquals(15, $r->faultCode());
227 public function testBrokenResponses()
229 $m = $this->newMsg('dummy');
230 // omitting the 'params' tag: no more tolerated by the lib...
231 $f = '<?xml version="1.0"?>
234 <value><string>system.methodHelp</string></value>
237 $r = $m->parseResponse($f);
238 $this->assertEquals(2, $r->faultCode());
239 // omitting the 'param' tag: no more tolerated by the lib...
240 $f = '<?xml version="1.0"?>
243 <value><string>system.methodHelp</string></value>
246 $r = $m->parseResponse($f);
247 $this->assertEquals(2, $r->faultCode());
248 // omitting a 'value' tag: KO
249 $f = '<?xml version="1.0"?>
252 <param><string>system.methodHelp</string></param>
255 $r = $m->parseResponse($f);
256 $this->assertEquals(2, $r->faultCode());
259 public function testBuggyHttp()
261 $s = $this->newMsg('dummy');
262 $f = 'HTTP/1.1 100 Welcome to the jungle
265 X-Content-Marx-Brothers: Harpo
267 Content-Length: who knows?
271 <?xml version="1.0"?>
272 <!-- First of all, let\'s check out if the lib properly handles a commented </methodResponse> tag... -->
273 <methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
274 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
277 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
278 <script type="text\javascript">document.write(\'Hello, my name is added nag, I\\\'m happy to serve your content for free\');</script>
280 $r = $s->parseResponse($f);
282 $s = $v->structmem('content');
283 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
286 public function testStringBug()
288 $s = $this->newMsg('dummy');
289 $f = '<?xml version="1.0"?>
291 <!-- found by 2z69xks7bpy001@sneakemail.com, amongst others
292 covers what happens when there\'s character data after </string>
293 and before </value> -->
306 <name>sessionID</name>
308 <string>S300510007I</string>
316 $r = $s->parseResponse($f);
318 $s = $v->structmem('sessionID');
319 $this->assertEquals('S300510007I', $s->scalarval());
322 public function testWhiteSpace()
324 $s = $this->newMsg('dummy');
325 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
326 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
329 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
331 $r = $s->parseResponse($f);
333 $s = $v->structmem('content');
334 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
337 public function testDoubleDataInArrayTag()
339 $s = $this->newMsg('dummy');
340 $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
343 </array></value></param></params></methodResponse>
345 $r = $s->parseResponse($f);
346 $v = $r->faultCode();
347 $this->assertEquals(2, $v);
348 $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
349 <data><value>Hello world</value></data>
351 </array></value></param></params></methodResponse>
353 $r = $s->parseResponse($f);
354 $v = $r->faultCode();
355 $this->assertEquals(2, $v);
358 public function testDoubleStuffInValueTag()
360 $s = $this->newMsg('dummy');
361 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
362 <string>hello world</string>
363 <array><data></data></array>
364 </value></param></params></methodResponse>
366 $r = $s->parseResponse($f);
367 $v = $r->faultCode();
368 $this->assertEquals(2, $v);
369 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
370 <string>hello</string>
371 <string>world</string>
372 </value></param></params></methodResponse>
374 $r = $s->parseResponse($f);
375 $v = $r->faultCode();
376 $this->assertEquals(2, $v);
377 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
378 <string>hello</string>
379 <struct><member><name>hello><value>world</value></member></struct>
380 </value></param></params></methodResponse>
382 $r = $s->parseResponse($f);
383 $v = $r->faultCode();
384 $this->assertEquals(2, $v);
387 public function testAutodecodeResponse()
389 $s = $this->newMsg('dummy');
390 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
391 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
394 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
396 $r = $s->parseResponse($f, true, 'phpvals');
399 $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s);
402 public function testNoDecodeResponse()
404 $s = $this->newMsg('dummy');
405 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
406 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
409 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>';
410 $r = $s->parseResponse($f, true, 'xml');
412 $this->assertEquals($f, $v);
415 public function testAutoCoDec()
417 $data1 = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
418 $data2 = array('zero' => $data1, 'one' => $data1, 'two' => $data1, 'three' => $data1, 'four' => $data1, 'five' => $data1, 'six' => $data1, 'seven' => $data1, 'eight' => $data1, 'nine' => $data1);
419 $data = array($data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2);
420 //$keys = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
421 $v1 = php_xmlrpc_encode($data, array('auto_dates'));
422 $v2 = php_xmlrpc_decode_xml($v1->serialize());
423 $this->assertEquals($v1, $v2);
424 $r1 = new PhpXmlRpc\Response($v1);
425 $r2 = php_xmlrpc_decode_xml($r1->serialize());
426 $r2->serialize(); // needed to set internal member payload
427 $this->assertEquals($r1, $r2);
428 $m1 = new PhpXmlRpc\Request('hello dolly', array($v1));
429 $m2 = php_xmlrpc_decode_xml($m1->serialize());
430 $m2->serialize(); // needed to set internal member payload
431 $this->assertEquals($m1, $m2);
434 public function testUTF8Request()
436 $sendstring = 'κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
437 $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
438 \PhpXmlRpc\PhpXmlRpc::importGlobals();
439 $f = new xmlrpcval($sendstring, 'string');
440 $v = $f->serialize();
441 $this->assertEquals("<value><string>κόσμε</string></value>\n", $v);
442 $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
443 \PhpXmlRpc\PhpXmlRpc::importGlobals();
446 public function testUTF8Response()
448 $string = chr(224) . chr(252) . chr(232);
450 $s = $this->newMsg('dummy');
451 $f = "HTTP/1.1 200 OK\r\nContent-type: text/xml; charset=UTF-8\r\n\r\n" . '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
452 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>' . utf8_encode($string) . '</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
454 $r = $s->parseResponse($f, false, 'phpvals');
457 $this->assertEquals($string, $v);
459 $f = '<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
460 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>' . utf8_encode($string) . '</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
462 $r = $s->parseResponse($f, false, 'phpvals');
465 $this->assertEquals($string, $v);
467 $r = php_xmlrpc_decode_xml($f);
469 $v = $v->structmem('content')->scalarval();
470 $this->assertEquals($string, $v);
473 public function testLatin1Response()
475 $string = chr(224) . chr(252) . chr(232);
477 $s = $this->newMsg('dummy');
478 $f = "HTTP/1.1 200 OK\r\nContent-type: text/xml; charset=ISO-8859-1\r\n\r\n" . '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
479 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>' . $string . '</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
481 $r = $s->parseResponse($f, false, 'phpvals');
484 $this->assertEquals($string, $v);
486 $f = '<?xml version="1.0" encoding="ISO-8859-1"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
487 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>' . $string . '</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
489 $r = $s->parseResponse($f, false, 'phpvals');
492 $this->assertEquals($string, $v);
494 $r = php_xmlrpc_decode_xml($f);
496 $v = $v->structmem('content')->scalarval();
497 $this->assertEquals($string, $v);
500 public function testUTF8IntString()
502 $v = new xmlrpcval(100, 'int');
503 $s = $v->serialize('UTF-8');
504 $this->assertequals("<value><int>100</int></value>\n", $s);
507 public function testStringInt()
509 $v = new xmlrpcval('hello world', 'int');
510 $s = $v->serialize();
511 $this->assertequals("<value><int>0</int></value>\n", $s);
514 public function testStructMemExists()
516 $v = php_xmlrpc_encode(array('hello' => 'world'));
517 $b = $v->structmemexists('hello');
518 $this->assertequals(true, $b);
519 $b = $v->structmemexists('world');
520 $this->assertequals(false, $b);
523 public function testNilvalue()
525 // default case: we do not accept nil values received
526 $v = new xmlrpcval('hello', 'null');
527 $r = new xmlrpcresp($v);
528 $s = $r->serialize();
529 $m = $this->newMsg('dummy');
530 $r = $m->parseresponse($s);
531 $this->assertequals(2, $r->faultCode());
532 // enable reception of nil values
533 $GLOBALS['xmlrpc_null_extension'] = true;
534 \PhpXmlRpc\PhpXmlRpc::importGlobals();
535 $r = $m->parseresponse($s);
537 $this->assertequals('null', $v->scalartyp());
538 // test with the apache version: EX:NIL
539 $GLOBALS['xmlrpc_null_apache_encoding'] = true;
540 \PhpXmlRpc\PhpXmlRpc::importGlobals();
542 $v = new xmlrpcval('hello', 'null');
543 $s = $v->serialize();
544 $this->assertequals(1, preg_match('#<value><ex:nil/></value>#', $s));
546 $r = new xmlrpcresp($v);
547 $s = $r->serialize();
548 $r = $m->parseresponse($s);
550 $this->assertequals('null', $v->scalartyp());
551 $GLOBALS['xmlrpc_null_extension'] = false;
552 \PhpXmlRpc\PhpXmlRpc::importGlobals();
553 $r = $m->parseresponse($s);
554 $this->assertequals(2, $r->faultCode());
557 public function testLocale()
559 $locale = setlocale(LC_NUMERIC, 0);
560 /// @todo on php 5.3/win setting locale to german does not seem to set decimal separator to comma...
561 if (setlocale(LC_NUMERIC, 'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false) {
562 $v = new xmlrpcval(1.1, 'double');
563 if (strpos($v->scalarval(), ',') == 1) {
564 $r = $v->serialize();
565 $this->assertequals(false, strpos($r, ','));
566 setlocale(LC_NUMERIC, $locale);
568 setlocale(LC_NUMERIC, $locale);
569 $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
572 $this->markTestSkipped('did not find a locale which sets decimal separator to comma');
576 public function testArrayAccess()
578 $v1 = new xmlrpcval(array(new xmlrpcval('one'), new xmlrpcval('two')), 'array');
579 $this->assertequals(1, count($v1));
580 $out = array('me' => array(), 'mytype' => 2, '_php_class' => null);
581 foreach($v1 as $key => $val)
583 $expected = each($out);
584 $this->assertequals($expected['key'], $key);
585 if (gettype($expected['value']) == 'array') {
586 $this->assertequals('array', gettype($val));
588 $this->assertequals($expected['value'], $val);
592 $v2 = new \PhpXmlRpc\Value(array(new \PhpXmlRpc\Value('one'), new \PhpXmlRpc\Value('two')), 'array');
593 $this->assertequals(2, count($v2));
594 $out = array(0 => 'object', 1 => 'object');
595 foreach($v2 as $key => $val)
597 $expected = each($out);
598 $this->assertequals($expected['key'], $key);
599 $this->assertequals($expected['value'], gettype($val));