Skip to content

Commit

Permalink
New fromEncoded & fromEncodedArray
Browse files Browse the repository at this point in the history
Fixes #40
  • Loading branch information
jdavid committed Apr 24, 2017
1 parent 6b728e9 commit a550edb
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,6 @@
tmp/
config.php
vendor/
composer.lock
composer.lock

.*.swp
38 changes: 37 additions & 1 deletion src/Models/BaseMessage.php
Expand Up @@ -95,6 +95,42 @@ public function fromJSON( $json, $keepOriginal = false ) {
$this->decode();
}

/**
* Creates and returns a new message from the given encoded message like object
* @param stdClass $obj Message-like object
* @param CipherParams|null $cipherParams
*/
public static function fromEncoded( $obj, CipherParams $cipherParams = null ) {
$class = get_called_class();

$msg = new $class();
if ($cipherParams != null) {
$msg->setCipherParams( $cipherParams );
}

foreach ($obj as $key => $value) {
if (property_exists( $class, $key )) {
$msg->$key = $value;
}
}

$msg->decode();

return $msg;
}

/**
* Creates and returns a new message from the given encoded message like object
* @param array $objs Array of Message-Like objects
* @param CipherParams|null $cipherParams
*/
public static function fromEncodedArray( $objs, CipherParams $cipherParams = null ) {
return array_map(
function( $obj ) use ($cipherParams) { return static::fromEncoded($obj, $cipherParams); },
$objs
);
}

/**
* Returns an encoded message as a stdClass ready for stringifying
*/
Expand Down Expand Up @@ -224,4 +260,4 @@ protected function clearFields() {
public function setCipherParams( CipherParams $cipherParams ) {
$this->cipherParams = $cipherParams;
}
}
}
1 change: 1 addition & 0 deletions src/Models/Message.php
Expand Up @@ -27,4 +27,5 @@ protected function encode() {

return $msg;
}

}
76 changes: 71 additions & 5 deletions tests/CryptoTest.php
Expand Up @@ -128,11 +128,44 @@ public function testMessageEncryptionAgainstFixture( $filename ) {
$decryptedExample->setCipherParams( $cipherParams );
$decryptedExample->fromJSON( $example->encrypted );

$this->assertEquals( $decodedExample->data, $decryptedExample->data, 'Expected unencrypted and decrypted message\'s contents to match' );
$this->assertEquals( $decodedExample->data, $decryptedExample->data,
'Expected unencrypted and decrypted message\'s contents to match' );

$decodedExample->setCipherParams( $cipherParams );
$encryptedJSON = json_decode( $decodedExample->toJSON() );
$this->assertEquals( $example->encrypted->data, $encryptedJSON->data, 'Expected encrypted and example encrypted message\'s contents to match' );
$this->assertEquals( $example->encrypted->data, $encryptedJSON->data,
'Expected encrypted and example encrypted message\'s contents to match' );
}
}

/**
* Tests Message:fromEncodedArray
*
* @dataProvider filenameProvider
*/
public function testMessageFromEncodedArray( $filename ) {
$fixture = json_decode( file_get_contents( $filename ) );

$cipherParams = Crypto::getDefaultParams([
'key' => $fixture->key,
'algorithm' => $fixture->algorithm,
'keyLength' => $fixture->keylength,
'mode' => $fixture->mode,
'iv' => $fixture->iv,
'base64Key' => true,
'base64Iv' => true,
]);

$encrypted = array_map( function ( $x ) { return $x->encrypted ; }, $fixture->items );
$encoded = array_map( function ( $x ) { return $x->encoded ; }, $fixture->items );

$encrypted = Message::fromEncodedArray( $encrypted, $cipherParams );
$encoded = Message::fromEncodedArray( $encoded );

foreach ($encrypted as $i => $decryptedExample) {
$decodedExample = $encoded[$i];
$this->assertEquals( $decodedExample->data, $decryptedExample->data,
'Expected unencrypted and decrypted message\'s contents to match' );
}
}

Expand Down Expand Up @@ -166,11 +199,44 @@ public function testPresenceMessageEncryptionAgainstFixture( $filename ) {
$decryptedExample->setCipherParams( $cipherParams );
$decryptedExample->fromJSON( $example->encrypted );

$this->assertEquals( $decodedExample->data, $decryptedExample->data, 'Expected unencrypted and decrypted message\'s contents to match' );
$this->assertEquals( $decodedExample->data, $decryptedExample->data,
'Expected unencrypted and decrypted message\'s contents to match' );

$decodedExample->setCipherParams( $cipherParams );
$encryptedJSON = json_decode( $decodedExample->toJSON() );
$this->assertEquals( $example->encrypted->data, $encryptedJSON->data, 'Expected encrypted and example encrypted message\'s contents to match' );
$this->assertEquals( $example->encrypted->data, $encryptedJSON->data,
'Expected encrypted and example encrypted message\'s contents to match' );
}
}

/**
* Tests PresenceMessage:fromEncodedArray
*
* @dataProvider filenameProvider
*/
public function testPresenceMessageFromEncodedArray( $filename ) {
$fixture = json_decode( file_get_contents( $filename ) );

$cipherParams = Crypto::getDefaultParams([
'key' => $fixture->key,
'algorithm' => $fixture->algorithm,
'keyLength' => $fixture->keylength,
'mode' => $fixture->mode,
'iv' => $fixture->iv,
'base64Key' => true,
'base64Iv' => true,
]);

$encrypted = array_map( function ( $x ) { return $x->encrypted ; }, $fixture->items );
$encoded = array_map( function ( $x ) { return $x->encoded ; }, $fixture->items );

$encrypted = PresenceMessage::fromEncodedArray( $encrypted, $cipherParams );
$encoded = PresenceMessage::fromEncodedArray( $encoded );

foreach ($encrypted as $i => $decryptedExample) {
$decodedExample = $encoded[$i];
$this->assertEquals( $decodedExample->data, $decryptedExample->data,
'Expected unencrypted and decrypted message\'s contents to match' );
}
}

Expand All @@ -180,4 +246,4 @@ public function filenameProvider() {
[ __DIR__ . '/../ably-common/test-resources/crypto-data-256.json'],
];
}
}
}

0 comments on commit a550edb

Please sign in to comment.