1#!/usr/bin/perl
2
3# $OpenBSD: check.perl,v 1.3 2020/07/16 01:50:25 beck Exp $
4#
5# Copyright (c) 2020 Bob Beck <beck@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18#
19
20my $num_args = $#ARGV + 1;
21if ($num_args != 3) {
22    print "\nUsage: test.perl expected known testoutput\n";
23    exit 1;
24}
25
26my $expected_file=$ARGV[0];
27my $known_file=$ARGV[1];
28my $output_file=$ARGV[2];
29
30open (OUT, "<$output_file") || die "can't open $output_file";
31open (KNOWN, "<$known_file") || die "can't open $known_file";
32open (EXPECTED, "<$expected_file") || die "can't open $expected_file";
33
34my @expectedip;
35my @expecteddns;
36my @knownip;
37my @knowndns;
38my @outip;
39my @outdns;
40
41my $i = 0;
42while(<OUT>) {
43    chomp;
44    my @line = split(',');
45    my $id = $line[0];
46    die "$id mismatch with $i" if ($id != $i + 1);
47    $outdns[$i] = $line[1];
48    $outip[$i] = $line[2];
49    $i++;
50}
51$i = 0;
52while(<KNOWN>) {
53    chomp;
54    my @line = split(',');
55    my $id = $line[0];
56    die "$id mismatch with $i" if ($id != $i + 1);
57    $knowndns[$i] = $line[1];
58    $knownip[$i] = $line[2];
59    $i++;
60}
61$i = 0;
62while(<EXPECTED>) {
63    chomp;
64    my @line = split(',');
65    my $id = $line[0];
66    die "$id mismatch with $i" if ($id != $i + 1);
67    $expecteddns[$i] = $line[1];
68    $expectedip[$i] = $line[2];
69    $i++;
70}
71my $id;
72my $regressions = 0;
73my $known = 0;
74for ($id = 0; $id < $i; $id++) {
75    my $cert = $id + 1;
76    my $ipknown = ($outip[$id] eq $knownip[$id]);
77    my $dnsknown = ($outdns[$id] eq $knowndns[$id]);
78    if ($expecteddns[$id] ne $outdns[$id] && $expecteddns[$id] !~ /WEAK/) {
79	print STDERR "$cert DNS expected $expecteddns[$id] known $knowndns[$id] result $outdns[$id]";
80	if ($dnsknown) {
81	    print STDERR " (known failure)\n";
82	    $known++;
83	} else {
84	    print STDERR " (REGRESSED)\n";
85	    $regressions++;
86	}
87    }
88    if ($expectedip[$id] ne $outip[$id] && $expectedip[$id] !~ /WEAK/) {
89	print STDERR "$cert IP expected $expectedip[$id] known $knownip[$id] result $outip[$id]";
90	if ($ipknown) {
91	    print STDERR " (known failure)\n";
92	    $known++;
93	} else {
94	    print STDERR " (REGRESSED)\n";
95	    $regressions++;
96	}
97    }
98}
99print "\n\nTested $i certificates\n";
100if ($regressions == 0) {
101    print STDERR "SUCCESS - no new regressions ($known known failures)\n";
102    exit 0;
103} else {
104    print STDERR "FAILED - $regressions new regressions ($known known failures)\n";
105    exit 1;
106}
107
108
109