Files
php-uuid/tests/Provider/Node/RandomNodeProviderTest.php
T
Ben Ramsey 692175901d Fix RandomNodeProvider behavior on 32-bit systems
The 6 bytes obtained from `random_bytes()` is a 48-bit integer, which
cannot be converted to decimal on a 32-bit system, without being
implicitly cast to a float by PHP. This was causing problems with
setting the multicast bit, and it led to non-random node values.

This new approach splits the 6 bytes up into two 3-byte values, each a
24-bit integer, and applies the multicast bit to the most significant
bits before re-combining the bytes as a string.
2019-11-30 20:26:42 -08:00

106 lines
3.0 KiB
PHP

<?php
namespace Ramsey\Uuid\Test\Provider\Node;
use Ramsey\Uuid\Provider\Node\RandomNodeProvider;
use Ramsey\Uuid\Test\TestCase;
use AspectMock\Test as AspectMock;
class RandomNodeProviderTest extends TestCase
{
protected function tearDown()
{
parent::tearDown();
AspectMock::clean();
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeUsesRandomBytes()
{
$bytes = hex2bin('38a675685d50');
$randomBytes = AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes);
$provider = new RandomNodeProvider();
$provider->getNode();
$randomBytes->verifyInvoked([6]);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeSetsMulticastBit()
{
$bytes = hex2bin('38a675685d50');
// Expected node has the multicast bit set, and it wasn't set in the bytes.
$expectedNode = '39a675685d50';
AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes);
$provider = new RandomNodeProvider();
$this->assertSame($expectedNode, $provider->getNode());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeAlreadyHasMulticastBit()
{
$bytesHex = '4161a1ff5d50';
$bytes = hex2bin($bytesHex);
// We expect the same hex value for the node.
$expectedNode = $bytesHex;
AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes);
$provider = new RandomNodeProvider();
$this->assertSame($expectedNode, $provider->getNode());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeSetsMulticastBitForLowNodeValue()
{
$bytes = hex2bin('100000000001');
$expectedNode = '110000000001';
AspectMock::func('Ramsey\Uuid\Provider\Node', 'random_bytes', $bytes);
$provider = new RandomNodeProvider();
$this->assertSame($expectedNode, $provider->getNode());
}
public function testGetNodeAlwaysSetsMulticastBit()
{
$provider = new RandomNodeProvider();
$nodeHex = $provider->getNode();
// Convert what we got into bytes so that we can mask out everything
// except the multicast bit. If the multicast bit doesn't exist, this
// test will fail appropriately.
$nodeBytes = hex2bin($nodeHex);
// Split the node bytes for math on 32-bit systems.
$nodeMsb = substr($nodeBytes, 0, 3);
$nodeLsb = substr($nodeBytes, 3);
// Only set bits that match the mask so we can see that the multicast
// bit is always set.
$nodeMsb = sprintf('%06x', hexdec(bin2hex($nodeMsb)) & 0x010000);
$nodeLsb = sprintf('%06x', hexdec(bin2hex($nodeLsb)) & 0x000000);
// Recombine the node bytes.
$node = $nodeMsb . $nodeLsb;
$this->assertSame('010000000000', $node);
}
}