Subversion Repositories vgwhois

Rev

Rev 5 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

#!/usr/bin/php
<?php

#
#  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
#  Maintenance / Developer utilities
#
#  (c) 2012-2019 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
#
#  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
#

# 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 __DIR__ . '/../../../shared/php_includes/common_functions.inc.php';

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

$patterns = glob(__DIR__ . '/../../../main/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($p, 1);

                                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";