1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
<?php
declare(strict_types=1);
namespace Ramsey\Uuid\Test\Provider\Node;
use Exception;
use Ramsey\Uuid\Exception\RandomSourceException;
use Ramsey\Uuid\Provider\Node\RandomNodeProvider;
use Ramsey\Uuid\Test\TestCase;
use phpmock\mockery\PHPMockery;
use function bin2hex;
use function hex2bin;
use function hexdec;
use function sprintf;
use function substr;
class RandomNodeProviderTest extends TestCase
{
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeUsesRandomBytes(): void
{
$bytes = hex2bin('38a675685d50');
$expectedNode = '39a675685d50';
PHPMockery::mock('Ramsey\Uuid\Provider\Node', 'random_bytes')
->once()
->with(6)
->andReturn($bytes);
$provider = new RandomNodeProvider();
$node = $provider->getNode();
$this->assertSame($expectedNode, $node->toString());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeAlreadyHasMulticastBit(): void
{
$bytesHex = '4161a1ff5d50';
$bytes = hex2bin($bytesHex);
// We expect the same hex value for the node.
$expectedNode = $bytesHex;
PHPMockery::mock('Ramsey\Uuid\Provider\Node', 'random_bytes')
->once()
->with(6)
->andReturn($bytes);
$provider = new RandomNodeProvider();
$this->assertSame($expectedNode, $provider->getNode()->toString());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeSetsMulticastBitForLowNodeValue(): void
{
$bytes = hex2bin('100000000001');
$expectedNode = '110000000001';
PHPMockery::mock('Ramsey\Uuid\Provider\Node', 'random_bytes')
->once()
->with(6)
->andReturn($bytes);
$provider = new RandomNodeProvider();
$this->assertSame($expectedNode, $provider->getNode()->toString());
}
public function testGetNodeAlwaysSetsMulticastBit(): void
{
$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 = (string) hex2bin((string) $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);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetNodeThrowsExceptionWhenExceptionThrownByRandombytes(): void
{
PHPMockery::mock('Ramsey\Uuid\Provider\Node', 'random_bytes')
->once()
->andThrow(new Exception('Could not gather sufficient random data'));
$provider = new RandomNodeProvider();
$this->expectException(RandomSourceException::class);
$this->expectExceptionMessage('Could not gather sufficient random data');
$provider->getNode();
}
}
|