This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added/updated tests for new encoding and encryption cases
- Loading branch information
Showing
5 changed files
with
193 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Aol\Offload\Encoders; | ||
|
||
class CacheLinkEncoderEncryptedTest extends OffloadEncoderTest | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->encoder = new OffloadEncoderEncryptedAes256($this->encoder, 'foo', [ | ||
'foo' => 'fookey', | ||
'bar' => 'barkey' | ||
]); | ||
} | ||
|
||
/** | ||
* @expectedException \Aol\Offload\Exceptions\OffloadConfigurationException | ||
* @expectedExceptionMessage Invalid $encryption_key_id | ||
*/ | ||
public function testEncryptionIdCannotHaveColon() | ||
{ | ||
new OffloadEncoderEncryptedAes256(new OffloadEncoderStandard(), 'foo:bar', []); | ||
} | ||
|
||
/** | ||
* @expectedException \Aol\Offload\Exceptions\OffloadConfigurationException | ||
* @expectedExceptionMessage Missing $encryption_key_id | ||
*/ | ||
public function testEncryptionIdMustBePresentInKeys() | ||
{ | ||
new OffloadEncoderEncryptedAes256(new OffloadEncoderStandard(), 'foo', ['bar'=>'baz']); | ||
} | ||
|
||
/** | ||
* @expectedException \Aol\Offload\Exceptions\OffloadEncoderException | ||
* @expectedExceptionMessage missing key ID | ||
*/ | ||
public function testDecodeMustReceiveKeyWithColon() | ||
{ | ||
$this->encoder->decode('not_a_valid_key'); | ||
} | ||
|
||
/** | ||
* @expectedException \Aol\Offload\Exceptions\OffloadEncoderException | ||
* @expectedExceptionMessage unknown key ID | ||
*/ | ||
public function testDecodeMustReceiveValidKey() | ||
{ | ||
$this->encoder->decode('bad_key:foobar'); | ||
} | ||
|
||
/** | ||
* @expectedException \Aol\Offload\Exceptions\OffloadEncoderException | ||
* @expectedExceptionMessage could not unserialize data | ||
*/ | ||
public function testDecodeFailsWithBadData() | ||
{ | ||
$this->encoder->decode('foo:' . base64_encode('$%^&*(')); | ||
} | ||
|
||
public function testDecodeFailureUsesBaseWhenFlagSet() | ||
{ | ||
$base = self::getMockBuilder('Aol\Offload\Encoders\OffloadEncoderStandard') | ||
->setMethods(['decode'])->getMock(); | ||
$base->expects(self::once())->method('decode')->willReturn('test'); | ||
$encoder = new OffloadEncoderEncryptedAes256($base, 'foo', ['foo' => 'bar'], true); | ||
self::assertEquals('test', $encoder->decode('foo:' . base64_encode('$%^&*('))); | ||
} | ||
|
||
public function testDecodesStandardIfNotEncrypted() | ||
{ | ||
$base = new OffloadEncoderStandard(); | ||
$encoder = new OffloadEncoderEncryptedAes256($base, 'foo', ['foo' => 'bar'], true); | ||
$encoded = $base->encode('hello world'); | ||
self::assertEquals('hello world', $encoder->decode($encoded)); | ||
} | ||
|
||
/** | ||
* @expectedException \Aol\Offload\Exceptions\OffloadEncoderException | ||
* @expectedExceptionMessage could not serialize data | ||
*/ | ||
public function testEncodeRethrowsException() | ||
{ | ||
$base = self::getMockBuilder('Aol\Offload\Encoders\OffloadEncoderStandard') | ||
->setMethods(['encode'])->getMock(); | ||
$base->expects(self::once())->method('encode')->willThrowException(new \RuntimeException('oh noes')); | ||
$encoder = new OffloadEncoderEncryptedAes256($base, 'foo', ['foo' => 'bar'], true); | ||
$encoder->encode('testing'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Aol\Offload\Encoders; | ||
|
||
class OffloadEncoderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var OffloadEncoderInterface */ | ||
protected $encoder; | ||
|
||
public function setUp() | ||
{ | ||
$this->encoder = new OffloadEncoderStandard(); | ||
} | ||
|
||
/** | ||
* @dataProvider dataWorks | ||
*/ | ||
public function testWorks($data) | ||
{ | ||
$encoded = $this->encoder->encode($data); | ||
self::assertTrue(is_string($encoded)); | ||
self::assertNotEquals($data, $encoded); | ||
$decoded = $this->encoder->decode($encoded); | ||
self::assertEquals($data, $decoded); | ||
} | ||
|
||
public function dataWorks() | ||
{ | ||
return [ | ||
['string'], | ||
[123], | ||
[123.22223], | ||
[true], | ||
[false], | ||
[[1,2,3]], | ||
[['1',2,false,true]], | ||
[['assoc'=>1,'two'=>true,'three'=>[1,2,true]]], | ||
[new \stdClass()], | ||
[call_user_func(function () { | ||
$obj = new \stdClass(); | ||
$obj->x = ['hello',1,2,3]; | ||
return $obj; | ||
})] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Aol\Offload; | ||
|
||
use Aol\Offload\Encoders\OffloadEncoderEncryptedAes256; | ||
|
||
class OffloadManagerMemcachedEncryptedTest extends OffloadManagerMemcachedTest | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->manager->getCache()->setEncoder( | ||
new OffloadEncoderEncryptedAes256( | ||
$this->manager->getCache()->getEncoder(), | ||
'foo', | ||
['foo' => 'bar'] | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters