Skip to content

Commit

Permalink
refactor: simplify SOA serial update logic. Fixes #533
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondas committed Dec 25, 2023
1 parent 4e30f75 commit 1de6af3
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
9 changes: 4 additions & 5 deletions edit_record.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@ public function saveRecord($zid): void
}

$ret_val = $dnsRecord->edit_record($postData);
if ($ret_val == "1") {
if ($_POST['type'] != "SOA") {
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$dnsRecord->update_soa_serial($zid);
}
if ($ret_val) {
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$dnsRecord->update_soa_serial($zid);

$new_record_info = DnsRecord::get_record_from_id($this->db, $_POST["rid"]);
$this->logger->log_info(sprintf('client_ip:%s user:%s operation:edit_record'
. ' old_record_type:%s old_record:%s old_content:%s old_ttl:%s old_priority:%s'
Expand Down
6 changes: 5 additions & 1 deletion lib/Dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,11 @@ public function is_valid_rr_soa_content(mixed &$content, $dns_hostmaster): bool
$addr_input = $fields[1] ?? $dns_hostmaster;
if (!str_contains($addr_input, "@")) {
$addr_input = preg_split('/(?<!\\\)\./', $addr_input, 2);
$addr_to_check = str_replace("\\", "", $addr_input[0]) . "@" . $addr_input[1];
if (count($addr_input) == 2) {
$addr_to_check = str_replace("\\", "", $addr_input[0]) . "@" . $addr_input[1];
} else {
$addr_to_check = str_replace("\\", "", $addr_input[0]);
}
} else {
$addr_to_check = $addr_input;
}
Expand Down
75 changes: 74 additions & 1 deletion tests/DnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,83 @@

use Poweradmin\Dns;
use PHPUnit\Framework\TestCase;
use Poweradmin\LegacyConfiguration;
use Poweradmin\PDOLayer;

class DnsTest extends TestCase
{
private Dns $dnsInstance;

protected function setUp(): void
{
$dbMock = $this->createMock(PDOLayer::class);
$configMock = $this->createMock(LegacyConfiguration::class);

$this->dnsInstance = new Dns($dbMock, $configMock);
}

public function testIsValidRrSoaContentWithValidData()
{
$content = "example.com hostmaster.example.com 2023122505 7200 1209600 3600 86400";
$dns_hostmaster = "hostmaster@example.com";
$this->assertTrue($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
}

public function testIsValidRrSoaContentWithValidNumber()
{
$content = "example.com hostmaster.example.com 5 7200 1209600 3600 86400";
$dns_hostmaster = "hostmaster@example.com";
$this->assertTrue($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
}

// public function testIsValidRrSoaContentWithEmptyContent()
// {
// $content = "";
// $dns_hostmaster = "hostmaster@example.com";
// $this->assertFalse($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
// }

public function testIsValidRrSoaContentWithMoreThanSevenFields()
{
$content = "example.com hostmaster.example.com 2023122505 7200 1209600 3600 86400 extraField";
$dns_hostmaster = "hostmaster@example.com";
$this->assertFalse($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
}

public function testIsValidRrSoaContentWithLessThanSevenFields()
{
$content = "example.com hostmaster.example.com 2023122505 7200 1209600";
$dns_hostmaster = "hostmaster@example.com";
$this->assertFalse($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
}

// public function testIsValidRrSoaContentWithInvalidHostname()
// {
// $content = "invalid_hostname hostmaster.example.com 2023122505 7200 1209600 3600 86400";
// $dns_hostmaster = "hostmaster@example.com";
// $this->assertFalse($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
// }

public function testIsValidRrSoaContentWithInvalidEmail()
{
$content = "example.com invalid_email 2023122505 7200 1209600 3600 86400";
$dns_hostmaster = "invalid_email";
$this->assertFalse($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
}

public function testIsValidRrSoaContentWithNonNumericSerialNumbers()
{
$content = "example.com hostmaster.example.com not_a_number 7200 1209600 3600 86400";
$dns_hostmaster = "hostmaster@example.com";
$this->assertFalse($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
}

public function testIsValidRrSoaContentWithArpaDomain()
{
$content = "example.arpa hostmaster.example.com 2023122505 7200 1209600 3600 86400";
$dns_hostmaster = "hostmaster@example.com";
$this->assertFalse($this->dnsInstance->is_valid_rr_soa_content($content, $dns_hostmaster));
}

public function testIs_valid_ds()
{
Expand Down Expand Up @@ -61,5 +135,4 @@ public function testIs_valid_rr_prio()
$this->assertFalse(Dns::is_valid_rr_prio("foo", "A"));
$this->assertTrue(Dns::is_valid_rr_prio("0", "A"));
}

}

0 comments on commit 1de6af3

Please sign in to comment.