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