xref: /php-src/ext/sockets/tests/bug76839.phpt (revision f39b5c4c)
1--TEST--
2Bug #76839: socket_recvfrom may return an invalid 'from' address on MacOS
3--EXTENSIONS--
4sockets
5--SKIPIF--
6<?php
7if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
8    die('skip not valid for Windows.');
9}
10?>
11--FILE--
12<?php
13
14// This bug only occurs when a specific portion of memory is unclean.
15// Unfortunately, looping around 10 times and using random paths is the
16// best way I could manage to reproduce this problem without modifying php itself :-/
17
18for ($i = 0; $i < 10; $i++) {
19    $senderSocketPath = '/tmp/' . substr(md5(rand()), 0, rand(8, 16)) . '.sock';
20    $senderSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
21    socket_bind($senderSocket, $senderSocketPath);
22
23    $receiverSocketPath = '/tmp/' . substr(md5(rand()), 0, rand(8, 16)) . '.sock';
24    $receiverSocket = socket_create(AF_UNIX, SOCK_DGRAM, 0);
25    socket_bind($receiverSocket, $receiverSocketPath);
26
27    // Send message from sender socket to receiver socket
28    socket_sendto($senderSocket, 'Ping!', 5, 0, $receiverSocketPath);
29
30    // Receive message on receiver socket
31    $from = '';
32    $message = '';
33    socket_recvfrom($receiverSocket, $message, 65535, 0, $from);
34    echo "Received '$message'\n";
35
36    // Respond to the sender using the 'from' address from socket_recvfrom
37    socket_sendto($receiverSocket, 'Pong!', 5, 0, $from);
38    echo "Responded to sender with 'Pong!'\n";
39
40    socket_close($receiverSocket);
41    unlink($receiverSocketPath);
42    socket_close($senderSocket);
43    unlink($senderSocketPath);
44}
45?>
46--EXPECT--
47Received 'Ping!'
48Responded to sender with 'Pong!'
49Received 'Ping!'
50Responded to sender with 'Pong!'
51Received 'Ping!'
52Responded to sender with 'Pong!'
53Received 'Ping!'
54Responded to sender with 'Pong!'
55Received 'Ping!'
56Responded to sender with 'Pong!'
57Received 'Ping!'
58Responded to sender with 'Pong!'
59Received 'Ping!'
60Responded to sender with 'Pong!'
61Received 'Ping!'
62Responded to sender with 'Pong!'
63Received 'Ping!'
64Responded to sender with 'Pong!'
65Received 'Ping!'
66Responded to sender with 'Pong!'
67