r94558 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94557‎ | r94558 | r94559 >
Date:20:16, 15 August 2011
Author:hashar
Status:reverted (Comments)
Tags:
Comment:
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
$
Modified paths:
  • /trunk/phase3/includes/ProxyTools.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/ProxyTools (added) (history)
  • /trunk/phase3/tests/phpunit/includes/ProxyTools/README (added) (history)
  • /trunk/phase3/tests/phpunit/includes/ProxyTools/wfGetIPTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/ProxyTools.php
@@ -64,12 +64,18 @@
6565 /**
6666 * Work out the IP address based on various globals
6767 * 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)
6870 * @return string
6971 */
70 -function wfGetIP() {
 72+function wfGetIP( $reset = false ) {
7173 global $wgUsePrivateIPs, $wgCommandLineMode;
7274 static $ip = false;
7375
 76+ if( $reset ) {
 77+ $ip = false;
 78+ }
 79+
7480 # Return cached result
7581 if ( !empty( $ip ) ) {
7682 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.

Follow-up revisions

RevisionCommit summaryAuthorDate
r94575avoid playing with $_SERVER in test for now...hashar21:18, 15 August 2011
r94577back off r94558:...hashar21:45, 15 August 2011
r94638Tests for wfGetIP() follow up r89407...hashar14:15, 16 August 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r89407Don't execute the loop if there's no X-Forwarded-For header, also don't use i...ialex10:55, 3 June 2011
r92960Make wfUrlEncode(null) reset the static. Two skipped tests work now.platonides20:14, 23 July 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   20:18, 15 August 2011

Can it check if $reset equals "reset" instead of an opaque boolean?

#Comment by 😂 (talk | contribs)   21:02, 15 August 2011

Breaks unit tests

PHP Notice:  Undefined index: SERVER_PROTOCOL in /home/ci/cruisecontrol-bin-2.8.3/projects/mw/source/includes/Exception.php on line 185
1. MWExceptionHandler::handle() /home/ci/cruisecontrol-bin-2.8.3/projects/mw/source/includes/Exception.php:0
2. MWExceptionHandler::report() /home/ci/cruisecontrol-bin-2.8.3/projects/mw/source/includes/Exception.php:501
3. MWException->report() /home/ci/cruisecontrol-bin-2.8.3/projects/mw/source/includes/Exception.php:422
4. MWException->reportHTML() /home/ci/cruisecontrol-bin-2.8.3/projects/mw/source/includes/Exception.php:222
#Comment by Hashar (talk | contribs)   21:13, 15 August 2011

Oh great! Lets add it to the broken group meanwhile.

#Comment by Hashar (talk | contribs)   07:10, 16 August 2011

Reverted. Will fix it later :-)

Status & tagging log