src.dualinventive.com/dinet/sec-multi-proxy/tests/time_synchronisation.php

184 lines
7.5 KiB
PHP

<?php
require_once (get_cfg_var ( 'COMMON_PATH' ) . "/ditest-php/test_framework.php");
$test = new DiTest("4.2.2 Time synchronisation");
$zmqCtx = new ZMQContext();
$pubsub = $zmqCtx->getSocket(ZMQ::SOCKET_SUB);
$pubsub->connect("tcp://127.0.0.1:".get_cfg_var('PUB_PORT'));
usleep(50*1000);
$pubsub->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
$pubsub->setSockOpt(ZMQ::SOCKOPT_RCVTIMEO, 1000);
$logging = $zmqCtx->getSocket(ZMQ::SOCKET_SUB);
$logging->connect("tcp://127.0.0.1:".get_cfg_var('LOGIF_PORT'));
usleep(50*1000);
$logging->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
$logging->setSockOpt(ZMQ::SOCKOPT_RCVTIMEO, 1000);
sleep(1);
///////////////////////////////////////////////////////////////////////////////
$test->step("1. Start Secure Multi Proxy server");
///////////////////////////////////////////////////////////////////////////////
sleep(1); // Wait for SMP to startup
///////////////////////////////////////////////////////////////////////////////
$test->step("2. Open connection on the device interface");
///////////////////////////////////////////////////////////////////////////////
$sock = fsockopen("localhost", get_cfg_var('TCP_PORT'));
$test->isTrue(is_resource($sock));
usleep(300 * 1000);
///////////////////////////////////////////////////////////////////////////////
$test->step("3. Send the handshake");
///////////////////////////////////////////////////////////////////////////////
$ret = fwrite($sock, hex2bin("444A52010026" . bin2hex("0200deadbeef00beef00cafebabe0000")), 38);
$test->equal(38, $ret);
$ret = fread($sock, 6);
$ret = bin2hex($ret);
$test->equal(12, strlen($ret));
$bytesRemain = hexdec(substr($ret, 8)) - 6;
$ret = fread($sock, $bytesRemain);
$test->equal('MKAY', $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("4. Check if a connection:connect message is sent on the publish interface");
///////////////////////////////////////////////////////////////////////////////
$msg = $pubsub->recv();
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('connection:connect', $msg['pub']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['result'][0]['peer']));
///////////////////////////////////////////////////////////////////////////////
$test->step("5. Check if a connection:connect message is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
$msg = $logging->recv();
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('connection:connect', $msg['pub']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['result'][0]['peer']));
///////////////////////////////////////////////////////////////////////////////
$test->step("6. Send the time sync message to the proxy");
///////////////////////////////////////////////////////////////////////////////
$ret = fwrite($sock, hex2bin("444A52400006"), 6);
$test->equal(6, $ret);
$t = microtime(true) * 1000;
///////////////////////////////////////////////////////////////////////////////
$test->step("7. Check if the current time is replied +/- 1 sec");
///////////////////////////////////////////////////////////////////////////////
$ret = fread($sock, 6);
$ret = bin2hex($ret);
$test->equal(12, strlen($ret));
$bytesRemain = hexdec(substr($ret, 8)) - 6;
$ret = fread($sock, $bytesRemain);
$t2 = unpack('J*', $ret);
$test->range(reset($t2), $t - 1000, $t + 1000);
///////////////////////////////////////////////////////////////////////////////
$test->step("8. Wait 1 second");
///////////////////////////////////////////////////////////////////////////////
sleep(1);
///////////////////////////////////////////////////////////////////////////////
$test->step("9. Send the time sync message to the proxy");
///////////////////////////////////////////////////////////////////////////////
$ret = fwrite($sock, hex2bin("444A52400006"), 6);
$test->equal(6, $ret);
$t = microtime(true) * 1000;
///////////////////////////////////////////////////////////////////////////////
$test->step("10. Check if the current time is replied +/- 1 sec");
///////////////////////////////////////////////////////////////////////////////
$ret = fread($sock, 6);
$ret = bin2hex($ret);
$test->equal(12, strlen($ret));
$bytesRemain = hexdec(substr($ret, 8)) - 6;
$ret = fread($sock, $bytesRemain);
$t2 = unpack('J*', $ret);
$test->range(reset($t2), $t - 1000, $t + 1000);
///////////////////////////////////////////////////////////////////////////////
$test->step("11. Disconnect");
///////////////////////////////////////////////////////////////////////////////
fclose($sock);
///////////////////////////////////////////////////////////////////////////////
$test->step("12. Check if a connection:disconnect message is sent on the publish interface");
///////////////////////////////////////////////////////////////////////////////
$msg = $pubsub->recv();
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('connection:disconnect', $msg['pub']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['result'][0]['peer']));
$test->isTrue(isset($msg['result'][0]['code']));
///////////////////////////////////////////////////////////////////////////////
$test->step("13. Check if a connection:disconnect message is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
$msg = $logging->recv();
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('connection:disconnect', $msg['pub']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['result'][0]['peer']));
$test->isTrue(isset($msg['result'][0]['code']));
///////////////////////////////////////////////////////////////////////////////
$test->step("14. Open connection on the device interface");
///////////////////////////////////////////////////////////////////////////////
$sock = fsockopen("localhost", get_cfg_var('TCP_PORT'));
$test->isTrue(is_resource($sock));
usleep(300 * 1000);
///////////////////////////////////////////////////////////////////////////////
$test->step("15. Send the time sync message to the proxy");
///////////////////////////////////////////////////////////////////////////////
$ret = fwrite($sock, hex2bin("444A52400006"), 6);
$test->equal(6, $ret);
$t = microtime(true) * 1000;
///////////////////////////////////////////////////////////////////////////////
$test->step("16. Check if the generic error message is returned");
///////////////////////////////////////////////////////////////////////////////
$ret = fread($sock, 6);
$ret = bin2hex($ret);
$test->equal(12, strlen($ret));
$bytesRemain = hexdec(substr($ret, 8)) - 6;
$ret = fread($sock, $bytesRemain);
$test->equal('WTF', $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("17. Check if no message is sent on the publish interface");
///////////////////////////////////////////////////////////////////////////////
$ret = $pubsub->recv();
$test->isFalse($ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("18. Check if no connection:connect message is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
$ret = $logging->recv();
$test->isFalse($ret);
$test->finish();