-
Notifications
You must be signed in to change notification settings - Fork 47
/
test.php
74 lines (67 loc) · 2.18 KB
/
test.php
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
<?php
require 'vendor/autoload.php';
$virtualPath = '/var/www/html/testcase.php';
error_reporting(E_ALL);
$testdir = dirname(__FILE__) . '/tests';
$d = opendir($testdir);
while ($testfile = readdir($d)) {
if ($testfile === '.' || $testfile === '..') {
continue;
}
$f = fopen($testdir . '/' . $testfile, 'r');
if (!$f) {
exit(1);
}
$tests = array();
$curTest = array('input' => array(), 'output' => array());
$lines = null;
while (!feof($f)) {
$line = fgets($f);
if (trim($line) === 'INPUT') {
if ($lines !== null) {
$tests[] = $curTest;
$curTest = array('input' => array(), 'output' => array());
}
$lines = &$curTest['input'];
continue;
} elseif (trim($line) === 'OUTPUT') {
$lines = &$curTest['output'];
continue;
}
if ($lines !== null) {
$lines[] = $line;
}
}
if ($lines !== null) {
$tests[] = $curTest;
}
fclose($f);
foreach ($tests as $i => $test) {
$name = $testfile . '/' . ($i + 1);
$code = "<?php\n" . trim(implode('', $test['input']));
$deobf = new \PHPDeobfuscator\Deobfuscator();
$deobf->getFilesystem()->write($virtualPath, $code);
$deobf->setCurrentFilename($virtualPath);
try {
$out = $deobf->prettyPrint($deobf->deobfuscate($deobf->parse($code)));
} catch (\Exception | \Error $e) {
echo "Test $name failed:\n";
echo "Exception: " . $e->getMessage() . "\n";
echo $e->getTraceAsString() . "\n";
continue;
}
$expect = "<?php\n\n" . trim(implode('', $test['output']));
if ($out !== $expect) {
echo "Test $name failed:\n";
echo "Expected:\n";
echo implode("\n", array_map(function($l) { return "[]: $l"; }, explode("\n", $expect)));
echo "\n";
echo "Got:\n";
echo implode("\n", array_map(function($l) { return "[]: $l"; }, explode("\n", $out)));
echo "\n";
} else {
echo "Test $name pass\n";
}
}
}
closedir($d);