Skip to content

Commit

Permalink
MDL-67861 core: Fix getremoteaddr unit test to match new proxy setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mickhawkins authored and andrewnicols committed Mar 5, 2020
1 parent 7251f15 commit cc446ad
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/tests/moodlelib_test.php
Expand Up @@ -3569,6 +3569,10 @@ public function test_count_words() {
* Tests the getremoteaddr() function.
*/
public function test_getremoteaddr() {
global $CFG;

$this->resetAfterTest();
$CFG->getremoteaddrconf = GETREMOTEADDR_SKIP_HTTP_CLIENT_IP;
$xforwardedfor = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null;

$_SERVER['HTTP_X_FORWARDED_FOR'] = '';
Expand All @@ -3585,27 +3589,27 @@ public function test_getremoteaddr() {

$_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,127.0.0.2';
$twoip = getremoteaddr();
$this->assertEquals('127.0.0.1', $twoip);
$this->assertEquals('127.0.0.2', $twoip);

$_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,127.0.0.2, 127.0.0.3';
$_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,127.0.0.2,127.0.0.3';
$threeip = getremoteaddr();
$this->assertEquals('127.0.0.1', $threeip);
$this->assertEquals('127.0.0.3', $threeip);

$_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1:65535,127.0.0.2';
$_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,127.0.0.2:65535';
$portip = getremoteaddr();
$this->assertEquals('127.0.0.1', $portip);
$this->assertEquals('127.0.0.2', $portip);

$_SERVER['HTTP_X_FORWARDED_FOR'] = '0:0:0:0:0:0:0:1,127.0.0.2';
$_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,0:0:0:0:0:0:0:2';
$portip = getremoteaddr();
$this->assertEquals('0:0:0:0:0:0:0:1', $portip);
$this->assertEquals('0:0:0:0:0:0:0:2', $portip);

$_SERVER['HTTP_X_FORWARDED_FOR'] = '0::1,127.0.0.2';
$_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,0::2';
$portip = getremoteaddr();
$this->assertEquals('0:0:0:0:0:0:0:1', $portip);
$this->assertEquals('0:0:0:0:0:0:0:2', $portip);

$_SERVER['HTTP_X_FORWARDED_FOR'] = '[0:0:0:0:0:0:0:1]:65535,127.0.0.2';
$_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,[0:0:0:0:0:0:0:2]:65535';
$portip = getremoteaddr();
$this->assertEquals('0:0:0:0:0:0:0:1', $portip);
$this->assertEquals('0:0:0:0:0:0:0:2', $portip);

$_SERVER['HTTP_X_FORWARDED_FOR'] = $xforwardedfor;

Expand Down

0 comments on commit cc446ad

Please sign in to comment.