-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathautoload-phpunit.php
More file actions
131 lines (120 loc) · 4.69 KB
/
autoload-phpunit.php
File metadata and controls
131 lines (120 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace FediE2EE\PKDServer;
use Exception;
use FediE2EE\PKD\Extensions\ExtensionInterface;
use ParagonIE\EasyDB\EasyDB;
use ParagonIE\EasyDB\EasyDBCache;
use PDO;
use RuntimeException;
require_once __DIR__ . '/vendor/autoload.php';
function tableExists(EasyDB $db, string $tableName): bool
{
try {
return match ($db->getDriver()) {
'mysql' => $db->exists(
"SELECT COUNT(*) FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name = ?",
$tableName
),
'pgsql' => $db->exists(
"SELECT COUNT(*) FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = ?",
$tableName
),
'sqlite' => $db->exists(
"SELECT COUNT(*) FROM sqlite_master
WHERE type='table' AND name=?",
$tableName
),
default => throw new RuntimeException("Unsupported driver: {$db->getDriver()}"),
};
} catch (Exception) {
return false;
}
}
if (!isset($GLOBALS['pkdConfig'])) {
echo 'Could not load PHP config.', PHP_EOL;
exit(255);
}
if (!($GLOBALS['pkdConfig'] instanceof ServerConfig)) {
echo 'Config is not an instance of the correct class.', PHP_EOL;
exit(254);
}
(function () {
$pkdConfig = $GLOBALS['pkdConfig'];
if (!($pkdConfig instanceof ServerConfig)) {
echo 'Config is not an instance of the correct class.', PHP_EOL;
exit(253);
}
if ($pkdConfig->getDb()->getDriver() === 'sqlite') {
if (!is_dir(__DIR__ . '/tmp/db/')) {
mkdir(__DIR__ . '/tmp/db/', 0777, true);
}
$temp = __DIR__ . '/tmp/db/' . sodium_bin2hex(random_bytes(16)) . '-test.db';
$mainPDO = new PDO('sqlite:' . $temp);
$mainPDO->exec('PRAGMA jounral_mode=WAL');
$mainPDO->exec('PRAGMA busy_timeout=5000');
$pkdConfig->withDatabase(new EasyDBCache($mainPDO));
chmod($temp, 0777);
// Create second DB connection for testing concurrency
$secondPDO = new PDO('sqlite:' . $temp);
$secondPDO->exec('PRAGMA jounral_mode=WAL');
$secondPDO->exec('PRAGMA busy_timeout=5000');
$GLOBALS['PKD_PHPUNIT_DB'] = new EasyDBCache($secondPDO);
// Call cleanup-test-db.php to cleanup test file after phpunit is finished.
if (getenv('AUTO_CLEANUP_TEST_DB')) {
register_shutdown_function(function () use ($temp) {
$script = __DIR__ . '/cmd/cleanup-test-db.php';
if (DIRECTORY_SEPARATOR === '\\') {
// Windows: use start with /B (background) flag
// nosemgrep: php.lang.security.exec-use.exec-use
pclose(popen("start /B " . PHP_BINARY . " " . escapeshellarg($script) . " " . escapeshellarg($temp), "r"));
} else {
// Unix: use nohup and redirect to /dev/null
// nosemgrep: php.lang.security.exec-use.exec-use
shell_exec(PHP_BINARY . " " . escapeshellarg($script) . " " . escapeshellarg($temp) . " > /dev/null 2>&1 &");
}
});
}
} else {
// Create second DB connection for testing concurrency
$GLOBALS['PKD_PHPUNIT_DB'] = require __DIR__ . '/config/database.php';
}
$db = $GLOBALS['pkdConfig']->getDb();
if (!tableExists($db, 'pkd_merkle_state')) {
$argv_backup = $_SERVER['argv'];
$_SERVER['argv'] = [$_SERVER['argv'][0]];
require __DIR__ . '/cmd/init-database.php';
$_SERVER['argv'] = $argv_backup;
if (tableExists($db, 'pkd_replica_actors_auxdata')) {
echo 'Imported!', PHP_EOL;
} else {
// This is normally dangerous, but we need to trigger it again just to be sure:
// nosemgrep: php.lang.security.exec-use.exec-use
shell_exec(PHP_BINARY . ' ' . __DIR__ . '/cmd/init-database.php');
}
}
// Create test extension:
$testRegistry = new class implements ExtensionInterface {
public function getAuxDataType(): string
{
return 'test-v1';
}
public function getRejectionReason(): string
{
return '';
}
public function isValid(string $auxData): bool
{
return true;
}
};
$pkdConfig->getAuxDataRegistry()->addAuxDataType($testRegistry, 'test');
// Add "test" extension to allow list
$pkdConfig->withAuxDataTypeAllowList(
$pkdConfig->getAuxDataTypeAllowList() + ['test-v1', 'age-v1']
);
})();