#!/usr/bin/env php
<?php
require_once(__DIR__ . '/../web-files/libs/dns_record_validator.php');

$ok = true;
function expect($label, $cond) { global $ok; if (!$cond) { $ok = false; fprintf(STDERR, "FAIL %s\n", $label); } }

// valid type passes; junk type fails
expect('type-ok',    dns_record_validate(['type'=>'A','name'=>'@','content'=>'1.2.3.4','ttl'=>300])['ok'] === true);
expect('type-bad',   dns_record_validate(['type'=>'WAT','name'=>'@','content'=>'x','ttl'=>300])['ok'] === false);

// MX requires numeric priority
expect('mx-noprio',  dns_record_validate(['type'=>'MX','name'=>'@','content'=>'mail.x.com.','ttl'=>300,'priority'=>null])['ok'] === false);
expect('mx-prio',    dns_record_validate(['type'=>'MX','name'=>'@','content'=>'mail.x.com.','ttl'=>300,'priority'=>10])['ok'] === true);

// TTL clamps into [300, 86400]
expect('ttl-low',    dns_record_normalize_ttl(50) === 300);
expect('ttl-high',   dns_record_normalize_ttl(999999) === 86400);
expect('ttl-mid',    dns_record_normalize_ttl(3600) === 3600);

// TXT auto-quote
expect('txt-quote',  dns_record_quote_txt('v=spf1 ~all') === '"v=spf1 ~all"');
expect('txt-noreq',  dns_record_quote_txt('"already"') === '"already"');

// MX content line builder includes priority
expect('mx-line',    dns_record_content_line('MX', 'mail.x.com.', 10) === '10 mail.x.com.');
expect('a-line',     dns_record_content_line('A', '1.2.3.4', null) === '1.2.3.4');

// SRV requires a numeric priority and recombines it into the content line
expect('srv-prio',   dns_record_validate(['type'=>'SRV','name'=>'_sip._tcp','content'=>'20 443 sip.x.com.','ttl'=>300,'priority'=>10])['ok'] === true);
expect('srv-noprio', dns_record_validate(['type'=>'SRV','name'=>'_sip._tcp','content'=>'20 443 sip.x.com.','ttl'=>300,'priority'=>null])['ok'] === false);
expect('srv-line',   dns_record_content_line('SRV', '20 443 sip.x.com.', 10) === '10 20 443 sip.x.com.');

echo $ok ? "dns_record_validator OK\n" : "dns_record_validator FAIL\n";
exit($ok ? 0 : 1);
