90c5844fc1421f1f84755e93e0c0a886380af1ee
[plcapi.git] / test / testsuite.php
1 <?php
2
3         include(getcwd().'/parse_args.php');
4
5         require_once('xmlrpc.inc');
6         require_once('xmlrpcs.inc');
7         require_once('xmlrpc_wrappers.inc');
8
9         require_once 'phpunit.php';
10         //require_once 'PHPUnit/TestDecorator.php';
11
12         // let testuite run for the needed time
13         if ((int)ini_get('max_execution_time') < 180)
14                 ini_set('max_execution_time', 180);
15
16         ini_set('max_execution_time', 180);
17
18         $suite = new PHPUnit_TestSuite();
19
20         // array with list of failed tests
21         $failed_tests = array();
22
23         class LocalhostTests extends PHPUnit_TestCase
24         {
25                 var $client = null;
26                 var $method = 'http';
27                 var $timeout = 10;
28                 var $request_compression = null;
29                 var $accepted_compression = '';
30
31                 function fail($message = '')
32                 {
33                         PHPUnit_TestCase::fail($message);
34                         // save in global var that this particular test has failed
35                         // (but only if not called from subclass objects / multitests)
36                         if (function_exists('debug_backtrace') && strtolower(get_class($this)) == 'localhosttests')
37                         {
38                                 global $failed_tests;
39                                 $trace = debug_backtrace();
40                                 for ($i = 0; $i < count($trace); $i++)
41                                 {
42                                         if (strpos($trace[$i]['function'], 'test') === 0)
43                                         {
44                                                 $failed_tests[$trace[$i]['function']] = true;
45                                                 break;
46                                         }
47                                 }
48                         }
49                 }
50
51                 function setUp()
52                 {
53                         global $DEBUG, $LOCALSERVER, $URI;
54                         $server = explode(':', $LOCALSERVER);
55                         if(count($server) > 1)
56                         {
57                                 $this->client=new xmlrpc_client($URI, $server[0], $server[1]);
58                         }
59                         else
60                         {
61                                 $this->client=new xmlrpc_client($URI, $LOCALSERVER);
62                         }
63                         if($DEBUG)
64                         {
65                                 $this->client->setDebug($DEBUG);
66                         }
67                         $this->client->request_compression = $this->request_compression;
68                         $this->client->accepted_compression = $this->accepted_compression;
69                 }
70
71                 function send($msg, $errrorcode=0, $return_response=false)
72                 {
73                         $r = $this->client->send($msg, $this->timeout, $this->method);
74                         // for multicall, return directly array of responses
75                         if(is_array($r))
76                         {
77                                 return $r;
78                         }
79                         $this->assertEquals($r->faultCode(), $errrorcode, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
80                         if(!$r->faultCode())
81                         {
82                                 if($return_response)
83                                         return $r;
84                                 else
85                                         return $r->value();
86                         }
87                         else
88                         {
89                                 return null;
90                         }
91                 }
92
93                 function testString()
94                 {
95                         $sendstring="here are 3 \"entities\": < > &" .
96                                 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
97                                 " - isn't that great? \\\"hackery\\\" at it's best " .
98                                 " also don't want to miss out on \$item[0]. ".
99                                 "The real weird stuff follows: CRLF here".chr(13).chr(10).
100                                 "a simple CR here".chr(13).
101                                 "a simple LF here".chr(10).
102                                 "and then LFCR".chr(10).chr(13).
103                                 "last but not least weird names: G�nter, El�ne, and an xml comment closing tag: -->";
104                         $f=new xmlrpcmsg('examples.stringecho', array(
105                                 new xmlrpcval($sendstring, 'string')
106                         ));
107                         $v=$this->send($f);
108                         if($v)
109                         {
110                                 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
111                                 // so we relax our tests...
112                                 $l1 = strlen($sendstring);
113                                 $l2 = strlen($v->scalarval());
114                                 if ($l1 == $l2)
115                                         $this->assertEquals($sendstring, $v->scalarval());
116                                 else
117                                         $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
118                         }
119                 }
120
121                 function testAddingDoubles()
122                 {
123                         // note that rounding errors mean i
124                         // keep precision to sensible levels here ;-)
125                         $a=12.13; $b=-23.98;
126                         $f=new xmlrpcmsg('examples.addtwodouble',array(
127                                 new xmlrpcval($a, 'double'),
128                                 new xmlrpcval($b, 'double')
129                         ));
130                         $v=$this->send($f);
131                         if($v)
132                         {
133                                 $this->assertEquals($a+$b,$v->scalarval());
134                         }
135                 }
136
137                 function testAdding()
138                 {
139                         $f=new xmlrpcmsg('examples.addtwo',array(
140                                 new xmlrpcval(12, 'int'),
141                                 new xmlrpcval(-23, 'int')
142                         ));
143                         $v=$this->send($f);
144                         if($v)
145                         {
146                                 $this->assertEquals(12-23, $v->scalarval());
147                         }
148                 }
149
150                 function testInvalidNumber()
151                 {
152                         $f=new xmlrpcmsg('examples.addtwo',array(
153                                 new xmlrpcval('fred', 'int'),
154                                 new xmlrpcval("\"; exec('ls')", 'int')
155                         ));
156                         $v=$this->send($f);
157                         /// @todo a fault condition should be generated here
158                         /// by the server, which we pick up on
159                         if($v)
160                         {
161                                 $this->assertEquals(0, $v->scalarval());
162                         }
163                 }
164
165                 function testBoolean()
166                 {
167                         $f=new xmlrpcmsg('examples.invertBooleans', array(
168                                 new xmlrpcval(array(
169                                         new xmlrpcval(true, 'boolean'),
170                                         new xmlrpcval(false, 'boolean'),
171                                         new xmlrpcval(1, 'boolean'),
172                                         new xmlrpcval(0, 'boolean'),
173                                         //new xmlrpcval('true', 'boolean'),
174                                         //new xmlrpcval('false', 'boolean')
175                                 ),
176                                 'array'
177                         )));
178                         $answer='0101';
179                         $v=$this->send($f);
180                         if($v)
181                         {
182                                 $sz=$v->arraysize();
183                                 $got='';
184                                 for($i=0; $i<$sz; $i++)
185                                 {
186                                         $b=$v->arraymem($i);
187                                         if($b->scalarval())
188                                         {
189                                                 $got.='1';
190                                         }
191                                         else
192                                         {
193                                                 $got.='0';
194                                         }
195                                 }
196                                 $this->assertEquals($answer, $got);
197                         }
198                 }
199
200                 function testBase64()
201                 {
202                         $sendstring='Mary had a little lamb,
203 Whose fleece was white as snow,
204 And everywhere that Mary went
205 the lamb was sure to go.
206
207 Mary had a little lamb
208 She tied it to a pylon
209 Ten thousand volts went down its back
210 And turned it into nylon';
211                         $f=new xmlrpcmsg('examples.decode64',array(
212                                 new xmlrpcval($sendstring, 'base64')
213                         ));
214                         $v=$this->send($f);
215                         if($v)
216                         {
217                                 if (strlen($sendstring) == strlen($v->scalarval()))
218                                         $this->assertEquals($sendstring, $v->scalarval());
219                                 else
220                                         $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
221                         }
222                 }
223
224         function testDateTime()
225         {
226             $time = time();
227             $t1 = new xmlrpcval($time, 'dateTime.iso8601');
228             $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
229             $this->assertEquals($t1->serialize(), $t2->serialize());
230             if (class_exists('DateTime'))
231             {
232                 $datetime = new DateTime();
233                 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
234                 $this->assertEquals($t1->serialize(), $t3->serialize());
235             }
236         }
237
238                 function testCountEntities()
239                 {
240                         $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
241                         $f = new xmlrpcmsg('validator1.countTheEntities',array(
242                                 new xmlrpcval($sendstring, 'string')
243                         ));
244                         $v = $this->send($f);
245                         if($v)
246                         {
247                                 $got = '';
248                                 $expected = '37210';
249                                 $expect_array = array('ctLeftAngleBrackets','ctRightAngleBrackets','ctAmpersands','ctApostrophes','ctQuotes');
250                                 while(list(,$val) = each($expect_array))
251                                 {
252                                         $b = $v->structmem($val);
253                                         $got .= $b->me['int'];
254                                 }
255                                 $this->assertEquals($expected, $got);
256                         }
257                 }
258
259                 function _multicall_msg($method, $params)
260                 {
261                         $struct['methodName'] = new xmlrpcval($method, 'string');
262                         $struct['params'] = new xmlrpcval($params, 'array');
263                         return new xmlrpcval($struct, 'struct');
264                 }
265
266                 function testServerMulticall()
267                 {
268                         // We manually construct a system.multicall() call to ensure
269                         // that the server supports it.
270
271                         // NB: This test will NOT pass if server does not support system.multicall.
272
273                         // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
274                         $good1 = $this->_multicall_msg(
275                                 'system.methodHelp',
276                                 array(php_xmlrpc_encode('system.listMethods')));
277                         $bad = $this->_multicall_msg(
278                                 'test.nosuch',
279                                 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
280                         $recursive = $this->_multicall_msg(
281                                 'system.multicall',
282                                 array(new xmlrpcval(array(), 'array')));
283                         $good2 = $this->_multicall_msg(
284                                 'system.methodSignature',
285                                 array(php_xmlrpc_encode('system.listMethods')));
286                         $arg = new xmlrpcval(
287                                 array($good1, $bad, $recursive, $good2),
288                                 'array'
289                         );
290
291                         $f = new xmlrpcmsg('system.multicall', array($arg));
292                         $v = $this->send($f);
293                         if($v)
294                         {
295                                 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
296                                 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
297
298                                 $r1 = $v->arraymem(0);
299                                 $this->assertTrue(
300                                         $r1->kindOf() == 'array' && $r1->arraysize() == 1,
301                                         "did not get array of size 1 from good1"
302                                 );
303
304                                 $r2 = $v->arraymem(1);
305                                 $this->assertTrue(
306                                         $r2->kindOf() == 'struct',
307                                         "no fault from bad"
308                                 );
309
310                                 $r3 = $v->arraymem(2);
311                                 $this->assertTrue(
312                                         $r3->kindOf() == 'struct',
313                                         "recursive system.multicall did not fail"
314                                 );
315
316                                 $r4 = $v->arraymem(3);
317                                 $this->assertTrue(
318                                         $r4->kindOf() == 'array' && $r4->arraysize() == 1,
319                                         "did not get array of size 1 from good2"
320                                 );
321                         }
322                 }
323
324                 function testClientMulticall1()
325                 {
326                         // NB: This test will NOT pass if server does not support system.multicall.
327
328                         $this->client->no_multicall = false;
329
330                         $good1 = new xmlrpcmsg('system.methodHelp',
331                                 array(php_xmlrpc_encode('system.listMethods')));
332                         $bad = new xmlrpcmsg('test.nosuch',
333                                 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
334                         $recursive = new xmlrpcmsg('system.multicall',
335                                 array(new xmlrpcval(array(), 'array')));
336                         $good2 = new xmlrpcmsg('system.methodSignature',
337                                 array(php_xmlrpc_encode('system.listMethods'))
338                         );
339
340                         $r = $this->send(array($good1, $bad, $recursive, $good2));
341                         if($r)
342                         {
343                                 $this->assertTrue(count($r) == 4, "wrong number of return values");
344                         }
345
346                         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
347                         if(!$r[0]->faultCode())
348                         {
349                                 $val = $r[0]->value();
350                                 $this->assertTrue(
351                                         $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
352                                         "good1 did not return string"
353                                 );
354                         }
355                         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
356                         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
357                         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
358                         if(!$r[3]->faultCode())
359                         {
360                                 $val = $r[3]->value();
361                                 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
362                         }
363                         // This is the only assert in this test which should fail
364                         // if the test server does not support system.multicall.
365                         $this->assertTrue($this->client->no_multicall == false,
366                                 "server does not support system.multicall"
367                         );
368                 }
369
370                 function testClientMulticall2()
371                 {
372                         // NB: This test will NOT pass if server does not support system.multicall.
373
374                         $this->client->no_multicall = true;
375
376                         $good1 = new xmlrpcmsg('system.methodHelp',
377                                 array(php_xmlrpc_encode('system.listMethods')));
378                         $bad = new xmlrpcmsg('test.nosuch',
379                                 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
380                         $recursive = new xmlrpcmsg('system.multicall',
381                                 array(new xmlrpcval(array(), 'array')));
382                         $good2 = new xmlrpcmsg('system.methodSignature',
383                                 array(php_xmlrpc_encode('system.listMethods'))
384                         );
385
386                         $r = $this->send(array($good1, $bad, $recursive, $good2));
387                         if($r)
388                         {
389                                 $this->assertTrue(count($r) == 4, "wrong number of return values");
390                         }
391
392                         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
393                         if(!$r[0]->faultCode())
394                         {
395                                 $val = $r[0]->value();
396                                 $this->assertTrue(
397                                         $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
398                                         "good1 did not return string");
399                         }
400                         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
401                         $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
402                         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
403                         if(!$r[3]->faultCode())
404                         {
405                                 $val = $r[3]->value();
406                                 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
407                         }
408                 }
409
410                 function testClientMulticall3()
411                 {
412                         // NB: This test will NOT pass if server does not support system.multicall.
413
414                         $this->client->return_type = 'phpvals';
415                         $this->client->no_multicall = false;
416
417                         $good1 = new xmlrpcmsg('system.methodHelp',
418                                 array(php_xmlrpc_encode('system.listMethods')));
419                         $bad = new xmlrpcmsg('test.nosuch',
420                                 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
421                         $recursive = new xmlrpcmsg('system.multicall',
422                                 array(new xmlrpcval(array(), 'array')));
423                         $good2 = new xmlrpcmsg('system.methodSignature',
424                                 array(php_xmlrpc_encode('system.listMethods'))
425                         );
426
427                         $r = $this->send(array($good1, $bad, $recursive, $good2));
428                         if($r)
429                         {
430                                 $this->assertTrue(count($r) == 4, "wrong number of return values");
431                         }
432                         $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
433                         if(!$r[0]->faultCode())
434                         {
435                                 $val = $r[0]->value();
436                                 $this->assertTrue(
437                                         is_string($val) , "good1 did not return string");
438                         }
439                         $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
440                         $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
441                         $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
442                         if(!$r[3]->faultCode())
443                         {
444                                 $val = $r[3]->value();
445                                 $this->assertTrue(is_array($val), "good2 did not return array");
446                         }
447                         $this->client->return_type = 'xmlrpcvals';
448                 }
449
450                 function testCatchWarnings()
451                 {
452                         $f = new xmlrpcmsg('examples.generatePHPWarning', array(
453                                 new xmlrpcval('whatever', 'string')
454                         ));
455                         $v = $this->send($f);
456                         if($v)
457                         {
458                                 $this->assertEquals($v->scalarval(), true);
459                         }
460                 }
461
462             function testCatchExceptions()
463             {
464                 global $URI;
465                 $f = new xmlrpcmsg('examples.raiseException', array(
466                         new xmlrpcval('whatever', 'string')
467                 ));
468                 $v = $this->send($f, $GLOBALS['xmlrpcerr']['server_error']);
469                 $this->client->path = $URI.'?EXCEPTION_HANDLING=1';
470                 $v = $this->send($f, 1);
471                 $this->client->path = $URI.'?EXCEPTION_HANDLING=2';
472                 $v = $this->send($f, $GLOBALS['xmlrpcerr']['invalid_return']);
473             }
474
475                 function testZeroParams()
476                 {
477                         $f = new xmlrpcmsg('system.listMethods');
478                         $v = $this->send($f);
479                 }
480
481                 function testCodeInjectionServerSide()
482                 {
483                         $f = new xmlrpcmsg('system.MethodHelp');
484                         $f->payload = "<?xml version=\"1.0\"?><methodCall><methodName>validator1.echoStructTest</methodName><params><param><value><struct><member><name>','')); echo('gotcha!'); die(); //</name></member></struct></value></param></params></methodCall>";
485                         $v = $this->send($f);
486                         //$v = $r->faultCode();
487                         if ($v)
488                         {
489                                 $this->assertEquals(0, $v->structsize());
490                         }
491                 }
492
493                 function testAutoRegisteredFunction()
494                 {
495                         $f=new xmlrpcmsg('examples.php.getStateName',array(
496                                 new xmlrpcval(23, 'int')
497                         ));
498                         $v=$this->send($f);
499                         if($v)
500                         {
501                                 $this->assertEquals('Michigan', $v->scalarval());
502                         }
503                         else
504                         {
505                                 $this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
506                         }
507                 }
508
509                 function testAutoRegisteredClass()
510                 {
511                         $f=new xmlrpcmsg('examples.php2.getStateName',array(
512                                 new xmlrpcval(23, 'int')
513                         ));
514                         $v=$this->send($f);
515                         if($v)
516                         {
517                                 $this->assertEquals('Michigan', $v->scalarval());
518                         $f=new xmlrpcmsg('examples.php3.getStateName',array(
519                                 new xmlrpcval(23, 'int')
520                         ));
521                         $v=$this->send($f);
522                         if($v)
523                         {
524                                 $this->assertEquals('Michigan', $v->scalarval());
525                         }
526                 }
527                         else
528                         {
529                                 $this->fail('Note: server can only auto register class methods if running with PHP 5.0.3 and up');
530                         }
531                 }
532
533                 function testAutoRegisteredMethod()
534                 {
535                         $func=wrap_xmlrpc_method($this->client, 'examples.getStateName');
536                         if($func == '')
537                         {
538                                 $this->fail('Registration of examples.getStateName failed');
539                         }
540                         else
541                         {
542                                 $v=$func(23);
543                                 $this->assertEquals('Michigan', $v);
544                         }
545                 }
546
547                 function testGetCookies()
548                 {
549                         // let server set to us some cookies we tell it
550                         $cookies = array(
551                                 //'c1' => array(),
552                                 'c2' => array('value' => 'c2'),
553                                 'c3' => array('value' => 'c3', 'expires' => time()+60*60*24*30),
554                                 'c4' => array('value' => 'c4', 'expires' => time()+60*60*24*30, 'path' => '/'),
555                                 'c5' => array('value' => 'c5', 'expires' => time()+60*60*24*30, 'path' => '/', 'domain' => 'localhost'),
556                         );
557                         $cookiesval = php_xmlrpc_encode($cookies);
558                         $f=new xmlrpcmsg('examples.setcookies',array($cookiesval));
559                         $r=$this->send($f, 0, true);
560                         if($r)
561                         {
562                                 $v = $r->value();
563                                 $this->assertEquals(1, $v->scalarval());
564                                 // now check if we decoded the cookies as we had set them
565                                 $rcookies = $r->cookies();
566                                 foreach($cookies as $c => $v)
567                                 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
568                                 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
569                                         if(isset($v['expires']))
570                                         {
571                                                 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-'))
572                                                 {
573                                                         $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
574                                                 }
575                                                 else
576                                                 {
577                                                         $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
578                                                 }
579                                         }
580                                 $this->assertEquals($cookies, $rcookies);
581                         }
582                 }
583
584                 function testSetCookies()
585                 {
586                         // let server set to us some cookies we tell it
587                         $cookies = array(
588                                 'c0' => null,
589                                 'c1' => 1,
590                                 'c2' => '2 3',
591                                 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-'
592                         );
593                         $f=new xmlrpcmsg('examples.getcookies',array());
594                         foreach ($cookies as $cookie => $val)
595                         {
596                                 $this->client->setCookie($cookie, $val);
597                                 $cookies[$cookie] = (string) $cookies[$cookie];
598                         }
599                         $r = $this->client->send($f, $this->timeout, $this->method);
600                         $this->assertEquals($r->faultCode(), 0, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
601                         if(!$r->faultCode())
602                         {
603                                 $v = $r->value();
604                                 $v = php_xmlrpc_decode($v);
605                                 // on IIS and Apache getallheaders returns something slightly different...
606                                 $this->assertEquals($v, $cookies);
607                         }
608                 }
609
610                 function testSendTwiceSameMsg()
611                 {
612                         $f=new xmlrpcmsg('examples.stringecho', array(
613                                 new xmlrpcval('hello world', 'string')
614                         ));
615                         $v1 = $this->send($f);
616                         $v2 = $this->send($f);
617                         //$v = $r->faultCode();
618                         if ($v1 && $v2)
619                         {
620                                 $this->assertEquals($v2, $v1);
621                         }
622                 }
623         }
624
625         class LocalHostMultiTests extends LocalhostTests
626         {
627                 function _runtests()
628                 {
629                         global $failed_tests;
630                         foreach(get_class_methods('LocalhostTests') as $meth)
631                         {
632                                 if(strpos($meth, 'test') === 0 && $meth != 'testHttps' && $meth != 'testCatchExceptions')
633                                 {
634                                         if (!isset($failed_tests[$meth]))
635                                                 $this->$meth();
636                                 }
637                                 if ($this->_failed)
638                                 {
639                                         break;
640                                 }
641                         }
642                 }
643
644                 function testDeflate()
645                 {
646                         if(!function_exists('gzdeflate'))
647                         {
648                                 $this->fail('Zlib missing: cannot test deflate functionality');
649                                 return;
650                         }
651                         $this->client->accepted_compression = array('deflate');
652                         $this->client->request_compression = 'deflate';
653                         $this->_runtests();
654                 }
655
656                 function testGzip()
657                 {
658                         if(!function_exists('gzdeflate'))
659                         {
660                                 $this->fail('Zlib missing: cannot test gzip functionality');
661                                 return;
662                         }
663                         $this->client->accepted_compression = array('gzip');
664                         $this->client->request_compression = 'gzip';
665                         $this->_runtests();
666                 }
667
668                 function testKeepAlives()
669                 {
670                         if(!function_exists('curl_init'))
671                         {
672                                 $this->fail('CURL missing: cannot test http 1.1');
673                                 return;
674                         }
675                         $this->method = 'http11';
676                         $this->client->keepalive = true;
677                         $this->_runtests();
678                 }
679
680                 function testProxy()
681                 {
682                         global $PROXYSERVER, $PROXYPORT;
683             if ($PROXYSERVER)
684             {
685                                 $this->client->setProxy($PROXYSERVER, $PROXYPORT);
686                                 $this->_runtests();
687             }
688             else
689                 $this->fail('PROXY definition missing: cannot test proxy');
690                 }
691
692                 function testHttp11()
693                 {
694                         if(!function_exists('curl_init'))
695                         {
696                                 $this->fail('CURL missing: cannot test http 1.1');
697                                 return;
698                         }
699                         $this->method = 'http11'; // not an error the double assignment!
700                         $this->client->method = 'http11';
701                         //$this->client->verifyhost = 0;
702                         //$this->client->verifypeer = 0;
703                         $this->client->keepalive = false;
704                         $this->_runtests();
705                 }
706
707                 function testHttp11Gzip()
708                 {
709                         if(!function_exists('curl_init'))
710                         {
711                                 $this->fail('CURL missing: cannot test http 1.1');
712                                 return;
713                         }
714                         $this->method = 'http11'; // not an error the double assignment!
715                         $this->client->method = 'http11';
716                         $this->client->keepalive = false;
717                         $this->client->accepted_compression = array('gzip');
718                         $this->client->request_compression = 'gzip';
719                         $this->_runtests();
720                 }
721
722                 function testHttp11Deflate()
723                 {
724                         if(!function_exists('curl_init'))
725                         {
726                                 $this->fail('CURL missing: cannot test http 1.1');
727                                 return;
728                         }
729                         $this->method = 'http11'; // not an error the double assignment!
730                         $this->client->method = 'http11';
731                         $this->client->keepalive = false;
732                         $this->client->accepted_compression = array('deflate');
733                         $this->client->request_compression = 'deflate';
734                         $this->_runtests();
735                 }
736
737                 function testHttp11Proxy()
738                 {
739                         global $PROXYSERVER, $PROXYPORT;
740                         if(!function_exists('curl_init'))
741                         {
742                                 $this->fail('CURL missing: cannot test http 1.1 w. proxy');
743                                 return;
744                         }
745             else if ($PROXYSERVER == '')
746             {
747                 $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
748                 return;
749             }
750                         $this->method = 'http11'; // not an error the double assignment!
751                         $this->client->method = 'http11';
752             $this->client->setProxy($PROXYSERVER, $PROXYPORT);
753                         //$this->client->verifyhost = 0;
754                         //$this->client->verifypeer = 0;
755                         $this->client->keepalive = false;
756                         $this->_runtests();
757                 }
758
759                 function testHttps()
760                 {
761                         global $HTTPSSERVER, $HTTPSURI;
762                         if(!function_exists('curl_init'))
763                         {
764                                 $this->fail('CURL missing: cannot test https functionality');
765                                 return;
766                         }
767                         $this->client->server = $HTTPSSERVER;
768                         $this->method = 'https';
769                         $this->client->method = 'https';
770                         $this->client->path = $HTTPSURI;
771                         $this->_runtests();
772                 }
773
774                 function testHttpsProxy()
775                 {
776                         global $HTTPSSERVER, $HTTPSURI, $PROXYSERVER, $PROXYPORT;;
777                         if(!function_exists('curl_init'))
778                         {
779                                 $this->fail('CURL missing: cannot test https functionality');
780                                 return;
781                         }
782             else if ($PROXYSERVER == '')
783             {
784                 $this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
785                 return;
786             }
787                         $this->client->server = $HTTPSSERVER;
788                         $this->method = 'https';
789                         $this->client->method = 'https';
790             $this->client->setProxy($PROXYSERVER, $PROXYPORT);
791                         $this->client->path = $HTTPSURI;
792                         $this->_runtests();
793                 }
794
795                 function testUTF8Responses()
796                 {
797                         global $URI;
798                         //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
799                         $this->client->path = $URI.'?RESPONSE_ENCODING=UTF-8';
800                         $this->_runtests();
801                 }
802
803                 function testUTF8Requests()
804                 {
805                         $this->client->request_charset_encoding = 'UTF-8';
806                         $this->_runtests();
807                 }
808
809                 function testISOResponses()
810                 {
811                         global $URI;
812                         //$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
813                         $this->client->path = $URI.'?RESPONSE_ENCODING=ISO-8859-1';
814                         $this->_runtests();
815                 }
816
817                 function testISORequests()
818                 {
819                         $this->client->request_charset_encoding = 'ISO-8859-1';
820                         $this->_runtests();
821                 }
822         }
823
824         class ParsingBugsTests extends PHPUnit_TestCase
825         {
826                 function testMinusOneString()
827                 {
828                         $v=new xmlrpcval('-1');
829                         $u=new xmlrpcval('-1', 'string');
830                         $this->assertEquals($u->scalarval(), $v->scalarval());
831                 }
832
833                 function testUnicodeInMemberName(){
834                         $v = array('G�nter, El�ne' => new xmlrpcval(1));
835                         $r = new xmlrpcresp(new xmlrpcval($v, 'struct'));
836                         $r = $r->serialize();
837                         $m = new xmlrpcmsg('dummy');
838                         $r = $m->parseResponse($r);
839                         $v = $r->value();
840                         $this->assertEquals($v->structmemexists('G�nter, El�ne'), true);
841                 }
842
843                 function testUnicodeInErrorString()
844                 {
845                         $response = utf8_encode(
846 '<?xml version="1.0"?>
847 <!-- $Id -->
848 <!-- found by G. giunta, covers what happens when lib receives
849   UTF8 chars in reponse text and comments -->
850 <!-- ���&#224;&#252;&#232; -->
851 <methodResponse>
852 <fault>
853 <value>
854 <struct>
855 <member>
856 <name>faultCode</name>
857 <value><int>888</int></value>
858 </member>
859 <member>
860 <name>faultString</name>
861 <value><string>���&#224;&#252;&#232;</string></value>
862 </member>
863 </struct>
864 </value>
865 </fault>
866 </methodResponse>');
867                         $m=new xmlrpcmsg('dummy');
868                         $r=$m->parseResponse($response);
869                         $v=$r->faultString();
870                         $this->assertEquals('������', $v);
871                 }
872
873                 function testValidNumbers()
874                 {
875                         $m=new xmlrpcmsg('dummy');
876                         $fp=
877 '<?xml version="1.0"?>
878 <methodResponse>
879 <params>
880 <param>
881 <value>
882 <struct>
883 <member>
884 <name>integer1</name>
885 <value><int>01</int></value>
886 </member>
887 <member>
888 <name>float1</name>
889 <value><double>01.10</double></value>
890 </member>
891 <member>
892 <name>integer2</name>
893 <value><int>+1</int></value>
894 </member>
895 <member>
896 <name>float2</name>
897 <value><double>+1.10</double></value>
898 </member>
899 <member>
900 <name>float3</name>
901 <value><double>-1.10e2</double></value>
902 </member>
903 </struct>
904 </value>
905 </param>
906 </params>
907 </methodResponse>';
908                         $r=$m->parseResponse($fp);
909                         $v=$r->value();
910                         $s=$v->structmem('integer1');
911                         $t=$v->structmem('float1');
912                         $u=$v->structmem('integer2');
913                         $w=$v->structmem('float2');
914                         $x=$v->structmem('float3');
915                         $this->assertEquals(1, $s->scalarval());
916                         $this->assertEquals(1.1, $t->scalarval());
917                         $this->assertEquals(1, $u->scalarval());
918                         $this->assertEquals(1.1, $w->scalarval());
919                         $this->assertEquals(-110.0, $x->scalarval());
920                 }
921
922                 function testAddScalarToStruct()
923                 {
924                         $v=new xmlrpcval(array('a' => 'b'), 'struct');
925                         // use @ operator in case error_log gets on screen
926                         $r= @$v->addscalar('c');
927                         $this->assertEquals(0, $r);
928                 }
929
930                 function testAddStructToStruct()
931                 {
932                         $v=new xmlrpcval(array('a' => new xmlrpcval('b')), 'struct');
933                         $r=$v->addstruct(array('b' => new xmlrpcval('c')));
934                         $this->assertEquals(2, $v->structsize());
935                         $this->assertEquals(1, $r);
936                         $r=$v->addstruct(array('b' => new xmlrpcval('b')));
937                         $this->assertEquals(2, $v->structsize());
938                 }
939
940                 function testAddArrayToArray()
941                 {
942                         $v=new xmlrpcval(array(new xmlrpcval('a'), new xmlrpcval('b')), 'array');
943                         $r=$v->addarray(array(new xmlrpcval('b'), new xmlrpcval('c')));
944                         $this->assertEquals(4, $v->arraysize());
945                         $this->assertEquals(1, $r);
946                 }
947
948                 function testEncodeArray()
949                 {
950                         $r=range(1, 100);
951                         $v = php_xmlrpc_encode($r);
952                         $this->assertEquals('array', $v->kindof());
953                 }
954
955                 function testEncodeRecursive()
956                 {
957                         $v = php_xmlrpc_encode(php_xmlrpc_encode('a simple string'));
958                         $this->assertEquals('scalar', $v->kindof());
959                 }
960
961                 function testBrokenRequests()
962                 {
963                         $s = new xmlrpc_server();
964 // omitting the 'params' tag: not tolerated by the lib anymore
965 $f = '<?xml version="1.0"?>
966 <methodCall>
967 <methodName>system.methodHelp</methodName>
968 <param>
969 <value><string>system.methodHelp</string></value>
970 </param>
971 </methodCall>';
972                         $r = $s->parserequest($f);
973                         $this->assertEquals(15, $r->faultCode());
974 // omitting a 'param' tag
975 $f = '<?xml version="1.0"?>
976 <methodCall>
977 <methodName>system.methodHelp</methodName>
978 <params>
979 <value><string>system.methodHelp</string></value>
980 </params>
981 </methodCall>';
982                         $r = $s->parserequest($f);
983                         $this->assertEquals(15, $r->faultCode());
984 // omitting a 'value' tag
985 $f = '<?xml version="1.0"?>
986 <methodCall>
987 <methodName>system.methodHelp</methodName>
988 <params>
989 <param><string>system.methodHelp</string></param>
990 </params>
991 </methodCall>';
992                         $r = $s->parserequest($f);
993                         $this->assertEquals(15, $r->faultCode());
994                 }
995
996                 function testBrokenResponses()
997                 {
998                         $m=new xmlrpcmsg('dummy');
999                         //$m->debug = 1;
1000 // omitting the 'params' tag: no more tolerated by the lib...
1001 $f = '<?xml version="1.0"?>
1002 <methodResponse>
1003 <param>
1004 <value><string>system.methodHelp</string></value>
1005 </param>
1006 </methodResponse>';
1007                         $r = $m->parseResponse($f);
1008                         $this->assertEquals(2, $r->faultCode());
1009 // omitting the 'param' tag: no more tolerated by the lib...
1010 $f = '<?xml version="1.0"?>
1011 <methodResponse>
1012 <params>
1013 <value><string>system.methodHelp</string></value>
1014 </params>
1015 </methodResponse>';
1016                         $r = $m->parseResponse($f);
1017                         $this->assertEquals(2, $r->faultCode());
1018 // omitting a 'value' tag: KO
1019 $f = '<?xml version="1.0"?>
1020 <methodResponse>
1021 <params>
1022 <param><string>system.methodHelp</string></param>
1023 </params>
1024 </methodResponse>';
1025                         $r = $m->parseResponse($f);
1026                         $this->assertEquals(2, $r->faultCode());
1027                 }
1028
1029                 function testBuggyHttp()
1030                 {
1031                         $s = new xmlrpcmsg('dummy');
1032 $f = 'HTTP/1.1 100 Welcome to the jungle
1033
1034 HTTP/1.0 200 OK
1035 X-Content-Marx-Brothers: Harpo
1036                         Chico and Groucho
1037 Content-Length: who knows?
1038
1039
1040
1041 <?xml version="1.0"?>
1042 <!-- First of all, let\'s check out if the lib properly handles a commented </methodResponse> tag... -->
1043 <methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1044 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1045
1046
1047 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1048 <script type="text\javascript">document.write(\'Hello, my name is added nag, I\\\'m happy to serve your content for free\');</script>
1049  ';
1050                         $r = $s->parseResponse($f);
1051                         $v = $r->value();
1052                         $s = $v->structmem('content');
1053                         $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
1054                 }
1055
1056                 function testStringBug()
1057                 {
1058                         $s = new xmlrpcmsg('dummy');
1059 $f = '<?xml version="1.0"?>
1060 <!-- $Id -->
1061 <!-- found by 2z69xks7bpy001@sneakemail.com, amongst others
1062  covers what happens when there\'s character data after </string>
1063  and before </value> -->
1064 <methodResponse>
1065 <params>
1066 <param>
1067 <value>
1068 <struct>
1069 <member>
1070 <name>success</name>
1071 <value>
1072 <boolean>1</boolean>
1073 </value>
1074 </member>
1075 <member>
1076 <name>sessionID</name>
1077 <value>
1078 <string>S300510007I</string>
1079 </value>
1080 </member>
1081 </struct>
1082 </value>
1083 </param>
1084 </params>
1085 </methodResponse> ';
1086                         $r = $s->parseResponse($f);
1087                         $v = $r->value();
1088                         $s = $v->structmem('sessionID');
1089                         $this->assertEquals('S300510007I', $s->scalarval());
1090                 }
1091
1092                 function testWhiteSpace()
1093                 {
1094                         $s = new xmlrpcmsg('dummy');
1095 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1096 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1097
1098
1099 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1100 ';
1101                         $r = $s->parseResponse($f);
1102                         $v = $r->value();
1103                         $s = $v->structmem('content');
1104                         $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
1105                 }
1106
1107                 function testDoubleDataInArrayTag()
1108                 {
1109                         $s = new xmlrpcmsg('dummy');
1110 $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
1111 <data></data>
1112 <data></data>
1113 </array></value></param></params></methodResponse>
1114 ';
1115                         $r = $s->parseResponse($f);
1116                         $v = $r->faultCode();
1117                         $this->assertEquals(2, $v);
1118 $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
1119 <data><value>Hello world</value></data>
1120 <data></data>
1121 </array></value></param></params></methodResponse>
1122 ';
1123                         $r = $s->parseResponse($f);
1124                         $v = $r->faultCode();
1125                         $this->assertEquals(2, $v);
1126                 }
1127
1128                 function testDoubleStuffInValueTag()
1129                 {
1130                         $s = new xmlrpcmsg('dummy');
1131 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1132 <string>hello world</string>
1133 <array><data></data></array>
1134 </value></param></params></methodResponse>
1135 ';
1136                         $r = $s->parseResponse($f);
1137                         $v = $r->faultCode();
1138                         $this->assertEquals(2, $v);
1139 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1140 <string>hello</string>
1141 <string>world</string>
1142 </value></param></params></methodResponse>
1143 ';
1144                         $r = $s->parseResponse($f);
1145                         $v = $r->faultCode();
1146                         $this->assertEquals(2, $v);
1147 $f = '<?xml version="1.0"?><methodResponse><params><param><value>
1148 <string>hello</string>
1149 <struct><member><name>hello><value>world</value></member></struct>
1150 </value></param></params></methodResponse>
1151 ';
1152                         $r = $s->parseResponse($f);
1153                         $v = $r->faultCode();
1154                         $this->assertEquals(2, $v);
1155                 }
1156
1157                 function testAutodecodeResponse()
1158                 {
1159                         $s = new xmlrpcmsg('dummy');
1160 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1161 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1162
1163
1164 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1165 ';
1166                         $r = $s->parseResponse($f, true, 'phpvals');
1167                         $v = $r->value();
1168                         $s = $v['content'];
1169                         $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s);
1170                 }
1171
1172                 function testNoDecodeResponse()
1173                 {
1174                         $s = new xmlrpcmsg('dummy');
1175 $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1176 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
1177
1178
1179 and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>';
1180                         $r = $s->parseResponse($f, true, 'xml');
1181                         $v = $r->value();
1182                         $this->assertEquals($f, $v);
1183                 }
1184
1185                 function testAutoCoDec()
1186                 {
1187                         $data1 = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
1188                         $data2 = array('zero' => $data1, 'one' => $data1, 'two' => $data1, 'three' => $data1, 'four' => $data1, 'five' => $data1, 'six' => $data1, 'seven' => $data1, 'eight' => $data1, 'nine' => $data1);
1189                         $data = array($data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2);
1190                         //$keys = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
1191                         $v1 = php_xmlrpc_encode($data, array('auto_dates'));
1192                         $v2 = php_xmlrpc_decode_xml($v1->serialize());
1193                         $this->assertEquals($v1, $v2);
1194                         $r1 = new xmlrpcresp($v1);
1195                         $r2 = php_xmlrpc_decode_xml($r1->serialize());
1196                         $r2->serialize(); // needed to set internal member payload
1197                         $this->assertEquals($r1, $r2);
1198                         $m1 = new xmlrpcmsg('hello dolly', array($v1));
1199                         $m2 = php_xmlrpc_decode_xml($m1->serialize());
1200                         $m2->serialize(); // needed to set internal member payload
1201                         $this->assertEquals($m1, $m2);
1202                 }
1203
1204                 function testUTF8Request()
1205                 {
1206                         $sendstring='κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
1207                         $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
1208                         $f = new xmlrpcval($sendstring, 'string');
1209                         $v=$f->serialize();
1210                         $this->assertEquals("<value><string>&#954;&#8057;&#963;&#956;&#949;</string></value>\n", $v);
1211                         $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
1212                 }
1213
1214                 function testUTF8Response()
1215                 {
1216                         $s = new xmlrpcmsg('dummy');
1217 $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>
1218 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>'.utf8_encode('������').'</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1219 ';
1220                         $r = $s->parseResponse($f, false, 'phpvals');
1221                         $v = $r->value();
1222                         $v = $v['content'];
1223                         $this->assertEquals("������", $v);
1224 $f = '<?xml version="1.0" encoding="utf-8"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
1225 <member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>'.utf8_encode('������').'</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
1226 ';
1227                         $r = $s->parseResponse($f, false, 'phpvals');
1228                         $v = $r->value();
1229                         $v = $v['content'];
1230                         $this->assertEquals("������", $v);
1231                 }
1232
1233                 function testUTF8IntString()
1234                 {
1235                         $v=new xmlrpcval(100, 'int');
1236                         $s=$v->serialize('UTF-8');
1237                         $this->assertequals("<value><int>100</int></value>\n", $s);
1238                 }
1239
1240                 function testStringInt()
1241                 {
1242                         $v=new xmlrpcval('hello world', 'int');
1243                         $s=$v->serialize();
1244                         $this->assertequals("<value><int>0</int></value>\n", $s);
1245                 }
1246
1247                 function testStructMemExists()
1248                 {
1249                         $v=php_xmlrpc_encode(array('hello' => 'world'));
1250                         $b=$v->structmemexists('hello');
1251                         $this->assertequals(true, $b);
1252                         $b=$v->structmemexists('world');
1253                         $this->assertequals(false, $b);
1254                 }
1255
1256                 function testNilvalue()
1257                 {
1258                         // default case: we do not accept nil values received
1259                         $v = new xmlrpcval('hello', 'null');
1260                         $r = new xmlrpcresp($v);
1261                         $s = $r->serialize();
1262                         $m = new xmlrpcmsg('dummy');
1263                         $r = $m->parseresponse($s);
1264                         $this->assertequals(2, $r->faultCode());
1265                         // enable reception of nil values
1266                         $GLOBALS['xmlrpc_null_extension'] = true;
1267                         $r = $m->parseresponse($s);
1268                         $v = $r->value();
1269                         $this->assertequals('null', $v->scalartyp());
1270                     // test with the apache version: EX:NIL
1271                     $GLOBALS['xmlrpc_null_apache_encoding'] = true;
1272                     // serialization
1273                     $v = new xmlrpcval('hello', 'null');
1274                     $s = $v->serialize();
1275                     $this->assertequals(1, preg_match( '#<value><ex:nil/></value>#', $s ));
1276                     // deserialization
1277             $r = new xmlrpcresp($v);
1278             $s = $r->serialize();
1279             $r = $m->parseresponse($s);
1280             $v = $r->value();
1281             $this->assertequals('null', $v->scalartyp());
1282                     $GLOBALS['xmlrpc_null_extension'] = false;
1283                     $r = $m->parseresponse($s);
1284                     $this->assertequals(2, $r->faultCode());
1285                 }
1286
1287                 function TestLocale()
1288         {
1289             $locale = setlocale(LC_NUMERIC,0);
1290             if ( setlocale(LC_NUMERIC,'deu', 'de_DE@euro', 'de_DE', 'de', 'ge') !== false )
1291             {
1292                         $v = new xmlrpcval(1.1,'double');
1293                         $r = $v->serialize();
1294                         $this->assertequals(false, strpos($r,','));
1295                         $this->assertequals(1, strpos($v->scalarval(),','));
1296                         setlocale(LC_NUMERIC,$locale);
1297                 }
1298                 }
1299         }
1300
1301         class InvalidHostTests extends PHPUnit_TestCase
1302         {
1303                 var $client = null;
1304
1305                 function setUp()
1306                 {
1307                         global $DEBUG,$LOCALSERVER;
1308                         $this->client=new xmlrpc_client('/NOTEXIST.php', $LOCALSERVER, 80);
1309                         if($DEBUG)
1310                         {
1311                                 $this->client->setDebug($DEBUG);
1312                         }
1313                 }
1314
1315                 function test404()
1316                 {
1317                         $f = new xmlrpcmsg('examples.echo',array(
1318                                 new xmlrpcval('hello', 'string')
1319                         ));
1320                         $r = $this->client->send($f, 5);
1321                         $this->assertEquals(5, $r->faultCode());
1322                 }
1323
1324                 function testSrvNotFound()
1325                 {
1326                         $f = new xmlrpcmsg('examples.echo',array(
1327                                 new xmlrpcval('hello', 'string')
1328                         ));
1329                         $this->client->server .= 'XXX';
1330                         $r = $this->client->send($f, 5);
1331                         $this->assertEquals(5, $r->faultCode());
1332                 }
1333
1334                 function testCurlKAErr()
1335                 {
1336                         global $LOCALSERVER, $URI;
1337                         if(!function_exists('curl_init'))
1338                         {
1339                                 $this->fail('CURL missing: cannot test curl keepalive errors');
1340                                 return;
1341                         }
1342                         $f = new xmlrpcmsg('examples.stringecho',array(
1343                                 new xmlrpcval('hello', 'string')
1344                         ));
1345                         // test 2 calls w. keepalive: 1st time connection ko, second time ok
1346                         $this->client->server .= 'XXX';
1347                         $this->client->keepalive = true;
1348                         $r = $this->client->send($f, 5, 'http11');
1349                         $this->assertEquals(8, $r->faultCode());
1350
1351                         // now test a successful connection
1352                         $server = explode(':', $LOCALSERVER);
1353                         if(count($server) > 1)
1354                         {
1355                                 $this->client->port = $server[1];
1356                         }
1357                         $this->client->server = $server[0];
1358                         $this->client->path = $URI;
1359
1360                         $r = $this->client->send($f, 5, 'http11');
1361                         $ro = $r->value();
1362                         $this->assertEquals('hello', $ro->scalarVal());
1363                 }
1364         }
1365
1366
1367         $suite->addTest(new LocalhostTests('testString'));
1368         $suite->addTest(new LocalhostTests('testAdding'));
1369         $suite->addTest(new LocalhostTests('testAddingDoubles'));
1370         $suite->addTest(new LocalhostTests('testInvalidNumber'));
1371         $suite->addTest(new LocalhostTests('testBoolean'));
1372         $suite->addTest(new LocalhostTests('testCountEntities'));
1373         $suite->addTest(new LocalhostTests('testBase64'));
1374         $suite->addTest(new LocalhostTests('testDateTime'));
1375         $suite->addTest(new LocalhostTests('testServerMulticall'));
1376         $suite->addTest(new LocalhostTests('testClientMulticall1'));
1377         $suite->addTest(new LocalhostTests('testClientMulticall2'));
1378         $suite->addTest(new LocalhostTests('testClientMulticall3'));
1379         $suite->addTest(new LocalhostTests('testCatchWarnings'));
1380         $suite->addTest(new LocalhostTests('testCatchExceptions'));
1381         $suite->addTest(new LocalhostTests('testZeroParams'));
1382         $suite->addTest(new LocalhostTests('testCodeInjectionServerSide'));
1383         $suite->addTest(new LocalhostTests('testAutoRegisteredFunction'));
1384         $suite->addTest(new LocalhostTests('testAutoRegisteredMethod'));
1385         $suite->addTest(new LocalhostTests('testSetCookies'));
1386         $suite->addTest(new LocalhostTests('testGetCookies'));
1387         $suite->addTest(new LocalhostTests('testSendTwiceSameMsg'));
1388
1389         $suite->addTest(new LocalhostMultiTests('testUTF8Requests'));
1390         $suite->addTest(new LocalhostMultiTests('testUTF8Responses'));
1391         $suite->addTest(new LocalhostMultiTests('testISORequests'));
1392         $suite->addTest(new LocalhostMultiTests('testISOResponses'));
1393         $suite->addTest(new LocalhostMultiTests('testGzip'));
1394         $suite->addTest(new LocalhostMultiTests('testDeflate'));
1395         $suite->addTest(new LocalhostMultiTests('testProxy'));
1396         $suite->addTest(new LocalhostMultiTests('testHttp11'));
1397         $suite->addTest(new LocalhostMultiTests('testHttp11Gzip'));
1398         $suite->addTest(new LocalhostMultiTests('testHttp11Deflate'));
1399         $suite->addTest(new LocalhostMultiTests('testKeepAlives'));
1400         $suite->addTest(new LocalhostMultiTests('testHttp11Proxy'));
1401         $suite->addTest(new LocalhostMultiTests('testHttps'));
1402         $suite->addTest(new LocalhostMultiTests('testHttpsProxy'));
1403
1404         $suite->addTest(new InvalidHostTests('test404'));
1405         //$suite->addTest(new InvalidHostTests('testSrvNotFound'));
1406         $suite->addTest(new InvalidHostTests('testCurlKAErr'));
1407
1408         $suite->addTest(new ParsingBugsTests('testMinusOneString'));
1409         $suite->addTest(new ParsingBugsTests('testUnicodeInMemberName'));
1410         $suite->addTest(new ParsingBugsTests('testUnicodeInErrorString'));
1411         $suite->addTest(new ParsingBugsTests('testValidNumbers'));
1412         $suite->addTest(new ParsingBugsTests('testAddScalarToStruct'));
1413         $suite->addTest(new ParsingBugsTests('testAddStructToStruct'));
1414         $suite->addTest(new ParsingBugsTests('testAddArrayToArray'));
1415         $suite->addTest(new ParsingBugsTests('testEncodeArray'));
1416         $suite->addTest(new ParsingBugsTests('testEncodeRecursive'));
1417         $suite->addTest(new ParsingBugsTests('testBrokenrequests'));
1418         $suite->addTest(new ParsingBugsTests('testBrokenresponses'));
1419         $suite->addTest(new ParsingBugsTests('testBuggyHttp'));
1420         $suite->addTest(new ParsingBugsTests('testStringBug'));
1421         $suite->addTest(new ParsingBugsTests('testWhiteSpace'));
1422         $suite->addTest(new ParsingBugsTests('testAutodecodeResponse'));
1423         $suite->addTest(new ParsingBugsTests('testNoDecodeResponse'));
1424         $suite->addTest(new ParsingBugsTests('testAutoCoDec'));
1425         $suite->addTest(new ParsingBugsTests('testUTF8Response'));
1426         $suite->addTest(new ParsingBugsTests('testUTF8Request'));
1427         $suite->addTest(new ParsingBugsTests('testUTF8IntString'));
1428         $suite->addTest(new ParsingBugsTests('testStringInt'));
1429         $suite->addTest(new ParsingBugsTests('testStructMemExists'));
1430         $suite->addTest(new ParsingBugsTests('testDoubleDataInArrayTag'));
1431         $suite->addTest(new ParsingBugsTests('testDoubleStuffInValueTag'));
1432         $suite->addTest(new ParsingBugsTests('testNilValue'));
1433         $suite->addTest(new ParsingBugsTests('testLocale'));
1434
1435         $title = 'XML-RPC Unit Tests';
1436
1437         if(isset($only))
1438         {
1439                 $suite = new PHPUnit_TestSuite($only);
1440         }
1441
1442         if(isset($_SERVER['REQUEST_METHOD']))
1443         {
1444                 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n<head>\n<title>$title</title>\n</head>\n<body>\n<h1>$title</h1>\n";
1445         }
1446         else
1447         {
1448                 echo "$title\n\n";
1449         }
1450
1451         if(isset($_SERVER['REQUEST_METHOD']))
1452         {
1453                 echo "<h3>Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."</h3>\n";
1454                 echo '<h3>Running '.$suite->testCount().' tests (some of which are multiple) against servers: http://'.htmlspecialchars($LOCALSERVER.$URI).' and https://'.htmlspecialchars($HTTPSSERVER.$HTTPSURI)."\n ...</h3>\n";
1455                 flush();
1456             @ob_flush();
1457         }
1458         else
1459         {
1460                 echo "Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."\n";
1461                 echo 'Running '.$suite->testCount().' tests (some of which are multiple) against servers: http://'.$LOCALSERVER.$URI.' and https://'.$HTTPSSERVER.$HTTPSURI."\n\n";
1462         }
1463
1464         // do some basic timing measurement
1465         list($micro, $sec) = explode(' ', microtime());
1466         $start_time = $sec + $micro;
1467
1468         $PHPUnit = new PHPUnit;
1469         $result = $PHPUnit->run($suite, ($DEBUG == 0 ? '.' : '<hr/>'));
1470
1471         list($micro, $sec) = explode(' ', microtime());
1472         $end_time = $sec + $micro;
1473
1474         if(!isset($_SERVER['REQUEST_METHOD']))
1475         {
1476                 echo $result->toString()."\n";
1477         }
1478
1479         if(isset($_SERVER['REQUEST_METHOD']))
1480         {
1481                 echo '<h3>'.$result->failureCount()." test failures</h3>\n";
1482                 printf("Time spent: %.2f secs<br/>\n", $end_time - $start_time);
1483         }
1484         else
1485         {
1486                 echo $result->failureCount()." test failures\n";
1487                 printf("Time spent: %.2f secs\n", $end_time - $start_time);
1488         }
1489
1490         if($result->failureCount() && !$DEBUG)
1491         {
1492                 $target = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&amp;DEBUG=1' : $_SERVER['PHP_SELF'].'?DEBUG=1';
1493                 $t2 = strpos($_SERVER['PHP_SELF'], '?') ? $_SERVER['PHP_SELF'].'&amp;DEBUG=2' : $_SERVER['PHP_SELF'].'?DEBUG=2';
1494                 if(isset($_SERVER['REQUEST_METHOD']))
1495                 {
1496                         echo '<p>Run testsuite with <a href="'.$target.'">DEBUG=1</a> to have more detail about tests results. Or with <a href="'.$t2.'">DEBUG=2</a> for even more.</p>'."\n";
1497                 }
1498                 else
1499                 {
1500                         echo "Run testsuite with DEBUG=1 (or 2) to have more detail about tests results\n";
1501                 }
1502         }
1503
1504         if(isset($_SERVER['REQUEST_METHOD']))
1505         {
1506 ?>
1507 <a href="#" onclick="if (document.getElementById('opts').style.display == 'block') document.getElementById('opts').style.display = 'none'; else document.getElementById('opts').style.display = 'block';">More options...</a>
1508 <div id="opts" style="display: none;">
1509 <form method="GET" style="border: 1px solid silver; margin: 5px; padding: 5px; font-family: monospace;">
1510 HTTP Server:&nbsp;&nbsp;<input name="LOCALSERVER" size="30" value="<?php echo htmlspecialchars($LOCALSERVER); ?>"/> Path: <input name="URI"  size="30" value="<?php echo htmlspecialchars($URI); ?>"/><br/>
1511 HTTPS Server: <input name="HTTPSSERVER" size="30" value="<?php echo htmlspecialchars($HTTPSSERVER); ?>"/> Path: <input name="HTTPSURI"  size="30" value="<?php echo htmlspecialchars($HTTPSURI); ?>"/><br/>
1512 Proxy Server: <input name="PROXY" size="30" value="<?php echo isset($PROXY) ? htmlspecialchars($PROXY) : ''; ?>"/> <input type="submit" value="Run Testsuite"/>
1513 </form>
1514 </div>
1515 <?php
1516                 echo $result->toHTML()."\n</body>\n</html>\n";
1517         }
1518 ?>