1--TEST--
2Test if socket_recvfrom() receives data sent by socket_sendto() through a Unix domain socket
3--SKIPIF--
4<?php
5if (substr(PHP_OS, 0, 3) == 'WIN') {
6	die('skip.. Not valid for Windows');
7}
8if (!extension_loaded('sockets')) {
9    die('SKIP The sockets extension is not loaded.');
10}
11--FILE--
12<?php
13    $socket = socket_create(AF_UNIX, SOCK_DGRAM, SOL_UDP); // cause warning
14    $socket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
15    if (!$socket) {
16        die('Unable to create AF_UNIX socket');
17    }
18    if (!socket_set_nonblock($socket)) {
19        die('Unable to set nonblocking mode for socket');
20    }
21    socket_recvfrom($socket, $buf, 12, 0, $from, $port); // cause warning
22    $address = sprintf("/tmp/%s.sock", uniqid());
23    if (!socket_bind($socket, $address)) {
24        die("Unable to bind to $address");
25    }
26
27    $msg = "Ping!";
28    $len = strlen($msg);
29    $bytes_sent = socket_sendto($socket, $msg, $len, 0); // cause warning
30    $bytes_sent = socket_sendto($socket, $msg, $len, 0, $address);
31    if ($bytes_sent == -1) {
32		@unlink($address);
33        die('An error occured while sending to the socket');
34    } else if ($bytes_sent != $len) {
35		@unlink($address);
36        die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected');
37    }
38
39    $from = "";
40    var_dump(socket_recvfrom($socket, $buf, 0, 0, $from)); // expect false
41    $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from);
42    if ($bytes_received == -1) {
43		@unlink($address);
44        die('An error occured while receiving from the socket');
45    } else if ($bytes_received != $len) {
46		@unlink($address);
47        die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected');
48    }
49    echo "Received $buf";
50
51    socket_close($socket);
52	@unlink($address);
53?>
54--EXPECTF--
55Warning: socket_create(): Unable to create socket [%d]: Protocol not supported in %s on line %d
56
57Warning: socket_recvfrom(): unable to recvfrom [%d]: Resource temporarily unavailable in %s on line %d
58
59Warning: socket_sendto() expects at least 5 parameters, 4 given in %s on line %d
60bool(false)
61Received Ping!
62--CREDITS--
63Falko Menge <mail at falko-menge dot de>
64PHP Testfest Berlin 2009-05-09
65