Files
zoneminder/tests/php/test_onvifprobe_host_order.php
T

53 lines
2.1 KiB
PHP

<?php
// The ONVIF probe lists the cameras it found ordered by host. It compared the
// hosts as text, so 192.168.1.100 came before 192.168.1.20 and 192.168.1.9 came
// last. The numbers in an address are compared by value.
//
// Takes the usort() of the detected cameras out of web/skins/classic/views/onvifprobe.php,
// with the comparison function it names, and runs it on a list of hosts.
//
// Run as: php tests/php/test_onvifprobe_host_order.php
$src = file_get_contents(__DIR__.'/../../web/skins/classic/views/onvifprobe.php');
if (!preg_match('/usort\(\$detcameras, \'[^\']+\'\);|usort\(\$detcameras, function.*?\n\s*\}\);/s', $src, $sort)) {
echo "FAIL - could not find the usort of \$detcameras\n";
exit(1);
}
if (preg_match('/^function compareCameraHosts\(.*?\n}\n/ms', $src, $fn)) {
eval($fn[0]);
}
function camera($host) {
return array('monitor' => array('Host' => $host));
}
function order($hosts) {
$detcameras = array_map('camera', $hosts);
eval($GLOBALS['sort'][0]);
return array_map(function($c) { return $c['monitor']['Host']; }, $detcameras);
}
function ep($ip) { return 'http://'.$ip.':80/onvif/device_service'; }
$failures = 0;
function check($name, $got, $want) {
global $failures;
if ($got === $want) {
echo "ok - $name\n";
} else {
echo "FAIL - $name\n got ".json_encode($got)."\n want ".json_encode($want)."\n";
$failures++;
}
}
$ips = array('192.168.1.100', '192.168.1.9', '192.168.1.20', '10.0.0.5', '192.168.1.3');
check('addresses are ordered by their numbers',
order(array_map('ep', $ips)),
array_map('ep', array('10.0.0.5', '192.168.1.3', '192.168.1.9', '192.168.1.20', '192.168.1.100')));
check('host names ignore case',
order(array('http://Cam2.local/x', 'http://cam1.local/x', 'http://CAM10.local/x')),
array('http://cam1.local/x', 'http://Cam2.local/x', 'http://CAM10.local/x'));
check('a host that cannot be read sorts first and the list keeps its other entries',
order(array('http://10.0.0.2/x', 'not a url ::', 'http://10.0.0.1/x')),
array('not a url ::', 'http://10.0.0.1/x', 'http://10.0.0.2/x'));
check('no cameras', order(array()), array());
exit($failures ? 1 : 0);