xref: /PHP-7.4/ext/sockets/tests/bug76839.phpt (revision 3c42c784)
1--TEST--
2Bug #76839: socket_recvfrom may return an invalid 'from' address on MacOS
3--SKIPIF--
4<?php
5if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
6    die('skip not valid for Windows.');
7}
8if (!extension_loaded('sockets')) {
9    die('skip sockets extension not available.');
10}
11--FILE--
12<?php
13
14// This bug only occurs when a specific portion of memory is unclean.
15// Unforunately, 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--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