mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-22 21:40:55 +01:00
Fix preamble-support
Summary: Phutil is not yet loaded during preamble, so we can't use it. This change fixes a regression introduced here, fixing PHP 8.1 support: {96ae4ba13acbf0e2f8932e950a92af0495f034d7} Ref T15064 Test Plan: Included test script. Reviewers: O1 Blessed Committers, valerio.bozzolan Reviewed By: O1 Blessed Committers, valerio.bozzolan Subscribers: speck, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno Maniphest Tasks: T15064 Differential Revision: https://we.phorge.it/D25114
This commit is contained in:
parent
a7b472284b
commit
9c759fe23a
2 changed files with 124 additions and 1 deletions
123
support/startup/__tests__/PreambleUtilsTestCase.php
Executable file
123
support/startup/__tests__/PreambleUtilsTestCase.php
Executable file
|
@ -0,0 +1,123 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/**
|
||||
* /startup/ is not a Phutil library, so it can't use the phutil test fixture.
|
||||
* This script will just run the tests directly.
|
||||
*
|
||||
* NOTE: This test file will not run as part of `arc unit` run!
|
||||
*/
|
||||
|
||||
|
||||
final class PreambleUtilsTestCase {
|
||||
|
||||
|
||||
public function testTrustXForwardValues() {
|
||||
// For specific values of `$_SERVER['HTTP_X_FORWARDED_FOR']`,
|
||||
// `$_SERVER['REMOTE_ADDR']` will be updated with the result.
|
||||
|
||||
|
||||
$undefined = 'SPECIAL::UNDEFINED';
|
||||
$null_value = 'SPECIAL::NULL';
|
||||
|
||||
$test_cases = array(
|
||||
'abc' => 'abc',
|
||||
$null_value => $undefined,
|
||||
'' => $undefined,
|
||||
|
||||
// Strange, unexpected cases:
|
||||
144 => '144',
|
||||
);
|
||||
|
||||
foreach ($test_cases as $input => $expected) {
|
||||
switch ($input) {
|
||||
case $undefined:
|
||||
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
|
||||
break;
|
||||
|
||||
case $null_value:
|
||||
$_SERVER['HTTP_X_FORWARDED_FOR'] = null;
|
||||
break;
|
||||
|
||||
default:
|
||||
$_SERVER['HTTP_X_FORWARDED_FOR'] = $input;
|
||||
break;
|
||||
}
|
||||
|
||||
unset($_SERVER['REMOTE_ADDR']);
|
||||
|
||||
preamble_trust_x_forwarded_for_header();
|
||||
|
||||
if (!isset($_SERVER['REMOTE_ADDR'])) {
|
||||
if ($expected === $undefined) {
|
||||
// test pass
|
||||
continue;
|
||||
} else {
|
||||
$this->failTest("Failed for input {$input} - result is not defined!");
|
||||
}
|
||||
}
|
||||
|
||||
$actual = $_SERVER['REMOTE_ADDR'];
|
||||
|
||||
if ($actual !== $expected) {
|
||||
var_dump($actual);
|
||||
|
||||
$this->failTest(
|
||||
"Failed for input {$input} - actual output is {$actual}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function failTest($message = null) {
|
||||
echo $message;
|
||||
echo "\n";
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all tests in this class.
|
||||
*
|
||||
* Return: True if all tests passed; False if any test failed.
|
||||
*/
|
||||
final public function run() {
|
||||
$reflection = new ReflectionClass($this);
|
||||
$methods = $reflection->getMethods();
|
||||
|
||||
$any_fail = false;
|
||||
|
||||
// Try to ensure that poorly-written tests which depend on execution order
|
||||
// (and are thus not properly isolated) will fail.
|
||||
shuffle($methods);
|
||||
|
||||
foreach ($methods as $method) {
|
||||
$name = $method->getName();
|
||||
if (!preg_match('/^test/', $name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
call_user_func_array(
|
||||
array($this, $name),
|
||||
array());
|
||||
echo "Test passed: {$name}\n";
|
||||
} catch (Throwable $ex) {
|
||||
$any_fail = true;
|
||||
echo "Failed test: {$name}\n";
|
||||
}
|
||||
}
|
||||
return !$any_fail;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
require_once dirname(__DIR__).'/preamble-utils.php';
|
||||
|
||||
$test_case = new PreambleUtilsTestCase();
|
||||
$good = $test_case->run();
|
||||
|
||||
if (!$good) {
|
||||
exit(3);
|
||||
}
|
|
@ -21,7 +21,7 @@ function preamble_trust_x_forwarded_for_header($layers = 1) {
|
|||
}
|
||||
|
||||
$forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
||||
if (!phutil_nonempty_string($forwarded_for)) {
|
||||
if (!strlen($forwarded_for)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue