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

216 lines
7.6 KiB
PHP

<?php
require_once (get_cfg_var ( 'COMMON_PATH' ) . "/ditest-php/test_framework.php");
$test = new DiTest("4.2.x Extra tests");
$zmqCtx = new ZMQContext();
$reqrep = $zmqCtx->getSocket(ZMQ::SOCKET_REQ);
$reqrep->connect("tcp://127.0.0.1:".get_cfg_var('REQREP_PORT'));
$reqrep2 = $zmqCtx->getSocket(ZMQ::SOCKET_REQ);
$reqrep2->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("Send another request with the same id");
///////////////////////////////////////////////////////////////////////////////
$start = microtime(true);
$reqrep2->send(json_encode($deviceInfo));
///////////////////////////////////////////////////////////////////////////////
$test->step("Check if a reply is sent from the request-reply socket denying the request");
///////////////////////////////////////////////////////////////////////////////
do {
$msg = $reqrep2->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->isTrue(isset($msg['error']['code']));
$test->equal(5, $msg['error']['code']);
///////////////////////////////////////////////////////////////////////////////
$test->step("Open 100 connection");
///////////////////////////////////////////////////////////////////////////////
$socks = [];
for ($i = 0; $i < 100; $i++) {
$socks[$i] = fsockopen("localhost", get_cfg_var('TCP_PORT'));
$test->isTrue(is_resource($sock));
usleep(20 * 1000);
$ret = fwrite($socks[$i], hex2bin("444A52010026" . bin2hex("0200deadbeef00beef00cafebabe0" . (100 + $i))), 38);
$test->equal(38, $ret);
$ret = fread($socks[$i], 6);
$ret = bin2hex($ret);
$test->equal(12, strlen($ret));
$bytesRemain = hexdec(substr($ret, 8)) - 6;
$ret = fread($socks[$i], $bytesRemain);
$test->equal('MKAY', $ret);
}
usleep(1500 * 1000);
///////////////////////////////////////////////////////////////////////////////
$test->step("Close 100 connection");
///////////////////////////////////////////////////////////////////////////////
foreach ($socks as $i => $s) {
unset($socks[$i]);
usleep(40 * 1000);
}
usleep(1500 * 1000);
///////////////////////////////////////////////////////////////////////////////
$test->step("Chunked transfer");
///////////////////////////////////////////////////////////////////////////////
$socks = fsockopen("localhost", get_cfg_var('TCP_PORT'));
$test->isTrue(is_resource($sock));
usleep(30 * 1000);
$ret = fwrite($socks, hex2bin("44"), 1);
$test->equal(1, $ret);
usleep(200 * 1000);
$ret = fwrite($socks, hex2bin("4A"), 1);
$test->equal(1, $ret);
usleep(200 * 1000);
$ret = fwrite($socks, hex2bin("52"), 1);
$test->equal(1, $ret);
usleep(200 * 1000);
$ret = fwrite($socks, hex2bin("01"), 1);
$test->equal(1, $ret);
usleep(200 * 1000);
$ret = fwrite($socks, hex2bin("00"), 1);
$test->equal(1, $ret);
usleep(200 * 1000);
$ret = fwrite($socks, hex2bin("26"), 1);
$test->equal(1, $ret);
usleep(200 * 1000);
$uid = "0200deadbeef00beef00cafebabe0999";
for ($i = 0; $i < 32; $i ++) {
$ret = fwrite($socks, $uid[$i], 1);
$test->equal(1, $ret);
usleep(50 * 1000);
}
$ret = fread($socks, 6);
$ret = bin2hex($ret);
$test->equal(12, strlen($ret));
$bytesRemain = hexdec(substr($ret, 8)) - 6;
$ret = fread($socks, $bytesRemain);
$test->equal('MKAY', $ret);
unset($socks);
$test->finish();