Source code of /usr/share/gwhois/maintenance/testcases/coverage

Last modified: 2014-11-27 09:01:30

Download

#!/usr/bin/php
<?php

#
#  generic Whois - Maintenance Framework: Testcases
#
#  (c) 2012-2014 Daniel Marschall, ViaThinkSoft [www.viathinksoft.de]
#
#  Distribution, usage etc. pp. regulated by the current version of GPL.
#
#
#  Version 2014-11-25
#

# There are 2 possibilities
# - Coverage by section. At least 1 line per command must be covered
#   => this is currently our behavior
# - Every line must be covered

require_once '/usr/share/gwhois/includes/common_functions.inc.php';

$testcases = array();
$testcases_tmp file('/etc/gwhois/maintenance/testcases.list');
foreach (
$testcases_tmp as &$tc) {
    
$tc trim($tc);
    if (
$tc == '') continue;
    if (
$tc[0] == '#') continue;
    
$testcases[] = $tc;
}

$patterns glob('/etc/gwhois/pattern*');

$count_total 0;
$count_covered 0;
$count_uncovered 0;

echo 
"Uncovered\n\n";

foreach (
$patterns as $pattern_file) {
    
$pattern file($pattern_file);

    
$pattern[] = ':end';
    
$count_total--; // wegen ":end"

    
$content '';
    
$covered false;
    
$cmd '';
    foreach (
$pattern as $p) {
        
$p trim($p);
        if (
$p == '') continue;
        if (
$p[0] == '#') continue; // comment

        
if ($p[0] == ':') {
            
$count_total++;
            if (
$covered) {
                
$count_covered++;
                
$covered false;
            } else {
                if (
$cmd != '') {
                    
$count_uncovered++;
                    echo 
"$pattern_file ($cmd): $content\n\n";
                }
            }
            
$cmd $p;
            
$content '';
        } else if (!
$covered) {
            if (
$p[0] == '=') {
                
// IP

                
$p_ substr($p1);

                foreach (
$testcases as $query) {
                    
$match false;
                    if (
strpos($p':') !== false) {
                        
// IPv6
                        
if (!ipv6_valid($query)) continue;
                        
$covered ipv6_in_cidr($p_$query);
                    } else {
                        
// IPv4
                        
if (!ipv4_valid($query)) continue;
                        
$covered ipv4_in_cidr($p_$query);
                    }
                    if (
$covered) break;
                }
            } else if (
$p[0] == '*') {
                
// ASN

                
preg_match('#\*(.*):(\d+)(-(\d+)){0,1}#isU'$p$m);
                
$prefix $m[1];
                
$min $m[2];
                
$max = (isset($m[4])) ? $m[4] : $min;

                foreach (
$testcases as $query) {
                    if (
preg_match('#'.preg_quote($prefix'#').'(\d+)#is'$query$m)) {
                        
$num $m[1];
                        if ((
$num >= $min) && ($num <= $max)) {
                            
$covered true;
                            break;
                        }
                    }
                }
            } else if (
preg_match('^(urn:){0,1}oid:(.*)i'$p$m)) {
                
// OIDs

                
$oid normalize_oid($m[2]);

                foreach (
$testcases as $query) {
                    if (
preg_match('^(urn:){0,1}oid:(.*)i'$query$m2)) {
                        
$oid_tc normalize_oid($m2[2]);

                        if (
strpos($oid.'.'$oid_tc.'.') === 0) {
                            
$covered true;
                            break;
                        }
                    }
                }
            } else {
                
// REGEX

                
$regex $p;

                foreach (
$testcases as $query) {
                    if (
preg_match('/'.$regex.'/i'$query$m)) {
                        
$covered true;
                        break;
                    }
                }
            }
        }
        
$content .= "$p\n";
    }
}

if (
$count_uncovered == 0) {
    echo 
'Every instruction is covered with at least one testcase!\n\n';
}

echo 
"Stats\n\n";

echo 
"Total     = $count_total\n";
echo 
"Covered   = $count_covered\n";
echo 
"Uncovered = $count_uncovered\n";

$coverage $count_covered/$count_total 100;
echo 
"Coverage  = $coverage %\n";