Skip to content

Commit

Permalink
Merge pull request #26 from skirmis/feature/add-caa-type
Browse files Browse the repository at this point in the history
Rdata: add new CAA record type and unit test
  • Loading branch information
samuelwilliams authored Feb 18, 2019
2 parents 7e50f58 + d0a36a7 commit 546b840
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
104 changes: 104 additions & 0 deletions lib/Rdata/CAA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* This file is part of Badcow DNS Library.
*
* (c) Samuel Williams <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Badcow\DNS\Rdata;

/**
* Class CaaRdata.
*
* CAA is defined in RFC 6844
*
* @see https://tools.ietf.org/html/rfc6844
*
* @author Samuel Williams <[email protected]>
*/
class CAA extends CNAME
{
const TYPE = 'CAA';

const MAX_FLAG = 255;

const ALLOWED_TAGS = ['issue', 'issuewild', 'iodef'];

/**
* It is currently used to represent the critical flag
*
* @var int
*/
private $flag;

/**
* An ASCII string that represents the identifier of the property represented by the record.
* The RFC currently defines 3 available tags:
* - issue: explicity authorizes a single certificate authority to issue a certificate (any type) for the hostname.
* - issuewild: explicity authorizes a single certificate authority to issue a wildcard certificate (and only wildcard) for the hostname.
* - iodef: specifies a URL to which a certificate authority may report policy violations.
*
* @var string
*/
private $tag;

/**
* @return int
*/
public function getFlag(): ?int
{
return $this->flag;
}

/**
* @param int $flag
*
* @throws \InvalidArgumentException
*/
public function setFlag(int $flag): void
{
if ($flag < 0 || $flag > static::MAX_FLAG) {
throw new \InvalidArgumentException('Flag must be an unsigned integer on the range [0-255]');
}

$this->flag = $flag;
}

/**
* @return int
*/
public function getTag(): ?string
{
return $this->tag;
}

/**
* @param int $tag
*
* @throws \InvalidArgumentException
*/
public function setTag(string $tag): void
{
if (!in_array($tag, static::ALLOWED_TAGS)) {
throw new \InvalidArgumentException('Tag can be one of this type '.implode(' ', static::ALLOWED_TAGS));
}

$this->tag = $tag;
}

/**
* {@inheritdoc}
*/
public function output(): string
{
return sprintf('%s %s "%s"',
$this->flag,
$this->tag,
$this->target
);
}
}
17 changes: 17 additions & 0 deletions lib/Rdata/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,21 @@ public static function Apl(array $includedRanges = [], array $excludedRanges = [

return $rdata;
}

/**
* @param integer $flag
* @param string $tag
* @param string $target
*
* @return CAA
*/
public static function Caa($flag, $tag, $target)
{
$rdata = new CAA();
$rdata->setFlag($flag);
$rdata->setTag($tag);
$rdata->setTarget($target);

return $rdata;
}
}
47 changes: 47 additions & 0 deletions lib/Tests/Rdata/CaaRdataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of Badcow DNS Library.
*
* (c) Samuel Williams <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Badcow\DNS\Tests\Rdata;

use Badcow\DNS\Rdata\CAA;
use Badcow\DNS\Rdata\Factory;

class CaaRdataTest extends \PHPUnit\Framework\TestCase
{
public function testOutput()
{
$caa = Factory::Caa(0, 'issue', 'letsencrypt.org');

$expectation = '0 issue "letsencrypt.org"';

$this->assertEquals($expectation, $caa->output());
$this->assertEquals(0, $caa->getFlag());
$this->assertEquals('issue', $caa->getTag());
}

/**
* @expectedException \InvalidArgumentException
*/
public function testFlagException()
{
$srv = new CAA();
$srv->setFlag(CAA::MAX_FLAG + 1);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testTagException()
{
$srv = new CAA();
$srv->setTag('not_exist');
}
}

0 comments on commit 546b840

Please sign in to comment.