Passed
Push — master ( 8534a5...c9b34c )
by Gaetano
08:47
created

HTTPTest::testHttpsSocket()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 7
eloc 27
c 3
b 1
f 0
nc 7
nop 1
dl 0
loc 44
rs 8.5546
1
<?php
2
3
include_once __DIR__ . '/../lib/xmlrpc.inc';
4
include_once __DIR__ . '/../lib/xmlrpc_wrappers.inc';
5
6
include_once __DIR__ . '/parse_args.php';
7
8
include_once __DIR__ . '/5ServerTest.php';
9
10
/**
11
 * Tests which stress http features of the library.
12
 * Each of these tests iterates over (almost) all the 'localhost' tests
13
 */
14
class HTTPTest extends ServerTest
15
{
16
    protected $expectHttp2 = false;
17
18
    /**
19
     * Returns all test methods from the base class, except the ones which failed already
20
     *
21
     * @todo (re)introduce skipping of tests which failed when executed individually even if test runs happen as separate processes
22
     * @todo reintroduce skipping of tests within the loop
23
     */
24
    public function getSingleHttpTestMethods()
25
    {
26
        $unsafeMethods = array(
27
            'testCatchExceptions', 'testUtf8Method', 'testServerComments',
28
            'testExoticCharsetsRequests', 'testExoticCharsetsRequests2', 'testExoticCharsetsRequests3',
29
            'testWrapInexistentUrl',
30
        );
31
32
        $methods = array();
33
        foreach(get_class_methods('ServerTest') as $method)
34
        {
35
            if (strpos($method, 'test') === 0 && !in_array($method, $unsafeMethods))
36
            {
37
                if (!isset(self::$failed_tests[$method])) {
38
                    $methods[$method] = array($method);
39
                }
40
            }
41
        }
42
43
        return $methods;
44
    }
45
46
    /**
47
     * @dataProvider getSingleHttpTestMethods
48
     * @param string $method
49
     */
50
    public function testDeflate($method)
51
    {
52
        if (!function_exists('gzdeflate'))
53
        {
54
            $this->markTestSkipped('Zlib missing: cannot test deflate functionality');
55
            return;
56
        }
57
58
        $this->client->accepted_compression = array('deflate');
59
        $this->client->request_compression = 'deflate';
60
61
        $this->$method();
62
    }
63
64
    /**
65
     * @dataProvider getSingleHttpTestMethods
66
     * @param string $method
67
     */
68
    public function testGzip($method)
69
    {
70
        if (!function_exists('gzdeflate'))
71
        {
72
            $this->markTestSkipped('Zlib missing: cannot test gzip functionality');
73
            return;
74
        }
75
76
        $this->client->accepted_compression = array('gzip');
77
        $this->client->request_compression = 'gzip';
78
79
        $this->$method();
80
    }
81
82
    public function testKeepAlives()
83
    {
84
        if (!function_exists('curl_init'))
85
        {
86
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
87
            return;
88
        }
89
90
        $this->method = 'http11';
91
        $this->client->method = 'http11';
92
        $this->client->keepalive = true;
93
94
        // to successfully test keepalive, we have to reuse the same client for all tests, we can not recreate one on setup/teardown...
95
        foreach ($this->getSingleHttpTestMethods() as $methods) {
96
            $method = $methods[0];
97
            $this->$method();
98
        }
99
    }
100
101
    /**
102
     * @dataProvider getSingleHttpTestMethods
103
     * @param string $method
104
     */
105
    public function testProxy($method)
106
    {
107
        if ($this->args['PROXYSERVER'] == '')
108
        {
109
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy');
110
            return;
111
        }
112
113
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
114
115
        $this->$method();
116
    }
117
118
    /**
119
     * @dataProvider getSingleHttpTestMethods
120
     * @param string $method
121
     */
122
    public function testHttp11($method)
123
    {
124
        if (!function_exists('curl_init'))
125
        {
126
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
127
            return;
128
        }
129
130
        $this->method = 'http11'; // not an error the double assignment!
131
        $this->client->method = 'http11';
132
        $this->client->keepalive = false;
133
134
        $this->$method();
135
    }
136
137
    /**
138
     * @dataProvider getSingleHttpTestMethods
139
     * @param string $method
140
     */
141
    public function testHttp10Curl($method)
142
    {
143
        if (!function_exists('curl_init'))
144
        {
145
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
146
            return;
147
        }
148
149
        $this->method = 'http10'; // not an error the double assignment!
150
        $this->client->method = 'http10';
151
        $this->client->keepalive = false;
152
        $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS);
153
154
        $this->$method();
155
    }
156
157
    /**
158
     * @dataProvider getSingleHttpTestMethods
159
     * @param string $method
160
     */
161
    public function testHttp11Gzip($method)
