Fix race condition in ext/sockets/tests
This time the race condition comes from binding sockets!
Esse commit está contido em:
@@ -4,8 +4,15 @@
|
||||
if (!$server) {
|
||||
die('Unable to create AF_INET socket [server]');
|
||||
}
|
||||
if (!socket_bind($server, '127.0.0.1', 31337)) {
|
||||
die('Unable to bind to 127.0.0.1:31337');
|
||||
$bound = false;
|
||||
for($port = 31337; $port < 31357; ++$port) {
|
||||
if (socket_bind($server, '127.0.0.1', $port)) {
|
||||
$bound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$bound) {
|
||||
die("Unable to bind to 127.0.0.1");
|
||||
}
|
||||
if (!socket_listen($server, 2)) {
|
||||
die('Unable to listen on socket');
|
||||
@@ -16,7 +23,7 @@
|
||||
if (!$client) {
|
||||
die('Unable to create AF_INET socket [client]');
|
||||
}
|
||||
if (!socket_connect($client, '127.0.0.1', 31337)) {
|
||||
if (!socket_connect($client, '127.0.0.1', $port)) {
|
||||
die('Unable to connect to server socket');
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/* Setup socket server */
|
||||
$server = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp'));
|
||||
if (!$server) {
|
||||
die('Unable to create AF_INET6 socket [server]');
|
||||
}
|
||||
$bound = false;
|
||||
for($port = 31337; $port < 31357; ++$port) {
|
||||
if (socket_bind($server, '::1', $port)) {
|
||||
$bound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$bound) {
|
||||
die("Unable to bind to [::1]:$port");
|
||||
}
|
||||
if (!socket_listen($server, 2)) {
|
||||
die('Unable to listen on socket');
|
||||
}
|
||||
|
||||
/* Connect to it */
|
||||
$client = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp'));
|
||||
if (!$client) {
|
||||
die('Unable to create AF_INET6 socket [client]');
|
||||
}
|
||||
if (!socket_connect($client, '::1', $port)) {
|
||||
die('Unable to connect to server socket');
|
||||
}
|
||||
|
||||
/* Accept that connection */
|
||||
$socket = socket_accept($server);
|
||||
if (!$socket) {
|
||||
die('Unable to accept connection');
|
||||
}
|
||||
|
||||
socket_write($client, "ABCdef123\n");
|
||||
|
||||
$data = socket_read($socket, 10, PHP_BINARY_READ);
|
||||
var_dump($data);
|
||||
|
||||
socket_close($client);
|
||||
socket_close($socket);
|
||||
socket_close($server);
|
||||
?>
|
||||
@@ -0,0 +1,2 @@
|
||||
string(10) "ABCdef123
|
||||
"
|
||||
@@ -2,17 +2,23 @@
|
||||
/* Bind and connect sockets to localhost */
|
||||
$localhost = '127.0.0.1';
|
||||
|
||||
/* Hold the port associated to address */
|
||||
$port = 31337;
|
||||
|
||||
/* Setup socket server */
|
||||
$server = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
|
||||
if (!$server) {
|
||||
die('Unable to create AF_INET socket [server]');
|
||||
}
|
||||
|
||||
if (!socket_bind($server, $localhost, $port)) {
|
||||
die('Unable to bind to '.$localhost.':'.$port);
|
||||
|
||||
$minport = 31337;
|
||||
$maxport = 31356;
|
||||
$bound = false;
|
||||
for($port = $minport; $port <= $maxport; ++$port) {
|
||||
if (socket_bind($server, $localhost, $port)) {
|
||||
$bound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$bound) {
|
||||
die('Unable to bind to '.$localhost);
|
||||
}
|
||||
if (!socket_listen($server, 2)) {
|
||||
die('Unable to listen on socket');
|
||||
@@ -33,10 +39,10 @@
|
||||
die('Unable to accept connection');
|
||||
}
|
||||
|
||||
if (!socket_getpeername($client, $address, $port)) {
|
||||
if (!socket_getpeername($client, $address, $peerport)) {
|
||||
die('Unable to retrieve peer name');
|
||||
}
|
||||
var_dump($address, $port);
|
||||
var_dump($address, $port === $peerport);
|
||||
|
||||
socket_close($client);
|
||||
socket_close($socket);
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
string(9) "127.0.0.1"
|
||||
int(31337)
|
||||
bool(true)
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/* Bind and connect sockets to localhost */
|
||||
$localhost = '::1';
|
||||
|
||||
/* Setup socket server */
|
||||
$server = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp'));
|
||||
if (!$server) {
|
||||
die('Unable to create AF_INET6 socket [server]');
|
||||
}
|
||||
|
||||
$minport = 31337;
|
||||
$maxport = 31356;
|
||||
$bound = false;
|
||||
for($port = $minport; $port <= $maxport; ++$port) {
|
||||
if (socket_bind($server, $localhost, $port)) {
|
||||
$bound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$bound) {
|
||||
die('Unable to bind to '.$localhost);
|
||||
}
|
||||
if (!socket_listen($server, 2)) {
|
||||
die('Unable to listen on socket');
|
||||
}
|
||||
|
||||
/* Connect to it */
|
||||
$client = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp'));
|
||||
if (!$client) {
|
||||
die('Unable to create AF_INET6 socket [client]');
|
||||
}
|
||||
if (!socket_connect($client, $localhost, $port)) {
|
||||
die('Unable to connect to server socket');
|
||||
}
|
||||
|
||||
/* Accept that connection */
|
||||
$socket = socket_accept($server);
|
||||
if (!$socket) {
|
||||
die('Unable to accept connection');
|
||||
}
|
||||
|
||||
if (!socket_getpeername($client, $address, $peerport)) {
|
||||
die('Unable to retrieve peer name');
|
||||
}
|
||||
var_dump($address, $port === $peerport);
|
||||
|
||||
socket_close($client);
|
||||
socket_close($socket);
|
||||
socket_close($server);
|
||||
?>
|
||||
@@ -0,0 +1,2 @@
|
||||
string(3) "::1"
|
||||
bool(true)
|
||||
Referência em uma Nova Issue
Bloquear um usuário