Skip to content

Commit

Permalink
Add the ZMQCert class
Browse files Browse the repository at this point in the history
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
phuedx committed Oct 8, 2014
1 parent fb0dbc7 commit fe8d16e
Show file tree
Hide file tree
Showing 11 changed files with 723 additions and 13 deletions.
27 changes: 26 additions & 1 deletion config.m4
Expand Up @@ -4,6 +4,9 @@ PHP_ARG_WITH(zmq, whether to enable 0MQ support,
PHP_ARG_ENABLE(zmq_pthreads, whether to enable support for php threads extension,
[ --enable-zmq-pthreads whether to enable support for php threads extension], no, no)

PHP_ARG_WITH(czmq, whether to enable CZMQ support,
[ --with-czmq[=DIR] Enable CZMQ support. DIR is the prefix to CZMQ installation directory.], no, no)

if test "$PHP_ZMQ" != "no"; then

AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
Expand Down Expand Up @@ -43,6 +46,29 @@ if test "$PHP_ZMQ" != "no"; then
AC_MSG_ERROR(Unable to find libzmq installation)
fi

if test "$PHP_CZMQ" != "no"; then
if test "x$PHP_CZMQ" != "xyes"; then
export PKG_CONFIG_PATH="${PHP_CZMQ}/${PHP_LIBDIR}/pkgconfig"
fi

AC_MSG_CHECKING(for CZMQ)
if $PKG_CONFIG --exists libczmq; then
PHP_CZMQ_VERSION=`$PKG_CONFIG libczmq --modversion`
PHP_CZMQ_PREFIX=`$PKG_CONFIG libczmq --variable=prefix`
AC_MSG_RESULT([found version $PHP_CZMQ_VERSION in $PHP_CZMQ_PREFIX])

PHP_CZMQ_LIBS=`$PKG_CONFIG libczmq --libs`
PHP_CZMQ_INCS=`$PKG_CONFIG libczmq --cflags`

PHP_EVAL_LIBLINE($PHP_CZMQ_LIBS, ZMQ_SHARED_LIBADD)
PHP_EVAL_INCLINE($PHP_CZMQ_INCS)

AC_DEFINE([HAVE_CZMQ], [], [CZMQ was found])
else
AC_MSG_RESULT([no])
fi
fi

AC_CHECK_HEADERS([stdint.h],[php_zmq_have_stdint=yes; break;])
if test $php_zmq_have_stdint != "yes"; then
AC_MSG_ERROR(Unable to find stdint.h)
Expand All @@ -60,4 +86,3 @@ if test "$PHP_ZMQ" != "no"; then
PHP_NEW_EXTENSION(zmq, zmq.c zmq_pollset.c zmq_device.c zmq_sockopt.c zmq_fd_stream.c zmq_clock.c, $ext_shared)
PKG_CONFIG_PATH="$ORIG_PKG_CONFIG_PATH"
fi

7 changes: 7 additions & 0 deletions package.xml
Expand Up @@ -90,6 +90,13 @@
<file name="035-capture.phpt" role="test" />
<file name="036-device.phpt" role="test" />
<file name="037-device-deprecated.phpt" role="test" />
<file name="038-cert-construct.phpt" role="test" />
<file name="039-cert-equals.phpt" role="test" />
<file name="040-cert-clone.phpt" role="test" />
<file name="041-cert-meta.phpt" role="test" />
<file name="042-cert-save.phpt" role="test" />
<file name="043-cert-load.phpt" role="test" />
<file name="046-cert-apply.phpt" role="test" />
<file name="bug_gh_43.phpt" role="test" />
<file name="bug_gh_49.phpt" role="test" />
<file name="bug_gh_50.phpt" role="test" />
Expand Down
13 changes: 13 additions & 0 deletions php_zmq_private.h
Expand Up @@ -37,6 +37,13 @@

#include <zmq.h>

#ifdef HAVE_CZMQ
# include <czmq.h>
# if CZMQ_VERSION_MAJOR >= 2
# define HAVE_CZMQ_2
# endif
#endif

#ifdef PHP_WIN32
# include "win32/php_stdint.h"
#else
Expand Down Expand Up @@ -263,5 +270,11 @@ ZEND_BEGIN_MODULE_GLOBALS(php_zmq)
php_zmq_clock_ctx_t *clock_ctx;
ZEND_END_MODULE_GLOBALS(php_zmq)

#ifdef HAVE_CZMQ_2
typedef struct _php_zmq_cert {
zend_object zend_object;
zcert_t *zcert;
} php_zmq_cert;
#endif

#endif /* _PHP_ZMQ_PRIVATE_H_ */
26 changes: 26 additions & 0 deletions tests/038-cert-construct.phpt
@@ -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"
21 changes: 21 additions & 0 deletions tests/039-cert-equals.phpt
@@ -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)
19 changes: 19 additions & 0 deletions tests/040-cert-clone.phpt
@@ -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)
48 changes: 48 additions & 0 deletions tests/041-cert-meta.phpt
@@ -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"
}
70 changes: 70 additions & 0 deletions tests/042-cert-save.phpt
@@ -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"
36 changes: 36 additions & 0 deletions tests/043-cert-load.phpt
@@ -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"
18 changes: 18 additions & 0 deletions tests/046-cert-apply.phpt
@@ -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--

0 comments on commit fe8d16e

Please sign in to comment.