From aecf8ea39662d55f2b756018b53a99ac1213c8d7 Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Mon, 15 Aug 2011 20:16:15 +0000 Subject: [PATCH] Tests for wfGetIP() follow up r89407 * wfGetIP() now support resetting its internal static variable. Thanks to Platonides which introduced this trick with r92960. * Various tests for $_SERVER['REMOTE_ADDR'] and $wgCommandLineMode. TODO: - implements tests for XFF headers. TEST PLAN: $ ./phpunit.php --filter wfGetIP --testdox PHPUnit 3.5.14 by Sebastian Bergmann. wfGetIP [x] Get loopback address when in command line [x] Get from server remote addr [x] Lack of remote addr throw an exception $ --- includes/ProxyTools.php | 8 +- tests/phpunit/includes/ProxyTools/README | 2 + .../includes/ProxyTools/wfGetIPTest.php | 79 +++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/includes/ProxyTools/README create mode 100644 tests/phpunit/includes/ProxyTools/wfGetIPTest.php diff --git a/includes/ProxyTools.php b/includes/ProxyTools.php index e68729fb52..f9671d9e60 100644 --- a/includes/ProxyTools.php +++ b/includes/ProxyTools.php @@ -64,12 +64,18 @@ function wfGetAgent() { /** * Work out the IP address based on various globals * For trusted proxies, use the XFF client IP (first of the chain) + * @param $reset boolean Used to reset the internal static variable + * tracking the IP address. (default: false) * @return string */ -function wfGetIP() { +function wfGetIP( $reset = false ) { global $wgUsePrivateIPs, $wgCommandLineMode; static $ip = false; + if( $reset ) { + $ip = false; + } + # Return cached result if ( !empty( $ip ) ) { return $ip; diff --git a/tests/phpunit/includes/ProxyTools/README b/tests/phpunit/includes/ProxyTools/README new file mode 100644 index 0000000000..20b29bfd03 --- /dev/null +++ b/tests/phpunit/includes/ProxyTools/README @@ -0,0 +1,2 @@ +This directory hold tests for includes/ProxyTools.php file +which is a pile of functions. diff --git a/tests/phpunit/includes/ProxyTools/wfGetIPTest.php b/tests/phpunit/includes/ProxyTools/wfGetIPTest.php new file mode 100644 index 0000000000..007af5a902 --- /dev/null +++ b/tests/phpunit/includes/ProxyTools/wfGetIPTest.php @@ -0,0 +1,79 @@ +assertEquals( + $expected, + wfGetIP( wfGetIPTest::doReset ), + $msg ); + } + + /** helper */ + public function assertIPNot( $expected, $msg = '' ) { + $this->assertNotEquals( + $expected, + wfGetIP( wfGetIPTest::doReset ), + $msg ); + } + + function testGetLoopbackAddressWhenInCommandLine() { + global $wgCommandLineMode; + $save = $wgCommandLineMode; + + $wgCommandLineMode = true; + $this->assertIP( '127.0.0.1' ); + + # restore global + $wgCommandLineMode = $save; + } + + function testGetFromServerRemoteAddr() { + global $_SERVER; + $save = null; + if( array_key_exists( 'REMOTE_ADDR', $_SERVER ) ) { + $save = $_SERVER['REMOTE_ADDR']; + } else { + $clearRemoteAddr = true; + } + + # Starting assertions + $_SERVER['REMOTE_ADDR'] = '192.0.2.77'; # example IP - RFC 5737 + $this->assertIP( '192.0.2.77' ); + + /* + #### wfGetIP() does not support IPv6 yet :(( + $_SERVER['REMOTE_ADDR'] = '2001:db8:0:77'; + $this->assertIP( '2001:db8:0:77' ); + */ + + # restore global + if( $clearRemoteAddr ) { + unset( $_SERVER['REMOTE_ADDR'] ); + } else { + $_SERVER['REMOTE_ADDR'] = $save; + } + } + + /** + * @expectedException MWException + */ + function testLackOfRemoteAddrThrowAnException() { + global $wgCommandLineMode; + $save = $wgCommandLineMode; + + # PHPUnit runs from the command line + $wgCommandLineMode = false; + # Next call throw an exception about lacking an IP + wfGetIP( wfGetIPTest::doReset ); + + $wgCommandLineMode = $save; + } +} -- 2.20.1