diff options
Diffstat (limited to 'ext/sockets/tests')
65 files changed, 2914 insertions, 0 deletions
diff --git a/ext/sockets/tests/bug46360.phpt b/ext/sockets/tests/bug46360.phpt new file mode 100644 index 0000000..c725a82 --- /dev/null +++ b/ext/sockets/tests/bug46360.phpt @@ -0,0 +1,17 @@ +--TEST-- +Bug 46360 - TCP_NODELAY constant (sock_get_option, sock_set_option) +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (version_compare(phpversion(), '5.2.7', '<')) { + die('skip old php, not eligible'); + } +?> +--FILE-- +<?php + var_dump('TCP_NODELAY'); +?> +--EXPECT-- +string(11) "TCP_NODELAY" diff --git a/ext/sockets/tests/bug49341.phpt b/ext/sockets/tests/bug49341.phpt new file mode 100644 index 0000000..961b5e7 --- /dev/null +++ b/ext/sockets/tests/bug49341.phpt @@ -0,0 +1,15 @@ +--TEST-- +Bug #49341: add SO_REUSEPORT support for socket_set_option() +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); +} +if (PHP_OS !== 'Darwin' && false === strpos(PHP_OS, 'BSD')) { + die('skip is not *BSD.'); +} +--FILE-- +<?php +var_dump(defined('SO_REUSEPORT')); +--EXPECTF-- +bool(true) diff --git a/ext/sockets/tests/bug51958.phpt b/ext/sockets/tests/bug51958.phpt new file mode 100644 index 0000000..afccd6c --- /dev/null +++ b/ext/sockets/tests/bug51958.phpt @@ -0,0 +1,22 @@ +--TEST--
+Bug #51958: socket_accept() fails on IPv6 server sockets
+--SKIPIF--
+<?php
+if (!extension_loaded('sockets')) {
+ die('skip sockets extension not available.');
+}
+if (!defined('IPPROTO_IPV6')) {
+ die('skip IPv6 not available.');
+}
+if (PHP_OS != "WINNT")
+ die('skip test relies Winsock\'s error code for WSAEWOULDBLOCK/EAGAIN');
+--FILE--
+<?php
+$listenfd = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
+socket_bind($listenfd, "::1", 13579);
+socket_listen($listenfd);
+socket_set_nonblock($listenfd);
+$connfd = @socket_accept($listenfd);
+echo socket_last_error();
+--EXPECT--
+10035
diff --git a/ext/sockets/tests/bug63000.phpt b/ext/sockets/tests/bug63000.phpt new file mode 100644 index 0000000..c806ba4 --- /dev/null +++ b/ext/sockets/tests/bug63000.phpt @@ -0,0 +1,22 @@ +--TEST-- +Bug #63000: Multicast on OSX +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); +} +if (PHP_OS !== 'Darwin') { + die('is not OSX.'); +} +--FILE-- +<?php +$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); +socket_bind($socket, '0.0.0.0', 31057); + +$so = socket_set_option($socket, IPPROTO_IP, MCAST_JOIN_GROUP, array( + "group" => '224.0.0.251', + "interface" => 0, +)); +var_dump($so); +--EXPECTF-- +bool(true) diff --git a/ext/sockets/tests/ipv4loop.phpt b/ext/sockets/tests/ipv4loop.phpt new file mode 100644 index 0000000..9fdcc17 --- /dev/null +++ b/ext/sockets/tests/ipv4loop.phpt @@ -0,0 +1,49 @@ +--TEST-- +IPv4 Loopback test +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + /* 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, '127.0.0.1', 31337)) { + die('Unable to bind to 127.0.0.1:31337'); + } + if (!socket_listen($server, 2)) { + die('Unable to listen on socket'); + } + + /* Connect to it */ + $client = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')); + if (!$client) { + die('Unable to create AF_INET socket [client]'); + } + if (!socket_connect($client, '127.0.0.1', 31337)) { + 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); +?> +--EXPECT-- +string(10) "ABCdef123 +" diff --git a/ext/sockets/tests/ipv6_skipif.inc b/ext/sockets/tests/ipv6_skipif.inc new file mode 100644 index 0000000..1f82463 --- /dev/null +++ b/ext/sockets/tests/ipv6_skipif.inc @@ -0,0 +1,6 @@ +<?php +if (!defined("AF_INET6")) { + die('skip no IPv6 support'); +} +if (@stream_socket_client('udp://[::1]:8888') === false) + die('skip no IPv6 support'); diff --git a/ext/sockets/tests/ipv6loop.phpt b/ext/sockets/tests/ipv6loop.phpt new file mode 100644 index 0000000..6967605 --- /dev/null +++ b/ext/sockets/tests/ipv6loop.phpt @@ -0,0 +1,50 @@ +--TEST-- +IPv6 Loopback test +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } + require 'ipv6_skipif.inc'; +?> +--FILE-- +<?php + /* Setup socket server */ + $server = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp')); + if (!$server) { + die('Unable to create AF_INET6 socket [server]'); + } + if (!socket_bind($server, '::1', 31337)) { + die('Unable to bind to [::1]:31337'); + } + 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', 31337)) { + 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); +?> +--EXPECT-- +string(10) "ABCdef123 +" diff --git a/ext/sockets/tests/mcast_helpers.php.inc b/ext/sockets/tests/mcast_helpers.php.inc new file mode 100644 index 0000000..ad65a3f --- /dev/null +++ b/ext/sockets/tests/mcast_helpers.php.inc @@ -0,0 +1,8 @@ +<?php +function checktimeout($sock, $limit) { + $readfs = array($sock); + $writefs = $exceptfs = array(); + if (socket_select($readfs, $writefs, $exceptfs, 0, $limit*1000) != 1) { + die("Socket read timeout hit. Can be a bug, a test bug, or a firewall issue."); + } +} diff --git a/ext/sockets/tests/mcast_ipv4_recv.phpt b/ext/sockets/tests/mcast_ipv4_recv.phpt new file mode 100644 index 0000000..8c90ae0 --- /dev/null +++ b/ext/sockets/tests/mcast_ipv4_recv.phpt @@ -0,0 +1,197 @@ +--TEST--
+Multicast support: IPv4 receive options
+--SKIPIF--
+<?php
+if (!extension_loaded('sockets')) {
+ die('skip sockets extension not available.');
+}
+$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+$br = socket_bind($s, '0.0.0.0', 3000);
+$so = socket_set_option($s, IPPROTO_IP, MCAST_JOIN_GROUP, array(
+ "group" => '224.0.0.23',
+ "interface" => 'lo',
+));
+if ($so === false) {
+ die('skip interface \'lo\' is unavailable.');
+}
+if (!defined("MCAST_BLOCK_SOURCE")) {
+ die('skip source operations are unavailable');
+}
+--FILE--
+<?php
+include __DIR__."/mcast_helpers.php.inc";
+$domain = AF_INET;
+$level = IPPROTO_IP;
+$interface = "lo";
+$mcastaddr = '224.0.0.23';
+$sblock = "127.0.0.1";
+
+echo "creating send socket bound to 127.0.0.1\n";
+$sends1 = socket_create($domain, SOCK_DGRAM, SOL_UDP);
+$br = socket_bind($sends1, '127.0.0.1');
+var_dump($br);
+
+echo "creating unbound socket and hoping the routing table causes an interface other than lo to be used for sending messages to $mcastaddr\n";
+$sends2 = socket_create($domain, SOCK_DGRAM, SOL_UDP);
+var_dump($br);
+
+echo "creating receive socket\n";
+$s = socket_create($domain, SOCK_DGRAM, SOL_UDP);
+var_dump($s);
+$br = socket_bind($s, '0.0.0.0', 3000);
+var_dump($br);
+
+$so = socket_set_option($s, $level, MCAST_JOIN_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+));
+var_dump($so);
+
+$r = socket_sendto($sends1, $m = "initial packet", strlen($m), 0, $mcastaddr, 3000);
+var_dump($r);
+
+$i = 0;
+checktimeout($s, 500);
+while (($str = socket_read($s, 3000)) !== FALSE) {
+ $i++;
+ echo "$i> ", $str, "\n";
+
+if ($i == 1) {
+ echo "leaving group\n";
+ $so = socket_set_option($s, $level, MCAST_LEAVE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "127.0.0.1", 3000);
+ var_dump($r);
+}
+if ($i == 2) {
+ echo "re-joining group\n";
+ $so = socket_set_option($s, $level, MCAST_JOIN_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends2, $m = "ignored mcast packet (different interface)", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+}
+if ($i == 3) {
+ echo "blocking source\n";
+ $so = socket_set_option($s, $level, MCAST_BLOCK_SOURCE, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored packet (blocked source)", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "127.0.0.1", 3000);
+ var_dump($r);
+}
+if ($i == 4) {
+ echo "unblocking source\n";
+ $so = socket_set_option($s, $level, MCAST_UNBLOCK_SOURCE, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "mcast packet from 127.0.0.1", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+}
+if ($i == 5) {
+ echo "leaving group\n";
+ $so = socket_set_option($s, $level, MCAST_LEAVE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "127.0.0.1", 3000);
+ var_dump($r);
+}
+if ($i == 6) {
+ echo "joining source group\n";
+ $so = socket_set_option($s, $level, MCAST_JOIN_SOURCE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "mcast packet from 127.0.0.1", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+}
+if ($i == 7) {
+ echo "leaving source group\n";
+ $so = socket_set_option($s, $level, MCAST_LEAVE_SOURCE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "127.0.0.1", 3000);
+ var_dump($r);
+}
+if ($i == 8) {
+/* echo "rjsg\n";
+ $so = socket_set_option($s, $level, MCAST_JOIN_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);*/
+ break;
+}
+
+}
+--EXPECTF--
+creating send socket bound to 127.0.0.1
+bool(true)
+creating unbound socket and hoping the routing table causes an interface other than lo to be used for sending messages to 224.0.0.23
+bool(true)
+creating receive socket
+resource(%d) of type (Socket)
+bool(true)
+bool(true)
+int(14)
+1> initial packet
+leaving group
+bool(true)
+int(20)
+int(14)
+2> unicast packet
+re-joining group
+bool(true)
+int(42)
+int(12)
+3> mcast packet
+blocking source
+bool(true)
+int(31)
+int(14)
+4> unicast packet
+unblocking source
+bool(true)
+int(27)
+5> mcast packet from 127.0.0.1
+leaving group
+bool(true)
+int(20)
+int(14)
+6> unicast packet
+joining source group
+bool(true)
+int(27)
+7> mcast packet from 127.0.0.1
+leaving source group
+bool(true)
+int(20)
+int(14)
+8> unicast packet
diff --git a/ext/sockets/tests/mcast_ipv4_send.phpt b/ext/sockets/tests/mcast_ipv4_send.phpt new file mode 100644 index 0000000..ac5bce9 --- /dev/null +++ b/ext/sockets/tests/mcast_ipv4_send.phpt @@ -0,0 +1,65 @@ +--TEST--
+Multicast support: IPv4 send options
+--SKIPIF--
+<?php
+if (!extension_loaded('sockets')) {
+ die('skip sockets extension not available.');
+}
+if (socket_set_option($s, $level, IP_MULTICAST_IF, 1) === false) {
+ die("skip interface 1 either doesn't exist or has no ipv4 address");
+}
+--FILE--
+<?php
+$domain = AF_INET;
+$level = IPPROTO_IP;
+$s = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("err");
+
+echo "Setting IP_MULTICAST_TTL\n";
+$r = socket_set_option($s, $level, IP_MULTICAST_TTL, 9);
+var_dump($r);
+$r = socket_get_option($s, $level, IP_MULTICAST_TTL);
+var_dump($r);
+echo "\n";
+
+echo "Setting IP_MULTICAST_LOOP\n";
+$r = socket_set_option($s, $level, IP_MULTICAST_LOOP, 0);
+var_dump($r);
+$r = socket_get_option($s, $level, IP_MULTICAST_LOOP);
+var_dump($r);
+$r = socket_set_option($s, $level, IP_MULTICAST_LOOP, 1);
+var_dump($r);
+$r = socket_get_option($s, $level, IP_MULTICAST_LOOP);
+var_dump($r);
+echo "\n";
+
+echo "Setting IP_MULTICAST_IF\n";
+echo "interface 0:\n";
+$r = socket_set_option($s, $level, IP_MULTICAST_IF, 0);
+var_dump($r);
+$r = socket_get_option($s, $level, IP_MULTICAST_IF);
+var_dump($r);
+echo "interface 1:\n";
+$r = socket_set_option($s, $level, IP_MULTICAST_IF, 1);
+var_dump($r);
+$r = socket_get_option($s, $level, IP_MULTICAST_IF);
+var_dump($r);
+echo "\n";
+
+--EXPECT--
+Setting IP_MULTICAST_TTL
+bool(true)
+int(9)
+
+Setting IP_MULTICAST_LOOP
+bool(true)
+int(0)
+bool(true)
+int(1)
+
+Setting IP_MULTICAST_IF
+interface 0:
+bool(true)
+int(0)
+interface 1:
+bool(true)
+int(1)
diff --git a/ext/sockets/tests/mcast_ipv4_send_error.phpt b/ext/sockets/tests/mcast_ipv4_send_error.phpt new file mode 100644 index 0000000..5cd3d8e --- /dev/null +++ b/ext/sockets/tests/mcast_ipv4_send_error.phpt @@ -0,0 +1,79 @@ +--TEST-- +Multicast support: IPv4 send options with unusual values +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); +} +if (socket_set_option($s, $level, IP_MULTICAST_IF, 1) === false) { + die("skip interface 1 either doesn't exist or has no ipv4 address"); +} +--FILE-- +<?php +$domain = AF_INET; +$level = IPPROTO_IP; +$s = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("err"); + +echo "Setting IP_MULTICAST_LOOP with 256\n"; +//if we had a simple cast to unsigned char, this would be the same as 0 +$r = socket_set_option($s, $level, IP_MULTICAST_LOOP, 256); +var_dump($r); +$r = socket_get_option($s, $level, IP_MULTICAST_LOOP); +var_dump($r); +echo "\n"; + +echo "Setting IP_MULTICAST_LOOP with false\n"; +//should convert to (unsigned char)0 +$r = socket_set_option($s, $level, IP_MULTICAST_LOOP, false); +var_dump($r); +$r = socket_get_option($s, $level, IP_MULTICAST_LOOP); +var_dump($r); +echo "\n"; + +echo "Setting IP_MULTICAST_TTL with 256\n"; +//if we had a simple cast to unsigned char, this would be the same as 0 +$r = socket_set_option($s, $level, IP_MULTICAST_TTL, 256); +var_dump($r); +$r = socket_get_option($s, $level, IP_MULTICAST_TTL); +var_dump($r); +echo "\n"; + +echo "Setting IP_MULTICAST_TTL with \"254\"\n"; +$r = socket_set_option($s, $level, IP_MULTICAST_TTL, "254"); +var_dump($r); +$r = socket_get_option($s, $level, IP_MULTICAST_TTL); +var_dump($r); +echo "\n"; + +echo "Setting IP_MULTICAST_TTL with -1\n"; +//should give error, not be the same as 255 +$r = socket_set_option($s, $level, IP_MULTICAST_TTL, -1); +var_dump($r); +$r = socket_get_option($s, $level, IP_MULTICAST_TTL); +var_dump($r); +echo "\n"; + +--EXPECTF-- +Setting IP_MULTICAST_LOOP with 256 +bool(true) +int(1) + +Setting IP_MULTICAST_LOOP with false +bool(true) +int(0) + +Setting IP_MULTICAST_TTL with 256 + +Warning: socket_set_option(): Expected a value between 0 and 255 in %s on line %d +bool(false) +int(1) + +Setting IP_MULTICAST_TTL with "254" +bool(true) +int(254) + +Setting IP_MULTICAST_TTL with -1 + +Warning: socket_set_option(): Expected a value between 0 and 255 in %s on line %d +bool(false) +int(254) diff --git a/ext/sockets/tests/mcast_ipv6_recv.phpt b/ext/sockets/tests/mcast_ipv6_recv.phpt new file mode 100644 index 0000000..38390c0 --- /dev/null +++ b/ext/sockets/tests/mcast_ipv6_recv.phpt @@ -0,0 +1,223 @@ +--TEST--
+Multicast support: IPv6 receive options
+--SKIPIF--
+<?php
+if (!extension_loaded('sockets')) {
+ die('skip sockets extension not available.');
+}
+if (!defined('IPPROTO_IPV6')) {
+ die('skip IPv6 not available.');
+}
+$s = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP);
+$br = socket_bind($s, '::', 3000);
+/* On Linux, there is no route ff00::/8 by default on lo, which makes it
+ * troublesome to send multicast traffic from lo, which we must since
+ * we're dealing with interface-local traffic... */
+$so = socket_set_option($s, IPPROTO_IPV6, MCAST_JOIN_GROUP, array(
+ "group" => 'ff01::114',
+ "interface" => 0,
+));
+if ($so === false) {
+ die('skip unable to join multicast group on any interface.');
+}
+$r = socket_sendto($s, $m = "testing packet", strlen($m), 0, 'ff01::114', 3000);
+if ($r === false) {
+ die('skip unable to send multicast packet.');
+}
+
+if (!defined("MCAST_JOIN_SOURCE_GROUP"))
+ die('skip source operations are unavailable');
+
+$so = socket_set_option($s, IPPROTO_IPV6, MCAST_LEAVE_GROUP, array(
+ "group" => 'ff01::114',
+ "interface" => 0,
+));
+$so = socket_set_option($s, IPPROTO_IPV6, MCAST_JOIN_SOURCE_GROUP, array(
+ "group" => 'ff01::114',
+ "interface" => 0,
+ "source" => '2001::dead:beef',
+));
+if ($so === false) {
+ die('skip protocol independent multicast API is unavailable.');
+}
+
+--FILE--
+<?php
+include __DIR__."/mcast_helpers.php.inc";
+$domain = AF_INET6;
+$level = IPPROTO_IPV6;
+$interface = 0;
+$mcastaddr = 'ff01::114';
+$sblock = "?";
+
+echo "creating send socket\n";
+$sends1 = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("err");
+var_dump($sends1);
+
+echo "creating receive socket\n";
+$s = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("err");
+var_dump($s);
+$br = socket_bind($s, '::0', 3000) or die("err");
+var_dump($br);
+
+$so = socket_set_option($s, $level, MCAST_JOIN_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+)) or die("err");
+var_dump($so);
+
+$r = socket_sendto($sends1, $m = "testing packet", strlen($m), 0, $mcastaddr, 3000);
+var_dump($r);
+checktimeout($s, 500);
+$r = socket_recvfrom($s, $str, 2000, 0, $from, $fromPort);
+var_dump($r, $str, $from);
+$sblock = $from;
+
+$r = socket_sendto($sends1, $m = "initial packet", strlen($m), 0, $mcastaddr, 3000);
+var_dump($r);
+
+$i = 0;
+checktimeout($s, 500);
+while (($str = socket_read($s, 3000)) !== FALSE) {
+ $i++;
+ echo "$i> ", $str, "\n";
+
+if ($i == 1) {
+ echo "leaving group\n";
+ $so = socket_set_option($s, $level, MCAST_LEAVE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "::1", 3000);
+ var_dump($r);
+}
+if ($i == 2) {
+ echo "re-joining group\n";
+ $so = socket_set_option($s, $level, MCAST_JOIN_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+}
+if ($i == 3) {
+ echo "blocking source\n";
+ $so = socket_set_option($s, $level, MCAST_BLOCK_SOURCE, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored packet (blocked source)", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "::1", 3000);
+ var_dump($r);
+}
+if ($i == 4) {
+ echo "unblocking source\n";
+ $so = socket_set_option($s, $level, MCAST_UNBLOCK_SOURCE, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+}
+if ($i == 5) {
+ echo "leaving group\n";
+ $so = socket_set_option($s, $level, MCAST_LEAVE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "::1", 3000);
+ var_dump($r);
+}
+if ($i == 6) {
+ echo "joining source group\n";
+ $so = socket_set_option($s, $level, MCAST_JOIN_SOURCE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "mcast packet from desired source", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+}
+if ($i == 7) {
+ echo "leaving source group\n";
+ $so = socket_set_option($s, $level, MCAST_LEAVE_SOURCE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "::1", 3000);
+ var_dump($r);
+}
+if ($i == 8) {
+ /*echo "joining source group\n";
+ $so = socket_set_option($s, $level, MCAST_JOIN_SOURCE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ "source" => $sblock,
+ ));
+ var_dump($so);*/
+ break;
+}
+
+}
+--EXPECTF--
+creating send socket
+resource(%d) of type (Socket)
+creating receive socket
+resource(%d) of type (Socket)
+bool(true)
+bool(true)
+int(14)
+int(14)
+string(14) "testing packet"
+string(%d) "%s"
+int(14)
+1> initial packet
+leaving group
+bool(true)
+int(20)
+int(14)
+2> unicast packet
+re-joining group
+bool(true)
+int(12)
+3> mcast packet
+blocking source
+bool(true)
+int(31)
+int(14)
+4> unicast packet
+unblocking source
+bool(true)
+int(12)
+5> mcast packet
+leaving group
+bool(true)
+int(20)
+int(14)
+6> unicast packet
+joining source group
+bool(true)
+int(32)
+7> mcast packet from desired source
+leaving source group
+bool(true)
+int(20)
+int(14)
+8> unicast packet
diff --git a/ext/sockets/tests/mcast_ipv6_recv_limited.phpt b/ext/sockets/tests/mcast_ipv6_recv_limited.phpt new file mode 100644 index 0000000..fda0998 --- /dev/null +++ b/ext/sockets/tests/mcast_ipv6_recv_limited.phpt @@ -0,0 +1,131 @@ +--TEST--
+Multicast support: IPv6 receive options (limited)
+--SKIPIF--
+<?php
+if (!extension_loaded('sockets')) {
+ die('skip sockets extension not available.');
+}
+if (!defined('IPPROTO_IPV6')) {
+ die('skip IPv6 not available.');
+}
+$s = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP);
+$br = socket_bind($s, '::', 3000);
+/* On Linux, there is no route ff00::/8 by default on lo, which makes it
+ * troublesome to send multicast traffic from lo, which we must since
+ * we're dealing with interface-local traffic... */
+$so = socket_set_option($s, IPPROTO_IPV6, MCAST_JOIN_GROUP, array(
+ "group" => 'ff01::114',
+ "interface" => 0,
+));
+if ($so === false) {
+ die('skip unable to join multicast group on any interface.');
+}
+$r = socket_sendto($s, $m = "testing packet", strlen($m), 0, 'ff01::114', 3000);
+if ($r === false) {
+ die('skip unable to send multicast packet.');
+}
+$so = socket_set_option($s, IPPROTO_IPV6, MCAST_LEAVE_GROUP, array(
+ "group" => 'ff01::114',
+ "interface" => 0,
+));
+if (defined("MCAST_JOIN_SOURCE_GROUP")) {
+ $so = socket_set_option($s, IPPROTO_IPV6, MCAST_JOIN_SOURCE_GROUP, array(
+ "group" => 'ff01::114',
+ "interface" => 0,
+ "source" => '2001::dead:beef',
+ ));
+ if ($so !== false) {
+ die('skip protocol independent multicast API is available.');
+ }
+}
+
+--FILE--
+<?php
+include __DIR__."/mcast_helpers.php.inc";
+$domain = AF_INET6;
+$level = IPPROTO_IPV6;
+$interface = 0;
+$mcastaddr = 'ff01::114';
+$sblock = "?";
+
+echo "creating send socket\n";
+$sends1 = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("err");
+var_dump($sends1);
+
+echo "creating receive socket\n";
+$s = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("err");
+var_dump($s);
+$br = socket_bind($s, '::0', 3000) or die("err");
+var_dump($br);
+
+$so = socket_set_option($s, $level, MCAST_JOIN_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+)) or die("err");
+var_dump($so);
+
+$r = socket_sendto($sends1, $m = "testing packet", strlen($m), 0, $mcastaddr, 3000);
+var_dump($r);
+checktimeout($s, 500);
+$r = socket_recvfrom($s, $str, 2000, 0, $from, $fromPort);
+var_dump($r, $str, $from);
+$sblock = $from;
+
+$r = socket_sendto($sends1, $m = "initial packet", strlen($m), 0, $mcastaddr, 3000);
+var_dump($r);
+
+$i = 0;
+checktimeout($s, 500);
+while (($str = socket_read($s, 3000, 500)) !== FALSE) {
+ $i++;
+ echo "$i> ", $str, "\n";
+
+if ($i == 1) {
+ echo "leaving group\n";
+ $so = socket_set_option($s, $level, MCAST_LEAVE_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "ignored mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+ $r = socket_sendto($sends1, $m = "unicast packet", strlen($m), 0, "::1", 3000);
+ var_dump($r);
+}
+if ($i == 2) {
+ echo "re-joining group\n";
+ $so = socket_set_option($s, $level, MCAST_JOIN_GROUP, array(
+ "group" => $mcastaddr,
+ "interface" => $interface,
+ ));
+ var_dump($so);
+ $r = socket_sendto($sends1, $m = "mcast packet", strlen($m), 0, $mcastaddr, 3000);
+ var_dump($r);
+}
+if ($i == 3) {
+ break;
+}
+
+}
+--EXPECTF--
+creating send socket
+resource(%d) of type (Socket)
+creating receive socket
+resource(%d) of type (Socket)
+bool(true)
+bool(true)
+int(14)
+int(14)
+string(14) "testing packet"
+string(%d) "%s"
+int(14)
+1> initial packet
+leaving group
+bool(true)
+int(20)
+int(14)
+2> unicast packet
+re-joining group
+bool(true)
+int(12)
+3> mcast packet
diff --git a/ext/sockets/tests/mcast_ipv6_send.phpt b/ext/sockets/tests/mcast_ipv6_send.phpt new file mode 100644 index 0000000..b8d38bf --- /dev/null +++ b/ext/sockets/tests/mcast_ipv6_send.phpt @@ -0,0 +1,70 @@ +--TEST--
+Multicast support: IPv6 send options
+--SKIPIF--
+<?php
+if (!extension_loaded('sockets')) {
+ die('skip sockets extension not available.');
+}
+if (!defined('IPPROTO_IPV6')) {
+ die('skip IPv6 not available.');
+}
+$level = IPPROTO_IPV6;
+$s = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("skip Can not create socket");
+if (socket_set_option($s, $level, IP_MULTICAST_IF, 1) === false) {
+ die("skip interface 1 either doesn't exist or has no ipv6 address");
+}
+--FILE--
+<?php
+$domain = AF_INET6;
+$level = IPPROTO_IPV6;
+$s = socket_create($domain, SOCK_DGRAM, SOL_UDP) or die("err");
+
+echo "Setting IPV6_MULTICAST_TTL\n";
+$r = socket_set_option($s, $level, IPV6_MULTICAST_HOPS, 9);
+var_dump($r);
+$r = socket_get_option($s, $level, IPV6_MULTICAST_HOPS);
+var_dump($r);
+echo "\n";
+
+echo "Setting IPV6_MULTICAST_LOOP\n";
+$r = socket_set_option($s, $level, IPV6_MULTICAST_LOOP, 0);
+var_dump($r);
+$r = socket_get_option($s, $level, IPV6_MULTICAST_LOOP);
+var_dump($r);
+$r = socket_set_option($s, $level, IPV6_MULTICAST_LOOP, 1);
+var_dump($r);
+$r = socket_get_option($s, $level, IPV6_MULTICAST_LOOP);
+var_dump($r);
+echo "\n";
+
+echo "Setting IPV6_MULTICAST_IF\n";
+echo "interface 0:\n";
+$r = socket_set_option($s, $level, IPV6_MULTICAST_IF, 0);
+var_dump($r);
+$r = socket_get_option($s, $level, IPV6_MULTICAST_IF);
+var_dump($r);
+echo "interface 1:\n";
+$r = socket_set_option($s, $level, IPV6_MULTICAST_IF, 1);
+var_dump($r);
+$r = socket_get_option($s, $level, IPV6_MULTICAST_IF);
+var_dump($r);
+echo "\n";
+
+--EXPECT--
+Setting IPV6_MULTICAST_TTL
+bool(true)
+int(9)
+
+Setting IPV6_MULTICAST_LOOP
+bool(true)
+int(0)
+bool(true)
+int(1)
+
+Setting IPV6_MULTICAST_IF
+interface 0:
+bool(true)
+int(0)
+interface 1:
+bool(true)
+int(1)
diff --git a/ext/sockets/tests/socket_accept-wrongparams.phpt b/ext/sockets/tests/socket_accept-wrongparams.phpt new file mode 100644 index 0000000..6bce05a --- /dev/null +++ b/ext/sockets/tests/socket_accept-wrongparams.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test parameter handling in socket_accept() +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +var_dump(socket_accept(null)); +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 +--EXPECTF-- +Warning: socket_accept() expects parameter 1 to be resource, null given in %s on line %d +NULL diff --git a/ext/sockets/tests/socket_bind.phpt b/ext/sockets/tests/socket_bind.phpt new file mode 100644 index 0000000..7ea2df8 --- /dev/null +++ b/ext/sockets/tests/socket_bind.phpt @@ -0,0 +1,41 @@ +--TEST-- +ext/sockets - socket_bind - basic test +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip - sockets extension not available.'); + } + if (getenv("SKIP_ONLINE_TESTS")) { + die("skip test requiring internet connection"); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + $s_c = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + $s_bind = socket_bind($s_c, '0.0.0.0', 31330+$rand); + var_dump($s_bind); + + // Connect to destination address + $s_conn = socket_connect($s_c, 'www.php.net', 80); + var_dump($s_conn); + + // Write + $request = 'GET / HTTP/1.1' . "\r\n"; + $s_write = socket_write($s_c, $request); + var_dump($s_write); + + // Close + $s_close = socket_close($s_c); + var_dump($s_close); +?> + +--EXPECTF-- +bool(true) +bool(true) +int(16) +NULL + diff --git a/ext/sockets/tests/socket_bind_params.phpt b/ext/sockets/tests/socket_bind_params.phpt new file mode 100644 index 0000000..d68a62a --- /dev/null +++ b/ext/sockets/tests/socket_bind_params.phpt @@ -0,0 +1,29 @@ +--TEST-- +ext/sockets - socket_bind - test with empty parameters +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip - sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + $s_c = socket_create_listen(31330+$rand); + $s_w = socket_bind(); + var_dump($s_w); + $s_w = socket_bind($s_c); + var_dump($s_w); + socket_close($s_c); + +?> +--EXPECTF-- + +Warning: socket_bind() expects at least 2 parameters, 0 given in %s on line %i +NULL + +Warning: socket_bind() expects at least 2 parameters, 1 given in %s on line %i +NULL diff --git a/ext/sockets/tests/socket_close_params.phpt b/ext/sockets/tests/socket_close_params.phpt new file mode 100644 index 0000000..a00330f --- /dev/null +++ b/ext/sockets/tests/socket_close_params.phpt @@ -0,0 +1,21 @@ +--TEST-- +ext/sockets - socket_close - test with empty parameters +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip - sockets extension not available.'); + } +?> +--FILE-- +<?php + // wrong parameter count + $s_c = socket_close(); + var_dump($s_c); +?> +--EXPECTF-- + +Warning: socket_close() expects exactly 1 parameter, 0 given in %s on line %i +NULL diff --git a/ext/sockets/tests/socket_connect_error.phpt b/ext/sockets/tests/socket_connect_error.phpt new file mode 100644 index 0000000..33e60f3 --- /dev/null +++ b/ext/sockets/tests/socket_connect_error.phpt @@ -0,0 +1,33 @@ +--TEST-- +Test error cases when creating a socket +--CREDITS-- +Russell Flynn <russ@redpill-linpro.com> +#PHPTestFest2009 Norway 2009-06-09 \o/ +--INI-- +error_reporting=E_ALL +display_errors=1 +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + echo 'skip sockets extension not available.'; +} +?> +--FILE-- +<?php + // Test with no arguments + $server = socket_create(); + + // Test with less arguments than required + $server = socket_create(SOCK_STREAM, getprotobyname('tcp')); + + // Test with non integer parameters + $server = socket_create(array(), 1, 1); + +?> +--EXPECTF-- +Warning: socket_create() expects exactly 3 parameters, 0 given in %s on line %d + +Warning: socket_create() expects exactly 3 parameters, 2 given in %s on line %d + +Warning: socket_create() expects parameter 1 to be long, array given in %s on line %d + diff --git a/ext/sockets/tests/socket_connect_params.phpt b/ext/sockets/tests/socket_connect_params.phpt new file mode 100644 index 0000000..44f0ffd --- /dev/null +++ b/ext/sockets/tests/socket_connect_params.phpt @@ -0,0 +1,33 @@ +--TEST-- +ext/sockets - socket_connect - test with empty parameters +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip - sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + $s_c = socket_create_listen(31330+$rand); + // wrong parameter count + $s_w = socket_connect(); + $s_w = socket_connect($s_c); + $s_w = socket_connect($s_c, '0.0.0.0'); + $s_w = socket_connect($s_c, '0.0.0.0', 31330+$rand); + + socket_close($s_c); + +?> +--EXPECTF-- + +Warning: socket_connect() expects at least 2 parameters, 0 given in %s on line %i + +Warning: socket_connect() expects at least 2 parameters, 1 given in %s on line %i + +Warning: socket_connect(): Socket of type AF_INET requires 3 arguments in %s on line %i + +Warning: socket_connect(): unable to connect [%i]: %a in %s on line %i diff --git a/ext/sockets/tests/socket_create_listen-nobind.phpt b/ext/sockets/tests/socket_create_listen-nobind.phpt new file mode 100644 index 0000000..90ae26e --- /dev/null +++ b/ext/sockets/tests/socket_create_listen-nobind.phpt @@ -0,0 +1,25 @@ +--TEST-- +Test if socket_create_listen() returns false, when it cannot bind to the port. +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +$filename = dirname(__FILE__) . '/006_root_check.tmp'; +$fp = fopen($filename, 'w'); +fclose($fp); +if (fileowner($filename) == 0) { + unlink ($filename); + die('SKIP Test cannot be run as root.'); +} +--FILE-- +<?php +$sock = socket_create_listen(80); +--EXPECTF-- +Warning: socket_create_listen(): unable to bind to given address [13]: Permission denied in %s on line %d +--CLEAN-- +<?php +unlink(dirname(__FILE__) . '/006_root_check.tmp'); +--CREDITS-- +Till Klampaeckel, till@php.net +PHP Testfest Berlin 2009-05-09
\ No newline at end of file diff --git a/ext/sockets/tests/socket_create_listen-win32.phpt b/ext/sockets/tests/socket_create_listen-win32.phpt new file mode 100644 index 0000000..23bf963 --- /dev/null +++ b/ext/sockets/tests/socket_create_listen-win32.phpt @@ -0,0 +1,21 @@ +--TEST-- +Test if socket binds on 31338 +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) != 'WIN') { + die('skip.. Not valid for non Windows'); +} +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sock = socket_create_listen(31338); +socket_getsockname($sock, $addr, $port); +var_dump($addr, $port); +--EXPECT-- +string(9) "127.0.0.1" +int(31338) +--CREDITS-- +Till Klampaeckel, till@php.net +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_create_listen-wrongparams.phpt b/ext/sockets/tests/socket_create_listen-wrongparams.phpt new file mode 100644 index 0000000..ecc5172 --- /dev/null +++ b/ext/sockets/tests/socket_create_listen-wrongparams.phpt @@ -0,0 +1,18 @@ +--TEST-- +Test if socket_create_listen throws E_WARNING with wrong parameters. +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sock1 = socket_create_listen(array()); +$sock2 = socket_create_listen(31337, array()); +--EXPECTF-- +Warning: socket_create_listen() expects parameter 1 to be long, array given in %s on line %d + +Warning: socket_create_listen() expects parameter 2 to be long, array given in %s on line %d +--CREDITS-- +Till Klampaeckel, till@php.net +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_create_listen.phpt b/ext/sockets/tests/socket_create_listen.phpt new file mode 100644 index 0000000..76f2942 --- /dev/null +++ b/ext/sockets/tests/socket_create_listen.phpt @@ -0,0 +1,21 @@ +--TEST-- +Test if socket binds on 31338 +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) == 'WIN') { + die('skip.. Not valid for Windows'); +} +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sock = socket_create_listen(31338); +socket_getsockname($sock, $addr, $port); +var_dump($addr, $port); +--EXPECT-- +string(7) "0.0.0.0" +int(31338) +--CREDITS-- +Till Klampaeckel, till@php.net +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_create_listen_params.phpt b/ext/sockets/tests/socket_create_listen_params.phpt new file mode 100644 index 0000000..56a9c8d --- /dev/null +++ b/ext/sockets/tests/socket_create_listen_params.phpt @@ -0,0 +1,23 @@ +--TEST-- +ext/sockets - socket_create_listen - test for empty parameters +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip - sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + $s_c_l = socket_create_listen(); + var_dump($s_c_l); + if ($s_c_l !== false) { + @socket_close($s_c_l); + } +?> +--EXPECTF-- +Warning: socket_create_listen() expects at least 1 parameter, 0 given in %s on line %i +NULL diff --git a/ext/sockets/tests/socket_create_listen_used.phpt b/ext/sockets/tests/socket_create_listen_used.phpt new file mode 100644 index 0000000..1e45f82 --- /dev/null +++ b/ext/sockets/tests/socket_create_listen_used.phpt @@ -0,0 +1,30 @@ +--TEST-- +ext/sockets - socket_create_listen - test for used socket +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip - sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + // wrong parameter count + $s_c_l = socket_create_listen(31330+$rand); + var_dump($s_c_l); + // default invocation + $s_c_l2 = socket_create_listen(31330+$rand); + var_dump($s_c_l2); + socket_close($s_c_l2); + socket_close($s_c_l); +?> +--EXPECTF-- +resource(%i) of type (Socket) + +Warning: socket_create_listen(): unable to bind to given address [%i]: %a in %s on line %i +bool(false) + +Warning: socket_close() expects parameter 1 to be resource, boolean given in %s on line %i diff --git a/ext/sockets/tests/socket_create_pair-wrongparams-win32.phpt b/ext/sockets/tests/socket_create_pair-wrongparams-win32.phpt new file mode 100644 index 0000000..de33d95 --- /dev/null +++ b/ext/sockets/tests/socket_create_pair-wrongparams-win32.phpt @@ -0,0 +1,38 @@ +--TEST-- +Test parameter handling in socket_create_pair() +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) != 'WIN') { + die('skip.. Not valid for non Windows'); +} +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +var_dump(socket_create_pair(AF_INET, null, null)); + +$domain = 'unknown'; +var_dump(socket_create_pair($domain, SOCK_STREAM, 0, $sockets)); + +var_dump(socket_create_pair(AF_INET, null, null, $sockets)); + +var_dump(socket_create_pair(31337, null, null, $sockets)); + +var_dump(socket_create_pair(AF_INET, 31337, 0, $sockets)); +--EXPECTF-- +Warning: socket_create_pair() expects exactly 4 parameters, 3 given in %s on line %d +NULL + +Warning: socket_create_pair() expects parameter 1 to be long, %unicode_string_optional% given in %s on line %d +NULL +bool(true) + +Warning: socket_create_pair(): invalid socket domain [31337] specified for argument 1, assuming AF_INET in %s on line %d +bool(true) + +Warning: socket_create_pair(): invalid socket type [31337] specified for argument 2, assuming SOCK_STREAM in %s on line %d +bool(true) +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_create_pair-wrongparams.phpt b/ext/sockets/tests/socket_create_pair-wrongparams.phpt new file mode 100644 index 0000000..afca2b8 --- /dev/null +++ b/ext/sockets/tests/socket_create_pair-wrongparams.phpt @@ -0,0 +1,44 @@ +--TEST-- +Test parameter handling in socket_create_pair() +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) == 'WIN') { + die('skip.. Not valid for Windows'); +} +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +var_dump(socket_create_pair(AF_INET, null, null)); + +$domain = 'unknown'; +var_dump(socket_create_pair($domain, SOCK_STREAM, 0, $sockets)); + +var_dump(socket_create_pair(AF_INET, null, null, $sockets)); + +var_dump(socket_create_pair(31337, null, null, $sockets)); + +var_dump(socket_create_pair(AF_INET, 31337, 0, $sockets)); +--EXPECTF-- +Warning: socket_create_pair() expects exactly 4 parameters, 3 given in %s on line %d +NULL + +Warning: socket_create_pair() expects parameter 1 to be long, %unicode_string_optional% given in %s on line %d +NULL + +Warning: socket_create_pair(): unable to create socket pair [%d]: %s not supported in %s on line %d +bool(false) + +Warning: socket_create_pair(): invalid socket domain [31337] specified for argument 1, assuming AF_INET in %s on line %d + +Warning: socket_create_pair(): unable to create socket pair [%d]: %s not supported in %s on line %d +bool(false) + +Warning: socket_create_pair(): invalid socket type [31337] specified for argument 2, assuming SOCK_STREAM in %s on line %d + +Warning: socket_create_pair(): unable to create socket pair [%d]: %s not supported %s on line %d +bool(false) +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_create_pair.phpt b/ext/sockets/tests/socket_create_pair.phpt new file mode 100644 index 0000000..6af6e42 --- /dev/null +++ b/ext/sockets/tests/socket_create_pair.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test for socket_create_pair() +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sockets = array(); +if (strtolower(substr(PHP_OS, 0, 3)) == 'win') { + $domain = AF_INET; +} else { + $domain = AF_UNIX; +} +socket_create_pair($domain, SOCK_STREAM, 0, $sockets); +var_dump($sockets); +--EXPECT-- +array(2) { + [0]=> + resource(4) of type (Socket) + [1]=> + resource(5) of type (Socket) +} diff --git a/ext/sockets/tests/socket_create_params.phpt b/ext/sockets/tests/socket_create_params.phpt new file mode 100644 index 0000000..13a1c73 --- /dev/null +++ b/ext/sockets/tests/socket_create_params.phpt @@ -0,0 +1,24 @@ +--TEST-- +ext/sockets - socket_create - test with empty parameters +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + $s_w = socket_create(); + $s_w = socket_create(AF_INET); + $s_w = socket_create(AF_INET, SOCK_STREAM); +?> +--EXPECTF-- + +Warning: socket_create() expects exactly 3 parameters, 0 given in %s on line %i + +Warning: socket_create() expects exactly 3 parameters, 1 given in %s on line %i + +Warning: socket_create() expects exactly 3 parameters, 2 given in %s on line %i diff --git a/ext/sockets/tests/socket_getpeername.phpt b/ext/sockets/tests/socket_getpeername.phpt new file mode 100644 index 0000000..280fa2a --- /dev/null +++ b/ext/sockets/tests/socket_getpeername.phpt @@ -0,0 +1,33 @@ +--TEST-- +ext/sockets - socket_getsockname - basic test +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + $s_c = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + $s_bind = socket_bind($s_c, '0.0.0.0', 31330+$rand); + var_dump($s_bind); + + // Connect to destination address + $s_peer = socket_getpeername($s_c, $ip, $port); + var_dump($s_peer); + var_dump($ip); + var_dump($port); + socket_close($s_c); +?> + +--EXPECTF-- +bool(true) + +Warning: socket_getpeername(): unable to retrieve peer name [%i]: %a in %s on line %i +bool(false) +NULL +NULL diff --git a/ext/sockets/tests/socket_getpeername_ipv4loop.phpt b/ext/sockets/tests/socket_getpeername_ipv4loop.phpt new file mode 100644 index 0000000..aa59abb --- /dev/null +++ b/ext/sockets/tests/socket_getpeername_ipv4loop.phpt @@ -0,0 +1,59 @@ +--TEST-- +ext/sockets - socket_getpeername_ipv4loop - basic test +--CREDITS-- +# TestFest 2009 - NorwayUG +# $Id: socket_getpeername_ipv4loop.phpt 494 2009-06-09 20:38:05Z tatjana.andersen@redpill-linpro.com $ +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + /* 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); + } + if (!socket_listen($server, 2)) { + die('Unable to listen on socket'); + } + + /* Connect to it */ + $client = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp')); + if (!$client) { + die('Unable to create AF_INET 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, $port)) { + die('Unable to retrieve peer name'); + } + var_dump($address, $port); + + socket_close($client); + socket_close($socket); + socket_close($server); +?> +--EXPECT-- +string(9) "127.0.0.1" +int(31337) diff --git a/ext/sockets/tests/socket_getpeername_ipv6loop.phpt b/ext/sockets/tests/socket_getpeername_ipv6loop.phpt new file mode 100644 index 0000000..e865f3e --- /dev/null +++ b/ext/sockets/tests/socket_getpeername_ipv6loop.phpt @@ -0,0 +1,60 @@ +--TEST-- +ext/sockets - socket_getpeername_ipv6loop - basic test +--CREDITS-- +# TestFest 2009 - NorwayUG +# $Id: socket_getpeername_ipv6loop.phpt 494 2009-06-09 20:38:05Z tatjana.andersen@redpill-linpro.com $ +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); +} +require 'ipv6_skipif.inc'; +?> +--FILE-- +<?php + /* Bind and connect sockets to localhost */ + $localhost = '::1'; + + /* Hold the port associated to address */ + $port = 31337; + + /* Setup socket server */ + $server = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp')); + if (!$server) { + die('Unable to create AF_INET6 socket [server]'); + } + + if (!socket_bind($server, $localhost, $port)) { + die('Unable to bind to '.$localhost.':'.$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, $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, $port)) { + die('Unable to retrieve peer name'); + } + var_dump($address, $port); + + socket_close($client); + socket_close($socket); + socket_close($server); +?> +--EXPECT-- +string(3) "::1" +int(31337) diff --git a/ext/sockets/tests/socket_getsockname.phpt b/ext/sockets/tests/socket_getsockname.phpt new file mode 100644 index 0000000..877bef5 --- /dev/null +++ b/ext/sockets/tests/socket_getsockname.phpt @@ -0,0 +1,32 @@ +--TEST-- +ext/sockets - socket_getsockname - basic test +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + $s_c = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + $s_bind = socket_bind($s_c, '0.0.0.0', 31330+$rand); + var_dump($s_bind); + + // Connect to destination address + $s_conn = socket_getsockname($s_c, $ip, $port); + var_dump($s_conn); + var_dump($ip); + var_dump($port); + socket_close($s_c); +?> + +--EXPECTF-- +bool(true) +bool(true) +string(7) "0.0.0.0" +int(%i) + diff --git a/ext/sockets/tests/socket_import_stream-1.phpt b/ext/sockets/tests/socket_import_stream-1.phpt new file mode 100644 index 0000000..222fca5 --- /dev/null +++ b/ext/sockets/tests/socket_import_stream-1.phpt @@ -0,0 +1,26 @@ +--TEST-- +socket_import_stream: Basic test +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} + +--FILE-- +<?php + +$domain = (strtoupper(substr(PHP_OS, 0, 3) == 'WIN') ? STREAM_PF_INET : STREAM_PF_UNIX); +$s = stream_socket_pair($domain, STREAM_SOCK_STREAM, 0); + +$s0 = reset($s); +$s1 = next($s); + +$sock = socket_import_stream($s0); +var_dump($sock); +socket_write($sock, "test message"); +socket_close($sock); + +var_dump(stream_get_contents($s1)); +--EXPECTF-- +resource(%d) of type (Socket) +string(12) "test message" diff --git a/ext/sockets/tests/socket_import_stream-2.phpt b/ext/sockets/tests/socket_import_stream-2.phpt new file mode 100644 index 0000000..085f0e3 --- /dev/null +++ b/ext/sockets/tests/socket_import_stream-2.phpt @@ -0,0 +1,49 @@ +--TEST-- +socket_import_stream: Bad arguments +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} + +--FILE-- +<?php + +var_dump(socket_import_stream()); +var_dump(socket_import_stream(1, 2)); +var_dump(socket_import_stream(1)); +var_dump(socket_import_stream(new stdclass)); +var_dump(socket_import_stream(fopen(__FILE__, "rb"))); +var_dump(socket_import_stream(socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))); +$s = stream_socket_server("udp://127.0.0.1:58392", $errno, $errstr, STREAM_SERVER_BIND); +var_dump($s); +var_dump(fclose($s)); +var_dump(socket_import_stream($s)); + + +echo "Done."; +--EXPECTF-- +Warning: socket_import_stream() expects exactly 1 parameter, 0 given in %s on line %d +NULL + +Warning: socket_import_stream() expects exactly 1 parameter, 2 given in %s on line %d +NULL + +Warning: socket_import_stream() expects parameter 1 to be resource, integer given in %s on line %d +NULL + +Warning: socket_import_stream() expects parameter 1 to be resource, object given in %s on line %d +NULL + +Warning: socket_import_stream(): cannot represent a stream of type STDIO as a Socket Descriptor in %s on line %d +bool(false) + +Warning: socket_import_stream(): supplied resource is not a valid stream resource in %s on line %d +bool(false) +resource(%d) of type (stream) +bool(true) + +Warning: socket_import_stream(): %d is not a valid stream resource in %s on line %d +bool(false) +Done. + diff --git a/ext/sockets/tests/socket_import_stream-3.phpt b/ext/sockets/tests/socket_import_stream-3.phpt new file mode 100644 index 0000000..2261a03 --- /dev/null +++ b/ext/sockets/tests/socket_import_stream-3.phpt @@ -0,0 +1,46 @@ +--TEST-- +socket_import_stream: Test with multicasting +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} +$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); +$br = socket_bind($s, '0.0.0.0', 58381); +if ($br === false) + die("SKIP IPv4/port 58381 not available"); +$so = socket_set_option($s, IPPROTO_IP, MCAST_JOIN_GROUP, array( + "group" => '224.0.0.23', + "interface" => "lo", +)); +if ($so === false) + die("SKIP joining group 224.0.0.23 on interface lo failed"); +--FILE-- +<?php + +$stream = stream_socket_server("udp://0.0.0.0:58381", $errno, $errstr, STREAM_SERVER_BIND); +$sock = socket_import_stream($stream); +var_dump($sock); +$so = socket_set_option($sock, IPPROTO_IP, MCAST_JOIN_GROUP, array( + "group" => '224.0.0.23', + "interface" => "lo", +)); +var_dump($so); + +$sendsock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); +var_dump($sendsock); +$br = socket_bind($sendsock, '127.0.0.1'); +$so = socket_sendto($sendsock, $m = "my message", strlen($m), 0, "224.0.0.23", 58381); +var_dump($so); + +stream_set_blocking($stream, 0); +var_dump(fread($stream, strlen($m))); +echo "Done.\n"; +--EXPECTF-- +resource(%d) of type (Socket) +bool(true) +resource(%d) of type (Socket) +int(10) +string(10) "my message" +Done. + diff --git a/ext/sockets/tests/socket_import_stream-4-win.phpt b/ext/sockets/tests/socket_import_stream-4-win.phpt new file mode 100644 index 0000000..b36764f --- /dev/null +++ b/ext/sockets/tests/socket_import_stream-4-win.phpt @@ -0,0 +1,103 @@ +--TEST-- +socket_import_stream: effects of closing +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} +if(substr(PHP_OS, 0, 3) != 'WIN' ) { + die("skip Not Valid for Linux"); +} + +--FILE-- +<?php + +function test($stream, $sock) { + if ($stream !== null) { + echo "stream_set_blocking "; + print_r(stream_set_blocking($stream, 0)); + echo "\n"; + } + if ($sock !== null) { + echo "socket_set_block "; + print_r(socket_set_block($sock)); + echo "\n"; + echo "socket_get_option "; + print_r(socket_get_option($sock, SOL_SOCKET, SO_TYPE)); + echo "\n"; + } + echo "\n"; +} + +echo "normal\n"; +$stream0 = stream_socket_server("udp://0.0.0.0:58380", $errno, $errstr, STREAM_SERVER_BIND); +$sock0 = socket_import_stream($stream0); +test($stream0, $sock0); + +echo "\nunset stream\n"; +$stream1 = stream_socket_server("udp://0.0.0.0:58381", $errno, $errstr, STREAM_SERVER_BIND); +$sock1 = socket_import_stream($stream1); +unset($stream1); +test(null, $sock1); + +echo "\nunset socket\n"; +$stream2 = stream_socket_server("udp://0.0.0.0:58382", $errno, $errstr, STREAM_SERVER_BIND); +$sock2 = socket_import_stream($stream2); +unset($sock2); +test($stream2, null); + +echo "\nclose stream\n"; +$stream3 = stream_socket_server("udp://0.0.0.0:58383", $errno, $errstr, STREAM_SERVER_BIND); +$sock3 = socket_import_stream($stream3); +fclose($stream3); +test($stream3, $sock3); + +echo "\nclose socket\n"; +$stream4 = stream_socket_server("udp://0.0.0.0:58384", $errno, $errstr, STREAM_SERVER_BIND); +$sock4 = socket_import_stream($stream4); +socket_close($sock4); +test($stream4, $sock4); + +echo "Done.\n"; +--EXPECTF-- +normal +stream_set_blocking 1 +socket_set_block 1 +socket_get_option 2 + + +unset stream +socket_set_block 1 +socket_get_option 2 + + +unset socket +stream_set_blocking 1 + + +close stream +stream_set_blocking +Warning: stream_set_blocking(): %d is not a valid stream resource in %s on line %d + +socket_set_block +Warning: socket_set_block(): unable to set blocking mode [%d]: An operation was attempted on something that is not a socket. + in %ssocket_import_stream-4-win.php on line %d + +socket_get_option +Warning: socket_get_option(): unable to retrieve socket option [%d]: An operation was attempted on something that is not a socket. + in %ssocket_import_stream-4-win.php on line %d + + + +close socket +stream_set_blocking +Warning: stream_set_blocking(): %d is not a valid stream resource in %s on line %d + +socket_set_block +Warning: socket_set_block(): %d is not a valid Socket resource in %s on line %d + +socket_get_option +Warning: socket_get_option(): %d is not a valid Socket resource in %s on line %d + + +Done. diff --git a/ext/sockets/tests/socket_import_stream-4.phpt b/ext/sockets/tests/socket_import_stream-4.phpt new file mode 100644 index 0000000..8095d8d --- /dev/null +++ b/ext/sockets/tests/socket_import_stream-4.phpt @@ -0,0 +1,100 @@ +--TEST-- +socket_import_stream: effects of closing +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} +if(substr(PHP_OS, 0, 3) == 'WIN' ) { + die("skip Not Valid for Windows"); +} +--FILE-- +<?php + +function test($stream, $sock) { + if ($stream !== null) { + echo "stream_set_blocking "; + print_r(stream_set_blocking($stream, 0)); + echo "\n"; + } + if ($sock !== null) { + echo "socket_set_block "; + print_r(socket_set_block($sock)); + echo "\n"; + echo "socket_get_option "; + print_r(socket_get_option($sock, SOL_SOCKET, SO_TYPE)); + echo "\n"; + } + echo "\n"; +} + +echo "normal\n"; +$stream0 = stream_socket_server("udp://0.0.0.0:58380", $errno, $errstr, STREAM_SERVER_BIND); +$sock0 = socket_import_stream($stream0); +test($stream0, $sock0); + +echo "\nunset stream\n"; +$stream1 = stream_socket_server("udp://0.0.0.0:58381", $errno, $errstr, STREAM_SERVER_BIND); +$sock1 = socket_import_stream($stream1); +unset($stream1); +test(null, $sock1); + +echo "\nunset socket\n"; +$stream2 = stream_socket_server("udp://0.0.0.0:58382", $errno, $errstr, STREAM_SERVER_BIND); +$sock2 = socket_import_stream($stream2); +unset($sock2); +test($stream2, null); + +echo "\nclose stream\n"; +$stream3 = stream_socket_server("udp://0.0.0.0:58383", $errno, $errstr, STREAM_SERVER_BIND); +$sock3 = socket_import_stream($stream3); +fclose($stream3); +test($stream3, $sock3); + +echo "\nclose socket\n"; +$stream4 = stream_socket_server("udp://0.0.0.0:58384", $errno, $errstr, STREAM_SERVER_BIND); +$sock4 = socket_import_stream($stream4); +socket_close($sock4); +test($stream4, $sock4); + +echo "Done.\n"; +--EXPECTF-- +normal +stream_set_blocking 1 +socket_set_block 1 +socket_get_option 2 + + +unset stream +socket_set_block 1 +socket_get_option 2 + + +unset socket +stream_set_blocking 1 + + +close stream +stream_set_blocking +Warning: stream_set_blocking(): %d is not a valid stream resource in %s on line %d + +socket_set_block +Warning: socket_set_block(): unable to set blocking mode [%d]: %s in %s on line %d + +socket_get_option +Warning: socket_get_option(): unable to retrieve socket option [%d]: %s in %s on line %d + + + +close socket +stream_set_blocking +Warning: stream_set_blocking(): %d is not a valid stream resource in %s on line %d + +socket_set_block +Warning: socket_set_block(): %d is not a valid Socket resource in %s on line %d + +socket_get_option +Warning: socket_get_option(): %d is not a valid Socket resource in %s on line %d + + +Done. diff --git a/ext/sockets/tests/socket_import_stream-5.phpt b/ext/sockets/tests/socket_import_stream-5.phpt new file mode 100644 index 0000000..d48531e --- /dev/null +++ b/ext/sockets/tests/socket_import_stream-5.phpt @@ -0,0 +1,23 @@ +--TEST-- +socket_import_stream: effects of leaked handles +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} +if (!function_exists('leak_variable')) + die('SKIP only for debug builds'); +--FILE-- +<?php + +$stream0 = stream_socket_server("udp://0.0.0.0:58380", $errno, $errstr, STREAM_SERVER_BIND); +$sock0 = socket_import_stream($stream0); +leak_variable($stream0, true); + +$stream1 = stream_socket_server("udp://0.0.0.0:58381", $errno, $errstr, STREAM_SERVER_BIND); +$sock1 = socket_import_stream($stream1); +leak_variable($sock1, true); + +echo "Done.\n"; +--EXPECT-- +Done. diff --git a/ext/sockets/tests/socket_listen-wrongparams.phpt b/ext/sockets/tests/socket_listen-wrongparams.phpt new file mode 100644 index 0000000..b5e1354 --- /dev/null +++ b/ext/sockets/tests/socket_listen-wrongparams.phpt @@ -0,0 +1,24 @@ +--TEST-- +Test parameter handling in socket_listen(). +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) == 'WIN') { + die('skip.. Not valid for Windows'); +} +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +var_dump(socket_listen(null)); +$socket = socket_create(AF_UNIX, SOCK_STREAM, 0); +var_dump(socket_listen($socket)); +--EXPECTF-- +Warning: socket_listen() expects parameter 1 to be resource, null given in %s on line %d +NULL + +Warning: socket_listen(): unable to listen on socket [%d]: Invalid argument in %s on line %d +bool(false) +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_listen_params.phpt b/ext/sockets/tests/socket_listen_params.phpt new file mode 100644 index 0000000..65c1117 --- /dev/null +++ b/ext/sockets/tests/socket_listen_params.phpt @@ -0,0 +1,21 @@ +--TEST-- +ext/sockets - socket_listen - test with empty parameters +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + // wrong parameter count + $s_c = socket_listen(); + var_dump($s_c); +?> +--EXPECTF-- + +Warning: socket_listen() expects at least 1 parameter, 0 given in %s on line %i +NULL diff --git a/ext/sockets/tests/socket_read_params.phpt b/ext/sockets/tests/socket_read_params.phpt new file mode 100644 index 0000000..3b4821a --- /dev/null +++ b/ext/sockets/tests/socket_read_params.phpt @@ -0,0 +1,28 @@ +--TEST-- +ext/sockets - socket_read- test with empty parameters +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + // wrong parameter count + $s_c = socket_read(); + $s_c = socket_read(14); + $s_c_l = socket_create_listen(31330+$rand); + $s_c = socket_read($s_c_l, 25); + socket_close($s_c_l); +?> +--EXPECTF-- + +Warning: socket_read() expects at least 2 parameters, 0 given in %s on line %i + +Warning: socket_read() expects at least 2 parameters, 1 given in %s on line %i + +Warning: socket_read(): unable to read from socket [%i]: %a in %s on line %i diff --git a/ext/sockets/tests/socket_select-wrongparams-1-win32.phpt b/ext/sockets/tests/socket_select-wrongparams-1-win32.phpt new file mode 100644 index 0000000..34d46d6 --- /dev/null +++ b/ext/sockets/tests/socket_select-wrongparams-1-win32.phpt @@ -0,0 +1,25 @@ +--TEST-- +Test parameter handling in socket_select(). +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) != 'WIN') { + die('skip.. Not valid for non Windows'); +} +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sockets = array(); +$domain = AF_INET; +socket_create_pair($domain, SOCK_STREAM, 0, $sockets); + +$write = null; +$except = null; +$time = -1; +var_dump(socket_select($sockets, $write, $except, $time)); +--EXPECTF-- +int(0) +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_select-wrongparams-1.phpt b/ext/sockets/tests/socket_select-wrongparams-1.phpt new file mode 100644 index 0000000..848088f --- /dev/null +++ b/ext/sockets/tests/socket_select-wrongparams-1.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test parameter handling in socket_select(). +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) == 'WIN') { + die('skip.. Not valid for Windows'); +} +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sockets = array(); +$domain = AF_UNIX; +socket_create_pair($domain, SOCK_STREAM, 0, $sockets); + +$write = null; +$except = null; +$time = -1; +var_dump(socket_select($sockets, $write, $except, $time)); +--EXPECTF-- +Warning: socket_select(): unable to select [%d]: Invalid argument in %s on line %d +bool(false) +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_select-wrongparams-2.phpt b/ext/sockets/tests/socket_select-wrongparams-2.phpt new file mode 100644 index 0000000..c149973 --- /dev/null +++ b/ext/sockets/tests/socket_select-wrongparams-2.phpt @@ -0,0 +1,23 @@ +--TEST-- +Test parameter handling in socket_select(). +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sockets = null; +$write = null; +$except = null; +$time = 0; +var_dump(socket_select($sockets, $write, $except, $time)); +socket_select($sockets, $write, $except); +--EXPECTF-- +Warning: socket_select(): no resource arrays were passed to select in %s on line %d +bool(false) + +Warning: socket_select() expects at least 4 parameters, 3 given in %s on line %d +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_select-wrongparams-3.phpt b/ext/sockets/tests/socket_select-wrongparams-3.phpt new file mode 100644 index 0000000..51686f9 --- /dev/null +++ b/ext/sockets/tests/socket_select-wrongparams-3.phpt @@ -0,0 +1,28 @@ +--TEST-- +Test parameter handling in socket_select(). +--DESCRIPTION-- +Time must be long, otherwise it's casted. +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sockets = array(); +if (strtolower(substr(PHP_OS, 0, 3)) == 'win') { + $domain = AF_INET; +} else { + $domain = AF_UNIX; +} +socket_create_pair($domain, SOCK_STREAM, 0, $sockets); + +$write = null; +$except = null; +$time = array(); +var_dump(socket_select($sockets, $write, $except, $time)); +--EXPECT-- +int(0) +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_select-wrongparams-4.phpt b/ext/sockets/tests/socket_select-wrongparams-4.phpt new file mode 100644 index 0000000..7a107b4 --- /dev/null +++ b/ext/sockets/tests/socket_select-wrongparams-4.phpt @@ -0,0 +1,29 @@ +--TEST-- +Test parameter handling in socket_select(). +--DESCRIPTION-- +usec > 999999 +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sockets = array(); +if (strtolower(substr(PHP_OS, 0, 3)) == 'win') { + $domain = AF_INET; +} else { + $domain = AF_UNIX; +} +socket_create_pair($domain, SOCK_STREAM, 0, $sockets); + +$write = null; +$except = null; +$time = 0; +$usec = 2000000; +var_dump(socket_select($sockets, $write, $except, $time, $usec)); +--EXPECT-- +int(0) +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_select.phpt b/ext/sockets/tests/socket_select.phpt new file mode 100644 index 0000000..3896a09 --- /dev/null +++ b/ext/sockets/tests/socket_select.phpt @@ -0,0 +1,25 @@ +--TEST-- +Test parameter handling in socket_select(). +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$sockets = array(); +if (strtolower(substr(PHP_OS, 0, 3)) == 'win') { + $domain = AF_INET; +} else { + $domain = AF_UNIX; +} +socket_create_pair($domain, SOCK_STREAM, 0, $sockets); + +$write = null; +$except = null; +var_dump(socket_select($sockets, $write, $except, 0)); +--EXPECT-- +int(0) +--CREDITS-- +Till Klampaeckel, till@php.net +Berlin TestFest 2009 diff --git a/ext/sockets/tests/socket_sentto_recvfrom_ipv4_udp.phpt b/ext/sockets/tests/socket_sentto_recvfrom_ipv4_udp.phpt new file mode 100644 index 0000000..bf95044 --- /dev/null +++ b/ext/sockets/tests/socket_sentto_recvfrom_ipv4_udp.phpt @@ -0,0 +1,57 @@ +--TEST-- +Test if socket_recvfrom() receives data sent by socket_sendto() via IPv4 UDP +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php + $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); + if (!$socket) { + die('Unable to create AF_INET socket'); + } + if (!socket_set_nonblock($socket)) { + die('Unable to set nonblocking mode for socket'); + } + socket_recvfrom($socket, $buf, 12, 0, $from, $port); // cause warning + $address = '127.0.0.1'; + socket_sendto($socket, '', 1, 0, $address); // cause warning + if (!socket_bind($socket, $address, 1223)) { + die("Unable to bind to $address:1223"); + } + + $msg = "Ping!"; + $len = strlen($msg); + $bytes_sent = socket_sendto($socket, $msg, $len, 0, $address, 1223); + if ($bytes_sent == -1) { + die('An error occurred while sending to the socket'); + } else if ($bytes_sent != $len) { + die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected'); + } + + $from = ""; + $port = 0; + socket_recvfrom($socket, $buf, 12, 0); // cause warning + socket_recvfrom($socket, $buf, 12, 0, $from); // cause warning + $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from, $port); + if ($bytes_received == -1) { + die('An error occurred while receiving from the socket'); + } else if ($bytes_received != $len) { + die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected'); + } + echo "Received $buf from remote address $from and remote port $port" . PHP_EOL; + + socket_close($socket); +--EXPECTF-- +Warning: socket_recvfrom(): unable to recvfrom [%d]: %a in %s on line %d + +Warning: Wrong parameter count for socket_sendto() in %s on line %d + +Warning: socket_recvfrom() expects at least 5 parameters, 4 given in %s on line %d + +Warning: Wrong parameter count for socket_recvfrom() in %s on line %d +Received Ping! from remote address 127.0.0.1 and remote port 1223 +--CREDITS-- +Falko Menge <mail at falko-menge dot de> +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_sentto_recvfrom_ipv6_udp-win32.phpt b/ext/sockets/tests/socket_sentto_recvfrom_ipv6_udp-win32.phpt new file mode 100644 index 0000000..ec96509 --- /dev/null +++ b/ext/sockets/tests/socket_sentto_recvfrom_ipv6_udp-win32.phpt @@ -0,0 +1,62 @@ +--TEST-- +Test if socket_recvfrom() receives data sent by socket_sendto() via IPv6 UDP (Win32) +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +if (substr(PHP_OS, 0, 3) != 'WIN') { + die('skip only for Windows'); +} +require 'ipv6_skipif.inc'; +--FILE-- +<?php + $socket = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP); + if (!$socket) { + die('Unable to create AF_INET6 socket'); + } + if (!socket_set_nonblock($socket)) { + die('Unable to set nonblocking mode for socket'); + } + socket_recvfrom($socket, $buf, 12, 0, $from, $port); // cause warning + $address = '::1'; + socket_sendto($socket, '', 1, 0, $address); // cause warning + if (!socket_bind($socket, $address, 1223)) { + die("Unable to bind to $address:1223"); + } + + $msg = "Ping!"; + $len = strlen($msg); + $bytes_sent = socket_sendto($socket, $msg, $len, 0, $address, 1223); + if ($bytes_sent == -1) { + die('An error occurred while sending to the socket'); + } else if ($bytes_sent != $len) { + die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected'); + } + + $from = ""; + $port = 0; + socket_recvfrom($socket, $buf, 12, 0); // cause warning + socket_recvfrom($socket, $buf, 12, 0, $from); // cause warning + $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from, $port); + if ($bytes_received == -1) { + die('An error occurred while receiving from the socket'); + } else if ($bytes_received != $len) { + die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected'); + } + echo "Received $buf from remote address $from and remote port $port" . PHP_EOL; + + socket_close($socket); +--EXPECTF-- +Warning: socket_recvfrom(): unable to recvfrom [10022]: An invalid argument was supplied. + in %s on line %d + +Warning: Wrong parameter count for socket_sendto() in %s on line %d + +Warning: socket_recvfrom() expects at least 5 parameters, 4 given in %s on line %d + +Warning: Wrong parameter count for socket_recvfrom() in %s on line %d +Received Ping! from remote address ::1 and remote port 1223 +--CREDITS-- +Falko Menge <mail at falko-menge dot de> +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_sentto_recvfrom_ipv6_udp.phpt b/ext/sockets/tests/socket_sentto_recvfrom_ipv6_udp.phpt new file mode 100644 index 0000000..2beb808 --- /dev/null +++ b/ext/sockets/tests/socket_sentto_recvfrom_ipv6_udp.phpt @@ -0,0 +1,61 @@ +--TEST-- +Test if socket_recvfrom() receives data sent by socket_sendto() via IPv6 UDP +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +if (substr(PHP_OS, 0, 3) == 'WIN') { + die('skip Not valid for Windows'); +} +require 'ipv6_skipif.inc'; +--FILE-- +<?php + $socket = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP); + if (!$socket) { + die('Unable to create AF_INET6 socket'); + } + if (!socket_set_nonblock($socket)) { + die('Unable to set nonblocking mode for socket'); + } + socket_recvfrom($socket, $buf, 12, 0, $from, $port); // cause warning + $address = '::1'; + socket_sendto($socket, '', 1, 0, $address); // cause warning + if (!socket_bind($socket, $address, 1223)) { + die("Unable to bind to $address:1223"); + } + + $msg = "Ping!"; + $len = strlen($msg); + $bytes_sent = socket_sendto($socket, $msg, $len, 0, $address, 1223); + if ($bytes_sent == -1) { + die('An error occurred while sending to the socket'); + } else if ($bytes_sent != $len) { + die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected'); + } + + $from = ""; + $port = 0; + socket_recvfrom($socket, $buf, 12, 0); // cause warning + socket_recvfrom($socket, $buf, 12, 0, $from); // cause warning + $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from, $port); + if ($bytes_received == -1) { + die('An error occurred while receiving from the socket'); + } else if ($bytes_received != $len) { + die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected'); + } + echo "Received $buf from remote address $from and remote port $port" . PHP_EOL; + + socket_close($socket); +--EXPECTF-- +Warning: socket_recvfrom(): unable to recvfrom [11]: Resource temporarily unavailable in %s on line %d + +Warning: Wrong parameter count for socket_sendto() in %s on line %d + +Warning: socket_recvfrom() expects at least 5 parameters, 4 given in %s on line %d + +Warning: Wrong parameter count for socket_recvfrom() in %s on line %d +Received Ping! from remote address ::1 and remote port 1223 +--CREDITS-- +Falko Menge <mail at falko-menge dot de> +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_sentto_recvfrom_unix.phpt b/ext/sockets/tests/socket_sentto_recvfrom_unix.phpt new file mode 100644 index 0000000..55ad75c --- /dev/null +++ b/ext/sockets/tests/socket_sentto_recvfrom_unix.phpt @@ -0,0 +1,64 @@ +--TEST-- +Test if socket_recvfrom() receives data sent by socket_sendto() through a Unix domain socket +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) == 'WIN') { + die('skip.. Not valid for Windows'); +} +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php + $socket = socket_create(AF_UNIX, SOCK_DGRAM, SOL_UDP); // cause warning + $socket = socket_create(AF_UNIX, SOCK_DGRAM, 0); + if (!$socket) { + die('Unable to create AF_UNIX socket'); + } + if (!socket_set_nonblock($socket)) { + die('Unable to set nonblocking mode for socket'); + } + socket_recvfrom($socket, $buf, 12, 0, $from, $port); // cause warning + $address = sprintf("/tmp/%s.sock", uniqid()); + if (!socket_bind($socket, $address)) { + die("Unable to bind to $address"); + } + + $msg = "Ping!"; + $len = strlen($msg); + $bytes_sent = socket_sendto($socket, $msg, $len, 0); // cause warning + $bytes_sent = socket_sendto($socket, $msg, $len, 0, $address); + if ($bytes_sent == -1) { + @unlink($address); + die('An error occurred while sending to the socket'); + } else if ($bytes_sent != $len) { + @unlink($address); + die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected'); + } + + $from = ""; + var_dump(socket_recvfrom($socket, $buf, 0, 0, $from)); // expect false + $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from); + if ($bytes_received == -1) { + @unlink($address); + die('An error occurred while receiving from the socket'); + } else if ($bytes_received != $len) { + @unlink($address); + die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected'); + } + echo "Received $buf"; + + socket_close($socket); + @unlink($address); +?> +--EXPECTF-- +Warning: socket_create(): Unable to create socket [%d]: Protocol not supported in %s on line %d + +Warning: socket_recvfrom(): unable to recvfrom [%d]: Resource temporarily unavailable in %s on line %d + +Warning: socket_sendto() expects at least 5 parameters, 4 given in %s on line %d +bool(false) +Received Ping! +--CREDITS-- +Falko Menge <mail at falko-menge dot de> +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_set_block-retval.phpt b/ext/sockets/tests/socket_set_block-retval.phpt new file mode 100644 index 0000000..2aa4b0e --- /dev/null +++ b/ext/sockets/tests/socket_set_block-retval.phpt @@ -0,0 +1,28 @@ +--TEST-- +Test socket_set_block return values +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +?> +--FILE-- +<?php + +$socket = socket_create_listen(31339); +var_dump(socket_set_block($socket)); +socket_close($socket); + +$socket2 = socket_create_listen(31340); +socket_close($socket2); +var_dump(socket_set_block($socket2)); + +?> +--EXPECTF-- +bool(true) + +Warning: socket_set_block(): %d is not a valid Socket resource in %s on line %d +bool(false) +--CREDITS-- +Robin Mehner, robin@coding-robin.de +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_set_block-wrongparams.phpt b/ext/sockets/tests/socket_set_block-wrongparams.phpt new file mode 100644 index 0000000..7c80695 --- /dev/null +++ b/ext/sockets/tests/socket_set_block-wrongparams.phpt @@ -0,0 +1,18 @@ +--TEST-- +Test if socket_set_block throws E_WARNING with wrong parameters. +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +?> +--FILE-- +<?php +socket_set_block(array()); +?> +--EXPECTF-- +Warning: socket_set_block() expects parameter 1 to be resource, array given in %s on line %d +--CREDITS-- +Robin Mehner, robin@coding-robin.de +PHP Testfest Berlin 2009-05-09 + diff --git a/ext/sockets/tests/socket_set_nonblock-retval.phpt b/ext/sockets/tests/socket_set_nonblock-retval.phpt new file mode 100644 index 0000000..b908618 --- /dev/null +++ b/ext/sockets/tests/socket_set_nonblock-retval.phpt @@ -0,0 +1,28 @@ +--TEST-- +Test socket_set_nonblock return values +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +?> +--FILE-- +<?php + +$socket = socket_create_listen(31339); +var_dump(socket_set_nonblock($socket)); +socket_close($socket); + +$socket2 = socket_create_listen(31340); +socket_close($socket2); +var_dump(socket_set_nonblock($socket2)); + +?> +--EXPECTF-- +bool(true) + +Warning: socket_set_nonblock(): %d is not a valid Socket resource in %s on line %d +bool(false) +--CREDITS-- +Robin Mehner, robin@coding-robin.de +PHP Testfest Berlin 2009-05-09 diff --git a/ext/sockets/tests/socket_set_nonblock-wrongparams.phpt b/ext/sockets/tests/socket_set_nonblock-wrongparams.phpt new file mode 100644 index 0000000..4b7e5be --- /dev/null +++ b/ext/sockets/tests/socket_set_nonblock-wrongparams.phpt @@ -0,0 +1,13 @@ +--TEST-- +Test if socket_set_nonblock throws E_WARNING with wrong parameters. +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP The sockets extension is not loaded.'); +} +--FILE-- +<?php +$socket = socket_set_nonblock(array()); +?> +--EXPECTF-- +Warning: socket_set_nonblock() expects parameter 1 to be resource, array given in %s on line %d diff --git a/ext/sockets/tests/socket_set_nonblock.phpt b/ext/sockets/tests/socket_set_nonblock.phpt new file mode 100644 index 0000000..55137d3 --- /dev/null +++ b/ext/sockets/tests/socket_set_nonblock.phpt @@ -0,0 +1,24 @@ +--TEST-- +ext/sockets - socket_set_block - basic test +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + $s_c_l = socket_create_listen(31330+$rand); + socket_set_nonblock($s_c_l); + var_dump($s_c_l); + #socket_accept($s_c_l); + socket_close($s_c_l); +?> + +--EXPECTF-- +resource(%i) of type (Socket) + diff --git a/ext/sockets/tests/socket_set_option_error_socket_option.phpt b/ext/sockets/tests/socket_set_option_error_socket_option.phpt new file mode 100644 index 0000000..eaa0e64 --- /dev/null +++ b/ext/sockets/tests/socket_set_option_error_socket_option.phpt @@ -0,0 +1,33 @@ +--TEST-- +Test if socket_set_option() returns 'unable to set socket option' failure for invalid options +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} +$filename = dirname(__FILE__) . '/006_root_check.tmp'; +$fp = fopen($filename, 'w'); +fclose($fp); +if (fileowner($filename) == 0) { + unlink ($filename); + die('SKIP Test cannot be run as root.'); +} +?> +--FILE-- +<?php +$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); +if (!$socket) { + die('Unable to create AF_INET socket [socket]'); +} + +socket_set_option( $socket, SOL_SOCKET, 1, 1); +socket_close($socket); +?> +--CLEAN-- +<?php +unlink(dirname(__FILE__) . '/006_root_check.tmp'); +--EXPECTF-- +Warning: socket_set_option(): unable to set socket option [%d]: Permission denied in %s on line %d +--CREDITS-- +Moritz Neuhaeuser, info@xcompile.net +PHP Testfest Berlin 2009-05-10 diff --git a/ext/sockets/tests/socket_set_option_rcvtimeo.phpt b/ext/sockets/tests/socket_set_option_rcvtimeo.phpt new file mode 100644 index 0000000..ea14fd3 --- /dev/null +++ b/ext/sockets/tests/socket_set_option_rcvtimeo.phpt @@ -0,0 +1,39 @@ +--TEST-- +Test if socket_set_option() works, option:SO_RCVTIMEO +--DESCRIPTION-- +-wrong params +-set/get params comparison +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} +?> +--FILE-- +<?php +$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); +if (!$socket) { + die('Unable to create AF_INET socket [socket]'); +} +socket_set_block($socket); + +//wrong params +$retval_1 = socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, array()); + +//set/get comparison +$options = array("sec" => 1, "usec" => 0); +$retval_2 = socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, $options); +$retval_3 = socket_get_option( $socket, SOL_SOCKET, SO_RCVTIMEO); + +var_dump($retval_2); +var_dump($retval_3 === $options); +socket_close($socket); +?> + +--EXPECTF-- +Warning: socket_set_option(): no key "sec" passed in optval in %s on line %d +bool(true) +bool(true) +--CREDITS-- +Moritz Neuhaeuser, info@xcompile.net +PHP Testfest Berlin 2009-05-10 diff --git a/ext/sockets/tests/socket_set_option_seolinger.phpt b/ext/sockets/tests/socket_set_option_seolinger.phpt new file mode 100644 index 0000000..36606b4 --- /dev/null +++ b/ext/sockets/tests/socket_set_option_seolinger.phpt @@ -0,0 +1,50 @@ +--TEST-- +Test if socket_set_option() works, option:SO_SEOLINGER +--DESCRIPTION-- +-wrong params +-set/get params comparison +-l_linger not given +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} +?> +--FILE-- +<?php +$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + +if (!$socket) { + die('Unable to create AF_INET socket [socket]'); +} +// wrong params +$retval_1 = socket_set_option( $socket, SOL_SOCKET, SO_LINGER, array()); + +// set/get comparison +$options = array("l_onoff" => 1, "l_linger" => 1); +$retval_2 = socket_set_option( $socket, SOL_SOCKET, SO_LINGER, $options); +$retval_3 = socket_get_option( $socket, SOL_SOCKET, SO_LINGER); + +//l_linger not given +$options_2 = array("l_onoff" => 1); +var_dump(socket_set_option( $socket, SOL_SOCKET, SO_LINGER, $options_2)); + +var_dump($retval_2); +var_dump($retval_3["l_linger"] === $options["l_linger"]); +// value of l_onoff is not always 1, Darwin returns 128 +var_dump((bool)$retval_3["l_onoff"] === (bool)$options["l_onoff"]); + +socket_close($socket); +?> + +--EXPECTF-- +Warning: socket_set_option(): no key "l_onoff" passed in optval in %s on line %d + +Warning: socket_set_option(): no key "l_linger" passed in optval in %s on line %d +bool(false) +bool(true) +bool(true) +bool(true) +--CREDITS-- +Moritz Neuhaeuser, info@xcompile.net +PHP Testfest Berlin 2009-05-10 diff --git a/ext/sockets/tests/socket_set_option_sndtimeo.phpt b/ext/sockets/tests/socket_set_option_sndtimeo.phpt new file mode 100644 index 0000000..6fd008c --- /dev/null +++ b/ext/sockets/tests/socket_set_option_sndtimeo.phpt @@ -0,0 +1,39 @@ +--TEST-- +Test if socket_set_option() works, option:SO_SNDTIMEO +--DESCRIPTION-- +-wrong params +-set/get params comparison +--SKIPIF-- +<?php +if (!extension_loaded('sockets')) { + die('SKIP sockets extension not available.'); +} +?> +--FILE-- +<?php +$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); +if (!$socket) { + die('Unable to create AF_INET socket [socket]'); +} +socket_set_block($socket); + +//wrong params +$retval_1 = socket_set_option( $socket, SOL_SOCKET, SO_SNDTIMEO, array()); + +//set/get comparison +$options = array("sec" => 1, "usec" => 0); +$retval_2 = socket_set_option( $socket, SOL_SOCKET, SO_SNDTIMEO, $options); +$retval_3 = socket_get_option( $socket, SOL_SOCKET, SO_SNDTIMEO); + +var_dump($retval_2); +var_dump($retval_3 === $options); +socket_close($socket); +?> + +--EXPECTF-- +Warning: socket_set_option(): no key "sec" passed in optval in %s on line %d +bool(true) +bool(true) +--CREDITS-- +Moritz Neuhaeuser, info@xcompile.net +PHP Testfest Berlin 2009-05-10 diff --git a/ext/sockets/tests/socket_strerror.phpt b/ext/sockets/tests/socket_strerror.phpt new file mode 100644 index 0000000..fb9925e --- /dev/null +++ b/ext/sockets/tests/socket_strerror.phpt @@ -0,0 +1,157 @@ +--TEST-- +ext/sockets - socket_strerror - basic test +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets') || !function_exists('socket_strerror')) { + die('skip sockets extension not available.'); + } + if (!stristr(PHP_OS, "Linux")) { + die('skip - test validates linux error strings only.'); + } +?> +--FILE-- +<?php + $s_s = socket_strerror(); + for ($i=0;$i<=132;$i++) { + var_dump(socket_strerror($i)); + } +?> +--EXPECTF-- + +Warning: socket_strerror() expects exactly 1 parameter, 0 given in %s on line %i +string(7) "Success" +string(23) "Operation not permitted" +string(25) "No such file or directory" +string(15) "No such process" +string(23) "Interrupted system call" +string(18) "Input/output error" +string(25) "No such device or address" +string(22) "Argument list too long" +string(17) "Exec format error" +string(19) "Bad file descriptor" +string(18) "No child processes" +string(32) "Resource temporarily unavailable" +string(22) "Cannot allocate memory" +string(17) "Permission denied" +string(11) "Bad address" +string(21) "Block device required" +string(23) "Device or resource busy" +string(11) "File exists" +string(25) "Invalid cross-device link" +string(14) "No such device" +string(15) "Not a directory" +string(14) "Is a directory" +string(16) "Invalid argument" +string(29) "Too many open files in system" +string(19) "Too many open files" +string(30) "Inappropriate ioctl for device" +string(14) "Text file busy" +string(14) "File too large" +string(23) "No space left on device" +string(12) "Illegal seek" +string(21) "Read-only file system" +string(14) "Too many links" +string(11) "Broken pipe" +string(32) "Numerical argument out of domain" +string(29) "Numerical result out of range" +string(25) "Resource deadlock avoided" +string(18) "File name too long" +string(18) "No locks available" +string(24) "Function not implemented" +string(19) "Directory not empty" +string(33) "Too many levels of symbolic links" +string(16) "Unknown error 41" +string(26) "No message of desired type" +string(18) "Identifier removed" +string(27) "Channel number out of range" +string(24) "Level 2 not synchronized" +string(14) "Level 3 halted" +string(13) "Level 3 reset" +string(24) "Link number out of range" +string(28) "Protocol driver not attached" +string(26) "No CSI structure available" +string(14) "Level 2 halted" +string(16) "Invalid exchange" +string(26) "Invalid request descriptor" +string(13) "Exchange full" +string(8) "No anode" +string(20) "Invalid request code" +string(12) "Invalid slot" +string(16) "Unknown error 58" +string(20) "Bad font file format" +string(19) "Device not a stream" +string(17) "No data available" +string(13) "Timer expired" +string(24) "Out of streams resources" +string(29) "Machine is not on the network" +string(21) "Package not installed" +string(16) "Object is remote" +string(21) "Link has been severed" +string(15) "Advertise error" +string(13) "Srmount error" +string(27) "Communication error on send" +string(14) "Protocol error" +string(18) "Multihop attempted" +string(18) "RFS specific error" +string(11) "Bad message" +string(37) "Value too large for defined data type" +string(26) "Name not unique on network" +string(28) "File descriptor in bad state" +string(22) "Remote address changed" +string(38) "Can not access a needed shared library" +string(36) "Accessing a corrupted shared library" +string(31) ".lib section in a.out corrupted" +string(47) "Attempting to link in too many shared libraries" +string(37) "Cannot exec a shared library directly" +string(49) "Invalid or incomplete multibyte or wide character" +string(43) "Interrupted system call should be restarted" +string(18) "Streams pipe error" +string(14) "Too many users" +string(30) "Socket operation on non-socket" +string(28) "Destination address required" +string(16) "Message too long" +string(30) "Protocol wrong type for socket" +string(22) "Protocol not available" +string(22) "Protocol not supported" +string(25) "Socket type not supported" +string(23) "Operation not supported" +string(29) "Protocol family not supported" +string(40) "Address family not supported by protocol" +string(22) "Address already in use" +string(31) "Cannot assign requested address" +string(15) "Network is down" +string(22) "Network is unreachable" +string(35) "Network dropped connection on reset" +string(32) "Software caused connection abort" +string(24) "Connection reset by peer" +string(25) "No buffer space available" +string(39) "Transport endpoint is already connected" +string(35) "Transport endpoint is not connected" +string(45) "Cannot send after transport endpoint shutdown" +string(34) "Too many references: cannot splice" +string(20) "Connection timed out" +string(18) "Connection refused" +string(12) "Host is down" +string(16) "No route to host" +string(29) "Operation already in progress" +string(25) "Operation now in progress" +string(%d) "Stale%sfile handle" +string(24) "Structure needs cleaning" +string(27) "Not a XENIX named type file" +string(29) "No XENIX semaphores available" +string(20) "Is a named type file" +string(16) "Remote I/O error" +string(19) "Disk quota exceeded" +string(15) "No medium found" +string(17) "Wrong medium type" +string(18) "Operation canceled" +string(26) "Required key not available" +string(15) "Key has expired" +string(20) "Key has been revoked" +string(27) "Key was rejected by service" +string(10) "Owner died" +string(21) "State not recoverable" +string(%d) "%s" diff --git a/ext/sockets/tests/socket_write_params.phpt b/ext/sockets/tests/socket_write_params.phpt new file mode 100644 index 0000000..00360f7 --- /dev/null +++ b/ext/sockets/tests/socket_write_params.phpt @@ -0,0 +1,28 @@ +--TEST-- +ext/sockets - socket_write - test with empty parameters +--CREDITS-- +Florian Anderiasch +fa@php.net +--SKIPIF-- +<?php + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + $rand = rand(1,999); + // wrong parameter count + $s_w = socket_write(); + $s_c = socket_create_listen(31330+$rand); + $s_w = socket_write($s_c); + $s_w = socket_write($s_c, "foo"); + socket_close($s_c); +?> +--EXPECTF-- + +Warning: socket_write() expects at least 2 parameters, 0 given in %s on line %i + +Warning: socket_write() expects at least 2 parameters, 1 given in %s on line %i + +Warning: socket_write(): unable to write to socket [%i]: %a in %s on line %i diff --git a/ext/sockets/tests/unixloop.phpt b/ext/sockets/tests/unixloop.phpt new file mode 100644 index 0000000..4a19444 --- /dev/null +++ b/ext/sockets/tests/unixloop.phpt @@ -0,0 +1,58 @@ +--TEST-- +Unix domain socket Loopback test +--SKIPIF-- +<?php +if (substr(PHP_OS, 0, 3) == 'WIN') { + die('skip.. Not valid for Windows'); +} + if (!extension_loaded('sockets')) { + die('skip sockets extension not available.'); + } +?> +--FILE-- +<?php + $sock_path = sprintf("/tmp/%s.sock", uniqid()); + + if (file_exists($sock_path)) + die('Temporary socket already exists.'); + + /* Setup socket server */ + $server = socket_create(AF_UNIX, SOCK_STREAM, 0); + if (!$server) { + die('Unable to create AF_UNIX socket [server]'); + } + if (!socket_bind($server, $sock_path)) { + die("Unable to bind to $sock_path"); + } + if (!socket_listen($server, 2)) { + die('Unable to listen on socket'); + } + + /* Connect to it */ + $client = socket_create(AF_UNIX, SOCK_STREAM, 0); + if (!$client) { + die('Unable to create AF_UNIX socket [client]'); + } + if (!socket_connect($client, $sock_path)) { + 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); + @unlink($sock_path); +?> +--EXPECT-- +string(10) "ABCdef123 +" |