Files
zoneminder/tests/php/test_monitorprobe_no_default_route.php
T

75 lines
2.6 KiB
PHP

<?php
// monitorprobe.php picks the interface to preselect from get_networks()'s
// 'default' entry, which is only there when the host has a default route of the
// form "default via <ip> dev <name>". Without one (an isolated camera network, a
// VPN default route, no ip tool) the view read a key that was not there and
// raised an undefined array key warning.
//
// The block is cut out of the view by its markers and run with the get_networks()
// answer set up here.
//
// Run: php tests/php/test_monitorprobe_no_default_route.php (from the repo root)
$passed = 0;
$failed = 0;
function check($label, $got, $want) {
global $passed, $failed;
if ($got === $want) {
$passed++;
echo " ok $label\n";
} else {
$failed++;
echo " FAIL $label\n got: ".var_export($got, true)."\n want: ".var_export($want, true)."\n";
}
}
$source = file_get_contents(__DIR__.'/../../web/skins/classic/views/monitorprobe.php');
$start = strpos($source, "\$interfaces = array('', 'select');");
$end = strpos($source, "unset(\$interfaces['default']);");
if ($start === false || $end === false) {
echo " FAIL could not find the interface block\n";
exit(1);
}
$block = substr($source, $start, $end + strlen("unset(\$interfaces['default']);") - $start);
$networks = array();
function get_networks() {
global $networks;
return $networks;
}
function run_block($block) {
$warnings = array();
set_error_handler(function($no, $str) use (&$warnings) {
$warnings[] = $str;
return true;
});
$vars = (function() use ($block) {
eval($block);
return get_defined_vars();
})();
restore_error_handler();
return array($vars, $warnings);
}
$networks = array('eth0' => 'eth0 192.168.1.0/24', 'default' => 'eth0');
list($v, $w) = run_block($block);
check('a default route raises no warning', $w, array());
check('a default route is preselected', $v['default_interface'], 'eth0');
check('the default entry is not offered as an interface', isset($v['interfaces']['default']), false);
check('the real interface is offered', $v['interfaces']['eth0'], 'eth0 192.168.1.0/24');
$networks = array('eth0' => 'eth0 192.168.1.0/24');
list($v, $w) = run_block($block);
check('no default route raises no warning', $w, array());
check('no default route preselects nothing', $v['default_interface'], '');
check('no default route still offers the interface', $v['interfaces']['eth0'], 'eth0 192.168.1.0/24');
$networks = array();
list($v, $w) = run_block($block);
check('no interfaces at all raises no warning', $w, array());
check('no interfaces at all preselects nothing', $v['default_interface'], '');
echo "\n$passed passed, $failed failed\n";
exit($failed ? 1 : 0);