Skip to content

Commit d39d3aa

Browse files
committed
Refactored unit test + added AuthenticationFailedException
1 parent 6ddb28c commit d39d3aa

8 files changed

+60
-19
lines changed

.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ matrix:
2121
- EXECUTE_TEST_COVERALLS=true
2222
- php: 7
2323
- php: 7.1
24+
- php: 7.2
2425
- php: hhvm
2526
allow_failures:
2627
- php: hhvm
@@ -34,8 +35,8 @@ install:
3435
- travis_retry composer install --no-interaction
3536

3637
script:
37-
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
38-
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then ./vendor/bin/phpunit ; fi
38+
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then composer test-coverage ; fi
39+
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then composer test ; fi
3940

4041
after_script:
4142
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls -v; fi

composer.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
"files": ["bin/register_secure_session.php"]
2424
},
2525
"autoload-dev": {
26+
"files": ["test/autoload.php"],
2627
"psr-4": {
2728
"PHPSecureSessionTest\\": "test/"
2829
}
2930
},
3031
"require-dev": {
31-
"phpunit/PHPUnit": "^5.7"
32+
"phpunit/PHPUnit": "^5.7.27 || ^6.5.6"
33+
},
34+
"scripts": {
35+
"test": "phpunit --colors=always --stderr",
36+
"test-coverage": "phpunit --colors=always --stderr --coverage-clover clover.xml"
3237
}
3338
}

phpunit.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<phpunit bootstrap="./test/bootstrap.php" colors="true">
1+
<phpunit bootstrap="./vendor/autoload.php" colors="true">
22
<testsuites>
33
<testsuite name="PHP-Secure-Session Tests">
44
<directory>./test</directory>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace PHPSecureSession\Exception;
4+
5+
class AuthenticationFailedException extends \RuntimeException {
6+
}

src/SecureHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function decrypt($data, $key)
122122
true
123123
);
124124
if (! hash_equals($hmac, $hmacNew)) {
125-
throw new \RuntimeException('Authentication failed');
125+
throw new Exception\AuthenticationFailedException('Authentication failed');
126126
}
127127
// Decrypt
128128
return openssl_decrypt(

test/SecureSessionTest.php test/SecureHandlerTest.php

+27-10
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@
22

33
namespace PHPSecureSessionTest;
44

5+
use PHPSecureSession\Exception\AuthenticationFailedException;
56
use PHPSecureSession\SecureHandler;
6-
use SessionHandler;
7+
use PHPUnit\Framework\TestCase;
78
use ReflectionObject;
89
use ReflectionClass;
10+
use SessionHandler;
911

10-
class HashTest extends \PHPUnit_Framework_TestCase
12+
class SecureHandlerTest extends TestCase
1113
{
1214
public function setUp()
1315
{
1416
$this->secureHandler = new SecureHandler();
17+
session_set_save_handler($this->secureHandler, true);
18+
session_start();
19+
}
20+
21+
public function tearDown()
22+
{
23+
session_write_close();
1524
}
1625

1726
public function testConstructor()
1827
{
1928
$this->assertInstanceOf(SessionHandler::class, $this->secureHandler);
2029
}
2130

22-
/**
23-
* @runInSeparateProcess
24-
*/
2531
public function testOpen()
2632
{
2733
$this->assertTrue($this->secureHandler->open(sys_get_temp_dir(), ''));
@@ -32,9 +38,6 @@ public function testOpen()
3238
$this->assertEquals(64, mb_strlen($key->getValue($this->secureHandler), '8bit'));
3339
}
3440

35-
/**
36-
* @runInSeparateProcess
37-
*/
3841
public function testWriteRead()
3942
{
4043
$this->assertTrue($this->secureHandler->open(sys_get_temp_dir(), ''));
@@ -47,8 +50,6 @@ public function testWriteRead()
4750
/**
4851
* Test for issue #27
4952
* @see https://github.com/ezimuel/PHP-Secure-Session/issues/27
50-
*
51-
* @runInSeparateProcess
5253
*/
5354
public function testDoubleOpen()
5455
{
@@ -67,4 +68,20 @@ public function testDoubleOpen()
6768
$this->assertEquals($id1, $id2);
6869
$this->assertEquals($key1, $key2);
6970
}
71+
72+
public function testAuthenticationFailureDecrypt()
73+
{
74+
$this->assertTrue($this->secureHandler->open(sys_get_temp_dir(), ''));
75+
$id = session_id();
76+
$data = "This is a test!";
77+
$this->assertTrue($this->secureHandler->write($id, $data));
78+
79+
// Change the session data to generate an authentication error
80+
$alteredData = str_replace('!', '.', $data);
81+
file_put_contents(sys_get_temp_dir() . "/sess_$id", $alteredData);
82+
83+
$this->expectException(AuthenticationFailedException::class);
84+
$this->assertEquals($data, $this->secureHandler->read($id));
85+
86+
}
7087
}

test/autoload.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
if (! interface_exists(\PHPUnit_Framework_Test::class)) {
3+
class_alias(\PHPUnit\Framework\Test::class, \PHPUnit_Framework_Test::class);
4+
}
5+
if (! class_exists(\PHPUnit_Framework_AssertionFailedError::class)) {
6+
class_alias(\PHPUnit\Framework\AssertionFailedError::class, \PHPUnit_Framework_AssertionFailedError::class);
7+
}
8+
if (! class_exists(\PHPUnit_Framework_TestSuite::class)) {
9+
class_alias(\PHPUnit\Framework\TestSuite::class, \PHPUnit_Framework_TestSuite::class);
10+
}
11+
if (! class_exists(\PHPUnit\Framework\Error\Error::class)) {
12+
class_alias(\PHPUnit_Framework_Error::class, \PHPUnit\Framework\Error\Error::class);
13+
}
14+
if (! class_exists(\PHPUnit\Framework\Error\Notice::class)) {
15+
class_alias(\PHPUnit_Framework_Error_Notice::class, \PHPUnit\Framework\Error\Notice::class);
16+
}

test/bootstrap.php

-4
This file was deleted.

0 commit comments

Comments
 (0)