@@ -48,6 +48,22 @@ public static function getHeaderValue($needle)
48
48
}
49
49
}
50
50
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 );
51
67
}
52
68
}
53
69
@@ -62,6 +78,11 @@ class csrfp_test extends PHPUnit_Framework_TestCase
62
78
*/
63
79
protected $ config = array ();
64
80
81
+ /**
82
+ * @var log directory for testing
83
+ */
84
+ private $ logDir = __DIR__ .'/logs ' ;
85
+
65
86
/**
66
87
* Function to be run before every test*() functions.
67
88
*/
@@ -70,6 +91,7 @@ public function setUp()
70
91
csrfprotector::$ config ['jsPath ' ] = '../js/csrfprotector.js ' ;
71
92
csrfprotector::$ config ['CSRFP_TOKEN ' ] = 'csrfp_token ' ;
72
93
csrfprotector::$ config ['secureCookie ' ] = false ;
94
+ csrfprotector::$ config ['logDirectory ' ] = '../test/logs ' ;
73
95
74
96
$ _SERVER ['REQUEST_URI ' ] = 'temp ' ; // For logging
75
97
$ _SERVER ['REQUEST_SCHEME ' ] = 'http ' ; // For authorizePost
@@ -98,6 +120,8 @@ public function setUp()
98
120
public function tearDown ()
99
121
{
100
122
unlink (__DIR__ .'/../libs/config.php ' );
123
+ if (is_dir (__DIR__ .'/logs ' ))
124
+ Helper::delTree (__DIR__ .'/logs ' );
101
125
}
102
126
103
127
/**
@@ -391,15 +415,48 @@ public function testob_handler_positioning()
391
415
*/
392
416
public function testgetCurrentUrl ()
393
417
{
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 ;
395
437
}
396
438
397
439
/**
398
440
* testing exception in logging function
399
441
*/
400
442
public function testLoggingException ()
401
443
{
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 " ));
403
460
}
404
461
405
462
/**
0 commit comments