Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The ZMQCert class wraps the CZMQ zcert API. Instances of the ZMQCert class can: * have their public and private text inspected, either as 32-byte binary strings or Z85 armoured strings * be tested for equality * be cloned * store and retrieve metadata * have their public and private test saved to and loaded from disk be applied to a ZMQSocket
- Loading branch information
Showing
11 changed files
with
723 additions
and
13 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
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,26 @@ | ||
--TEST-- | ||
Test a ZMQCert can be constructed. | ||
--SKIPIF-- | ||
<?php | ||
require_once __DIR__ . '/skipif.inc'; | ||
|
||
if (!class_exists('ZMQCert')) { | ||
die('skip'); | ||
} | ||
--FILE-- | ||
<?php | ||
|
||
$cert = new ZMQCert(); | ||
var_dump((bool)$cert); | ||
|
||
date_default_timezone_set('Europe/London'); | ||
$dateTime = new DateTime(); | ||
|
||
try { | ||
$cert = new ZMQCert($dateTime); | ||
} catch (ZMQCertException $e) { | ||
var_dump($e->getMessage()); | ||
} | ||
--EXPECT-- | ||
bool(true) | ||
string(69) "ZMQCert::__construct() expects parameter 1 to be string, object given" |
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,21 @@ | ||
--TEST-- | ||
Test two ZMQCerts can be tested for equality. | ||
--SKIPIF-- | ||
<?php | ||
|
||
require_once __DIR__ . '/skipif.inc'; | ||
|
||
if (!class_exists('ZMQCert')) { | ||
die('skip'); | ||
} | ||
--FILE-- | ||
<?php | ||
|
||
$cert = new ZMQCert(); | ||
$newCert = new ZMQCert(); | ||
|
||
var_dump($cert->equals($cert)); | ||
var_dump($newCert->equals($cert)); | ||
--EXPECT-- | ||
bool(true) | ||
bool(false) |
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,19 @@ | ||
--TEST-- | ||
Test a ZMQCert can be cloned. | ||
--SKIPIF-- | ||
<?php | ||
|
||
require_once __DIR__ . '/skipif.inc'; | ||
|
||
if (!class_exists('ZMQCert')) { | ||
die('skip'); | ||
} | ||
--FILE-- | ||
<?php | ||
|
||
$cert = new ZMQCert(); | ||
$clonedCert = clone $cert; | ||
|
||
var_dump($cert->equals($clonedCert)); | ||
--EXPECT-- | ||
bool(true) |
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,48 @@ | ||
--TEST-- | ||
Test a ZMQCert can get and set metadata. | ||
--SKIPIF-- | ||
<?php | ||
|
||
require_once __DIR__ . '/skipif.inc'; | ||
|
||
if (!class_exists('ZMQCert')) { | ||
die('skip'); | ||
} | ||
--FILE-- | ||
<?php | ||
|
||
$cert = new ZMQCert(); | ||
var_dump($cert->getMeta('foo')); | ||
var_dump($cert->getMetaKeys()); | ||
|
||
$cert->setMeta('foo', 'bar'); | ||
var_dump($cert->getMeta('foo')); | ||
var_dump($cert->getMetaKeys()); | ||
|
||
$cert->setMeta('baz', 'qux'); | ||
var_dump($cert->getMetaKeys()); | ||
|
||
// If the parameters are incorrect, then it should still return the metadata | ||
// keys. | ||
var_dump($cert->getMetaKeys(123)); | ||
--EXPECT-- | ||
NULL | ||
array(0) { | ||
} | ||
string(3) "bar" | ||
array(1) { | ||
[0]=> | ||
string(3) "foo" | ||
} | ||
array(2) { | ||
[0]=> | ||
string(3) "baz" | ||
[1]=> | ||
string(3) "foo" | ||
} | ||
array(2) { | ||
[0]=> | ||
string(3) "baz" | ||
[1]=> | ||
string(3) "foo" | ||
} |
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,70 @@ | ||
--TEST-- | ||
Test a ZMQCert can be saved. | ||
--SKIPIF-- | ||
<?php | ||
|
||
require_once __DIR__ . '/skipif.inc'; | ||
|
||
if (!class_exists('ZMQCert')) { | ||
die('skip'); | ||
} | ||
--FILE-- | ||
<?php | ||
|
||
define('BASE_CERT_DIR', __DIR__ . '/certs'); | ||
mkdir(BASE_CERT_DIR); | ||
|
||
// #save | ||
$certPath = BASE_CERT_DIR . '/cert'; | ||
|
||
$cert = new ZMQCert(); | ||
$cert->save($certPath); | ||
var_dump(is_file($certPath)); | ||
var_dump(is_file($certPath . '_secret')); | ||
|
||
unlink($certPath); | ||
unlink($certPath . '_secret'); | ||
|
||
try { | ||
$cert->save('/path/to/cert'); | ||
} catch (ZMQCertException $e) { | ||
var_dump($e->getMessage()); | ||
} | ||
|
||
// #savePublic | ||
$certPath = BASE_CERT_DIR . '/cert_public'; | ||
|
||
$cert = new ZMQCert(); | ||
$cert->savePublic($certPath); | ||
var_dump(is_file($certPath)); | ||
|
||
unlink($certPath); | ||
|
||
try { | ||
$cert->savePublic('/path/to/cert_public'); | ||
} catch (ZMQCertException $e) { | ||
var_dump($e->getMessage()); | ||
} | ||
|
||
// #saveSecret | ||
$certPath = BASE_CERT_DIR . '/cert_secret'; | ||
$cert->saveSecret($certPath); | ||
var_dump(is_file($certPath)); | ||
|
||
unlink($certPath); | ||
|
||
try { | ||
$cert->saveSecret('/path/to/cert_secret'); | ||
} catch (ZMQCertException $e) { | ||
var_dump($e->getMessage()); | ||
} | ||
|
||
rmdir(BASE_CERT_DIR); | ||
--EXPECT-- | ||
bool(true) | ||
bool(true) | ||
string(47) "Failed to save the certificate to /path/to/cert" | ||
bool(true) | ||
string(61) "Failed to save the public certificate to /path/to/cert_public" | ||
bool(true) | ||
string(61) "Failed to save the secret certificate to /path/to/cert_secret" |
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,36 @@ | ||
--TEST-- | ||
Test a ZMQCert can be loaded. | ||
--SKIPIF-- | ||
<?php | ||
|
||
require_once __DIR__ . '/skipif.inc'; | ||
|
||
if (! class_exists('ZMQCert')) { | ||
die('skip'); | ||
} | ||
--FILE-- | ||
<?php | ||
|
||
define('BASE_CERT_DIR', __DIR__ . '/certs'); | ||
mkdir(BASE_CERT_DIR); | ||
|
||
$certPath = BASE_CERT_DIR . '/cert'; | ||
$cert = new ZMQCert(); | ||
$cert->save($certPath); | ||
|
||
$certCloneEquivalent = new ZMQCert($certPath); | ||
var_dump($certCloneEquivalent->equals($cert)); | ||
|
||
unlink($certPath); | ||
unlink($certPath . '_secret'); | ||
|
||
try { | ||
new ZMQCert('/path/to/cert'); | ||
} catch (ZMQCertException $e) { | ||
var_dump($e->getMessage()); | ||
} | ||
|
||
rmdir(BASE_CERT_DIR); | ||
--EXPECT-- | ||
bool(true) | ||
string(49) "Failed to load the certificate from /path/to/cert" |
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,18 @@ | ||
--TEST-- | ||
Test a ZMQCert can be applied to a ZMQSocket. | ||
--SKIPIF-- | ||
<?php | ||
|
||
require_once __DIR__ . '/skipif.inc'; | ||
|
||
if (!class_exists('ZMQCert')) { | ||
die('skip'); | ||
} | ||
--FILE-- | ||
<?php | ||
|
||
$context = new ZMQContext(); | ||
$socket = $context->getSocket(ZMQ::SOCKET_REQ); | ||
$cert = new ZMQCert(); | ||
$cert->apply($socket); | ||
--EXPECT-- |
Oops, something went wrong.