Index: trunk/phase3/includes/ProxyTools.php |
— | — | @@ -64,12 +64,18 @@ |
65 | 65 | /** |
66 | 66 | * Work out the IP address based on various globals |
67 | 67 | * For trusted proxies, use the XFF client IP (first of the chain) |
| 68 | + * @param $reset boolean Used to reset the internal static variable |
| 69 | + * tracking the IP address. (default: false) |
68 | 70 | * @return string |
69 | 71 | */ |
70 | | -function wfGetIP() { |
| 72 | +function wfGetIP( $reset = false ) { |
71 | 73 | global $wgUsePrivateIPs, $wgCommandLineMode; |
72 | 74 | static $ip = false; |
73 | 75 | |
| 76 | + if( $reset ) { |
| 77 | + $ip = false; |
| 78 | + } |
| 79 | + |
74 | 80 | # Return cached result |
75 | 81 | if ( !empty( $ip ) ) { |
76 | 82 | return $ip; |
Index: trunk/phase3/tests/phpunit/includes/ProxyTools/wfGetIPTest.php |
— | — | @@ -0,0 +1,79 @@ |
| 2 | +<?php |
| 3 | +/* |
| 4 | + * Unit tests for wfGetIP() |
| 5 | + * |
| 6 | + * TODO: need to cover the part dealing with XFF headers. |
| 7 | + */ |
| 8 | +class wfGetIPTest extends MediaWikiTestCase { |
| 9 | + // constant used to reset wfGetIP() internal static variable |
| 10 | + const doReset = true; |
| 11 | + |
| 12 | + /** helper */ |
| 13 | + public function assertIP( $expected, $msg = '' ) { |
| 14 | + $this->assertEquals( |
| 15 | + $expected, |
| 16 | + wfGetIP( wfGetIPTest::doReset ), |
| 17 | + $msg ); |
| 18 | + } |
| 19 | + |
| 20 | + /** helper */ |
| 21 | + public function assertIPNot( $expected, $msg = '' ) { |
| 22 | + $this->assertNotEquals( |
| 23 | + $expected, |
| 24 | + wfGetIP( wfGetIPTest::doReset ), |
| 25 | + $msg ); |
| 26 | + } |
| 27 | + |
| 28 | + function testGetLoopbackAddressWhenInCommandLine() { |
| 29 | + global $wgCommandLineMode; |
| 30 | + $save = $wgCommandLineMode; |
| 31 | + |
| 32 | + $wgCommandLineMode = true; |
| 33 | + $this->assertIP( '127.0.0.1' ); |
| 34 | + |
| 35 | + # restore global |
| 36 | + $wgCommandLineMode = $save; |
| 37 | + } |
| 38 | + |
| 39 | + function testGetFromServerRemoteAddr() { |
| 40 | + global $_SERVER; |
| 41 | + $save = null; |
| 42 | + if( array_key_exists( 'REMOTE_ADDR', $_SERVER ) ) { |
| 43 | + $save = $_SERVER['REMOTE_ADDR']; |
| 44 | + } else { |
| 45 | + $clearRemoteAddr = true; |
| 46 | + } |
| 47 | + |
| 48 | + # Starting assertions |
| 49 | + $_SERVER['REMOTE_ADDR'] = '192.0.2.77'; # example IP - RFC 5737 |
| 50 | + $this->assertIP( '192.0.2.77' ); |
| 51 | + |
| 52 | + /* |
| 53 | + #### wfGetIP() does not support IPv6 yet :(( |
| 54 | + $_SERVER['REMOTE_ADDR'] = '2001:db8:0:77'; |
| 55 | + $this->assertIP( '2001:db8:0:77' ); |
| 56 | + */ |
| 57 | + |
| 58 | + # restore global |
| 59 | + if( $clearRemoteAddr ) { |
| 60 | + unset( $_SERVER['REMOTE_ADDR'] ); |
| 61 | + } else { |
| 62 | + $_SERVER['REMOTE_ADDR'] = $save; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @expectedException MWException |
| 68 | + */ |
| 69 | + function testLackOfRemoteAddrThrowAnException() { |
| 70 | + global $wgCommandLineMode; |
| 71 | + $save = $wgCommandLineMode; |
| 72 | + |
| 73 | + # PHPUnit runs from the command line |
| 74 | + $wgCommandLineMode = false; |
| 75 | + # Next call throw an exception about lacking an IP |
| 76 | + wfGetIP( wfGetIPTest::doReset ); |
| 77 | + |
| 78 | + $wgCommandLineMode = $save; |
| 79 | + } |
| 80 | +} |
Index: trunk/phase3/tests/phpunit/includes/ProxyTools/README |
— | — | @@ -0,0 +1,2 @@ |
| 2 | +This directory hold tests for includes/ProxyTools.php file |
| 3 | +which is a pile of functions. |