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

574 lines
23 KiB
PHP

<?php
require_once (get_cfg_var ( 'COMMON_PATH' ) . "/ditest-php/test_framework.php");
$test = new DiTest("4.2.x Request-Reply");
$zmqCtx = new ZMQContext();
$reqrep = $zmqCtx->getSocket(ZMQ::SOCKET_REQ);
$reqrep->connect("tcp://127.0.0.1:".get_cfg_var('REQREP_PORT'));
$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);
$deviceInfo = [
'dinetrpc' => 1,
'id' => 1,
'req' => 'device:info',
'time' => 1234567890,
'device:uid' => '0200deadbeef00beef00cafebabe0000'
];
///////////////////////////////////////////////////////////////////////////////
$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("5-1. Send a 'device:info' request from the request-reply socket");
///////////////////////////////////////////////////////////////////////////////
$start = microtime(true);
$reqrep->send(json_encode($deviceInfo));
///////////////////////////////////////////////////////////////////////////////
$test->step("5-2. Check if the message is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
do {
$msg = $logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('device:info', $msg['req']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->equal($msg['time'], 1234567890);
$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("5-3. Check if the request is sent through to the device interface");
///////////////////////////////////////////////////////////////////////////////
$ret = fread($sock, 6);
$ret = bin2hex($ret);
$test->equal(12, strlen($ret));
$test->equal("444a5210005c", $ret);
$bytesRemain = hexdec(substr($ret, 8)) - 6;
$ret = fread($sock, $bytesRemain);
$test->equal("85", substr(bin2hex($ret), 0, 2));
///////////////////////////////////////////////////////////////////////////////
$test->step("5-4. Send a reply through the device interface");
///////////////////////////////////////////////////////////////////////////////
$mpackHex = "85aa6465766963653a756964d9203032303064656164626565663030626565663030636166656261626530303030a864696e657472706301a2696401a3726570ab6465766963653a696e666fa474696d65ce499602d2";
$start = microtime(true);
$ret = fwrite($sock, hex2bin("444A521000" . dechex((strlen($mpackHex) / 2) + 6) . $mpackHex), (strlen($mpackHex) / 2) + 6);
$test->equal((strlen($mpackHex) / 2) + 6, $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("5-5. Check if a reply is sent to the request-reply socket");
///////////////////////////////////////////////////////////////////////////////
do {
$msg = $reqrep->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$end = microtime(true);
$test->info("reqrep roundtrip took: " . round(($end - $start) * 1000, 3) . 'ms');
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('device:info', $msg['rep']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['time']));
$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("5-6. Check if the reply is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
do {
$msg = $logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('device:info', $msg['rep']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['time']));
$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("6. Send a 'device:info' request from the request-reply socket");
///////////////////////////////////////////////////////////////////////////////
$reqrep->send(json_encode($deviceInfo));
///////////////////////////////////////////////////////////////////////////////
$test->step("7. Check if the message is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
do {
$msg = $logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('device:info', $msg['req']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->equal($msg['time'], 1234567890);
$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("8. Check if the request is sent through to the device interface");
///////////////////////////////////////////////////////////////////////////////
$ret = fread($sock, 6);
$ret = bin2hex($ret);
$test->equal(12, strlen($ret));
$test->equal("444a5210005c", $ret);
$bytesRemain = hexdec(substr($ret, 8)) - 6;
$ret = fread($sock, $bytesRemain);
$test->equal("85", substr(bin2hex($ret), 0, 2));
///////////////////////////////////////////////////////////////////////////////
$test->step("9. Wait 10 seconds");
///////////////////////////////////////////////////////////////////////////////
sleep(10);
///////////////////////////////////////////////////////////////////////////////
$test->step("10. Check if a reply is sent to the request-reply socket containing a timeout");
///////////////////////////////////////////////////////////////////////////////
do {
$msg = $reqrep->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('device:info', $msg['rep']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['time']));
$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("10a. Check if the reply is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
do {
$msg = $logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('device:info', $msg['rep']);
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['error']['code']));
$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("11. Send a reply through the device interface");
///////////////////////////////////////////////////////////////////////////////
$mpackHex = "85aa6465766963653a756964d9203032303064656164626565663030626565663030636166656261626530303030a864696e657472706301a2696401a3726570ab6465766963653a696e666fa474696d65ce499602d2";
$start = microtime(true);
$ret = fwrite($sock, hex2bin("444A521000" . dechex((strlen($mpackHex) / 2) + 6) . $mpackHex), (strlen($mpackHex) / 2) + 6);
$test->equal((strlen($mpackHex) / 2) + 6, $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('WTF', $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("12. Check if no message is sent on the request-reply interface");
///////////////////////////////////////////////////////////////////////////////
$ret = $pubsub->recv();
$test->isFalse($ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("13. Check if no message is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
$ret = $logging->recv();
$test->isFalse($ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("14. Send a 'device:info' request message without 'device:uid'");
///////////////////////////////////////////////////////////////////////////////
$devI = $deviceInfo;
unset($devI['device:uid']);
$reqrep->send(json_encode($devI));
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
///////////////////////////////////////////////////////////////////////////////
$test->step("15. Check if no message is sent on the device interface");
///////////////////////////////////////////////////////////////////////////////
$r = [$sock];
$w = null;
$e = null;
$ret = stream_select($r, $w, $e, 0, 500 * 1000);
$test->equal(0, $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("16. Check if a reply is sent denying the request");
///////////////////////////////////////////////////////////////////////////////
$msg = $reqrep->recv();
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['error']['code']));
///////////////////////////////////////////////////////////////////////////////
$test->step("17. Send a 'device:info' request message without 'dinetrpc'");
///////////////////////////////////////////////////////////////////////////////
$devI = $deviceInfo;
unset($devI['dinetrpc']);
$reqrep->send(json_encode($devI));
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
///////////////////////////////////////////////////////////////////////////////
$test->step("18. Check if no message is sent on the device interface");
///////////////////////////////////////////////////////////////////////////////
$r = [$sock];
$w = null;
$e = null;
$ret = stream_select($r, $w, $e, 0, 500 * 1000);
$test->equal(0, $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("19. Check if a reply is sent denying the request");
///////////////////////////////////////////////////////////////////////////////
$msg = $reqrep->recv();
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['error']['code']));
$test->notEqual(7, $msg['error']['code']);
///////////////////////////////////////////////////////////////////////////////
$test->step("20. Send a 'device:info' request message without 'time'");
///////////////////////////////////////////////////////////////////////////////
$devI = $deviceInfo;
unset($devI['time']);
$reqrep->send(json_encode($devI));
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
///////////////////////////////////////////////////////////////////////////////
$test->step("21. Check if no message is sent on the device interface");
///////////////////////////////////////////////////////////////////////////////
$r = [$sock];
$w = null;
$e = null;
$ret = stream_select($r, $w, $e, 0, 500 * 1000);
$test->equal(0, $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("22. Check if a reply is sent denying the request");
///////////////////////////////////////////////////////////////////////////////
$msg = $reqrep->recv();
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->isTrue(isset($msg['error']['code']));
$test->notEqual(7, $msg['error']['code']);
///////////////////////////////////////////////////////////////////////////////
$test->step("23. Send a 'device:info' request message without 'id'");
///////////////////////////////////////////////////////////////////////////////
$devI = $deviceInfo;
unset($devI['id']);
$reqrep->send(json_encode($devI));
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
///////////////////////////////////////////////////////////////////////////////
$test->step("24. Check if no message is sent on the device interface");
///////////////////////////////////////////////////////////////////////////////
$r = [$sock];
$w = null;
$e = null;
$ret = stream_select($r, $w, $e, 0, 500 * 1000);
$test->equal(0, $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("25. Check if a reply is sent denying the request");
///////////////////////////////////////////////////////////////////////////////
$msg = $reqrep->recv();
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['error']['code']));
$test->notEqual(7, $msg['error']['code']);
//$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("26. Send a 'device:info' request message with a 'device:uid' of 31 characters");
///////////////////////////////////////////////////////////////////////////////
$devI = $deviceInfo;
$devI['device:uid'] = '0200deadbeef00beef00cafebabe000';
$reqrep->send(json_encode($devI));
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
///////////////////////////////////////////////////////////////////////////////
$test->step("27. Check if no message is sent on the device interface");
///////////////////////////////////////////////////////////////////////////////
$r = [$sock];
$w = null;
$e = null;
$ret = stream_select($r, $w, $e, 0, 500 * 1000);
$test->equal(0, $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("28. Check if a reply is sent denying the request");
///////////////////////////////////////////////////////////////////////////////
$msg = $reqrep->recv();
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['error']['code']));
$test->notEqual(7, $msg['error']['code']);
///////////////////////////////////////////////////////////////////////////////
$test->step("29. Send a 'device:info' request message with a 'device:uid' of 33 characters");
///////////////////////////////////////////////////////////////////////////////
$devI = $deviceInfo;
$devI['device:uid'] = '0200deadbeef00beef00cafebabe0000a';
$reqrep->send(json_encode($devI));
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
///////////////////////////////////////////////////////////////////////////////
$test->step("30. Check if no message is sent on the device interface");
///////////////////////////////////////////////////////////////////////////////
$r = [$sock];
$w = null;
$e = null;
$ret = stream_select($r, $w, $e, 0, 500 * 1000);
$test->equal(0, $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("31. Check if a reply is sent denying the request");
///////////////////////////////////////////////////////////////////////////////
$msg = $reqrep->recv();
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->isTrue(isset($msg['time']));
$test->isTrue(isset($msg['error']['code']));
$test->notEqual(7, $msg['error']['code']);
///////////////////////////////////////////////////////////////////////////////
$test->step("32. Disconnect from the device interface");
///////////////////////////////////////////////////////////////////////////////
fclose($sock);
///////////////////////////////////////////////////////////////////////////////
$test->step("32a. 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("32b. Check if a connection:disconnect message is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
do {
$msg = $logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$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']));
usleep(250 * 1000);
///////////////////////////////////////////////////////////////////////////////
$test->step("33. Send a 'device:info' request from the request-reply socket");
///////////////////////////////////////////////////////////////////////////////
$reqrep->send(json_encode($deviceInfo));
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
///////////////////////////////////////////////////////////////////////////////
$test->step("34. Check if a reply is sent denying the request");
///////////////////////////////////////////////////////////////////////////////
$msg = $reqrep->recv();
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('device:info', $msg['rep']);
$test->isTrue(isset($msg['time']));
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['error']['code']));
$test->equal(3, $msg['error']['code']);
$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("35. Open connection on the device interface");
///////////////////////////////////////////////////////////////////////////////
$sock = fsockopen("localhost", get_cfg_var('TCP_PORT'));
$test->isTrue(is_resource($sock));
usleep(300 * 1000);
///////////////////////////////////////////////////////////////////////////////
$test->step("36. Send a 'device:info' request from the request-reply socket");
///////////////////////////////////////////////////////////////////////////////
$reqrep->send(json_encode($deviceInfo));
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
///////////////////////////////////////////////////////////////////////////////
$test->step("37. Check if a reply is sent denying the request");
///////////////////////////////////////////////////////////////////////////////
$msg = $reqrep->recv();
do {
$logging->recv();
} while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE));
$msg = json_decode($msg, true);
$test->equal(1, $msg['dinetrpc']);
$test->equal('device:info', $msg['rep']);
$test->isTrue(isset($msg['time']));
$test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']);
$test->isTrue(isset($msg['error']['code']));
$test->notEqual(7, $msg['error']['code']);
$test->equal($msg['id'], 1);
///////////////////////////////////////////////////////////////////////////////
$test->step("38. Send a reply via the device interface");
///////////////////////////////////////////////////////////////////////////////
$mpackHex = "85aa6465766963653a756964d9203032303064656164626565663030626565663030636166656261626530303030a864696e657472706301a2696401a3726570ab6465766963653a696e666fa474696d65ce499602d2";
$start = microtime(true);
$ret = fwrite($sock, hex2bin("444A521000" . dechex((strlen($mpackHex) / 2) + 6) . $mpackHex), (strlen($mpackHex) / 2) + 6);
$test->equal((strlen($mpackHex) / 2) + 6, $ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("39. 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("40. Check if no message is sent on the publish interface");
///////////////////////////////////////////////////////////////////////////////
$ret = $pubsub->recv();
$test->isFalse($ret);
///////////////////////////////////////////////////////////////////////////////
$test->step("41. Check if no message is sent on the logging interface");
///////////////////////////////////////////////////////////////////////////////
$ret = $logging->recv();
$test->isFalse($ret);
$test->finish();