xref: /PHP-5.4/ext/openssl/tests/bug48182.phpt (revision 9c97c268)
1--TEST--
2#48182,ssl handshake fails during asynchronous socket connection
3--SKIPIF--
4<?php
5if (!extension_loaded("openssl")) die("skip, openssl required");
6if (!extension_loaded("pcntl")) die("skip, pcntl required");
7if (OPENSSL_VERSION_NUMBER < 0x009070af) die("skip");
8?>
9--FILE--
10<?php
11
12function ssl_server($port) {
13	$host = 'ssl://127.0.0.1'.':'.$port;
14	$flags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
15	$data = "Sending bug48182\n";
16
17	$pem = dirname(__FILE__) . '/bug46127.pem';
18	$ssl_params = array( 'verify_peer' => false, 'allow_self_signed' => true, 'local_cert' => $pem);
19	$ssl = array('ssl' => $ssl_params);
20
21	$context = stream_context_create($ssl);
22	$sock = stream_socket_server($host, $errno, $errstr, $flags, $context);
23	if (!$sock) return false;
24
25	$link = stream_socket_accept($sock);
26	if (!$link) return false; // bad link?
27
28	$r = array($link);
29	$w = array();
30	$e = array();
31	if (stream_select($r, $w, $e, 1, 0) != 0)
32		$data .= fread($link, 8192);
33
34	$r = array();
35	$w = array($link);
36	if (stream_select($r, $w, $e, 1, 0) != 0)
37		$wrote = fwrite($link, $data, strlen($data));
38
39	// close stuff
40	fclose($link);
41	fclose($sock);
42
43	exit;
44}
45
46function ssl_async_client($port) {
47	$host = 'ssl://127.0.0.1'.':'.$port;
48	$flags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
49	$data = "Sending data over to SSL server in async mode with contents like Hello World\n";
50
51	$socket = stream_socket_client($host, $errno, $errstr, 10, $flags);
52	stream_set_blocking($socket, 0);
53
54	while ($socket && $data) {
55		$wrote = fwrite($socket, $data, strlen($data));
56		$data = substr($data, $wrote);
57	}
58
59	$r = array($socket);
60	$w = array();
61	$e = array();
62	if (stream_select($r, $w, $e, 1, 0) != 0)
63	{
64		$data .= fread($socket, 1024);
65	}
66
67	echo "$data";
68
69	fclose($socket);
70}
71
72echo "Running bug48182\n";
73
74$port = rand(15000, 32000);
75
76$pid = pcntl_fork();
77if ($pid == 0) { // child
78	ssl_server($port);
79	exit;
80}
81
82// client or failed
83sleep(1);
84ssl_async_client($port);
85
86pcntl_waitpid($pid, $status);
87
88?>
89--EXPECTF--
90Running bug48182
91Sending bug48182
92Sending data over to SSL server in async mode with contents like Hello World
93