227 lines
11 KiB
PHP
227 lines
11 KiB
PHP
<?php
|
|
|
|
require_once (get_cfg_var ( 'COMMON_PATH' ) . "/ditest-php/test_framework.php");
|
|
require_once(__DIR__ . '/generic.inc.php');
|
|
|
|
$test = new DiTest("UDP publish to devices");
|
|
|
|
$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);
|
|
|
|
$sub = $zmqCtx->getSocket(ZMQ::SOCKET_PUB);
|
|
$sub->connect("tcp://127.0.0.1:".get_cfg_var('SUB_PORT'));
|
|
sleep(1);
|
|
|
|
$actionSet = [
|
|
'dinetrpc' => 1,
|
|
'id' => 1,
|
|
'pub' => 'action:set',
|
|
'time' => 1234567890,
|
|
'device:uid' => '012d2d2d4c45474143592d2d0000026f'
|
|
];
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$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 = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
|
|
$test->isTrue(is_resource($sock));
|
|
|
|
usleep(300 * 1000);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("3. Send a 'action:set' publish message to the device");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$sub->send(json_encode($actionSet));
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("4. Check if a 'action:set' message is sent on the logging interface");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$msg = $logging->recv();
|
|
$msg = json_decode($msg, true);
|
|
$test->equal(1, $msg['dinetrpc']);
|
|
$test->equal('action:set', $msg['pub']);
|
|
$test->equal('012d2d2d4c45474143592d2d0000026f', $msg['device:uid']);
|
|
$test->equal(1234567890, $msg['time']);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("5. Test for no message to the device");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$res = recvUDPWithTimeout(1000);
|
|
$test->equal(0, strlen($res[0]));
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("6. Send a 'device:info' publish message");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// it is {"device:uid":"012d2d2d4c45474143592d2d0000026f","dinetrpc":1,"pub":"device:info","result":[{"type":"ZKL 3000 RC"}],"time":1452243176109}
|
|
$mpackHex = '85A864696E657472706301AA6465766963653A756964D9203031326432643264346334353437343134333539326432643030303030323666A3707562AB6465766963653A696E666FA6726573756C749181A474797065AB5A4B4C2033303030205243A474696D65CF0000015220704AAD';
|
|
|
|
$ret = sendUDP(hex2bin("444A52100076" . $mpackHex), (strlen($mpackHex) / 2) + 6);
|
|
$test->equal((strlen($mpackHex) / 2) + 6, $ret);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("7. Check if a 'device:info' message is sent on the publish interface");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$msg = $pubsub->recv();
|
|
$msg = json_decode($msg, true);
|
|
$test->equal(1, $msg['dinetrpc']);
|
|
$test->equal('device:info', $msg['pub']);
|
|
$test->equal('012d2d2d4c45474143592d2d0000026f', $msg['device:uid']);
|
|
$test->equal($msg['time'], 1452243176109);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("8. Check if a 'device:info' message is sent on the logging interface");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$msg = $logging->recv();
|
|
$msg = json_decode($msg, true);
|
|
$test->equal(1, $msg['dinetrpc']);
|
|
$test->equal('device:info', $msg['pub']);
|
|
$test->equal('012d2d2d4c45474143592d2d0000026f', $msg['device:uid']);
|
|
$test->equal($msg['time'], 1452243176109);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("9. Send a 'action:set' publish message to the device");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$sub->send(json_encode($actionSet));
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("10. Check if a 'action:set' message is sent on the logging interface");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$msg = $logging->recv();
|
|
$msg = json_decode($msg, true);
|
|
$test->equal(1, $msg['dinetrpc']);
|
|
$test->equal('action:set', $msg['pub']);
|
|
$test->equal('012d2d2d4c45474143592d2d0000026f', $msg['device:uid']);
|
|
$test->equal(1234567890, $msg['time']);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("11. Test for message to the device");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$res = recvUDP();
|
|
$msgPayload = bin2hex($res[0]);
|
|
$test->equal(91*2, strlen($msgPayload));
|
|
// Check header DJR(0x44,0x4a,0x52)|PLAIN(0x10)|LENGTH91(0x00,0x5b)
|
|
$test->equal("444a5210005b", substr($msgPayload, 0, 12));
|
|
|
|
// Cannot check payload, because the payload changed due changes in json ordering
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("12. Create a new UDP connection");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$sock_new = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
|
|
$test->isTrue(is_resource($sock));
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("13. Send a 'device:info' publish message over new UDP connection");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// it is {"device:uid":"012d2d2d4c45474143592d2d0000026f","dinetrpc":1,"pub":"device:info","result":[{"type":"ZKL 3000 RC"}],"time":1452243176109}
|
|
$mpackHex = '85A864696E657472706301AA6465766963653A756964D9203031326432643264346334353437343134333539326432643030303030323666A3707562AB6465766963653A696E666FA6726573756C749181A474797065AB5A4B4C2033303030205243A474696D65CF0000015220704AAD';
|
|
|
|
$ret = sendUDP(hex2bin("444A52100076" . $mpackHex), (strlen($mpackHex) / 2) + 6, $sock_new);
|
|
$test->equal((strlen($mpackHex) / 2) + 6, $ret);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("14. Check if a 'device:info' message is sent on the publish interface");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$msg = $pubsub->recv();
|
|
$msg = json_decode($msg, true);
|
|
$test->equal(1, $msg['dinetrpc']);
|
|
$test->equal('device:info', $msg['pub']);
|
|
$test->equal('012d2d2d4c45474143592d2d0000026f', $msg['device:uid']);
|
|
$test->equal($msg['time'], 1452243176109);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("15. Check if a 'device:info' message is sent on the logging interface");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$msg = $logging->recv();
|
|
$msg = json_decode($msg, true);
|
|
$test->equal(1, $msg['dinetrpc']);
|
|
$test->equal('device:info', $msg['pub']);
|
|
$test->equal('012d2d2d4c45474143592d2d0000026f', $msg['device:uid']);
|
|
$test->equal($msg['time'], 1452243176109);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("16. Send a 'action:set' publish message to the device");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$sub->send(json_encode($actionSet));
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("17. Check if a 'action:set' message is sent on the logging interface");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$msg = $logging->recv();
|
|
$msg = json_decode($msg, true);
|
|
$test->equal(1, $msg['dinetrpc']);
|
|
$test->equal('action:set', $msg['pub']);
|
|
$test->equal('012d2d2d4c45474143592d2d0000026f', $msg['device:uid']);
|
|
$test->equal(1234567890, $msg['time']);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("18. Test for message to the device UDP socket 1");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$res = recvUDP($sock);
|
|
$msgPayload = bin2hex($res[0]);
|
|
$test->equal(91*2, strlen($msgPayload));
|
|
// Check header DJR(0x44,0x4a,0x52)|PLAIN(0x10)|LENGTH91(0x00,0x5b)
|
|
$test->equal("444a5210005b", substr($msgPayload, 0, 12));
|
|
|
|
// Cannot check payload, because the payload changed due changes in json ordering
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("19. Test for message to the device UDP socket 2");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$res = recvUDP($sock_new);
|
|
$msgPayload = bin2hex($res[0]);
|
|
$test->equal(91*2, strlen($msgPayload));
|
|
// Check header DJR(0x44,0x4a,0x52)|PLAIN(0x10)|LENGTH91(0x00,0x5b)
|
|
$test->equal("444a5210005b", substr($msgPayload, 0, 12));
|
|
|
|
// Cannot check payload, because the payload changed due changes in json ordering
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("20. Sleep to make SMP think the sockets are 'broken'");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
sleep(11);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("21. Send a 'action:set' publish message to the device");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$sub->send(json_encode($actionSet));
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("22. Check if a 'action:set' message is sent on the logging interface");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$msg = $logging->recv();
|
|
$msg = json_decode($msg, true);
|
|
$test->equal(1, $msg['dinetrpc']);
|
|
$test->equal('action:set', $msg['pub']);
|
|
$test->equal('012d2d2d4c45474143592d2d0000026f', $msg['device:uid']);
|
|
$test->equal(1234567890, $msg['time']);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("23. Test for no message to the device");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$res = recvUDPWithTimeout(1000);
|
|
$test->equal(0, strlen($res[0]));
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
$test->step("24. Disconnect");
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
socket_close($sock);
|
|
socket_close($sock_new);
|
|
|
|
$test->finish();
|