91 lines
3.0 KiB
PHP
91 lines
3.0 KiB
PHP
|
|
#!/usr/bin/env php
|
||
|
|
<?php
|
||
|
|
/**
|
||
|
|
* 随机域名选取与多配置绑定逻辑验证
|
||
|
|
*
|
||
|
|
* 用法:php tools/verify_domain_random.php
|
||
|
|
*/
|
||
|
|
if (PHP_SAPI !== 'cli') {
|
||
|
|
fwrite(STDERR, "请在 CLI 下运行\n");
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
require_once dirname(__DIR__) . '/cong.php';
|
||
|
|
require_once dirname(__DIR__) . '/lib/Cloak/DomainRepository.php';
|
||
|
|
|
||
|
|
$passed = 0;
|
||
|
|
$failed = 0;
|
||
|
|
|
||
|
|
function vassert($name, $cond, $detail = '')
|
||
|
|
{
|
||
|
|
global $passed, $failed;
|
||
|
|
if ($cond) {
|
||
|
|
$passed++;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
$failed++;
|
||
|
|
echo "FAIL: {$name}\n";
|
||
|
|
if ($detail !== '') {
|
||
|
|
echo " {$detail}\n";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$pdo = DomainRepository::pdo();
|
||
|
|
DomainRepository::ensureTable($pdo);
|
||
|
|
|
||
|
|
$prefix = 'verify-rand-' . substr(md5((string) microtime(true)), 0, 8) . '.test';
|
||
|
|
$hostA = 'a.' . $prefix;
|
||
|
|
$hostB = 'b.' . $prefix;
|
||
|
|
$hostBound = 'bound.' . $prefix;
|
||
|
|
|
||
|
|
$ids = [];
|
||
|
|
foreach ([$hostA, $hostB, $hostBound] as $host) {
|
||
|
|
$res = DomainRepository::add($pdo, $host);
|
||
|
|
vassert('add domain ' . $host, !empty($res['ok']), $res['error'] ?? '');
|
||
|
|
if (!empty($res['id'])) {
|
||
|
|
$ids[] = (int) $res['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (count($ids) >= 3) {
|
||
|
|
DomainRepository::bindConfigToDomain($pdo, 'cfg_one', $ids[2]);
|
||
|
|
DomainRepository::bindConfigToDomain($pdo, 'cfg_two', $ids[2]);
|
||
|
|
}
|
||
|
|
|
||
|
|
$map = DomainRepository::groupConfigsByDomainId($pdo);
|
||
|
|
vassert('多配置可共用同一域名', count($map[$ids[2]] ?? []) === 2, json_encode($map[$ids[2]] ?? []));
|
||
|
|
|
||
|
|
$picked = DomainRepository::pickRandomDomainId($pdo);
|
||
|
|
vassert('pickRandom 返回有效 id', $picked > 0, (string) $picked);
|
||
|
|
vassert('pickRandom 从全部域名中选取', in_array($picked, $ids, true), 'picked=' . $picked);
|
||
|
|
|
||
|
|
DomainRepository::bindConfigToDomain($pdo, 'cfg_three', $ids[0]);
|
||
|
|
DomainRepository::bindConfigToDomain($pdo, 'cfg_four', $ids[0]);
|
||
|
|
vassert('domainIdForConfig 读取映射', DomainRepository::domainIdForConfig($pdo, 'cfg_three') === $ids[0]);
|
||
|
|
vassert('primaryHostnameForConfig', DomainRepository::primaryHostnameForConfig($pdo, 'cfg_three') === $hostA);
|
||
|
|
|
||
|
|
DomainRepository::clearConfigBinding($pdo, 'cfg_three');
|
||
|
|
vassert('clearConfigBinding 仅清除指定配置', DomainRepository::domainIdForConfig($pdo, 'cfg_three') === 0);
|
||
|
|
vassert('clearConfigBinding 不影响同域其他配置', DomainRepository::domainIdForConfig($pdo, 'cfg_four') === $ids[0]);
|
||
|
|
|
||
|
|
foreach ($ids as $id) {
|
||
|
|
DomainRepository::deleteById($pdo, $id);
|
||
|
|
}
|
||
|
|
vassert('deleteById 级联清理映射', DomainRepository::domainIdForConfig($pdo, 'cfg_two') === 0);
|
||
|
|
vassert('cleanup', DomainRepository::findById($pdo, $ids[0]) === null);
|
||
|
|
} catch (Throwable $e) {
|
||
|
|
echo "WARN: DB test skipped — " . $e->getMessage() . "\n";
|
||
|
|
}
|
||
|
|
|
||
|
|
$all = [
|
||
|
|
['id' => 1],
|
||
|
|
['id' => 2],
|
||
|
|
['id' => 3],
|
||
|
|
];
|
||
|
|
vassert('offline 全部域名均可随机', count($all) === 3);
|
||
|
|
|
||
|
|
$result = ['ok' => $failed === 0, 'passed' => $passed, 'failed' => $failed];
|
||
|
|
echo json_encode($result, JSON_UNESCAPED_UNICODE) . "\n";
|
||
|
|
exit($failed > 0 ? 1 : 0);
|