@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 123 lines 2.7 kB view raw
1#!/usr/bin/env php 2<?php 3 4/** 5 * /startup/ is not a Phutil library, so it can't use the phutil test fixture. 6 * This script will just run the tests directly. 7 * 8 * NOTE: This test file will not run as part of `arc unit` run! 9 */ 10 11 12final class PreambleUtilsTestCase { 13 14 15 public function testTrustXForwardValues() { 16 // For specific values of `$_SERVER['HTTP_X_FORWARDED_FOR']`, 17 // `$_SERVER['REMOTE_ADDR']` will be updated with the result. 18 19 20 $undefined = 'SPECIAL::UNDEFINED'; 21 $null_value = 'SPECIAL::NULL'; 22 23 $test_cases = array( 24 'abc' => 'abc', 25 $null_value => $undefined, 26 '' => $undefined, 27 28 // Strange, unexpected cases: 29 144 => '144', 30 ); 31 32 foreach ($test_cases as $input => $expected) { 33 switch ($input) { 34 case $undefined: 35 unset($_SERVER['HTTP_X_FORWARDED_FOR']); 36 break; 37 38 case $null_value: 39 $_SERVER['HTTP_X_FORWARDED_FOR'] = null; 40 break; 41 42 default: 43 $_SERVER['HTTP_X_FORWARDED_FOR'] = $input; 44 break; 45 } 46 47 unset($_SERVER['REMOTE_ADDR']); 48 49 preamble_trust_x_forwarded_for_header(); 50 51 if (!isset($_SERVER['REMOTE_ADDR'])) { 52 if ($expected === $undefined) { 53 // test pass 54 continue; 55 } else { 56 $this->failTest("Failed for input {$input} - result is not defined!"); 57 } 58 } 59 60 $actual = $_SERVER['REMOTE_ADDR']; 61 62 if ($actual !== $expected) { 63 var_dump($actual); 64 65 $this->failTest( 66 "Failed for input {$input} - actual output is {$actual}"); 67 } 68 69 } 70 71 } 72 73 74 private function failTest($message = null) { 75 echo $message; 76 echo "\n"; 77 throw new Exception(); 78 } 79 80 /** 81 * Run all tests in this class. 82 * 83 * Return: True if all tests passed; False if any test failed. 84 */ 85 final public function run() { 86 $reflection = new ReflectionClass($this); 87 $methods = $reflection->getMethods(); 88 89 $any_fail = false; 90 91 // Try to ensure that poorly-written tests which depend on execution order 92 // (and are thus not properly isolated) will fail. 93 shuffle($methods); 94 95 foreach ($methods as $method) { 96 $name = $method->getName(); 97 if (!preg_match('/^test/', $name)) { 98 continue; 99 } 100 101 try { 102 call_user_func_array( 103 array($this, $name), 104 array()); 105 echo "Test passed: {$name}\n"; 106 } catch (Throwable $ex) { 107 $any_fail = true; 108 echo "Failed test: {$name}\n"; 109 } 110 } 111 return !$any_fail; 112 } 113 114} 115 116require_once dirname(__DIR__).'/preamble-utils.php'; 117 118$test_case = new PreambleUtilsTestCase(); 119$good = $test_case->run(); 120 121if (!$good) { 122 exit(3); 123}