2016-12-30 04:16:14 +01:00
|
|
|
<?php
|
2014-07-19 15:37:48 +02:00
|
|
|
|
2014-09-24 19:31:27 +02:00
|
|
|
namespace Amp\Dns\Test;
|
2014-09-23 22:47:55 +02:00
|
|
|
|
2017-06-17 12:30:38 +02:00
|
|
|
use Amp\Dns;
|
|
|
|
use Amp\Dns\Record;
|
2017-03-17 05:01:58 +01:00
|
|
|
use Amp\Loop;
|
2017-06-17 10:49:54 +02:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2016-12-30 04:17:07 +01:00
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
class IntegrationTest extends TestCase
|
|
|
|
{
|
2014-07-19 15:37:48 +02:00
|
|
|
/**
|
2017-06-23 13:14:51 +02:00
|
|
|
* @param string $hostname
|
2014-07-19 15:37:48 +02:00
|
|
|
* @group internet
|
2017-01-25 16:25:41 +01:00
|
|
|
* @dataProvider provideHostnames
|
2014-07-19 15:37:48 +02:00
|
|
|
*/
|
2019-01-25 02:27:47 +01:00
|
|
|
public function testResolve($hostname)
|
|
|
|
{
|
2017-03-17 05:01:58 +01:00
|
|
|
Loop::run(function () use ($hostname) {
|
2017-06-17 12:30:38 +02:00
|
|
|
$result = yield Dns\resolve($hostname);
|
|
|
|
|
2017-06-22 19:22:45 +02:00
|
|
|
/** @var Record $record */
|
|
|
|
$record = $result[0];
|
|
|
|
$inAddr = @\inet_pton($record->getValue());
|
2017-01-25 16:25:41 +01:00
|
|
|
$this->assertNotFalse(
|
|
|
|
$inAddr,
|
|
|
|
"Server name $hostname did not resolve to a valid IP address"
|
|
|
|
);
|
2017-03-17 05:01:58 +01:00
|
|
|
});
|
2017-01-25 16:25:41 +01:00
|
|
|
}
|
2015-08-02 04:18:44 +02:00
|
|
|
|
2017-06-23 18:43:04 +02:00
|
|
|
/**
|
|
|
|
* @group internet
|
|
|
|
*/
|
2019-01-25 02:27:47 +01:00
|
|
|
public function testWorksAfterConfigReload()
|
|
|
|
{
|
2017-06-23 18:43:04 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
yield Dns\query("google.com", Record::A);
|
|
|
|
$this->assertNull(yield Dns\resolver()->reloadConfig());
|
|
|
|
$this->assertInternalType("array", yield Dns\query("example.com", Record::A));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public function testResolveIPv4only()
|
|
|
|
{
|
2017-06-23 18:35:48 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$records = yield Dns\resolve("google.com", Record::A);
|
|
|
|
|
|
|
|
/** @var Record $record */
|
|
|
|
foreach ($records as $record) {
|
|
|
|
$this->assertSame(Record::A, $record->getType());
|
|
|
|
$inAddr = @\inet_pton($record->getValue());
|
|
|
|
$this->assertNotFalse(
|
|
|
|
$inAddr,
|
|
|
|
"Server name google.com did not resolve to a valid IP address"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public function testResolveIPv6only()
|
|
|
|
{
|
2017-06-23 18:35:48 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$records = yield Dns\resolve("google.com", Record::AAAA);
|
|
|
|
|
|
|
|
/** @var Record $record */
|
|
|
|
foreach ($records as $record) {
|
|
|
|
$this->assertSame(Record::AAAA, $record->getType());
|
|
|
|
$inAddr = @\inet_pton($record->getValue());
|
|
|
|
$this->assertNotFalse(
|
|
|
|
$inAddr,
|
|
|
|
"Server name google.com did not resolve to a valid IP address"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public function testPtrLookup()
|
|
|
|
{
|
2017-06-13 19:28:06 +02:00
|
|
|
Loop::run(function () {
|
2017-06-22 20:08:27 +02:00
|
|
|
$result = yield Dns\query("8.8.4.4", Record::PTR);
|
2017-06-17 12:30:38 +02:00
|
|
|
|
2017-06-22 19:22:45 +02:00
|
|
|
/** @var Record $record */
|
|
|
|
$record = $result[0];
|
|
|
|
$this->assertSame("google-public-dns-b.google.com", $record->getValue());
|
2017-06-23 18:26:42 +02:00
|
|
|
$this->assertNotNull($record->getTtl());
|
2017-06-22 20:08:27 +02:00
|
|
|
$this->assertSame(Record::PTR, $record->getType());
|
2017-02-05 14:58:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-23 17:48:03 +02:00
|
|
|
/**
|
|
|
|
* Test that two concurrent requests to the same resource share the same request and do not result in two requests
|
|
|
|
* being sent.
|
|
|
|
*/
|
2019-01-25 02:27:47 +01:00
|
|
|
public function testRequestSharing()
|
|
|
|
{
|
2017-06-23 17:48:03 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$promise1 = Dns\query("example.com", Record::A);
|
|
|
|
$promise2 = Dns\query("example.com", Record::A);
|
|
|
|
|
|
|
|
$this->assertSame($promise1, $promise2);
|
|
|
|
$this->assertSame(yield $promise1, yield $promise2);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public function provideHostnames()
|
|
|
|
{
|
2017-01-25 16:25:41 +01:00
|
|
|
return [
|
|
|
|
["google.com"],
|
|
|
|
["github.com"],
|
|
|
|
["stackoverflow.com"],
|
2017-06-23 13:16:55 +02:00
|
|
|
["blog.kelunik.com"], /* that's a CNAME to GH pages */
|
2017-01-25 16:25:41 +01:00
|
|
|
["localhost"],
|
|
|
|
["192.168.0.1"],
|
|
|
|
["::1"],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-01-25 02:27:47 +01:00
|
|
|
public function provideServers()
|
|
|
|
{
|
2017-01-25 16:25:41 +01:00
|
|
|
return [
|
|
|
|
["8.8.8.8"],
|
|
|
|
["8.8.8.8:53"],
|
|
|
|
];
|
|
|
|
}
|
2014-07-19 15:37:48 +02:00
|
|
|
}
|