Subversion Repositories vgwhois

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
#!/usr/bin/php
2
<?php
3
 
4
#
11 daniel-mar 5
#  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
5 daniel-mar 6
#  Maintenance / Developer utilities
2 daniel-mar 7
#
5 daniel-mar 8
#  (c) 2012-2019 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
2 daniel-mar 9
#
5 daniel-mar 10
#  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
2 daniel-mar 11
#
12
 
13
# There are 2 possibilities
14
# - Coverage by section. At least 1 line per command must be covered
15
#   => this is currently our behavior
16
# - Every line must be covered
17
 
4 daniel-mar 18
require_once __DIR__ . '/../../../shared/php_includes/common_functions.inc.php';
2 daniel-mar 19
 
20
$testcases = array();
4 daniel-mar 21
$testcases_tmp = file(__DIR__ . '/../../config/testcases.list');
2 daniel-mar 22
foreach ($testcases_tmp as &$tc) {
23
	$tc = trim($tc);
24
	if ($tc == '') continue;
25
	if ($tc[0] == '#') continue;
26
	$testcases[] = $tc;
27
}
28
 
4 daniel-mar 29
$patterns = glob(__DIR__ . '/../../../main/pattern/*');
2 daniel-mar 30
 
31
$count_total = 0;
32
$count_covered = 0;
33
$count_uncovered = 0;
34
 
35
echo "Uncovered\n\n";
36
 
37
foreach ($patterns as $pattern_file) {
38
	$pattern = file($pattern_file);
39
 
40
	$pattern[] = ':end';
41
	$count_total--; // wegen ":end"
42
 
43
	$content = '';
44
	$covered = false;
45
	$cmd = '';
46
	foreach ($pattern as $p) {
47
		$p = trim($p);
48
		if ($p == '') continue;
49
		if ($p[0] == '#') continue; // comment
50
 
51
		if ($p[0] == ':') {
52
			$count_total++;
53
			if ($covered) {
54
				$count_covered++;
55
				$covered = false;
56
			} else {
57
				if ($cmd != '') {
58
					$count_uncovered++;
59
					echo "$pattern_file ($cmd): $content\n\n";
60
				}
61
			}
62
			$cmd = $p;
63
			$content = '';
64
		} else if (!$covered) {
65
			if ($p[0] == '=') {
66
				// IP
67
 
68
				$p_ = substr($p, 1);
69
 
70
				foreach ($testcases as $query) {
71
					$match = false;
72
					if (strpos($p, ':') !== false) {
73
						// IPv6
74
						if (!ipv6_valid($query)) continue;
75
						$covered = ipv6_in_cidr($p_, $query);
76
					} else {
77
						// IPv4
78
						if (!ipv4_valid($query)) continue;
79
						$covered = ipv4_in_cidr($p_, $query);
80
					}
81
					if ($covered) break;
82
				}
83
			} else if ($p[0] == '*') {
84
				// ASN
85
 
86
				preg_match('#\*(.*):(\d+)(-(\d+)){0,1}#isU', $p, $m);
87
				$prefix = $m[1];
88
				$min = $m[2];
89
				$max = (isset($m[4])) ? $m[4] : $min;
90
 
91
				foreach ($testcases as $query) {
92
					if (preg_match('#'.preg_quote($prefix, '#').'(\d+)#is', $query, $m)) {
93
						$num = $m[1];
94
						if (($num >= $min) && ($num <= $max)) {
95
							$covered = true;
96
							break;
97
						}
98
					}
99
				}
100
			} else if (preg_match('@^(urn:){0,1}oid:(.*)@i', $p, $m)) {
101
				// OIDs
102
 
103
				$oid = normalize_oid($m[2]);
104
 
105
				foreach ($testcases as $query) {
106
					if (preg_match('@^(urn:){0,1}oid:(.*)@i', $query, $m2)) {
107
						$oid_tc = normalize_oid($m2[2]);
108
 
109
						if (strpos($oid.'.', $oid_tc.'.') === 0) {
110
							$covered = true;
111
							break;
112
						}
113
					}
114
				}
115
			} else {
116
				// REGEX
117
 
118
				$regex = $p;
119
 
120
				foreach ($testcases as $query) {
121
					if (preg_match('/'.$regex.'/i', $query, $m)) {
122
						$covered = true;
123
						break;
124
					}
125
				}
126
			}
127
		}
128
		$content .= "$p\n";
129
	}
130
}
131
 
132
if ($count_uncovered == 0) {
133
	echo 'Every instruction is covered with at least one testcase!\n\n';
134
}
135
 
136
echo "Stats\n\n";
137
 
138
echo "Total     = $count_total\n";
139
echo "Covered   = $count_covered\n";
140
echo "Uncovered = $count_uncovered\n";
141
 
142
$coverage = $count_covered/$count_total * 100;
143
echo "Coverage  = $coverage %\n";