162
    {
163
        if (!function_exists('curl_init'))
164
        {
165
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
166
            return;
167
        }
168
        $this->method = 'http11'; // not an error the double assignment!
169
        $this->client->method = 'http11';
170
        $this->client->keepalive = false;
171
        $this->client->accepted_compression = array('gzip');
172
        $this->client->request_compression = 'gzip';
173
174
        $this->$method();
175
    }
176
177
    /**
178
     * @dataProvider getSingleHttpTestMethods
179
     * @param string $method
180
     */
181
    public function testHttp11Deflate($method)
182
    {
183
        if (!function_exists('curl_init'))
184
        {
185
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
186
            return;
187
        }
188
        $this->method = 'http11'; // not an error the double assignment!
189
        $this->client->method = 'http11';
190
        $this->client->keepalive = false;
191
        $this->client->accepted_compression = array('deflate');
192
        $this->client->request_compression = 'deflate';
193
194
        $this->$method();
195
    }
196
197
    /**
198
     * @dataProvider getSingleHttpTestMethods
199
     * @param string $method
200
     */
201
    public function testHttp11Proxy($method)
202
    {
203
        if (!function_exists('curl_init'))
204
        {
205
            $this->markTestSkipped('CURL missing: cannot test http 1.1 w. proxy');
206
            return;
207
        }
208
        else if ($this->args['PROXYSERVER'] == '')
209
        {
210
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. http 1.1');
211
            return;
212
        }
213
214
        $this->method = 'http11'; // not an error the double assignment!
215
        $this->client->method = 'http11';
216
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
217
        $this->client->keepalive = false;
218
219
        $this->$method();
220
    }
221
222
    /**
223
     * @dataProvider getSingleHttpTestMethods
224
     * @param string $method
225
     */
226
    public function testHttps($method)
227
    {
228
        if (!function_exists('curl_init'))
229
        {
230
            $this->markTestSkipped('CURL missing: cannot test https functionality');
231
            return;
232
        }
233
        else if ($this->args['HTTPSSERVER'] == '')
234
        {
235
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https');
236
            return;
237
        }
238
239
        $this->client->server = $this->args['HTTPSSERVER'];
240
        $this->method = 'https';
241
        $this->client->method = 'https';
242
        $this->client->path = $this->args['HTTPSURI'];
243
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
244
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
245
        $this->client->setSSLVersion($this->args['SSLVERSION']);
246
247
        $this->$method();
248
    }
249
250
    /**
251
     * @dataProvider getSingleHttpTestMethods
252
     * @param string $method
253
     */
254
    public function testHttpsSocket($method)
255
    {
256
        if ($this->args['HTTPSSERVER'] == '')
257
        {
258
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https');
259
            return;
260
        }
261
262
        /// @todo investigate: can we make this work?
263
        if (version_compare(PHP_VERSION, '5.6.0', '<'))
264
        {
265
            $this->markTestSkipped('HTTPS via Socket known to fail on php 5.5 and earlier');
266
            return;
267
        }
268
269
        /// @todo investigate: can we make this work?
270
        if (version_compare(PHP_VERSION, '5.6.1', '>=') && version_compare(PHP_VERSION, '7.2', '<'))
271
        {
272
            if (is_readable('/etc/os-release')) {
273
                $output = file_get_contents('/etc/os-release');
274
                preg_match('/VERSION="?([0-9]+)/', $output, $matches);
275
                $ubuntuVersion = @$matches[1];
276
            } else {
277
                exec('uname -a', $output, $retval);
278
                preg_match('/ubunutu([0-9]+)/', $output[0], $matches);
279
                $ubuntuVersion = @$matches[1];
280
            }
281
            if ($ubuntuVersion >= 20) {
282
                /// @todo investigate: can we make this work?
283
                $this->markTestSkipped('HTTPS via Socket known to fail on php 5.6.1 to 7.1 on Ubuntu 20 and higher');
284
                return;
285
            }
286
        }
287
288
        $this->client->server = $this->args['HTTPSSERVER'];
289
        $this->method = 'https';
290
        $this->client->method = 'https';
291
        $this->client->path = $this->args['HTTPSURI'];
292
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
293
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
294
        $this->client->setSSLVersion($this->args['SSLVERSION']);
295
        $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_NEVER);
296
297
        $this->$method();
298
    }
299
300
    /**
301
     * @dataProvider getSingleHttpTestMethods
302
     * @param string $method
303
     */
304
    public function testHttpsProxy($method)
