2023-07-17 19:15:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
// Test that the current setup is working
|
2023-06-08 15:36:44 +00:00
|
|
|
|
2023-06-26 02:13:52 +00:00
|
|
|
require __DIR__ . '/../init.php';
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-14 19:07:42 +00:00
|
|
|
const CORE_URL = 'https://' . CONF['common']['public_domains'][0] . ':' . CONF['check']['https_port'];
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-14 19:07:42 +00:00
|
|
|
foreach (CONF['reg']['suffixes'] as $suffix => $openness)
|
|
|
|
if ($openness === 'all')
|
|
|
|
define('SUFFIX', $suffix);
|
|
|
|
if (!defined('SUFFIX'))
|
|
|
|
exit('Unable to start tests: no suffix open to registration');
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
$test_start = 0;
|
|
|
|
function startTest(string $test_name): void {
|
|
|
|
global $test_start;
|
|
|
|
$test_start = microtime(true);
|
|
|
|
echo 'Testing ' . $test_name . '... ';
|
|
|
|
}
|
|
|
|
function stopTest(): void {
|
|
|
|
global $test_start;
|
|
|
|
echo 'OK (' . round(microtime(true) - $test_start, 2) . 's)' . LF;
|
|
|
|
}
|
|
|
|
|
|
|
|
startTest('DNSSEC resolution');
|
2023-08-13 16:52:34 +00:00
|
|
|
if (kdig(name: 'nlnet.nl', type: 'AAAA')['AD'] !== 1)
|
|
|
|
exit('DNS queries don\'t seem to be DNSSEC-validated.' . LF);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
define('COOKIE_FILE', sys_get_temp_dir() . '/cookie-' . bin2hex(random_bytes(16)) . '.txt');
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-06-19 22:36:58 +00:00
|
|
|
function curlTest(string $address, array $post = [], bool $tor = false): string {
|
2023-03-18 17:40:04 +00:00
|
|
|
$req = curl_init();
|
2023-04-15 14:39:41 +00:00
|
|
|
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
if (str_starts_with($address, '/'))
|
|
|
|
curl_setopt_array($req, [
|
|
|
|
CURLOPT_POST => true,
|
|
|
|
CURLOPT_HEADER => true,
|
|
|
|
CURLOPT_HTTPHEADER => [
|
|
|
|
'Sec-Fetch-Site: none',
|
|
|
|
],
|
|
|
|
CURLOPT_URL => CORE_URL . $address,
|
|
|
|
CURLOPT_COOKIEFILE => COOKIE_FILE,
|
|
|
|
CURLOPT_COOKIEJAR => COOKIE_FILE,
|
|
|
|
CURLOPT_POSTFIELDS => $post,
|
|
|
|
]);
|
|
|
|
else
|
|
|
|
curl_setopt($req, CURLOPT_URL, $address);
|
|
|
|
|
|
|
|
if ($tor)
|
2023-08-14 19:07:42 +00:00
|
|
|
curl_setopt($req, CURLOPT_PROXY, CONF['check']['tor_proxy']);
|
2023-04-15 14:39:41 +00:00
|
|
|
else
|
|
|
|
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
|
|
|
$result = curl_exec($req);
|
2023-03-18 17:40:04 +00:00
|
|
|
$status_code = curl_getinfo($req, CURLINFO_RESPONSE_CODE);
|
2023-04-15 14:39:41 +00:00
|
|
|
if ($status_code >= 400 OR $result === false) {
|
2023-04-19 12:59:07 +00:00
|
|
|
var_dump($result);
|
2023-04-15 14:39:41 +00:00
|
|
|
var_dump(curl_error($req));
|
2023-06-20 00:32:36 +00:00
|
|
|
throw new Exception($address . ' test failed with status code ' . $status_code);
|
2023-03-18 17:40:04 +00:00
|
|
|
}
|
2023-04-15 14:39:41 +00:00
|
|
|
return $result;
|
2023-03-18 17:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$username = 'check-' . bin2hex(random_bytes(16));
|
|
|
|
$password = bin2hex(random_bytes(16));
|
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
startTest('account registration');
|
2023-03-18 17:40:04 +00:00
|
|
|
curlTest('/auth/register', [
|
|
|
|
'username' => $username,
|
|
|
|
'password' => $password,
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
startTest('account password change');
|
2023-03-18 17:40:04 +00:00
|
|
|
$new_password = bin2hex(random_bytes(16));
|
|
|
|
curlTest('/auth/password', [
|
|
|
|
'current-password' => $password,
|
|
|
|
'new-password' => $new_password,
|
|
|
|
]);
|
|
|
|
$password = $new_password;
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-06-20 00:32:36 +00:00
|
|
|
curlTest('/auth/register', [
|
|
|
|
'username' => $username . '2',
|
|
|
|
'password' => $password,
|
|
|
|
]);
|
2023-08-14 19:07:42 +00:00
|
|
|
curlTest('/auth/logout');
|
2023-06-20 00:32:36 +00:00
|
|
|
|
|
|
|
curlTest('/auth/login', [
|
|
|
|
'username' => $username,
|
|
|
|
'password' => $password,
|
|
|
|
]);
|
|
|
|
|
2023-03-18 17:40:04 +00:00
|
|
|
curlTest('/auth/username', [
|
|
|
|
'current-password' => $password,
|
2023-06-20 00:32:36 +00:00
|
|
|
'new-username' => $username . '3',
|
2023-03-18 17:40:04 +00:00
|
|
|
]);
|
|
|
|
curlTest('/auth/username', [
|
|
|
|
'current-password' => $password,
|
|
|
|
'new-username' => $username,
|
|
|
|
]);
|
|
|
|
|
|
|
|
echo 'Created account with username "' . $username . '" and password "' . $password . '".' . LF;
|
|
|
|
|
2023-06-19 22:36:58 +00:00
|
|
|
function testReg(): string {
|
2023-06-20 00:32:36 +00:00
|
|
|
global $username, $password;
|
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
$subdomain = bin2hex(random_bytes(16));
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
startTest('domain registration');
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/reg/register', [
|
|
|
|
'subdomain' => $subdomain,
|
|
|
|
'suffix' => SUFFIX,
|
|
|
|
'action' => 'register',
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
$domain = $subdomain . '.' . SUFFIX;
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
{
|
|
|
|
startTest('NS record writing in registry');
|
|
|
|
curlTest('/reg/ns', [
|
|
|
|
'action' => 'add',
|
|
|
|
'domain' => $domain,
|
|
|
|
'ns' => 'ns1.servnest.invalid.',
|
|
|
|
]);
|
|
|
|
stopTest();
|
|
|
|
|
|
|
|
startTest('NS record reading in registry');
|
|
|
|
$results = kdig(name: $domain, type: 'NS', server: CONF['reg']['address']);
|
|
|
|
if (($results['authorityRRs'][0]['rdataNS'] ?? NULL) !== 'ns1.servnest.invalid.')
|
|
|
|
exit('Error: /reg/ns: NS record not set' . LF);
|
|
|
|
stopTest();
|
|
|
|
}
|
2023-04-15 14:39:41 +00:00
|
|
|
|
|
|
|
curlTest('/reg/ns', [
|
|
|
|
'action' => 'delete',
|
|
|
|
'domain' => $domain,
|
|
|
|
'ns' => 'ns1.servnest.invalid.',
|
|
|
|
]);
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-06-20 00:32:36 +00:00
|
|
|
{ // Domain transfer
|
2023-08-18 01:04:47 +00:00
|
|
|
startTest('domain transfer procedure');
|
2023-08-14 19:07:42 +00:00
|
|
|
curlTest('/auth/logout');
|
2023-06-20 00:32:36 +00:00
|
|
|
curlTest('/auth/login', [
|
|
|
|
'username' => $username . '2',
|
|
|
|
'password' => $password,
|
|
|
|
]);
|
2023-08-14 19:07:42 +00:00
|
|
|
preg_match('#\<code\>(?<token>[0-9a-z-]{16,128}\._transfer-verification\.' . preg_quote(CONF['common']['public_domains'][0], '#') . '\.)\</code\>#', curlTest('/reg/transfer'), $matches);
|
2023-06-20 00:32:36 +00:00
|
|
|
|
2023-08-14 19:07:42 +00:00
|
|
|
curlTest('/auth/logout');
|
2023-06-20 00:32:36 +00:00
|
|
|
curlTest('/auth/login', [
|
|
|
|
'username' => $username,
|
|
|
|
'password' => $password,
|
|
|
|
]);
|
|
|
|
curlTest('/reg/ns', [
|
|
|
|
'action' => 'add',
|
|
|
|
'domain' => $domain,
|
|
|
|
'ns' => $matches['token'],
|
|
|
|
]);
|
|
|
|
|
2023-08-14 19:07:42 +00:00
|
|
|
curlTest('/auth/logout');
|
2023-06-20 00:32:36 +00:00
|
|
|
curlTest('/auth/login', [
|
|
|
|
'username' => $username . '2',
|
|
|
|
'password' => $password,
|
|
|
|
]);
|
|
|
|
curlTest('/reg/transfer', [
|
|
|
|
'subdomain' => $subdomain,
|
|
|
|
'suffix' => SUFFIX,
|
|
|
|
'ns' => $matches['token'],
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-06-20 23:23:19 +00:00
|
|
|
|
|
|
|
$username = $username . '2';
|
2023-06-20 00:32:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
return $domain;
|
|
|
|
}
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-06-19 22:36:58 +00:00
|
|
|
function testNs(string $domain): void {
|
2023-04-15 14:39:41 +00:00
|
|
|
foreach (CONF['ns']['servers'] as $ns)
|
|
|
|
curlTest('/reg/ns', [
|
|
|
|
'action' => 'add',
|
|
|
|
'domain' => $domain,
|
|
|
|
'ns' => $ns,
|
|
|
|
]);
|
|
|
|
|
2023-08-14 19:07:42 +00:00
|
|
|
preg_match('#\<code\>(?<token>[0-9a-z-]{16,128}\._domain-verification\.' . preg_quote(CONF['common']['public_domains'][0], '#') . '\.)\</code\>#', curlTest('/ns/zone-add'), $matches);
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/reg/ns', [
|
|
|
|
'action' => 'add',
|
|
|
|
'domain' => $domain,
|
|
|
|
'ns' => $matches['token'],
|
|
|
|
]);
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
startTest('/ns/zone-add');
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/ns/zone-add', [
|
|
|
|
'domain' => $domain,
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/reg/ns', [
|
|
|
|
'action' => 'delete',
|
|
|
|
'domain' => $domain,
|
|
|
|
'ns' => $matches['token'],
|
|
|
|
]);
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
{
|
|
|
|
startTest('/ns/caa writing');
|
|
|
|
curlTest('/ns/caa', [
|
|
|
|
'action' => 'add',
|
|
|
|
'subdomain' => '@',
|
|
|
|
'zone' => $domain,
|
|
|
|
'ttl-value' => '2',
|
|
|
|
'ttl-multiplier' => '3600',
|
|
|
|
'flag' => '0',
|
|
|
|
'tag' => 'issue',
|
|
|
|
'value' => 'letsencrypt.org',
|
|
|
|
]);
|
|
|
|
stopTest();
|
|
|
|
|
|
|
|
startTest('/ns/caa reading');
|
|
|
|
$results = kdig(name: $domain, type: 'CAA', server: CONF['reg']['address']);
|
|
|
|
if (($results['answerRRs'][0]['TTL'] ?? NULL) !== 7200)
|
|
|
|
exit('Error: /ns/caa: wrong TTL' . LF);
|
|
|
|
if (($results['answerRRs'][0]['rdataCAA'] ?? NULL) !== '0 issue "letsencrypt.org" ')
|
|
|
|
exit('Error: /ns/caa: CAA record not set' . LF);
|
|
|
|
stopTest();
|
|
|
|
}
|
2023-04-15 14:39:41 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
{
|
|
|
|
startTest('/ns/edit writing');
|
|
|
|
curlTest('/ns/edit', [
|
|
|
|
'domain' => $domain,
|
|
|
|
'records' => 'aaaa.' . $domain . ' 3600 AAAA ' . CONF['ht']['ipv6_address'] . "\r\n"
|
|
|
|
. '@ 86400 NS ' . CONF['ns']['servers'][0] . "\r\n",
|
|
|
|
]);
|
|
|
|
stopTest();
|
|
|
|
|
|
|
|
startTest('/ns/edit reading');
|
|
|
|
$results = kdig(name: 'aaaa.' . $domain, type: 'AAAA', server: CONF['reg']['address']);
|
|
|
|
if (($results['answerRRs'][0]['rdataAAAA'] ?? NULL) !== CONF['ht']['ipv6_address'])
|
|
|
|
exit('Error: /ns/edit: AAAA record not set' . LF);
|
|
|
|
stopTest();
|
|
|
|
}
|
2023-04-15 14:39:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 22:36:58 +00:00
|
|
|
function testHt(string $username, string $password): void {
|
2023-08-18 01:04:47 +00:00
|
|
|
startTest('SFTP file upload');
|
2023-06-02 21:12:59 +00:00
|
|
|
define('TEST_CONTENT', 'test-' . bin2hex(random_bytes(16)));
|
2023-04-15 14:39:41 +00:00
|
|
|
|
|
|
|
file_put_contents(sys_get_temp_dir() . '/index.html', TEST_CONTENT);
|
|
|
|
|
|
|
|
file_put_contents(sys_get_temp_dir() . '/exec.txt', 'mkdir /_site0-
|
2023-03-18 17:40:04 +00:00
|
|
|
put ' . sys_get_temp_dir() . '/index.html /_site0-/index.html
|
|
|
|
exit
|
|
|
|
');
|
|
|
|
|
2023-08-14 19:07:42 +00:00
|
|
|
$process = proc_open(CONF['check']['sshpass_path'] . ' ' . CONF['check']['sftp_path'] . ' -o BatchMode=no -b ' . sys_get_temp_dir() . '/exec.txt -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P ' . CONF['ht']['public_sftp_port'] . ' ' . $username . '@' . CONF['ht']['sftp_domain'], [0 => ['pipe', 'r']], $pipes);
|
2023-04-15 14:39:41 +00:00
|
|
|
if (is_resource($process) !== true)
|
|
|
|
exit('Can\'t spawn sftp with sshpass.' . LF);
|
|
|
|
fwrite($pipes[0], $password);
|
|
|
|
fclose($pipes[0]);
|
|
|
|
if (proc_close($process) !== 0)
|
|
|
|
exit('File not sent successfully.' . LF);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-04-15 14:39:41 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
$ht_subpath = bin2hex(random_bytes(16));
|
2023-08-18 01:04:47 +00:00
|
|
|
|
|
|
|
startTest('subpath site creation');
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/ht/add-subpath', [
|
|
|
|
'path' => $ht_subpath,
|
|
|
|
'dir' => '_site0-',
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
|
|
|
|
|
|
|
startTest('subpath site reachability');
|
2023-08-18 01:10:30 +00:00
|
|
|
if (curlTest('https://' . CONF['ht']['subpath_domain'] . ':' . CONF['check']['https_port'] . '/' . $ht_subpath . '/') !== TEST_CONTENT)
|
2023-04-15 14:39:41 +00:00
|
|
|
exit('Unexpected subpath response' . LF);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
|
|
|
|
|
|
|
startTest('subpath site deletion');
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/ht/del', [
|
|
|
|
'site' => 'subpath:' . $ht_subpath,
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-04-15 14:39:41 +00:00
|
|
|
}
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
{
|
2023-08-18 13:10:49 +00:00
|
|
|
$ht_subdomain = bin2hex(random_bytes(16));
|
2023-08-18 01:04:47 +00:00
|
|
|
|
|
|
|
startTest('subdomain site creation');
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/ht/add-subdomain', [
|
|
|
|
'subdomain' => $ht_subdomain,
|
|
|
|
'dir' => '_site0-',
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
|
|
|
|
|
|
|
startTest('subdomain site reachability');
|
2023-08-18 01:10:30 +00:00
|
|
|
if (curlTest('https://' . $ht_subdomain . '.' . CONF['ht']['subpath_domain'] . ':' . CONF['check']['https_port'] . '/') !== TEST_CONTENT)
|
2023-04-15 14:39:41 +00:00
|
|
|
exit('Unexpected subpath response' . LF);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
|
|
|
|
|
|
|
startTest('subdomain site deletion');
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/ht/del', [
|
|
|
|
'site' => 'subdomain:' . $ht_subdomain,
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-04-15 14:39:41 +00:00
|
|
|
}
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
{
|
2023-08-18 01:04:47 +00:00
|
|
|
startTest('onion site creation');
|
2023-04-15 14:39:41 +00:00
|
|
|
$html = curlTest('/ht/add-onion', [
|
|
|
|
'dir' => '_site0-',
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
if (preg_match('#\<code\>http\://(?<onion>[0-9a-z]{56})\.onion/\</code\>#D', $html, $matches) !== 1)
|
|
|
|
exit('Can\'t find onion address.' . LF);
|
2023-04-19 12:59:07 +00:00
|
|
|
sleep(5); // Onion services are not immediately reachable
|
2023-08-18 01:04:47 +00:00
|
|
|
|
|
|
|
startTest('onion site reachability');
|
2023-04-15 14:39:41 +00:00
|
|
|
if (curlTest('http://' . $matches['onion'] . '.onion/', tor: true) !== TEST_CONTENT)
|
|
|
|
exit('Unexpected onion service response (' . $matches['onion'] . '.onion)' . LF);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
|
|
|
|
|
|
|
startTest('onion site deletion');
|
2023-04-15 14:39:41 +00:00
|
|
|
curlTest('/ht/del', [
|
|
|
|
'site' => 'onion:' . $matches['onion'] . '.onion',
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-04-15 14:39:41 +00:00
|
|
|
}
|
2023-03-18 17:40:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:39:41 +00:00
|
|
|
$domain = testReg();
|
|
|
|
testNs($domain);
|
|
|
|
testHt($username, $password);
|
2023-03-18 17:40:04 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
startTest('account deletion');
|
2023-03-18 17:40:04 +00:00
|
|
|
curlTest('/auth/unregister', [
|
|
|
|
'current-password' => $password,
|
|
|
|
'delete' => 'on',
|
|
|
|
]);
|
2023-08-18 01:04:47 +00:00
|
|
|
stopTest();
|
2023-03-18 17:40:04 +00:00
|
|
|
|
|
|
|
unlink(COOKIE_FILE);
|
2023-04-11 00:43:16 +00:00
|
|
|
|
2023-08-18 01:04:47 +00:00
|
|
|
echo 'All tests succeeded! 🎉' . LF;
|