DShield/t/move2_Q.t

filter: Statementstmt 99.2% 138 / 139 Branchbran 59.5% 25 / 42 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 88.3% 175 / 198 CC 7 SCAR 7.0
CC
7
Cov
89%
compare_files
4.0
END
2.1
next_sec
2.0
CRAP 2.0
« DShield/t/deliver.t ↑ Summary blib/lib/LaBrea/Tarpit/Report.pm »
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as erl test.pl'
3  
4 ######################### We start with some black magic to print on failure.
5  
6 # Change 1..1 below to 1..last_test_to_print .
7 # (It may become useful if the test is moved to ./t subdirectory.)
8  
9 2 ▶BEGIN { $| = 1; print "1..18\n"; }
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶END {print "not ok 1\n" unless $loaded;}
Subroutine END: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 2 2
11 2 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use LaBrea::Tarpit::DShield qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 move2_Q
14 2 );
15 2 $loaded = 1;
16 2 print "ok 1\n";
17  
18 2 $test = 2;
19  
20 20 ▶sub ok {
Subroutine ok: called CC=1 SCAR=0.0
21 20 print "ok $test\n";
22 20 ++$test;
23 }
24  
25 2 ▶sub next_sec {
Subroutine next_sec: called CC=2 SCAR=6.9
26 2 my ($then) = @_;
27 2 my $now;
28 # wait for epoch
29 2 ▶ do { select(undef,undef,undef,0.1); $now = time }
Branch: 1 branch
BranchTrueFalseTotal
$then >= $now and do { do { select undef, undef, undef, "0.1"; $now = time } } 14 2 16
30 while ( $then >= $now );
31 2 $now;
32 }
33  
34 2 ▶local $SIG{ALRM} = sub { die "test timeout"; };
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 2 umask 027;
37 2 ▶if (-d 'tmp') { # clean up previous test runs
Branch: 1 branch
BranchTrueFalseTotal
if (-d 'tmp') { } 2 0 2
38 2 opendir(T,'tmp');
39 2 @_ = grep(!/^\./, readdir(T));
40 2 closedir T;
41 2 foreach(@_) {
42 8 unlink "tmp/$_";
43 }
44 } else {
45 0 ▶ mkdir 'tmp', 0750 unless (-e 'tmp' && -d 'tmp');
Branch: 1 branch
BranchTrueFalseTotal
unless -e "tmp" and -d "tmp" 0 0 0
Condition0%0/3
-e "tmp" and -d "tmp"
!ll&&!rl&&r
000
MC/DC0%0/2
-e "tmp" and -d "tmp"
-e "tmp" -d "tmp"
Truth table0%0/3
A and B
A= -e "tmp"B= -d "tmp"
ABresult
0X0
100
111
46 }
47  
48 2 my $result1 = './dshield.result1';
49 2 my $result2 = './dshield.result2';
50 2 my $result3 = './dshield.result3';
51  
52 3 ▶sub compare_result {
Subroutine compare_result: called CC=1 SCAR=0.0
53 3 my ($cf,$cfg) = @_;
54 3 my $qf = $cfg->{DShield} . '.q.tmp';
55 3 compare_files($cf,$qf);
56 }
57  
58 5 ▶sub compare_files {
Subroutine compare_files: called CC=4 SCAR=13.9
59 5 my ($cf,$q) = @_; # comparison file, q file
60 5 local(*F);
61 5 open (F,$cf);
62 5 $cf = '';
63 5 ▶ while (<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 50 5 55
64 50 $cf .= $_;
65 }
66 5 tweak_version_string(\$cf);
67 5 close F;
68 5 open (F,$q);
69 5 $q = '';
70 5 ▶ while (<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 50 5 55
71 50 $q .= $_;
72 }
73 5 tweak_version_string(\$q);
74 5 ▶ return ($q eq $cf) ? ''
Branch: 1 branch
BranchTrueFalseTotal
$q eq $cf ? : 5 0 5
75 : "queue file:\n$q\nne expected:\n$cf\nnot ";
76 }
77  
78 10 ▶sub tweak_version_string {
Subroutine tweak_version_string: called CC=1 SCAR=0.0
79 10 my ($tp) = @_;
80 10 $$tp =~ s/VERSION/$LaBrea::Tarpit::DShield::VERSION/;
81 10 $$tp =~ s/LaBrea_Tarpit_DShield\s+[\S]+/LaBrea_Tarpit_DShield current:version:string/;
82 }
83  
84 2 ▶sub create_cache {
Subroutine create_cache: called CC=2 SCAR=6.9
85 2 my ($in,$out) = @_;
86 2 local (*IN,*OUT);
87 2 open (IN,$in);
88 2 select IN;
89 2 $| = 1;
90 2 open (OUT,'>'.$out);
91 2 select OUT;
92 2 $| = 1;
93 2 select STDOUT;
94 2 ▶ while (<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 8 2 10
95 8 print OUT $_;
96 }
97 2 close IN;
98 2 close OUT;
99 }
100  
101 2 my $config = {
102 'DShield' => 'tmp/DShield.cache', # path/to/file
103 'UserID' => '12321', # DShield UserID
104 'To' => 'test@dshield.org', # or report@dshield.org
105 'From' => 'john.doe@foo.com',
106 'Reply-To' => 'mary.doe@foo.com', # optional
107 # optional
108 'Obfuscate' => 'complete or partial',
109 # either one or more working SMTP server's
110 'smtp' => 'iceman.dshield.org,mail.euclidian.com',
111 # or a sendmail compatible mail transport command
112 'sendmail' => '/usr/lib/sendmail -t -oi',
113 };
114  
115  
116 ##
117 ## 2 trap chk_config errors
118 2 delete $config->{sendmail};
119 2 ▶print "did not trap chk_config errors\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless move2_Q($config, 2) =~ /unknown/ 0 2 2
120 unless move2_Q($config,2) =~ /unknown/;
121 2 &ok;
122  
123 2 delete $config->{Obfuscate};
124  
125 ## 3 nothing to do
126 2 $_ = move2_Q($config,2);
127 2 ▶print qq|unknown error:\n$_\nnot |
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 2 2
128 if $_;
129 2 &ok;
130  
131 2 my $ds = $config->{DShield};
132 2 my $in = './dshield.cache';
133  
134 ## 4 attempt to read directory only
135 2 $config->{DShield} = './tmp'; # directory
136 2 ▶print "accepted directory as read file\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless move2_Q($config, 2) =~ /plain file/ 0 2 2
137 unless move2_Q($config,2) =~ /plain file/;
138 2 &ok;
139  
140 ## 5 file locking
141 2 &create_cache($in,$ds);
142 2 $config->{DShield} = $ds;
143 2 local (*L,*TEST);
144 2 sysopen L, $ds . '.flock', O_RDWR|O_CREAT|O_TRUNC;
145 2 flock(L,LOCK_EX);
146  
147 2 ▶if (open(TEST,'-|')) {
Branch: 1 branch
BranchTrueFalseTotal
if (open TEST, '-|') { } 1 1 2
148 1 print (<TEST>);
149 } else {
150 1 eval {
151 1 alarm 2;
152 1 move2_Q($config,2);
153 };
154 1 alarm 0;
155 1 ▶ print "faulty lock\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /test timeout/ 0 1 1
156 unless $@ =~ /test timeout/;
157 1 &ok;
158 1 exit;
159 }
160 1 close L;
161 1 close TEST;
162 1 ++$test;
163  
164 ## 6 creat real output
165 1 $_ = move2_Q($config,2);
166 1 ▶print "failed: $_\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 1 1
167 if $_;
168 1 &ok;
169  
170 ## 7 check output against expected
171 1 print compare_result($result1,$config);
172 1 &ok;
173  
174 ## 8 obfuscate leading quad
175 1 $config->{Obfuscate} = 'partial';
176 1 $_ = move2_Q($config,2);
177 1 ▶print "failed: $_\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 1 1
178 if $_;
179 1 &ok;
180  
181 ## 9 check output against expected
182 1 print compare_result($result2,$config);
183 1 &ok;
184  
185 ## 10 obfuscate complete
186 1 $config->{Obfuscate} = 'complete';
187 1 $_ = move2_Q($config,2);
188 1 ▶print "failed: $_\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 1 1
189 if $_;
190 1 &ok;
191  
192 ## 11 check output against expected
193 1 print compare_result($result3,$config);
194 1 &ok;
195  
196 1 my $now = &next_sec(time);
197  
198 ## 12 create qF file
199 1 $_ = move2_Q($config,1);
200 1 ▶print "failed: $_\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 1 1
201 if $_;
202 1 &ok;
203  
204 1 $config->{DShield} =~ m|(.*/)|;
205 1 my $dir = $1;
206  
207 ## 13 check that it is there
208 1 my $qf = 'qF' . $now . '.' . $$ .'.'. 0;
209 1 ▶print "failed to create queue file\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir . $qf 0 1 1
210 unless -e $dir . $qf;
211 1 &ok;
212  
213 ## 14 check the contents
214 1 print compare_files($result3,$dir . $qf);
215 1 &ok;
216  
217 ## 15 check for original delete and Q creation
218  
219 1 my $qf1 = $qf;
220  
221 1 $now = &next_sec(time);
222  
223 ## 15 create qF file, and delete dshield file
224 1 $_ = move2_Q($config);
225 1 ▶print "failed: $_\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 1 1
226 if $_;
227 1 &ok;
228  
229 ## 16 check that it is there
230 1 $qf = 'qF' . $now . '.' . $$ .'.'. 0;
231 1 ▶print "failed to create queue file\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir . $qf 0 1 1
232 unless -e $dir . $qf;
233 1 &ok;
234  
235 ## 17 check the contents
236 1 print compare_files($result3,$dir . $qf);
237 1 &ok;
238  
239 ## 18 check that previous queue file exists
240 1 ▶print "first queue file destroyed\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir . $qf1 0 1 1
241 unless -e $dir . $qf1;
242 1 &ok;
« DShield/t/deliver.t ↑ Summary blib/lib/LaBrea/Tarpit/Report.pm »