testssl.sh/t/07_isJSON_valid.t

111 lines
2.8 KiB
Perl
Raw Normal View History

#!/usr/bin/env perl
use strict;
use Test::More;
use JSON;
my $tests = 0;
my $prg="./testssl.sh";
2019-08-12 12:38:47 +02:00
my $check2run ="--ip=one -q --color 0";
2020-01-30 19:27:20 +01:00
my $filechecks = "--jsonfile-pretty tmp.json --csvfile tmp.csv";
my $uri="cloudflare.com";
my $json="";
2020-01-30 19:27:20 +01:00
my $csv="";
my $out=""; # not in use currently
# Blacklists we use to trigger an error:
my $socket_regex_bl='(e|E)rror|\.\/testssl\.sh: line |(f|F)atal';
my $openssl_regex_bl='(e|E)rror|(f|F)atal|\.\/testssl\.sh: line |Oops|s_client connect problem';
die "Unable to open $prg" unless -f $prg;
2020-01-30 19:27:20 +01:00
# Provide proper start conditions
unlink 'tmp.json';
2020-01-30 19:27:20 +01:00
unlink 'tmp.csv';
printf "\n%s\n", "Unit testing JSON and CSV output ...";
2020-01-13 23:50:14 +01:00
#1
2020-01-30 19:27:20 +01:00
printf "%s\n", ".. CSV and plain JSON --> $uri ";
$out = `$prg $check2run $filechecks $uri`;
$json = json('tmp.json');
2020-01-30 19:27:20 +01:00
$csv = csv('tmp.csv');
unlink 'tmp.json';
2020-01-30 19:27:20 +01:00
unlink 'tmp.csv';
my @errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
2020-01-30 19:27:20 +01:00
#2
# check header ~ "id","fqdn/ip","port","severity","finding","cve","cwe"
# macth or count: "cipherorder", ~ cert_ ~cert\ , HTTP, HSTS , cipher_, clientsimulation
# later: not FATAL
#2
printf "%s\n", ".. pretty JSON --> $uri ";
2020-01-30 19:27:20 +01:00
$out = `$prg $check2run $filechecks $uri`;
$json = json('tmp.json');
unlink 'tmp.json';
2020-01-30 19:27:20 +01:00
unlink 'tmp.csv';
@errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
#3
# This testss.sh run deliberately does NOT work as travis-ci.org blocks port 25 egress.
# but the output should be fine. The idea is to have a unit test for a failed connection.
2020-01-30 19:27:20 +01:00
printf "%s\n", ".. plain JSON for a failed run: '--mx $uri' (deliberate messy output follows)";
$out = `$prg --ssl-native --openssl-timeout=10 $check2run $filechecks --mx $uri`;
$json = json('tmp.json');
2020-01-30 19:27:20 +01:00
$csv = csv('tmp.csv');
unlink 'tmp.json';
2020-01-30 19:27:20 +01:00
unlink 'tmp.csv';
printf "$csv\n";
@errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
2020-01-30 19:27:20 +01:00
# check both for FATAL / Can't connect / scanProblem
#4
2020-01-30 19:27:20 +01:00
# Same as #3 with pretty JSON
printf "%s\n", ".. pretty JSON for a failed run '--mx $uri' (deliberate messy output follows)";
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile-pretty tmp.json --csvfile tmp.csv --mx $uri`;
$json = json('tmp.json');
unlink 'tmp.json';
2020-01-30 19:27:20 +01:00
unlink 'tmp.csv';
@errors=eval { decode_json($json) };
is(@errors,0,"no errors");
$tests++;
2020-01-30 19:27:20 +01:00
# check both for FATAL / Can't connect / scanProblem
2019-04-17 09:04:39 +02:00
#5
my $uri = "smtp-relay.gmail.com:587";
printf "%s\n", " .. plain JSON and STARTTLS --> $uri ...";
2020-01-30 19:27:20 +01:00
$out = `$prg $filechecks $check2run -t smtp $uri`;
2019-04-17 09:04:39 +02:00
$json = json('tmp.json');
2020-01-30 19:27:20 +01:00
$csv = csv('tmp.csv');
2019-04-17 09:04:39 +02:00
unlink 'tmp.json';
2020-01-30 19:27:20 +01:00
unlink 'tmp.csv';
@errors=eval { decode_json($json) };
2019-04-17 09:04:39 +02:00
is(@errors,0,"no errors");
$tests++;
done_testing($tests);
2020-01-30 19:27:20 +01:00
printf "\n";
sub json($) {
my $file = shift;
$file = `cat $file`;
return from_json($file);
}
2020-01-30 19:27:20 +01:00
sub csv($) {
my $file = shift;
$file = `cat $file`;
return ($file);
}