Skip to content

Commit

Permalink
Year 2038 problem fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Feb 20, 2017
1 parent 7ccc59d commit 222349d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -292,7 +292,7 @@ public function check($path)
* Writes data to the cookie
*
* @param string $path Path to write to
* @param mixer $value Value to write
* @param mixed $value Value to write
* @return $this
*/
public function write($path, $value)
Expand Down Expand Up @@ -332,7 +332,7 @@ public function read($path = null)
*/
public function willNeverExpire()
{
$this->expiresAt = Chronos::now()->addYears(50)->format('U');
$this->expiresAt = Chronos::now()->setDate(2038, 1, 1)->format('U');

return $this;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Http/Cookie/CookieCryptTrait.php
Expand Up @@ -104,12 +104,11 @@ protected function _encrypt($value, $encrypt, $key = null)
if (is_array($value)) {
$value = $this->_flatten($value);
}
if ($encrypt === false) {
return $value;
}

$this->checkCipher($encrypt);
$prefix = 'Q2FrZQ==.';
$cipher = null;

if ($key === null) {
$key = $this->getCryptoKey();
}
Expand Down
23 changes: 16 additions & 7 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -22,6 +22,13 @@
class CookieTest extends TestCase
{

/**
* Encryption key used in the tests
*
* @var string
*/
protected $encryptionKey = 'someverysecretkeythatisatleast32charslong';

/**
* Test invalid cookie name
*
Expand Down Expand Up @@ -58,7 +65,7 @@ public function testDecrypt()
$cookie = new Cookie('cakephp', $encryptedCookieValue);
$this->assertEquals($expected, $cookie->getValue());

$cookie->decrypt('someverysecretkeythatisatleast32charslong');
$cookie->decrypt($this->encryptionKey);
$this->assertEquals('cakephp-rocks-and-is-awesome', $cookie->getValue());
}

Expand All @@ -69,11 +76,13 @@ public function testDecrypt()
*/
public function testEncrypt()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks-and-is-awesome');
$cookie->encrypt('someverysecretkeythatisatleast32charslong');
$value = 'cakephp-rocks-and-is-awesome';

$cookie = new Cookie('cakephp', $value);
$cookie->encrypt($this->encryptionKey);

$expected = 'cakephp-rocks-and-is-awesome';
$this->assertNotEmpty($expected, $cookie->getValue());
$this->assertNotEquals($value, $cookie->getValue());
$this->assertNotEmpty($cookie->getValue());
}

/**
Expand All @@ -87,14 +96,14 @@ public function testToHeaderValue()
$result = $cookie->toHeaderValue();
$this->assertEquals('cakephp=cakephp-rocks', $result);

$date = Chronos::createFromFormat('m/d/Y h:m:s', '12/1/2050 12:00:00');
$date = Chronos::createFromFormat('m/d/Y h:m:s', '12/1/2027 12:00:00');

$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie->setDomain('cakephp.org');
$cookie->expiresAt($date);
$result = $cookie->toHeaderValue();

$expected = 'cakephp=cakephp-rocks; expires=Wed, 01-Dec-2049 12:00:00 GMT; domain=cakephp.org';
$expected = 'cakephp=cakephp-rocks; expires=Tue, 01-Dec-2026 12:00:00 GMT; domain=cakephp.org';

$this->assertEquals($expected, $result);
}
Expand Down

0 comments on commit 222349d

Please sign in to comment.