Passed
Push — master ( 6f4e22...ac15c5 )
by Gaetano
09:07
created

MessageTests::testNilvalue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 32
rs 9.52
1
<?php
2
3
include_once __DIR__ . '/../lib/xmlrpc.inc';
4
include_once __DIR__ . '/../lib/xmlrpcs.inc';
5
6
include_once __DIR__ . '/parse_args.php';
7
8
include_once __DIR__ . '/PolyfillTestCase.php';
9
10
use PHPUnit\Runner\BaseTestRunner;
11
12
/**
13
 * Tests involving the Request and Response classes.
14
 * @todo some tests are here only because we use a Response to trigger parsing of xml for a single Value, but they
15
 *       logically belong elsewhere...
16
 */
17
class MessageTests extends PhpXmlRpc_PolyfillTestCase
18
{
19
    public $args = array();
20
21
    protected function set_up()
22
    {
23
        $this->args = argParser::getArgs();
24
        if ($this->args['DEBUG'] == 1)
25
            ob_start();
26
    }
27
28
    protected function tear_down()
29
    {
30
        if ($this->args['DEBUG'] != 1)
31
            return;
32
        $out = ob_get_clean();
33
        $status = $this->getStatus();
34
        if ($status == BaseTestRunner::STATUS_ERROR
35
            || $status == BaseTestRunner::STATUS_FAILURE) {
36
            echo $out;
37
        }
38
    }
39
40
    protected function newMsg($methodName, $params = array())
41
    {
42
        $msg = new xmlrpcmsg($methodName, $params);
43
        $msg->setDebug($this->args['DEBUG']);
44
        return $msg;
45
    }
46
47
48
    public function testValidNumbers()
49
    {
50
        $m = $this->newMsg('dummy');
51
        $fp =
52
            '<?xml version="1.0"?>
53
<methodResponse>
54
<params>
55
<param>
56
<value>
57
<struct>
58
<member>
59
<name>integer1</name>
60
<value><int>01</int></value>
61
</member>
62
<member>
63
<name>integer2</name>
64
<value><int>+1</int></value>
65
</member>
66
<member>
67
<name>integer3</name>
68
<value><i4>1</i4></value>
69
</member>
70
<member>
71
<name>float1</name>
72
<value><double>01.10</double></value>
73
</member>
74
<member>
75
<name>float2</name>
76
<value><double>+1.10</double></value>
77
</member>
78
<member>
79
<name>float3</name>
80
<value><double>-1.10e2</double></value>
81
</member>
82
</struct>
83
</value>
84
</param>
85
</params>
86
</methodResponse>';
87
        $r = $m->parseResponse($fp);
88
        $v = $r->value();
89
        $s = $v->structmem('integer1');
0 ignored issues
show
Bug introduced by
The method structmem() does not exist on integer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        /** @scrutinizer ignore-call */ 
90
        $s = $v->structmem('integer1');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        $t = $v->structmem('integer2');
91
        $u = $v->structmem('integer3');
92
        $x = $v->structmem('float1');
93
        $y = $v->structmem('float2');
94
        $z = $v->structmem('float3');
95
        $this->assertEquals(1, $s->scalarval());
96
        $this->assertEquals(1, $t->scalarval());
97
        $this->assertEquals(1, $u->scalarval());
98
99
        $this->assertEquals(1.1, $x->scalarval());
100
        $this->assertEquals(1.1, $y->scalarval());
101
        $this->assertEquals(-110.0, $z->scalarval());
102
    }
103
104
    public function testI8()
105
    {
106
        if (PHP_INT_SIZE == 4 ) {
107
            $this->markTestSkipped('Can not test i8 as php is compiled in 32 bit mode');
108
            return;
109
        }
110
111
        $m = $this->newMsg('dummy');
112
        $fp =
113
            '<?xml version="1.0"?>
114
<methodResponse>
115
<params>
116
<param>
117
<value>
118
<struct>
119
<member>
120
<name>integer1</name>
121
<value><i8>1</i8></value>
122
</member>
123
</struct>
124
</value>
125
</param>
126
</params>
127
</methodResponse>';
128
        $r = $m->parseResponse($fp);
129
        $v = $r->value();
130
        $s = $v->structmem('integer1');
131
        $this->assertEquals(1, $s->scalarval());
132
    }
133
134
    public function testUnicodeInMemberName()
135
    {
136
        $str = "G" . chr(252) . "nter, El" . chr(232) . "ne";
137
        $v = array($str => new xmlrpcval(1));
138
        $r = new xmlrpcresp(new xmlrpcval($v, 'struct'));
139
        $r = $r->serialize();
140
        $m = $this->newMsg('dummy');
141
        $r = $m->parseResponse($r);
142
        $v = $r->value();
143
        $this->assertEquals(true, $v->structmemexists($str));
144
    }
145
146
    public function testUnicodeInErrorString()
147
    {
148
        // the warning suppression is due to utf8_decode being deprecated in php 8.2
149
        $response = @utf8_encode(
150
            '<?xml version="1.0"?>
151
<!-- $Id -->
152
<!-- found by G. Giunta, covers what happens when lib receives UTF8 chars in response text and comments -->
153
<!-- ' . chr(224) . chr(252) . chr(232) . '&#224;&#252;&#232; -->
154
<methodResponse>
155
<fault>
156
<value>
157
<struct>
158
<member>
159
<name>faultCode</name>
160
<value><int>888</int></value>
161
</member>
162
<member>
163
<name>faultString</name>
164
<value><string>' . chr(224) . chr(252) . chr(232) . '&#224;&#252;&#232;</string></value>
165
</member>
166
</struct>
167
</value>
168
</fault>
169
</methodResponse>');
170
        $m = $this->newMsg('dummy');
171
        $r = $m->parseResponse($response);
172
        $v = $r->faultString();
173
        $this->assertEquals(chr(224) . chr(252) . chr(232) . chr(224) . chr(252) . chr(232), $v);
174
    }
175
176
    public function testBrokenRequests()
177
    {
178
        $s = new xmlrpc_server();
179
        // omitting the 'params' tag: not tolerated by the lib anymore
180
        $f = '<?xml version="1.0"?>
181
<methodCall>
182
<methodName>system.methodHelp</methodName>
183
<param>
184
<value><string>system.methodHelp</string></value>
185
</param>
186
</methodCall>';
187
        $r = $s->parserequest($f);
188
        $this->assertEquals(15, $r->faultCode());
189
        // omitting a 'param' tag
190
        $f = '<?xml version="1.0"?>
191
<methodCall>
192
<methodName>system.methodHelp</methodName>
193
<params>
194
<value><string>system.methodHelp</string></value>
195
</params>
196
</methodCall>';
197
        $r = $s->parserequest($f);
198
        $this->assertEquals(15, $r->faultCode());
199
        // omitting a 'value' tag
200
        $f = '<?xml version="1.0"?>
201
<methodCall>
202
<methodName>system.methodHelp</methodName>
203
<params>
204
<param><string>system.methodHelp</string></param>
205
</params>
206
</methodCall>';
207
        $r = $s->parserequest($f);
208
        $this->assertEquals(15, $r->faultCode());
209
    }
210
211
    public function testBrokenResponses()
212
    {
213
        $m = $this->newMsg('dummy');
214
        // omitting the 'params' tag: no more tolerated by the lib...
215
        $f = '<?xml version="1.0"?>
216
<methodResponse>
217
<param>
218
<value><string>system.methodHelp</string></value>
219
</param>
220
</methodResponse>';
221
        $r = $m->parseResponse($f);
222
        $this->assertEquals(2, $r->faultCode());
223
        // omitting the 'param' tag: no more tolerated by the lib...
224
        $f = '<?xml version="1.0"?>
225
<methodResponse>
226
<params>
227
<value><string>system.methodHelp</string></value>
228
</params>
229
</methodResponse>';
230
        $r = $m->parseResponse($f);
231
        $this->assertEquals(2, $r->faultCode());
232
        // omitting a 'value' tag: KO
233
        $f = '<?xml version="1.0"?>
234
<methodResponse>
235
<params>
236
<param><string>system.methodHelp</string></param>
237
</params>
238
</methodResponse>';
239
        $r = $m->parseResponse($f);
240
        $this->assertEquals(2, $r->faultCode());
241
    }
242
243
    public function testBuggyHttp()
244
    {
245
        $s = $this->newMsg('dummy');
246
        $f = 'HTTP/1.1 100 Welcome to the jungle
247
248
HTTP/1.0 200 OK
249
X-Content-Marx-Brothers: Harpo
250
        Chico and Groucho
251
Content-Length: who knows?
252
253
254
255
<?xml version="1.0"?>
256
<!-- First of all, let\'s check out if the lib properly handles a commented </methodResponse> tag... -->
257
<methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
258
<member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
259
260
261
and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
262
<script type="text\javascript">document.write(\'Hello, my name is added nag, I\\\'m happy to serve your content for free\');</script>
263
 ';
264
        $r = $s->parseResponse($f);
265
        $v = $r->value();
266
        $s = $v->structmem('content');
267
        $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
268
    }
269
270
    public function testStringBug()
271
    {
272
        $s = $this->newMsg('dummy');
273
        $f = '<?xml version="1.0"?>
274
<!-- $Id -->
275
<!-- found by [email protected], amongst others
276
 covers what happens when there\'s character data after </string>
277
 and before </value> -->
278
<methodResponse>
279
<params>
280
<param>
281
<value>
282
<struct>
283
<member>
284
<name>success</name>
285
<value>
286
<boolean>1</boolean>
287
</value>
288
</member>
289
<member>
290
<name>sessionID</name>
291
<value>
292
<string>S300510007I</string>
293
</value>
294
</member>
295
</struct>
296
</value>
297
</param>
298
</params>
299
</methodResponse> ';
300
        $r = $s->parseResponse($f);
301
        $v = $r->value();
302
        $s = $v->structmem('sessionID');
303
        $this->assertEquals('S300510007I', $s->scalarval());
304
    }
305
306
    public function testWhiteSpace()
307
    {
308
        $s = $this->newMsg('dummy');
309
        $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
310
<member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 2 newlines follow
311
312
313
and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
314
';
315
        $r = $s->parseResponse($f);
316
        $v = $r->value();
317
        $s = $v->structmem('content');
318
        $this->assertEquals("hello world. 2 newlines follow\n\n\nand there they were.", $s->scalarval());
319
    }
320
321
    public function testDoubleDataInArrayTag()
322
    {
323
        $s = $this->newMsg('dummy');
324
        $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
325
<data></data>
326
<data></data>
327
</array></value></param></params></methodResponse>
328
';
329
        $r = $s->parseResponse($f);
330
        $v = $r->faultCode();
331
        $this->assertEquals(2, $v);
332
        $f = '<?xml version="1.0"?><methodResponse><params><param><value><array>
333
<data><value>Hello world</value></data>
334
<data></data>
335
</array></value></param></params></methodResponse>
336
';
337
        $r = $s->parseResponse($f);
338
        $v = $r->faultCode();
339
        $this->assertEquals(2, $v);
340
    }
341
342
    public function testDoubleStuffInValueTag()
343
    {
344
        $s = $this->newMsg('dummy');
345
        $f = '<?xml version="1.0"?><methodResponse><params><param><value>
346
<string>hello world</string>
347
<array><data></data></array>
348
</value></param></params></methodResponse>
349
';
350
        $r = $s->parseResponse($f);
351
        $v = $r->faultCode();
352
        $this->assertEquals(2, $v);
353
        $f = '<?xml version="1.0"?><methodResponse><params><param><value>
354
<string>hello</string>
355
<string>world</string>
356
</value></param></params></methodResponse>
357
';
358
        $r = $s->parseResponse($f);
359
        $v = $r->faultCode();
360
        $this->assertEquals(2, $v);
361
        $f = '<?xml version="1.0"?><methodResponse><params><param><value>
362
<string>hello</string>
363
<struct><member><name>hello><value>world</value></member></struct>
364
</value></param></params></methodResponse>
365
';
366
        $r = $s->parseResponse($f);
367
        $v = $r->faultCode();
368
        $this->assertEquals(2, $v);
369
    }
370
371
    public function testAutoDecodeResponse()
372
    {
373
        $s = $this->newMsg('dummy');
374
        $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
375
<member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 3 newlines follow
376
377
378
and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
379
';
380
        $r = $s->parseResponse($f, true, 'phpvals');
381
        $v = $r->value();
382
        $s = $v['content'];
383
        $this->assertEquals("hello world. 3 newlines follow\n\n\nand there they were.", $s);
384
    }
385
386
    public function testNoDecodeResponse()
387
    {
388
        $s = $this->newMsg('dummy');
389
        $f = '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
390
<member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>hello world. 3 newlines follow
391
392
393
and there they were.</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>';
394
        $r = $s->parseResponse($f, true, 'xml');
395
        $v = $r->value();
396
        $this->assertEquals($f, $v);
397
    }
398
399
    public function testUTF8Request()
400
    {
401
        $sendstring = 'κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
402
        $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
403
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
404
        $f = new xmlrpcval($sendstring, 'string');
405
        $v = $f->serialize();
406
        $this->assertEquals("<value><string>&#954;&#8057;&#963;&#956;&#949;</string></value>\n", $v);
407
        $GLOBALS['xmlrpc_internalencoding'] = 'ISO-8859-1';
408
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
409
    }
410
411
    public function testUTF8Response()
412
    {
413
        $string = chr(224) . chr(252) . chr(232);
414
415
        $s = $this->newMsg('dummy');
416
        $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>
417
<member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>' . @utf8_encode($string) . '</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
418
';
419
        $r = $s->parseResponse($f, false, 'phpvals');
420
        $v = $r->value();
421
        $v = $v['content'];
422
        $this->assertEquals($string, $v);
423
424
        $f = '<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
425
<member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>' . @utf8_encode($string) . '</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
426
';
427
        $r = $s->parseResponse($f, false, 'phpvals');
428
        $v = $r->value();
429
        $v = $v['content'];
430
        $this->assertEquals($string, $v);
431
432
        /// @todo move to EncoderTest
433
        $r = php_xmlrpc_decode_xml($f);
434
        $v = $r->value();
0 ignored issues
show
Bug introduced by
The method value() does not exist on PhpXmlRpc\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

434
        /** @scrutinizer ignore-call */ 
435
        $v = $r->value();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method value() does not exist on PhpXmlRpc\Value. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

434
        /** @scrutinizer ignore-call */ 
435
        $v = $r->value();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
435
        $v = $v->structmem('content')->scalarval();
436
        $this->assertEquals($string, $v);
437
    }
438
439
    public function testLatin1Response()
440
    {
441
        $string = chr(224) . chr(252) . chr(232);
442
443
        $s = $this->newMsg('dummy');
444
        $f = "HTTP/1.1 200 OK\r\nContent-type: text/xml; charset=ISO-8859-1\r\n\r\n" . '<?xml version="1.0"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
445
<member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>' . $string . '</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
446
';
447
        $r = $s->parseResponse($f, false, 'phpvals');
448
        $v = $r->value();
449
        $v = $v['content'];
450
        $this->assertEquals($string, $v);
451
452
        $f = '<?xml version="1.0" encoding="ISO-8859-1"?><methodResponse><params><param><value><struct><member><name>userid</name><value>311127</value></member>
453
<member><name>dateCreated</name><value><dateTime.iso8601>20011126T09:17:52</dateTime.iso8601></value></member><member><name>content</name><value>' . $string . '</value></member><member><name>postid</name><value>7414222</value></member></struct></value></param></params></methodResponse>
454
';
455
        $r = $s->parseResponse($f, false, 'phpvals');
456
        $v = $r->value();
457
        $v = $v['content'];
458
        $this->assertEquals($string, $v);
459
460
        /// @todo move to EncoderTest
461
        $r = php_xmlrpc_decode_xml($f);
462
        $v = $r->value();
463
        $v = $v->structmem('content')->scalarval();
464
        $this->assertEquals($string, $v);
465
    }
466
467
    /// @todo can we change this test to purely using the Value class ?
468
    public function testNilvalue()
469
    {
470
        // default case: we do not accept nil values received
471
        $v = new xmlrpcval('hello', 'null');
472
        $r = new xmlrpcresp($v);
473
        $s = $r->serialize();
474
        $m = $this->newMsg('dummy');
475
        $r = $m->parseresponse($s);
476
        $this->assertequals(2, $r->faultCode());
477
        // enable reception of nil values
478
        $GLOBALS['xmlrpc_null_extension'] = true;
479
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
480
        $r = $m->parseresponse($s);
481
        $v = $r->value();
482
        $this->assertequals('null', $v->scalartyp());
483
        // test with the apache version: EX:NIL
484
        $GLOBALS['xmlrpc_null_apache_encoding'] = true;
485
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
486
        // serialization
487
        $v = new xmlrpcval('hello', 'null');
488
        $s = $v->serialize();
489
        $this->assertequals(1, preg_match('#<value><ex:nil/></value>#', $s));
490
        // deserialization
491
        $r = new xmlrpcresp($v);
492
        $s = $r->serialize();
493
        $r = $m->parseresponse($s);
494
        $v = $r->value();
495
        $this->assertequals('null', $v->scalartyp());
496
        $GLOBALS['xmlrpc_null_extension'] = false;
497
        \PhpXmlRpc\PhpXmlRpc::importGlobals();
498
        $r = $m->parseresponse($s);
499
        $this->assertequals(2, $r->faultCode());
500
    }
501
}
502