Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/DNS/Message/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

final readonly class Question
{
public string $name;

public function __construct(
public string $name,
string $name,
public int $type,
public int $class = Record::CLASS_IN
) {
$this->name = strtolower($name);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/DNS/Message/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Utopia\DNS\Exception\DecodingException;

/**
* A DNS record.
*/
final readonly class Record
{
public string $name;
Expand Down Expand Up @@ -95,6 +98,18 @@
self::TYPE_CAA => 'CAA',
];

/**
* Creates a DNS record.
*
* @param string $name Domain names are absolute, lowercase and without a trailing dot.
* @param int $type Record type. One of `Record::TYPE_*` constants.
* @param int $class Record class. One of `Record::CLASS_*` constants. Defaults to `Record::CLASS_IN`.
* @param int $ttl Time to live in seconds. Defaults to 0.
* @param string $rdata Record data.
* @param int|null $priority Priority. Used for MX and SRV records.
* @param int|null $weight Weight. Used for SRV records.
* @param int|null $port Port. Used for SRV records.
*/
public function __construct(
string $name,
public int $type,
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/DNS/Message/QuestionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@

final class QuestionTest extends TestCase
{
public function testConstructorSetsName(): void
{
$question = new Question('www.example.com', Record::TYPE_A, Record::CLASS_IN);

$this->assertSame('www.example.com', $question->name);
}

public function testConstructorSetsNameCaseInsensitive(): void
{
$question = new Question('WWW.EXAMPLE.COM', Record::TYPE_A, Record::CLASS_IN);

$this->assertSame('www.example.com', $question->name);
}

public function testEncodeProducesExactBytes(): void
{
$question = new Question('www.example.com', Record::TYPE_A, Record::CLASS_IN);
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/DNS/Zone/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,47 @@ public function testImportExportRoundTripForAaaa(): void
$this->assertSame($zone->records[0]->rdata, $roundTrip->records[0]->rdata);
}

public function testCanExportZoneWithTemplateRecords(): void
{
$soa = new Record(
'example.com',
Record::TYPE_SOA,
ttl: 3600,
rdata: 'ns1.example.com hostmaster.example.com 1 7200 3600 1209600 300'
);
$records = [
new Record('api.example.com', Record::TYPE_A, ttl: 120, rdata: 'a.a.a.a'),
new Record('api.example.com', Record::TYPE_A, ttl: 120, rdata: 'b:b::b:b:b'),
];

$zone = new Zone('example.com', $records, $soa);
$this->assertInstanceOf(Zone::class, $zone);

$contents = File::export($zone);

$this->assertStringContainsString('a.a.a.a', $contents);
$this->assertStringContainsString('b:b::b:b:b', $contents);
}
Comment thread
loks0n marked this conversation as resolved.

public function testCanImportZoneWithTemplateRecords(): void
{
$contents = sprintf(
<<<'ZONE'
$ORIGIN example.com.
%s
www 600 IN AAAA b:b::b:b:b
ZONE,
self::DEFAULT_SOA
);

$zone = File::import($contents);

$this->assertInstanceOf(Zone::class, $zone);
$this->assertCount(1, $zone->records);
$this->assertSame('www.example.com', $zone->records[0]->name);
$this->assertSame('b:b::b:b:b', $zone->records[0]->rdata);
}

public function testImportExportRoundTripForCaa(): void
{
$contents = sprintf(
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/DNS/ZoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,23 @@ public function testConstructorAcceptsNestedWildcardRecord(): void
$this->assertInstanceOf(Zone::class, $zone);
$this->assertCount(2, $zone->records);
}

public function testConstructorAcceptsTemplateRecords(): void
{
$soa = new Record(
'example.com',
Record::TYPE_SOA,
ttl: 3600,
rdata: 'ns1.example.com hostmaster.example.com 1 7200 3600 1209600 300'
);
$records = [
new Record('api.example.com', Record::TYPE_A, ttl: 120, rdata: 'a.a.a.a'),
new Record('api.example.com', Record::TYPE_AAAA, ttl: 120, rdata: 'b:b::b:b:b'),
];

$zone = new Zone('example.com', $records, $soa);

$this->assertInstanceOf(Zone::class, $zone);
$this->assertCount(2, $zone->records);
}
Comment thread
loks0n marked this conversation as resolved.
}