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