json and html unit tests more seamless

- html_file / json_file
- file name comes in command, not earlier
- Both a title
- avoid fixed string for file names over and over
This commit is contained in:
Dirk
2025-11-27 20:38:12 +01:00
parent 964e8924a4
commit 7823699982
2 changed files with 38 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env perl
# This is more a PoC. Improvements welcome!
# Checking whether both JSON outputs are valid
#
use strict;
@@ -9,7 +9,8 @@ use JSON;
my $tests = 0;
my $prg="./testssl.sh";
my $json="tmp.json";
my $json="";
my $json_file="";
my $check2run ="--ip=one --ids-friendly -q --color 0";
my $uri="example.com"; # Cloudflare blocks too often
my $out="";
@@ -26,36 +27,37 @@ STDOUT->autoflush(1);
die "Unable to open $prg" unless -f $prg;
# Provide proper start conditions
unlink $json;
$json_file="tmp.json";
unlink $json_file;
# Title
printf "\n%s\n", "Unit testing JSON output ...";
#1
printf "%s\n", ".. plain JSON --> $uri ";
$out = `$prg $check2run --jsonfile tmp.json $uri`;
$json = json('tmp.json');
unlink 'tmp.json';
$out = `$prg $check2run --jsonfile $json_file $uri`;
$json = json($json_file);
unlink $json_file;
my @errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
#2
printf "%s\n", ".. pretty JSON --> $uri ";
$out = `$prg $check2run --jsonfile-pretty tmp.json $uri`;
$json = json('tmp.json');
unlink 'tmp.json';
$out = `$prg $check2run --jsonfile-pretty $json_file $uri`;
$json = json($json_file);
unlink $json_file;
@errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
#3
my $uri = "smtp-relay.gmail.com:587";
uri = "smtp-relay.gmail.com:587";
printf "%s\n", " .. plain JSON and STARTTLS --> $uri ...";
$out = `$prg --jsonfile tmp.json $check2run -t smtp $uri`;
$json = json('tmp.json');
unlink 'tmp.json';
$out = `$prg --jsonfile $json_file $check2run -t smtp $uri`;
$json = json($json_file);
unlink $json_file;
@errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
@@ -68,9 +70,9 @@ if ( $os eq "linux" ){
# This testssl.sh run deliberately does NOT work as github actions block port 25 egress.
# but the output should be fine. The idea is to have a unit test for a failed connection.
printf "%s\n", ".. plain JSON for a failed run: '--mx $uri' ...";
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile tmp.json --mx $uri`;
$json = json('tmp.json');
unlink 'tmp.json';
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile $json_file --mx $uri`;
$json = json($json_file);
unlink $json_file;
@errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
@@ -78,9 +80,9 @@ if ( $os eq "linux" ){
#5
# Same as above but with pretty JSON
printf "%s\n", ".. pretty JSON for a failed run '--mx $uri' ...";
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile-pretty tmp.json --mx $uri`;
$json = json('tmp.json');
unlink 'tmp.json';
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile-pretty $json_file --mx $uri`;
$json = json($json_file);
unlink $json_file;
@errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
@@ -90,7 +92,7 @@ if ( $os eq "linux" ){
}
done_testing($tests);
printf "\n";
printf "\n\n";
sub json($) {
my $file = shift;

View File

@@ -11,8 +11,8 @@ use Text::Diff;
my $tests = 0;
my $prg="./testssl.sh";
my $html="";
my $htmlfile="tmp.html";
my $check2run="--ip=one -4 --openssl /usr/bin/openssl --sneaky --ids-friendly --color 0 --htmlfile $htmlfile";
my $html_file="";
my $check2run="--ip=one -4 --openssl /usr/bin/openssl --sneaky --ids-friendly --color 0 --htmlfile";
my $uri="github.com";
my $out="";
my $debughtml="";
@@ -21,27 +21,27 @@ my $edited_html="";
my $diff="";
my $ip="";
die "Unable to open $prg" unless -f $prg;
printf "\n%s\n", "Doing HTML output checks";
# useful against "failed to flush stdout" messages
STDOUT->autoflush(1);
die "Unable to open $prg" unless -f $prg;
# Provide proper start conditions
unlink $htmlfile;
$html_file="tmp.html";
unlink $html_file;
# Title
printf "\n%s\n", "Unit testing HTML output ...";
#1
printf "%s\n", " .. running $prg against \"$uri\" to create HTML and terminal outputs (may take ~2 minutes)";
# specify a TERM_WIDTH so that the two calls to testssl.sh don't create HTML files with different values of TERM_WIDTH
$out = `TERM_WIDTH=120 $prg $check2run $uri`;
$html = `cat $htmlfile`;
$out = `TERM_WIDTH=120 $prg $check2run $html_file $uri`;
$html = `cat $html_file`;
# $edited_html will contain the HTML with formatting information removed in order to compare against terminal output
# Start by removing the HTML header.
$edited_html = `tail -n +11 $htmlfile`;
unlink $htmlfile;
$edited_html = `tail -n +11 $html_file`;
unlink $html_file;
# Remove the HTML footer
$edited_html =~ s/\n\<\/pre\>\n\<\/body\>\n\<\/html\>//;
@@ -74,9 +74,9 @@ if ( $^O eq "darwin" ){
#2
printf "%s\n", " .. running again $prg against \"$uri\", now with --debug 4 to create HTML output (may take another ~2 minutes)";
# Redirect stderr to /dev/null in order to avoid some unexplained "date: invalid date" error messages
$out = `TERM_WIDTH=120 $prg $check2run --debug 4 $uri 2>/dev/null`;
$debughtml = `cat $htmlfile`;
unlink $htmlfile;
$out = `TERM_WIDTH=120 $prg $check2run $html_file --debug 4 $uri 2>/dev/null`;
$debughtml = `cat $html_file`;
unlink $html_file;
# Remove date information from the Start and Done banners in the two HTML files, since they were created at different times
$html =~ s/Start 2[0-9][0-9][0-9]-[0-3][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]/Start XXXX-XX-XX XX:XX:XX/;
@@ -116,10 +116,9 @@ ok($debughtml eq $html, "Checking if HTML file created with --debug 4 matches HT
diag ("\n%s\n", "$diff");
$tests++;
printf "\n\n";
done_testing($tests);
printf "\n\n";
# vim:ts=5:sw=5:expandtab
# vim:ts=5:sw=5:expandtab