-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # src/WindowsDnsConfigLoader.php
- Loading branch information
Showing
9 changed files
with
189 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Amp\Dns; | ||
|
||
use Amp\ForbidCloning; | ||
use Amp\ForbidSerialization; | ||
|
||
final class StaticDnsConfigLoader implements DnsConfigLoader | ||
{ | ||
use ForbidCloning; | ||
use ForbidSerialization; | ||
|
||
public function __construct( | ||
private readonly DnsConfig $config | ||
) { | ||
} | ||
|
||
public function loadConfig(): DnsConfig | ||
{ | ||
return $this->config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Amp\Dns\Test; | ||
|
||
use Amp\Cache\Cache; | ||
use Amp\Dns\DnsRecord; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DnsCacheTrainer | ||
{ | ||
private Cache $mock; | ||
|
||
private array $operations = []; | ||
|
||
public function __construct(TestCase $testCase) | ||
{ | ||
/** @noinspection PhpFieldAssignmentTypeMismatchInspection */ | ||
$this->mock = $testCase->getMockBuilder(Cache::class) | ||
->disableOriginalConstructor() | ||
->disableOriginalClone() | ||
->disableArgumentCloning() | ||
->disallowMockingUnknownTypes() | ||
->getMock(); | ||
} | ||
|
||
public function givenResponse(DnsRecord ...$records): void | ||
{ | ||
$this->mock->method('get')->willReturnCallback(function ($key) use ($records) { | ||
$this->operations[] = ['get', $key]; | ||
|
||
return \array_map(fn ($record) => [$record->getValue(), $record->getType()], $records); | ||
}); | ||
} | ||
|
||
public function getOperations(): array | ||
{ | ||
return $this->operations; | ||
} | ||
|
||
public function getMock(): Cache | ||
{ | ||
return $this->mock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,35 +2,115 @@ | |
|
||
namespace Amp\Dns\Test; | ||
|
||
use Amp\Cancellation; | ||
use Amp\Dns\DnsConfig; | ||
use Amp\Dns\DnsException; | ||
use Amp\Dns\DnsRecord; | ||
use Amp\Dns\InvalidNameException; | ||
use Amp\Dns\MissingDnsRecordException; | ||
use Amp\Dns\Rfc1035StubDnsResolver; | ||
use Amp\Dns\StaticDnsConfigLoader; | ||
use Amp\PHPUnit\AsyncTestCase; | ||
|
||
class Rfc1035StubResolverTest extends AsyncTestCase | ||
{ | ||
private DnsCacheTrainer $cacheTrainer; | ||
private ?DnsConfig $dnsConfig = null; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->cacheTrainer = new DnsCacheTrainer($this); | ||
} | ||
|
||
public function testResolveSecondParameterAcceptedValues(): void | ||
{ | ||
$this->expectException(\Error::class); | ||
(new Rfc1035StubDnsResolver)->resolve("abc.de", DnsRecord::TXT); | ||
$this->whenResolve("abc.de", DnsRecord::TXT); | ||
} | ||
|
||
public function testIpAsArgumentWithIPv4Restriction(): void | ||
{ | ||
$this->expectException(DnsException::class); | ||
(new Rfc1035StubDnsResolver)->resolve("::1", DnsRecord::A); | ||
$this->whenResolve("::1", DnsRecord::A); | ||
} | ||
|
||
public function testIpAsArgumentWithIPv6Restriction(): void | ||
{ | ||
$this->expectException(DnsException::class); | ||
(new Rfc1035StubDnsResolver)->resolve("127.0.0.1", DnsRecord::AAAA); | ||
$this->whenResolve("127.0.0.1", DnsRecord::AAAA); | ||
} | ||
|
||
public function testInvalidName(): void | ||
{ | ||
$this->expectException(InvalidNameException::class); | ||
(new Rfc1035StubDnsResolver)->resolve("[email protected]", DnsRecord::A); | ||
$this->whenResolve("[email protected]", DnsRecord::A); | ||
} | ||
|
||
public function testSearchListDot(): void | ||
{ | ||
$this->dnsConfig = (new DnsConfig(['127.0.0.1'])) | ||
->withNdots(1) | ||
->withSearchList(['.']); | ||
|
||
$this->cacheTrainer->givenResponse(new DnsRecord('1.2.3.4', DnsRecord::A)); | ||
|
||
$this->whenResolve('foobar'); | ||
|
||
$this->assertSame([ | ||
['get', 'amphp.dns.foobar#1'], | ||
['get', 'amphp.dns.foobar#28'], | ||
], $this->cacheTrainer->getOperations()); | ||
} | ||
|
||
public function testSearchListDomain(): void | ||
{ | ||
$this->dnsConfig = (new DnsConfig(['127.0.0.1'])) | ||
->withNdots(1) | ||
->withSearchList(['search']); | ||
|
||
$this->cacheTrainer->givenResponse(new DnsRecord('1.2.3.4', DnsRecord::A)); | ||
|
||
$this->whenResolve('foobar'); | ||
|
||
$this->assertSame([ | ||
['get', 'amphp.dns.foobar.search#1'], | ||
['get', 'amphp.dns.foobar.search#28'], | ||
], $this->cacheTrainer->getOperations()); | ||
} | ||
|
||
public function testSearchListDomainDots(): void | ||
{ | ||
$this->dnsConfig = (new DnsConfig(['127.0.0.1'])) | ||
->withNdots(1) | ||
->withSearchList(['search']); | ||
|
||
$this->cacheTrainer->givenResponse(new DnsRecord('1.2.3.4', DnsRecord::A)); | ||
|
||
$this->whenResolve('foo.bar'); | ||
|
||
$this->assertSame([ | ||
['get', 'amphp.dns.foo.bar#1'], | ||
['get', 'amphp.dns.foo.bar#28'], | ||
], $this->cacheTrainer->getOperations()); | ||
} | ||
|
||
public function testNonExistingDomain(): void | ||
{ | ||
$this->dnsConfig = new DnsConfig(['8.8.8.8:53']); | ||
|
||
$this->expectException(MissingDnsRecordException::class); | ||
$this->expectExceptionMessage('No records returned for'); | ||
|
||
$this->whenResolve('not.existing.domain.com'); | ||
} | ||
|
||
private function whenResolve(string $name, ?int $typeRestriction = null, ?Cancellation $cancellation = null): void | ||
{ | ||
$configLoader = $this->dnsConfig !== null ? new StaticDnsConfigLoader($this->dnsConfig) : null; | ||
|
||
$resolver = new Rfc1035StubDnsResolver($this->cacheTrainer->getMock(), $configLoader); | ||
$resolver->resolve($name, $typeRestriction, $cancellation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters