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