2017-08-06 02:10:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Allocations;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Services\Allocations\AssignmentService;
|
|
|
|
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
|
|
|
|
|
|
|
class AssignmentServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2018-03-10 19:10:40 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
2017-08-06 02:10:32 +00:00
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Models\Node
|
|
|
|
*/
|
|
|
|
protected $node;
|
|
|
|
|
|
|
|
/**
|
2018-03-10 19:10:40 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
|
2017-08-06 02:10:32 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->node = factory(Node::class)->make();
|
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
|
|
|
$this->repository = m::mock(AllocationRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a non-CIDR notated IP address without a port range.
|
|
|
|
*/
|
|
|
|
public function testIndividualIpAddressWithoutRange()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'allocation_ip' => '192.168.1.1',
|
2018-03-10 19:10:40 +00:00
|
|
|
'allocation_ports' => ['2222'],
|
2017-08-06 02:10:32 +00:00
|
|
|
];
|
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
2017-08-06 02:10:32 +00:00
|
|
|
$this->repository->shouldReceive('insertIgnore')->with([
|
|
|
|
[
|
|
|
|
'node_id' => $this->node->id,
|
|
|
|
'ip' => '192.168.1.1',
|
2018-03-10 19:10:40 +00:00
|
|
|
'port' => 2222,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip_alias' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
],
|
2018-01-05 04:49:50 +00:00
|
|
|
])->once()->andReturn(true);
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->getService()->handle($this->node, $data);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a non-CIDR IP address with a port range provided.
|
|
|
|
*/
|
|
|
|
public function testIndividualIpAddressWithRange()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'allocation_ip' => '192.168.1.1',
|
2018-03-10 19:10:40 +00:00
|
|
|
'allocation_ports' => ['1025-1027'],
|
2017-08-06 02:10:32 +00:00
|
|
|
];
|
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
2017-08-06 02:10:32 +00:00
|
|
|
[
|
|
|
|
'node_id' => $this->node->id,
|
|
|
|
'ip' => '192.168.1.1',
|
2018-03-10 19:10:40 +00:00
|
|
|
'port' => 1025,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip_alias' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'node_id' => $this->node->id,
|
|
|
|
'ip' => '192.168.1.1',
|
2018-03-10 19:10:40 +00:00
|
|
|
'port' => 1026,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip_alias' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'node_id' => $this->node->id,
|
|
|
|
'ip' => '192.168.1.1',
|
2018-03-10 19:10:40 +00:00
|
|
|
'port' => 1027,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip_alias' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
],
|
2018-03-10 19:10:40 +00:00
|
|
|
])->andReturn(true);
|
|
|
|
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->getService()->handle($this->node, $data);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a non-CIRD IP address with a single port and an alias.
|
|
|
|
*/
|
|
|
|
public function testIndividualIPAddressWithAlias()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'allocation_ip' => '192.168.1.1',
|
2018-03-10 19:10:40 +00:00
|
|
|
'allocation_ports' => ['2222'],
|
2017-08-06 02:10:32 +00:00
|
|
|
'allocation_alias' => 'my.alias.net',
|
|
|
|
];
|
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
2017-08-06 02:10:32 +00:00
|
|
|
[
|
|
|
|
'node_id' => $this->node->id,
|
|
|
|
'ip' => '192.168.1.1',
|
2018-03-10 19:10:40 +00:00
|
|
|
'port' => 2222,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip_alias' => 'my.alias.net',
|
|
|
|
'server_id' => null,
|
|
|
|
],
|
2018-03-10 19:10:40 +00:00
|
|
|
])->andReturn(true);
|
|
|
|
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->getService()->handle($this->node, $data);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a domain name can be passed in place of an IP address.
|
|
|
|
*/
|
|
|
|
public function testDomainNamePassedInPlaceOfIPAddress()
|
|
|
|
{
|
|
|
|
$data = [
|
2018-03-10 19:10:40 +00:00
|
|
|
'allocation_ip' => 'unit-test-static.pterodactyl.io',
|
|
|
|
'allocation_ports' => ['2222'],
|
2017-08-06 02:10:32 +00:00
|
|
|
];
|
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
2017-08-06 02:10:32 +00:00
|
|
|
[
|
|
|
|
'node_id' => $this->node->id,
|
2018-03-10 19:10:40 +00:00
|
|
|
'ip' => '127.0.0.1',
|
|
|
|
'port' => 2222,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip_alias' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
],
|
2018-03-10 19:10:40 +00:00
|
|
|
])->andReturn(true);
|
|
|
|
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->getService()->handle($this->node, $data);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a CIDR IP address without a range works properly.
|
|
|
|
*/
|
|
|
|
public function testCIDRNotatedIPAddressWithoutRange()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'allocation_ip' => '192.168.1.100/31',
|
2018-03-10 19:10:40 +00:00
|
|
|
'allocation_ports' => ['2222'],
|
2017-08-06 02:10:32 +00:00
|
|
|
];
|
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
2017-08-06 02:10:32 +00:00
|
|
|
[
|
|
|
|
'node_id' => $this->node->id,
|
|
|
|
'ip' => '192.168.1.100',
|
2018-03-10 19:10:40 +00:00
|
|
|
'port' => 2222,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip_alias' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
],
|
2018-03-10 19:10:40 +00:00
|
|
|
])->andReturn(true);
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->repository->shouldReceive('insertIgnore')->once()->with([
|
2017-08-06 02:10:32 +00:00
|
|
|
[
|
|
|
|
'node_id' => $this->node->id,
|
|
|
|
'ip' => '192.168.1.101',
|
2018-03-10 19:10:40 +00:00
|
|
|
'port' => 2222,
|
2017-08-06 02:10:32 +00:00
|
|
|
'ip_alias' => null,
|
|
|
|
'server_id' => null,
|
|
|
|
],
|
2018-03-10 19:10:40 +00:00
|
|
|
])->andReturn(true);
|
|
|
|
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->getService()->handle($this->node, $data);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a CIDR IP address with a range works properly.
|
2018-03-10 19:10:40 +00:00
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
|
|
|
|
* @expectedExceptionMessage CIDR notation only allows masks between /25 and /32.
|
2017-08-06 02:10:32 +00:00
|
|
|
*/
|
|
|
|
public function testCIDRNotatedIPAddressOutsideRangeLimit()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'allocation_ip' => '192.168.1.100/20',
|
2018-03-10 19:10:40 +00:00
|
|
|
'allocation_ports' => ['2222'],
|
2017-08-06 02:10:32 +00:00
|
|
|
];
|
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->getService()->handle($this->node, $data);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if there are too many ports.
|
2018-03-10 19:10:40 +00:00
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
|
|
|
|
* @expectedExceptionMessage Adding more than 1000 ports in a single range at once is not supported.
|
2017-08-06 02:10:32 +00:00
|
|
|
*/
|
|
|
|
public function testAllocationWithPortsExceedingLimit()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'allocation_ip' => '192.168.1.1',
|
|
|
|
'allocation_ports' => ['5000-7000'],
|
|
|
|
];
|
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->getService()->handle($this->node, $data);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if an invalid port is provided.
|
2018-03-10 19:10:40 +00:00
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
|
|
|
|
* @expectedExceptionMessage The mapping provided for test123 was invalid and could not be processed.
|
2017-08-06 02:10:32 +00:00
|
|
|
*/
|
|
|
|
public function testInvalidPortProvided()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'allocation_ip' => '192.168.1.1',
|
|
|
|
'allocation_ports' => ['test123'],
|
|
|
|
];
|
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
|
|
|
$this->getService()->handle($this->node, $data);
|
|
|
|
}
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
/**
|
|
|
|
* Test that ports outside of defined limits throw an error.
|
|
|
|
*
|
|
|
|
* @param array $ports
|
|
|
|
*
|
|
|
|
* @dataProvider invalidPortsDataProvider
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
|
|
|
|
* @expectedExceptionMessage Ports in an allocation must be greater than 1024 and less than or equal to 65535.
|
|
|
|
*/
|
|
|
|
public function testPortRangeOutsideOfRangeLimits(array $ports)
|
|
|
|
{
|
|
|
|
$data = ['allocation_ip' => '192.168.1.1', 'allocation_ports' => $ports];
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
|
|
|
$this->getService()->handle($this->node, $data);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-10 19:10:40 +00:00
|
|
|
* Provide ports and ranges of ports that exceed the viable port limits for the software.
|
|
|
|
*
|
|
|
|
* @return array
|
2017-08-06 02:10:32 +00:00
|
|
|
*/
|
2018-03-10 19:10:40 +00:00
|
|
|
public function invalidPortsDataProvider(): array
|
2017-08-06 02:10:32 +00:00
|
|
|
{
|
2018-03-10 19:10:40 +00:00
|
|
|
return [
|
|
|
|
[['65536']],
|
|
|
|
[['1024']],
|
|
|
|
[['1000']],
|
|
|
|
[['0']],
|
|
|
|
[['65530-65540']],
|
|
|
|
[['65540-65560']],
|
|
|
|
[[PHP_INT_MAX]],
|
2017-08-06 02:10:32 +00:00
|
|
|
];
|
2018-03-10 19:10:40 +00:00
|
|
|
}
|
2017-08-06 02:10:32 +00:00
|
|
|
|
2018-03-10 19:10:40 +00:00
|
|
|
/**
|
|
|
|
* Returns an instance of the service with mocked dependencies for testing.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Allocations\AssignmentService
|
|
|
|
*/
|
|
|
|
private function getService(): AssignmentService
|
|
|
|
{
|
|
|
|
return new AssignmentService($this->repository, $this->connection);
|
2017-08-06 02:10:32 +00:00
|
|
|
}
|
|
|
|
}
|