305
    {
306
        if (!function_exists('curl_init'))
307
        {
308
            $this->markTestSkipped('CURL missing: cannot test https w. proxy');
309
            return;
310
        }
311
        else if ($this->args['PROXYSERVER'] == '')
312
        {
313
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. https');
314
            return;
315
        }
316
        else if ($this->args['HTTPSSERVER'] == '')
317
        {
318
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https w. proxy');
319
            return;
320
        }
321
322
        $this->client->server = $this->args['HTTPSSERVER'];
323
        $this->method = 'https';
324
        $this->client->method = 'https';
325
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
326
        $this->client->path = $this->args['HTTPSURI'];
327
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
328
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
329
        $this->client->setSSLVersion($this->args['SSLVERSION']);
330
331
        $this->$method();
332
    }
333
334
    /**
335
     * @dataProvider getSingleHttpTestMethods
336
     * @param string $method
337
     */
338
    public function testHttp2($method)
339
    {
340
        if (!function_exists('curl_init'))
341
        {
342
            $this->markTestSkipped('CURL missing: cannot test http/2');
343
            return;
344
        } else if (!defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE'))
345
        {
346
            $this->markTestSkipped('CURL http/2 support missing: cannot test http/2');
347
            return;
348
        }
349
350
        $this->method = 'h2c'; // not an error the double assignment!
351
        $this->client->method = 'h2c';
352
        //$this->client->keepalive = false; // q: is this a good idea?
353
354
        $this->expectHttp2 = true;
355
        $this->$method();
356
        $this->expectHttp2 = false;
357
    }
358
359
    /**
360
     * @dataProvider getSingleHttpTestMethods
361
     * @param string $method
362
     */
363
    public function testHttp2tls($method)
364
    {
365
        if (!function_exists('curl_init'))
366
        {
367
            $this->markTestSkipped('CURL missing: cannot test http/2 tls');
368
            return;
369
        } else if ($this->args['HTTPSSERVER'] == '')
370
        {
371
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test http/2 tls');
372
            return;
373
        } else if (!defined('CURL_HTTP_VERSION_2_0'))
374
        {
375
            $this->markTestSkipped('CURL http/2 support missing: cannot test http/2 tls');
376
            return;
377
        }
378
379
        $this->client->server = $this->args['HTTPSSERVER'];
380
        $this->method = 'h2';
381
        $this->client->method = 'h2';
382
        $this->client->path = $this->args['HTTPSURI'];
383
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
384
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
385
        $this->client->setSSLVersion($this->args['SSLVERSION']);
386
387
        $this->expectHttp2 = true;
388
        $this->$method();
389
        $this->expectHttp2 = false;
390
    }
391
392
    /**
393
     * @dataProvider getSingleHttpTestMethods
394
     * @param string $method
395
     */
396
    public function testUTF8Responses($method)
397
    {
398
        $this->addQueryParams(array('RESPONSE_ENCODING' => 'UTF-8'));
399
400
        $this->$method();
401
    }
402
403
    /**
404
     * @dataProvider getSingleHttpTestMethods
405
     * @param string $method
406
     */
407
    public function testUTF8Requests($method)
408
    {
409
        $this->client->request_charset_encoding = 'UTF-8';
410
411
        $this->$method();
412
    }
413
414
    /**
415
     * @dataProvider getSingleHttpTestMethods
416
     * @param string $method
417
     */
418
    public function testISOResponses($method)
419
    {
420
        $this->addQueryParams(array('RESPONSE_ENCODING' => 'ISO-8859-1'));
421
422
        $this->$method();
423
    }
424
425
    /**
426
     * @dataProvider getSingleHttpTestMethods
427
     * @param string $method
428
     */
429
    public function testISORequests($method)
430
    {
431
        $this->client->request_charset_encoding = 'ISO-8859-1';
432
433
        $this->$method();
434
    }
435
436
    /**
437
     * @dataProvider getSingleHttpTestMethods
438
     * @param string $method
439
     */
440
    public function testBasicAuth($method)
441
    {
442
        $this->client->setCredentials('test', 'test');
443
        $this->addQueryParams(array('FORCE_AUTH' => 'Basic'));
444
445
        $this->$method();
446
    }
447
448
    /**
449
     * @dataProvider getSingleHttpTestMethods
450
     * @param string $method
451
     */
452
    public function testDigestAuth($method)
453
    {
454
        if (!function_exists('curl_init'))
455
        {
456
            $this->markTestSkipped('CURL missing: cannot test digest auth functionality');
457
            return;
458
        }
459
460
        $this->client->setCredentials('test', 'test', CURLAUTH_DIGEST);
461
        $this->addQueryParams(array('FORCE_AUTH' => 'Digest'));
462
        $this->method = 'http11';
463
        $this->client->method = 'http11';
464
465
        $this->$method();
466
    }
467
468
    /**
469
     * @param \PhpXmlRpc\Response $r
470
     * @return void
471
     */
472
    protected function validateResponse($r)
473
    {
474
        if ($this->expectHttp2) {
475
            $hr = $r->httpResponse();
476
            $this->assertEquals("2", $hr['protocol_version']);
477
        } else {
478
479
        }
480
    }
481
}
482