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    var_dump(socket_recvfrom($socket, $buf, 12, 0, $from, $port)); //false (EAGAIN, no 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, $address);
30    if ($bytes_sent == -1) {
31        @unlink($address);
32        die('An error occurred while sending to the socket');
33    } else if ($bytes_sent != $len) {
34        @unlink($address);
35        die($bytes_sent . ' bytes have been sent instead of the ' . $len . ' bytes expected');
36    }
37
38    $from = "";
39    var_dump(socket_recvfrom($socket, $buf, 0, 0, $from)); // expect false
40    $bytes_received = socket_recvfrom($socket, $buf, 12, 0, $from);
41    if ($bytes_received == -1) {
42        @unlink($address);
43        die('An error occurred while receiving from the socket');
44    } else if ($bytes_received != $len) {
45        @unlink($address);
46        die($bytes_received . ' bytes have been received instead of the ' . $len . ' bytes expected');
47    }
48    echo "Received $buf";
49
50    socket_close($socket);
51    @unlink($address);
52?>
53--EXPECTF--
54Warning: socket_create(): Unable to create socket [%d]: Protocol not supported in %s on line %d
55bool(false)
56bool(false)
57Received Ping!
58--CREDITS--
59Falko Menge <mail at falko-menge dot de>
60PHP Testfest Berlin 2009-05-09
61