Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Query/HostsExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php


namespace React\Dns\Query;


use React\Dns\Model\Message;
use React\Dns\Model\Record;
use React\Promise\Deferred;

class HostsExecutor implements ExecutorInterface
{
private $ipv4 = [];
private $ivp6 = [];

public function __construct($file = '/etc/hosts')
{
if (file_exists($file)) {
foreach(file($file) as $line) {
// IPV4
if (preg_match('/^(?<ip>(?:\d+\.){3}\d+)\s*(?<name>.*?)\s*$/', $line, $matches)) {
$this->add4($matches['ip'], $matches['name']);
} elseif (preg_match('/^(?<ip>(?:[:a-f0-9]+))\s*(?<name>.*?)\s*$/', $line, $matches)) {
$this->add6($matches['ip'], $matches['name']);
}
}
}
}


public function query($nameserver, Query $query)
{
$result = new Deferred();
if ($query->type === Message::TYPE_A
&& isset($this->ipv4[$query->name])) {
$response = new Message();
$response->answers[] = new Record($query->name, $query->type, $query->class, 0, $this->ipv4[$query->name]);
$result->resolve($response);
} else {
$result->reject('Name not found in hosts file.');
}
return $result->promise();
}

public function add4($address, $name)
{
$this->ipv4[$name] = $address;
}

public function add6($address, $name)
{
$this->ipv6[$name] = $address;

}
}
9 changes: 9 additions & 0 deletions tests/Fixtures/etc/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
127.0.0.1 localhost
127.0.1.1 host-pc

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
70 changes: 70 additions & 0 deletions tests/Query/HostsExecutorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace React\Tests\Dns\Query;

use React\Dns\Query\ExecutorInterface;
use React\Dns\Query\HostsExecutor;
use React\Dns\Query\Query;
use React\Dns\Model\Message;
use React\Dns\Model\Record;

class HostsExecutorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ExecutorInterface
*/
public $executor;

public function setUp()
{
$this->loop = $this->getMock('React\EventLoop\LoopInterface');
$this->executor = new HostsExecutor(__DIR__.'/../Fixtures/etc/hosts');

}

public function testRegisteredName()
{
$query = new Query('host-pc', Message::TYPE_A, Message::CLASS_IN, 1345656451);
/** @var \React\Promise\Promise$response */
$response = $this->executor->query(null, $query);

$success = $this->createCallableMock();
$success
->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf('React\Dns\Model\Message'))
;
$response->then($success, $this->expectCallableNever());

}

public function testUnRegisteredName()
{
$query = new Query('some-host', Message::TYPE_A, Message::CLASS_IN, 1345656451);
/** @var \React\Promise\Promise$response */
$response = $this->executor->query(null, $query);

$err = $this->createCallableMock();
$err
->expects($this->once())
->method('__invoke')
;
$response->then($this->expectCallableNever(), $err);

}

protected function expectCallableNever()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');

return $mock;
}

protected function createCallableMock()
{
return $this->getMock('React\Tests\Dns\CallableStub');
}
}