Skip to content

Commit 7823699

Browse files
committed
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
1 parent 964e892 commit 7823699

2 files changed

Lines changed: 38 additions & 37 deletions

File tree

t/31_isJSON_valid.t

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env perl
22

3-
# This is more a PoC. Improvements welcome!
3+
# Checking whether both JSON outputs are valid
44
#
55

66
use strict;
@@ -9,7 +9,8 @@ use JSON;
99

1010
my $tests = 0;
1111
my $prg="./testssl.sh";
12-
my $json="tmp.json";
12+
my $json="";
13+
my $json_file="";
1314
my $check2run ="--ip=one --ids-friendly -q --color 0";
1415
my $uri="example.com"; # Cloudflare blocks too often
1516
my $out="";
@@ -26,36 +27,37 @@ STDOUT->autoflush(1);
2627
die "Unable to open $prg" unless -f $prg;
2728

2829
# Provide proper start conditions
29-
unlink $json;
30+
$json_file="tmp.json";
31+
unlink $json_file;
3032

3133
# Title
3234
printf "\n%s\n", "Unit testing JSON output ...";
3335

3436
#1
3537
printf "%s\n", ".. plain JSON --> $uri ";
36-
$out = `$prg $check2run --jsonfile tmp.json $uri`;
37-
$json = json('tmp.json');
38-
unlink 'tmp.json';
38+
$out = `$prg $check2run --jsonfile $json_file $uri`;
39+
$json = json($json_file);
40+
unlink $json_file;
3941
my @errors=eval { decode_json($json) };
4042
is(@errors,0,"no errors");
4143
$tests++;
4244

4345
#2
4446
printf "%s\n", ".. pretty JSON --> $uri ";
45-
$out = `$prg $check2run --jsonfile-pretty tmp.json $uri`;
46-
$json = json('tmp.json');
47-
unlink 'tmp.json';
47+
$out = `$prg $check2run --jsonfile-pretty $json_file $uri`;
48+
$json = json($json_file);
49+
unlink $json_file;
4850
@errors=eval { decode_json($json) };
4951
is(@errors,0,"no errors");
5052
$tests++;
5153

5254

