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