Skip to content

Commit 16603e1

Browse files
committed
added test cases for private protected members using reflection
1 parent 4dc3e03 commit 16603e1

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"homepage": "https://github.com/mebjas/CSRF-Protector-PHP",
77
"license": "APACHE",
88
"require-dev": {
9-
"satooshi/php-coveralls": ">=0.6"
9+
"satooshi/php-coveralls": "dev-master"
1010
},
1111
"autoload": {
1212
"classmap": ["libs/csrf/"]

test/csrfprotector_test.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public static function getHeaderValue($needle)
4848
}
4949
}
5050
return $hvalue;
51+
}
52+
}
53+
54+
/**
55+
* helper methods
56+
*/
57+
class Helper {
58+
/**
59+
* Function to recusively delete a dir
60+
*/
61+
public static function delTree($dir) {
62+
$files = array_diff(scandir($dir), array('.','..'));
63+
foreach ($files as $file) {
64+
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
65+
}
66+
return rmdir($dir);
5167
}
5268
}
5369

@@ -62,6 +78,11 @@ class csrfp_test extends PHPUnit_Framework_TestCase
6278
*/
6379
protected $config = array();
6480

81+
/**
82+
* @var log directory for testing
83+
*/
84+
private $logDir = __DIR__ .'/logs';
85+
6586
/**
6687
* Function to be run before every test*() functions.
6788
*/
@@ -70,6 +91,7 @@ public function setUp()
7091
csrfprotector::$config['jsPath'] = '../js/csrfprotector.js';
7192
csrfprotector::$config['CSRFP_TOKEN'] = 'csrfp_token';
7293
csrfprotector::$config['secureCookie'] = false;
94+
csrfprotector::$config['logDirectory'] = '../test/logs';
7395

7496
$_SERVER['REQUEST_URI'] = 'temp'; // For logging
7597
$_SERVER['REQUEST_SCHEME'] = 'http'; // For authorizePost
@@ -98,6 +120,8 @@ public function setUp()
98120
public function tearDown()
99121
{
100122
unlink(__DIR__ .'/../libs/config.php');
123+
if (is_dir(__DIR__ .'/logs'))
124+
Helper::delTree(__DIR__ .'/logs');
101125
}
102126

103127
/**
@@ -391,15 +415,48 @@ public function testob_handler_positioning()
391415
*/
392416
public function testgetCurrentUrl()
393417
{
394-
$this->markTestSkipped('Cannot test private methods');
418+
$stub = new ReflectionClass('csrfprotector');
419+
$method = $stub->getMethod('getCurrentUrl');
420+
$method->setAccessible(true);
421+
$this->assertEquals($method->invoke(null, []), "http://test/index.php");
422+
423+
$tmp_request_scheme = $_SERVER['REQUEST_SCHEME'];
424+
unset($_SERVER['REQUEST_SCHEME']);
425+
426+
// server-https is not set
427+
$this->assertEquals($method->invoke(null, []), "http://test/index.php");
428+
429+
$_SERVER['HTTPS'] = 'on';
430+
$this->assertEquals($method->invoke(null, []), "https://test/index.php");
431+
unset($_SERVER['HTTPS']);
432+
433+
$_SERVER['REQUEST_SCHEME'] = "https";
434+
$this->assertEquals($method->invoke(null, []), "https://test/index.php");
435+
436+
$_SERVER['REQUEST_SCHEME'] = $tmp_request_scheme;
395437
}
396438

397439
/**
398440
* testing exception in logging function
399441
*/
400442
public function testLoggingException()
401443
{
402-
$this->markTestSkipped('Cannot test private methods');
444+
$stub = new ReflectionClass('csrfprotector');
445+
$method = $stub->getMethod('logCSRFattack');
446+
$method->setAccessible(true);
447+
448+
try {
449+
$method->invoke(null, []);
450+
$this->fail("logFileWriteError was not caught");
451+
} catch (Exception $ex) {
452+
// pass
453+
$this->assertTrue(true);
454+
}
455+
456+
if (!is_dir($this->logDir))
457+
mkdir($this->logDir);
458+
$method->invoke(null, []);
459+
$this->assertTrue(file_exists($this->logDir ."/" .date("m-20y") .".log"));
403460
}
404461

405462
/**

0 commit comments

Comments
 (0)