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