Update random ID method to use str_random and not random_bytes
The use of random_bytes in combination with bin2hex was producing a lot of duplicate keys when tested in batches of 10k (anywhere from 2 to 6). The use of str_random yielded no duplicates even at scales of 100k keys that were 8 characters.
This commit is contained in:
parent
0e518be6ca
commit
a8c4d6afdb
14 changed files with 50 additions and 59 deletions
|
@ -84,8 +84,8 @@ class DetailsModificationServiceTest extends TestCase
|
|||
$this->repository = m::mock(ServerRepository::class);
|
||||
$this->writer = m::mock(Writer::class);
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'bin2hex')
|
||||
->expects($this->any())->willReturn('randomString');
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
|
||||
->expects($this->any())->willReturn('random_string');
|
||||
|
||||
$this->service = new DetailsModificationService(
|
||||
$this->database,
|
||||
|
@ -171,7 +171,7 @@ class DetailsModificationServiceTest extends TestCase
|
|||
'owner_id' => $data['owner_id'],
|
||||
'name' => $data['name'],
|
||||
'description' => $data['description'],
|
||||
'daemonSecret' => 'randomString',
|
||||
'daemonSecret' => 'random_string',
|
||||
], true, true)->once()->andReturnNull();
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
|
||||
|
@ -179,7 +179,7 @@ class DetailsModificationServiceTest extends TestCase
|
|||
->shouldReceive('update')->with([
|
||||
'keys' => [
|
||||
$server->daemonSecret => [],
|
||||
'randomString' => DaemonServerRepository::DAEMON_PERMISSIONS,
|
||||
'random_string' => DaemonServerRepository::DAEMON_PERMISSIONS,
|
||||
],
|
||||
])->once()->andReturnNull();
|
||||
|
||||
|
@ -206,7 +206,7 @@ class DetailsModificationServiceTest extends TestCase
|
|||
'owner_id' => $data['owner_id'],
|
||||
'name' => $data['name'],
|
||||
'description' => $data['description'],
|
||||
'daemonSecret' => 'randomString',
|
||||
'daemonSecret' => 'random_string',
|
||||
], true, true)->once()->andReturnNull();
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
|
||||
|
@ -214,7 +214,7 @@ class DetailsModificationServiceTest extends TestCase
|
|||
->shouldReceive('update')->with([
|
||||
'keys' => [
|
||||
$server->daemonSecret => [],
|
||||
'randomString' => DaemonServerRepository::DAEMON_PERMISSIONS,
|
||||
'random_string' => DaemonServerRepository::DAEMON_PERMISSIONS,
|
||||
],
|
||||
])->once()->andReturnNull();
|
||||
|
||||
|
@ -244,7 +244,7 @@ class DetailsModificationServiceTest extends TestCase
|
|||
'owner_id' => $data['owner_id'],
|
||||
'name' => $data['name'],
|
||||
'description' => $data['description'],
|
||||
'daemonSecret' => 'randomString',
|
||||
'daemonSecret' => 'random_string',
|
||||
], true, true)->once()->andReturnNull();
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->andThrow($this->exception);
|
||||
|
@ -286,7 +286,7 @@ class DetailsModificationServiceTest extends TestCase
|
|||
'owner_id' => $data['owner_id'],
|
||||
'name' => $data['name'],
|
||||
'description' => $data['description'],
|
||||
'daemonSecret' => 'randomString',
|
||||
'daemonSecret' => 'random_string',
|
||||
], true, true)->once()->andReturnNull();
|
||||
|
||||
$this->daemonServerRepository->shouldReceive('setNode')->andThrow(new Exception());
|
||||
|
|
|
@ -155,8 +155,8 @@ class ServerCreationServiceTest extends TestCase
|
|||
$this->uuid = m::mock('overload:Ramsey\Uuid\Uuid');
|
||||
$this->writer = m::mock(Writer::class);
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'bin2hex')
|
||||
->expects($this->any())->willReturn('randomstring');
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
|
||||
->expects($this->any())->willReturn('random_string');
|
||||
|
||||
$this->getFunctionMock('\\Ramsey\\Uuid\\Uuid', 'uuid4')
|
||||
->expects($this->any())->willReturn('s');
|
||||
|
@ -187,12 +187,12 @@ class ServerCreationServiceTest extends TestCase
|
|||
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->uuid->shouldReceive('uuid4')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('toString')->withNoArgs()->once()->andReturn('uuid-0000');
|
||||
$this->usernameService->shouldReceive('generate')->with($this->data['name'], 'randomstring')
|
||||
$this->usernameService->shouldReceive('generate')->with($this->data['name'], 'random_string')
|
||||
->once()->andReturn('user_name');
|
||||
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'uuid' => 'uuid-0000',
|
||||
'uuidShort' => 'randomstring',
|
||||
'uuidShort' => 'random_string',
|
||||
'node_id' => $this->data['node_id'],
|
||||
'name' => $this->data['name'],
|
||||
'description' => $this->data['description'],
|
||||
|
@ -210,7 +210,7 @@ class ServerCreationServiceTest extends TestCase
|
|||
'option_id' => $this->data['option_id'],
|
||||
'pack_id' => null,
|
||||
'startup' => $this->data['startup'],
|
||||
'daemonSecret' => 'randomstring',
|
||||
'daemonSecret' => 'random_string',
|
||||
'image' => $this->data['docker_image'],
|
||||
'username' => 'user_name',
|
||||
'sftp_password' => null,
|
||||
|
|
|
@ -46,12 +46,9 @@ class UsernameGenerationServiceTest extends TestCase
|
|||
|
||||
$this->service = new UsernameGenerationService();
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'bin2hex')
|
||||
->expects($this->any())->willReturn('dddddddd');
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
|
||||
->expects($this->any())->willReturnCallback(function ($count) {
|
||||
return str_pad('', $count, 'a');
|
||||
return str_pad('', $count, '0');
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -62,7 +59,7 @@ class UsernameGenerationServiceTest extends TestCase
|
|||
{
|
||||
$response = $this->service->generate('testname');
|
||||
|
||||
$this->assertEquals('testna_dddddddd', $response);
|
||||
$this->assertEquals('testna_00000000', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,7 +79,7 @@ class UsernameGenerationServiceTest extends TestCase
|
|||
{
|
||||
$response = $this->service->generate('testname', 'xyz');
|
||||
|
||||
$this->assertEquals('testna_xyzaaaaa', $response);
|
||||
$this->assertEquals('testna_xyz00000', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,7 +99,7 @@ class UsernameGenerationServiceTest extends TestCase
|
|||
{
|
||||
$response = $this->service->generate('');
|
||||
|
||||
$this->assertEquals('aaaaaa_dddddddd', $response);
|
||||
$this->assertEquals('000000_00000000', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,7 +109,7 @@ class UsernameGenerationServiceTest extends TestCase
|
|||
{
|
||||
$response = $this->service->generate('$%#*#(@#(#*$&#(#!#@');
|
||||
|
||||
$this->assertEquals('aaaaaa_dddddddd', $response);
|
||||
$this->assertEquals('000000_00000000', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Reference in a new issue