5355
#3
54-
my $uri = "smtp-relay.gmail.com:587";
56+
uri = "smtp-relay.gmail.com:587";
5557
printf "%s\n", " .. plain JSON and STARTTLS --> $uri ...";
56-
$out = `$prg --jsonfile tmp.json $check2run -t smtp $uri`;
57-
$json = json('tmp.json');
58-
unlink 'tmp.json';
58+
$out = `$prg --jsonfile $json_file $check2run -t smtp $uri`;
59+
$json = json($json_file);
60+
unlink $json_file;
5961
@errors=eval { decode_json($json) };
6062
is(@errors,0,"no errors");
6163
$tests++;
@@ -68,19 +70,19 @@ if ( $os eq "linux" ){
6870
# This testssl.sh run deliberately does NOT work as github actions block port 25 egress.
6971
# but the output should be fine. The idea is to have a unit test for a failed connection.
7072
printf "%s\n", ".. plain JSON for a failed run: '--mx $uri' ...";
71-
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile tmp.json --mx $uri`;
72-
$json = json('tmp.json');
73-
unlink 'tmp.json';
73+
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile $json_file --mx $uri`;
74+
$json = json($json_file);
75+
unlink $json_file;
7476
@errors=eval { decode_json($json) };
7577
is(@errors,0,"no errors");
7678
$tests++;
7779

7880
#5
7981
# Same as above but with pretty JSON
8082
printf "%s\n", ".. pretty JSON for a failed run '--mx $uri' ...";
81-
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile-pretty tmp.json --mx $uri`;
82-
$json = json('tmp.json');
83-
unlink 'tmp.json';
83+
$out = `$prg --ssl-native --openssl-timeout=10 $check2run --jsonfile-pretty $json_file --mx $uri`;
84+
$json = json($json_file);
85+
unlink $json_file;
8486
@errors=eval { decode_json($json) };
8587
is(@errors,0,"no errors");
8688
$tests++;
@@ -90,7 +92,7 @@ if ( $os eq "linux" ){
9092
}
9193

9294
done_testing($tests);
93-
printf "\n";
95+
printf "\n\n";
9496

9597
sub json($) {
9698
my $file = shift;

t/32_isHTML_valid.t

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use Text::Diff;
1111
my $tests = 0;
1212
my $prg="./testssl.sh";
1313
my $html="";
14-
my $htmlfile="tmp.html";
15-
my $check2run="--ip=one -4 --openssl /usr/bin/openssl --sneaky --ids-friendly --color 0 --htmlfile $htmlfile";
14+
my $html_file="";
15+
my $check2run="--ip=one -4 --openssl /usr/bin/openssl --sneaky --ids-friendly --color 0 --htmlfile";
1616
my $uri="github.com";
1717
my $out="";
1818
my $debughtml="";
@@ -21,27 +21,27 @@ my $edited_html="";
2121
my $diff="";
2222
my $ip="";
2323

24-
die "Unable to open $prg" unless -f $prg;
25-
26-
printf "\n%s\n", "Doing HTML output checks";
27-
2824
# useful against "failed to flush stdout" messages
2925
STDOUT->autoflush(1);
3026

27+
die "Unable to open $prg" unless -f $prg;
3128

3229
# Provide proper start conditions
33-
unlink $htmlfile;
30+
$html_file="tmp.html";
31+
unlink $html_file;
3432

33+
# Title
34+
printf "\n%s\n", "Unit testing HTML output ...";
3535

3636
#1
3737
printf "%s\n", " .. running $prg against \"$uri\" to create HTML and terminal outputs (may take ~2 minutes)";
3838
# specify a TERM_WIDTH so that the two calls to testssl.sh don't create HTML files with different values of TERM_WIDTH
39-
$out = `TERM_WIDTH=120 $prg $check2run $uri`;
40-
$html = `cat $htmlfile`;
39+
$out = `TERM_WIDTH=120 $prg $check2run $html_file $uri`;
40+
$html = `cat $html_file`;
4141
# $edited_html will contain the HTML with formatting information removed in order to compare against terminal output
4242
# Start by removing the HTML header.
43-
$edited_html = `tail -n +11 $htmlfile`;
44-
unlink $htmlfile;
43+
$edited_html = `tail -n +11 $html_file`;
44+
unlink $html_file;
4545

4646
# Remove the HTML footer
4747
$edited_html =~ s/\n\<\/pre\>\n\<\/body\>\n\<\/html\>//;
@@ -74,9 +74,9 @@ if ( $^O eq "darwin" ){
7474
#2
7575
printf "%s\n", " .. running again $prg against \"$uri\", now with --debug 4 to create HTML output (may take another ~2 minutes)";
7676
# Redirect stderr to /dev/null in order to avoid some unexplained "date: invalid date" error messages
77-
$out = `TERM_WIDTH=120 $prg $check2run --debug 4 $uri 2>/dev/null`;
78-
$debughtml = `cat $htmlfile`;
79-
unlink $htmlfile;
77+
$out = `TERM_WIDTH=120 $prg $check2run $html_file --debug 4 $uri 2>/dev/null`;
78+
$debughtml = `cat $html_file`;
79+
unlink $html_file;
8080

8181
# Remove date information from the Start and Done banners in the two HTML files, since they were created at different times
8282
$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
116116
diag ("\n%s\n", "$diff");
117117
$tests++;
118118

119-
120-
printf "\n\n";
121119
done_testing($tests);
120+
printf "\n\n";
122121

123122

124-
# vim:ts=5:sw=5:expandtab
123+
# vim:ts=5:sw=5:expandtab
125124

0 commit comments

Comments
 (0)