SafeBrowsing4/Storage/File.pm

filter: Statementstmt 15.9% 38 / 239 Branchbran 4.6% 6 / 130 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 41.6% 10 / 24 Podpod 85.7% 12 / 14 totaltotal 12.5% 66 / 527 CC 99 SCAR 48.2
CC
99
Cov
10%
save
380.0
get_prefixes
182.0
add_full_hashes
132.0
CRAP 124.1
« SafeBrowsing4.pm ↑ Summary SafeBrowsing4/URI.pm »
1 package Net::Google::SafeBrowsing4::Storage::File;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base qw(Net::Google::SafeBrowsing4::Storage);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Storable qw(nstore retrieve);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use List::BinarySearch qw(binsearch);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 3 our $VERSION = '0.2';
16  
17 =head1 NAME
18  
19 Net::Google::SafeBrowsing4::Storage::File - File storage for the Google Safe Browsing v4 database
20  
21 =head1 SYNOPSIS
22  
23 use Net::Google::SafeBrowsing4::Storage::File;
24  
25 my $storage = Net::Google::SafeBrowsing4::Storage::File->new(path => '.');
26 $storage->save(...);
27  
28 =head1 DESCRIPTION
29  
30 This is the a file-based implementation of L<Net::Google::SafeBrowsing4::Storage> to manage the Google Safe Browsing v4 local database.
31  
32 =cut
33  
34  
35 =head1 CONSTRUCTOR
36  
37 =over 4
38  
39 =back
40  
41 =head2 new()
42  
43 Create a Net::Google::SafeBrowsing4::Storage::File object
44  
45 my $storage => Net::Google::SafeBrowsing4::Storage::File->new(path => '.');
46  
47 Arguments
48  
49 =over 4
50  
51 =item path
52  
53 Optional. Path to store the database files Use current directory by default.
54  
55 =item keep_all
56  
57 Optional. Set to 1 to keep old information (such as expiring full hashes) in the database. 0 (delete) by default.
58  
59 =item sticky
60  
61 Optional. Set to 1 to if you are going to do multiple lookup. More memory will be used but lookup will be sped up. 0 by default.
62  
63 =item files
64  
65 Optional. Hash reference to map file types to file names. Default:
66  
67 {
68 updates => "updates.gdb4",
69 full_hashes => "full_hashes.gsb4"
70 }
71  
72 =item logger
73  
74 Optional. Log4Perl compatible object reference. By default this option is unset, making Net::Google::SafeBrowsing4::Storage::File silent.
75  
76 =back
77  
78 =cut
79  
80 sub new {
81 11 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
82  
83 11 my $self = { # default arguments
84 keep_all => 0,
85 path => '.',
86 sticky => 0,
87 files => {
88 updates => "updates.gdb4",
89 full_hashes => "full_hashes.gsb4"
90 },
91 data => { },
92 %args,
93 };
94  
95 11 ▶ bless($self, $class) or croak("Can't bless $class: $!");
Branch: 1 branch
BranchTrueFalseTotal
bless $self, $class or croak("Can't bless ${class}: $!") 0 11 11
96  
97 11 $self->init();
98  
99 11 return $self;
100 }
101  
102 =head1 PUBLIC FUNCTIONS
103  
104 =over 4
105  
106 See L<Net::Google::SafeBrowsing4::Storage> for a complete list of public functions.
107  
108 =back
109  
110 =head2 close()
111  
112 Cleanup old full hashes, and close the connection to the database.
113  
114 $storage->close();
115  
116 =cut
117  
118  
119 sub init {
120 11 ▶ my ($self, %args) = @_;
Subroutine init: called CC=6 SCAR=20.5
Pod undocumented
121  
122 # make sure path exists
123 11 ▶ if (! -d $self->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"path"}) 0 11 11
124 0 ▶ mkdir($self->{path}) or croak("Cannot create directory " . $self->{path} . ": $!\n");
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->{"path"} or croak('Cannot create directory ' . $self->{"path"} . ": $!\n") 0 0 0
125 }
126  
127 # file to hold all updates
128 11 my $file = path(join("/", $self->{path}, $self->{files}->{updates}));
129 11 ▶ if (! -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $file) 1 10 11
130 1 my %update = (last_update => 0, next_update => 0, errors => 0);
131 1 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 1 1
132 0 $self->{data}->{ $self->{files}->{updates} } = { %update };
133 }
134  
135 1 ▶ nstore(\%update, $file) or croak("Cannot store information into $file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
nstore(\%update, $file) or croak("Cannot store information into ${file}: $!\n") 0 1 1
136 }
137 }
138  
139 sub save {
140 0 ▶ my ($self, %args) = @_;
Subroutine save: not called CC=19 SCAR=59.4
Pod documented
141 0 ▶ my $list = $args{list} || croak("Missing list information");
Condition0%0/3
$args{'list'} || croak('Missing list information')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'list'} || croak('Missing list information')
$args{'list'} croak('Missing list information')
Truth table0%0/3
A || B
A= $args{'list'}B= croak('Missing list information')
ABresult
1X1
011
000
142 0 ▶ my $override = $args{override} || 0;
Condition0%0/2
$args{'override'} || 0
l!l
00
MC/DC0%0/1
$args{'override'} || 0
$args{'override'}
Truth table0%0/2
A
A= $args{'override'}
Aresult
11
00
143 0 ▶ my @hashes = @{ $args{add} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$args{"add"} or [] 0 0 0
144 0 ▶ my @remove = @{ $args{remove} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$args{"remove"} or [] 0 0 0
145 0 ▶ my $state = $args{'state'} || '';
Condition0%0/2
$args{'state'} || ''
l!l
00
MC/DC0%0/1
$args{'state'} || ''
$args{'state'}
Truth table0%0/2
A
A= $args{'state'}
Aresult
11
00
146  
147 # save the information somewhere
148 0 my $file = path(join("/", $self->{path}, $self->list_to_file($list)));
149 0 ▶ $self->{logger} && $self->{logger}->debug("Save hashes to $file");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Save hashes to $file") 0 0 0
150  
151 0 my %data = ('state' => $state, hashes => [@hashes]); # hashes are already stored
152 0 ▶ if (-e $file && !$override) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and not $override) 0 0 0
Condition0%0/3
-e $file and not $override
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and not $override
-e $file not $override
Truth table0%0/3
A and not(B)
A= -e $fileB= not $override
ABresult
0X0
100
111
153 0 my $db = retrieve($file);
154 0 ▶ $self->{logger} && $self->{logger}->debug("Load $file (save)");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Load $file (save)") 0 0 0
155  
156 0 ▶ $self->{logger} && $self->{logger}->debug("hashes to remove: ", scalar(@remove));
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("hashes to remove: ", scalar @remove) 0 0 0
157 0 ▶ $self->{logger} && $self->{logger}->debug("hashes to add: ", scalar(@hashes));
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("hashes to add: ", scalar @hashes) 0 0 0
158  
159 0 ▶ $self->{logger} && $self->{logger}->debug("Number of hashes before removal: ", scalar(@{ $db->{hashes} }));
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Number of hashes before removal: ", scalar @{$$db{"hashes"};}) 0 0 0
160 0 foreach my $index (@remove) {
161 0 ▶ $self->{logger} && $self->{logger}->debug("Remove index $index");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Remove index $index") 0 0 0
162 0 $db->{hashes}->[$index] = '';
163 }
164 0 $db->{hashes} = [ grep { $_ ne '' } @{ $db->{hashes} } ];
165 0 ▶ $self->{logger} && $self->{logger}->debug("Number of hashes after removal: ", scalar(@{ $db->{hashes} }));
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Number of hashes after removal: ", scalar @{$$db{"hashes"};}) 0 0 0
166  
167 0 $data{hashes} = [sort { $a cmp $b } (@hashes, @{ $db->{hashes} })];
168 }
169  
170 0 ▶ nstore(\%data, $file) or croak("Cannot save data to $file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
nstore(\%data, $file) or croak("Cannot save data to ${file}: $!\n") 0 0 0
171 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
172 0 $self->{data}->{ $self->list_to_file($list) } = { %data };
173 }
174  
175 # return the list of hashes, sorted, from the new storage
176 0 ▶ $self->{logger} && $self->{logger}->debug("Number of hashes at end: ", scalar(@{ $data{hashes} }));
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Number of hashes at end: ", scalar @{$data{"hashes"};}) 0 0 0
177 0 return @{ $data{hashes} };
178 }
179  
180  
181  
182 sub reset {
183 0 ▶ my ($self, %args) = @_;
Subroutine reset: not called CC=3 SCAR=24.8
Pod documented
184 0 ▶ my $list = $args{list} || croak("Missing list information");
Condition0%0/3
$args{'list'} || croak('Missing list information')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'list'} || croak('Missing list information')
$args{'list'} croak('Missing list information')
Truth table0%0/3
A || B
A= $args{'list'}B= croak('Missing list information')
ABresult
1X1
011
000
185  
186 0 my $file = path(join("/", $self->{path}, $self->list_to_file($list)));
187 0 unlink($file);
188  
189 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
190 0 $self->{data}->{ $self->list_to_file($list) } = { };
191 }
192 }
193  
194  
195 sub next_update {
196 0 ▶ my ($self, %args) = @_;
Subroutine next_update: not called CC=6 SCAR=37.4
Pod documented
197  
198 # make sure the file exists
199 0 $self->init();
200  
201 0 my $update = { };
202 0 ▶ if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{updates} })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sticky'} and exists $self->{'data'}{$self->{'files'}{'updates'}}) { } 0 0 0
Condition0%0/3
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"updates"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"updates"}}
$self->{"sticky"} exists $self->{"data"}{$self->{"files"}{"updates"}}
Truth table0%0/3
A and B
A= $self->{"sticky"}B= exists $self->{"data"}{$self->{"files"}{"updates"}}
ABresult
0X0
100
111
203 0 $update = $self->{data}->{ $self->{files}->{updates} };
204 }
205 else {
206 # retrieve information from storage
207 0 my $file = path(join("/", $self->{path}, $self->{files}->{updates}));
208 0 $update = retrieve($file);
209 0 ▶ $self->{logger} && $self->{logger}->debug("Load $file (reset)");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Load $file (reset)") 0 0 0
210  
211 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
212 0 $self->{data}->{ $self->{files}->{updates} } = $update;;
213 }
214 }
215  
216 0 ▶ return $update->{next_update} || 0;
Condition0%0/2
$update->{'next_update'} || 0
l!l
00
MC/DC0%0/1
$update->{'next_update'} || 0
$update->{'next_update'}
Truth table0%0/2
A
A= $update->{'next_update'}
Aresult
11
00
217 }
218  
219 sub last_update {
220 0 ▶ my ($self, %args) = @_;
Subroutine last_update: not called CC=7 SCAR=40.3
Pod documented
221  
222 # make sure the file exists
223 0 $self->init();
224  
225 0 my $update = { };
226 0 ▶ if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{updates} })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sticky'} and exists $self->{'data'}{$self->{'files'}{'updates'}}) { } 0 0 0
Condition0%0/3
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"updates"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"updates"}}
$self->{"sticky"} exists $self->{"data"}{$self->{"files"}{"updates"}}
Truth table0%0/3
A and B
A= $self->{"sticky"}B= exists $self->{"data"}{$self->{"files"}{"updates"}}
ABresult
0X0
100
111
227 0 $update = $self->{data}->{ $self->{files}->{updates} };
228 }
229 else {
230 # retrieve information from storage
231 0 my $file = path(join("/", $self->{path}, $self->{files}->{updates}));
232 0 $update = retrieve($file);
233 0 ▶ $self->{logger} && $self->{logger}->debug("Load $file (last_udpate)");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Load $file (last_udpate)") 0 0 0
234  
235 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
236 0 $self->{data}->{ $self->{files}->{updates} } = $update;
237 }
238 }
239  
240 0 ▶ return { last_update => $update->{last_update} || 0, errors => $update->{errors} || 0 };
Condition0%0/4
$update->{'last_update'} || 0
l!l
00
$update->{'errors'} || 0
l!l
00
MC/DC0%0/1
$update->{'last_update'} || 0
$update->{'last_update'}
MC/DC0%0/1
$update->{'errors'} || 0
$update->{'errors'}
Truth table0%0/2
A
A= $update->{'last_update'}
Aresult
11
00
Truth table0%0/2
A
A= $update->{'errors'}
Aresult
11
00
241 }
242  
243  
244 sub get_state {
245 0 ▶ my ($self, %args) = @_;
Subroutine get_state: not called CC=8 SCAR=42.8
Pod documented
246 0 ▶ my $list = $args{list} || croak("Missing list information\n");
Condition0%0/3
$args{'list'} || croak("Missing list information\n")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'list'} || croak("Missing list information\n")
$args{'list'} croak("Missing list information\n")
Truth table0%0/3
A || B
A= $args{'list'}B= croak("Missing list information\n")
ABresult
1X1
011
000
247  
248 0 my $update = { };
249 0 ▶ if ($self->{sticky} && exists($self->{data}->{ $self->list_to_file($list) })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sticky'} and exists $self->{'data'}{$self->list_to_file($list)}) { } 0 0 0
Condition0%0/3
$self->{"sticky"} and exists $self->{"data"}{$self->list_to_file($list)}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sticky"} and exists $self->{"data"}{$self->list_to_file($list)}
$self->{"sticky"} exists $self->{"data"}{$self->list_to_file($list)}
Truth table0%0/3
A and B
A= $self->{"sticky"}B= exists $self->{"data"}{$self->list_to_file($list)}
ABresult
0X0
100
111
250 0 $update = $self->{data}->{ $self->list_to_file($list) };
251 }
252 else {
253 0 my $file = path(join("/", $self->{path}, $self->list_to_file($list)));
254 0 ▶ if (! -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $file) { } 0 0 0
255 0 return "";
256 }
257 else {
258 0 ▶ $self->{logger} && $self->{logger}->debug("Load $file (get_state)");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Load $file (get_state)") 0 0 0
259 0 $update = retrieve($file);
260  
261 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
262 0 $self->{data}->{ $self->list_to_file($list) } = $update;
263 }
264 }
265 }
266  
267 0 ▶ return $update->{'state'} || '';
Condition0%0/2
$update->{'state'} || ''
l!l
00
MC/DC0%0/1
$update->{'state'} || ''
$update->{'state'}
Truth table0%0/2
A
A= $update->{'state'}
Aresult
11
00
268 }
269  
270 sub updated {
271 0 ▶ my ($self, %args) = @_;
Subroutine updated: not called CC=8 SCAR=42.8
Pod documented
272 0 ▶ my $time = $args{'time'} || time();
Condition0%0/3
$args{'time'} || time
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'time'} || time
$args{'time'} time
Truth table0%0/3
A || B
A= $args{'time'}B= time
ABresult
1X1
011
000
273 0 ▶ my $next = $args{'next'} || time() + 1800;
Condition0%0/3
$args{'next'} || time + 1800
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'next'} || time + 1800
$args{'next'} time + 1800
Truth table0%0/3
A || B
A= $args{'next'}B= time + 1800
ABresult
1X1
011
000
274  
275 # next update applies to all lists, save it
276 # make sure the file exists
277 0 $self->init();
278  
279 0 my $file = path(join("/", $self->{path}, $self->{files}->{updates}));
280 0 my $update = { };
281 0 ▶ if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{updates} })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sticky'} and exists $self->{'data'}{$self->{'files'}{'updates'}}) { } 0 0 0
Condition0%0/3
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"updates"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"updates"}}
$self->{"sticky"} exists $self->{"data"}{$self->{"files"}{"updates"}}
Truth table0%0/3
A and B
A= $self->{"sticky"}B= exists $self->{"data"}{$self->{"files"}{"updates"}}
ABresult
0X0
100
111
282 0 $update = $self->{data}->{ $self->{files}->{updates} };
283 }
284 else {
285 # retrieve information from storage
286 0 ▶ $self->{logger} && $self->{logger}->debug("Load $file (updated)");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Load $file (updated)") 0 0 0
287 0 $update = retrieve($file);
288 }
289  
290 0 $update->{next_update} = $next;
291 0 $update->{last_udpate} = $time;
292 0 $update->{errors} = 0;
293  
294 0 ▶ nstore($update, $file) or croak("Cannot save data to $file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
nstore($update, $file) or croak("Cannot save data to ${file}: $!\n") 0 0 0
295  
296 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
297 0 $self->{data}->{ $self->{files}->{updates} } = $update;
298 }
299 }
300  
301  
302 sub update_error {
303 0 ▶ my ($self, %args) = @_;
Subroutine update_error: not called CC=9 SCAR=45.0
Pod documented
304 0 ▶ my $time = $args{'time'} || time();
Condition0%0/3
$args{'time'} || time
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'time'} || time
$args{'time'} time
Truth table0%0/3
A || B
A= $args{'time'}B= time
ABresult
1X1
011
000
305 0 ▶ my $wait = $args{'wait'} || 1800;
Condition0%0/2
$args{'wait'} || 1800
l!l
00
MC/DC0%0/1
$args{'wait'} || 1800
$args{'wait'}
Truth table0%0/2
A
A= $args{'wait'}
Aresult
11
00
306 0 ▶ my $errors = $args{errors} || 0;
Condition0%0/2
$args{'errors'} || 0
l!l
00
MC/DC0%0/1
$args{'errors'} || 0
$args{'errors'}
Truth table0%0/2
A
A= $args{'errors'}
Aresult
11
00
307  
308 # make sure the file exists
309 0 $self->init();
310  
311 0 my $file = path(join("/", $self->{path}, $self->{files}->{updates}));
312 0 my $update = { };
313 0 ▶ if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{updates} })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sticky'} and exists $self->{'data'}{$self->{'files'}{'updates'}}) { } 0 0 0
Condition0%0/3
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"updates"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"updates"}}
$self->{"sticky"} exists $self->{"data"}{$self->{"files"}{"updates"}}
Truth table0%0/3
A and B
A= $self->{"sticky"}B= exists $self->{"data"}{$self->{"files"}{"updates"}}
ABresult
0X0
100
111
314 0 $update = $self->{data}->{ $self->{files}->{updates} };
315 }
316 else {
317 # retrieve information from storage
318 0 ▶ $self->{logger} && $self->{logger}->debug("Load $file (update_error)");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Load $file (update_error)") 0 0 0
319 0 $update = retrieve($file);
320 }
321  
322 0 $update->{next_update} = $time + $wait;
323 0 $update->{last_udpate} = $time;
324 0 $update->{errors} = $errors;
325  
326 0 ▶ nstore($update, $file) or croak("Cannot save data to $file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
nstore($update, $file) or croak("Cannot save data to ${file}: $!\n") 0 0 0
327 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
328 0 $self->{data}->{ $self->{files}->{updates} } = $update;
329 }
330 }
331  
332  
333 sub get_prefixes {
334 0 ▶ my ($self, %args) = @_;
Subroutine get_prefixes: not called CC=13 SCAR=52.0
Pod documented
335 0 ▶ my @lists = @{ $args{lists} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$args{"lists"} or [] 0 0 0
336 0 ▶ my @hashes = @{ $args{hashes} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$args{"hashes"} or [] 0 0 0
337 0 my @data = ();
338  
339 0 ▶ $self->{logger} && $self->{logger}->debug("Number of lists: ", scalar(@lists));
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Number of lists: ", scalar @lists) 0 0 0
340 0 foreach my $list (@lists) {
341 0 my $db = { };
342 0 ▶ if ($self->{sticky} && exists($self->{data}->{ $self->list_to_file($list) })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sticky'} and exists $self->{'data'}{$self->list_to_file($list)}) { } 0 0 0
Condition0%0/3
$self->{"sticky"} and exists $self->{"data"}{$self->list_to_file($list)}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sticky"} and exists $self->{"data"}{$self->list_to_file($list)}
$self->{"sticky"} exists $self->{"data"}{$self->list_to_file($list)}
Truth table0%0/3
A and B
A= $self->{"sticky"}B= exists $self->{"data"}{$self->list_to_file($list)}
ABresult
0X0
100
111
343 0 $db = $self->{data}->{ $self->list_to_file($list) };
344 }
345 else {
346 0 my $file = path(join("/", $self->{path}, $self->list_to_file($list)));
347 0 ▶ if (! -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
348 0 ▶ $self->{logger} && $self->{logger}->debug("File $file does not exist");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("File $file does not exist") 0 0 0
349 0 next;
350 }
351  
352 0 ▶ $self->{logger} && $self->{logger}->debug("Load $file (get_prefixes)");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Load $file (get_prefixes)") 0 0 0
353 0 $db = retrieve($file);
354  
355 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
356 0 $self->{data}->{ $self->list_to_file($list) } = $db;
357 }
358 }
359  
360 0 foreach my $hash (@hashes) {
361 0 my $prefix = undef;
362 0 ▶ my $index = binsearch {substr($a,0,length($b)) cmp $b} $hash, @{ $db->{hashes} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
363 0 ▶ if (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) 0 0 0
364 0 $prefix = $db->{hashes}->[$index];
365 0 push(@data, { prefix => $prefix, list => $list });
366 }
367 }
368 }
369  
370 0 return @data;
371 }
372  
373  
374 sub add_full_hashes {
375 0 ▶ my ($self, %args) = @_;
Subroutine add_full_hashes: not called CC=11 SCAR=48.8
Pod documented
376 0 ▶ my @hashes = @{ $args{hashes} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$args{"hashes"} or [] 0 0 0
377 0 ▶ my $timestamp = $args{timestamp} || time();
Condition0%0/3
$args{'timestamp'} || time
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'timestamp'} || time
$args{'timestamp'} time
Truth table0%0/3
A || B
A= $args{'timestamp'}B= time
ABresult
1X1
011
000
378  
379  
380 0 my $file = path(join("/", $self->{path}, $self->{files}->{full_hashes}));
381 0 my $db = { hashes => [] };
382 0 ▶ if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{full_hashes} })) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'sticky'} and exists $self->{'data'}{$self->{'files'}{'full_hashes'}}) { } 0 0 0
elsif (-e $file) { } 0 0 0
Condition0%0/3
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"full_hashes"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"full_hashes"}}
$self->{"sticky"} exists $self->{"data"}{$self->{"files"}{"full_hashes"}}
Truth table0%0/3
A and B
A= $self->{"sticky"}B= exists $self->{"data"}{$self->{"files"}{"full_hashes"}}
ABresult
0X0
100
111
383 0 $db = $self->{data}->{ $self->{files}->{full_hashes} };
384 }
385 elsif (-e $file) {
386 0 $db = retrieve($file);
387 }
388  
389 0 foreach my $hash (@hashes) {
390 0 my $cache = $hash->{cache};
391 0 $cache =~ s/s//;
392 0 ▶ $self->{logger} && $self->{logger}->debug("cache: $cache");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("cache: $cache") 0 0 0
393  
394 0 $hash->{expire} = $cache + $timestamp;
395 0 push(@{ $db->{hashes} }, $hash);
396 }
397  
398 0 ▶ $self->{logger} && $self->{logger}->debug("Save ", scalar(@{ $db->{hashes} }), " full hashes to $file");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Save ", scalar @{$$db{"hashes"};}, " full hashes to $file") 0 0 0
399 0 ▶ nstore($db, $file) or croak("Cannot save data to $file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
nstore($db, $file) or croak("Cannot save data to ${file}: $!\n") 0 0 0
400  
401 0 ▶ if ($self->{sticky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sticky"}) 0 0 0
402 0 $self->{data}->{ $self->{files}->{full_hashes} } = $db;
403 }
404  
405 0 return (@{ $db->{hashes} });
406 }
407  
408  
409 sub get_full_hashes {
410 0 ▶ my ($self, %args) = @_;
Subroutine get_full_hashes: not called CC=10 SCAR=47.0
Pod documented
411 0 ▶ my @lists = @{ $args{lists} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$args{"lists"} or [] 0 0 0
412 0 ▶ my $hash = $args{hash} || return ();
Condition0%0/2
$args{'hash'} || (return ())
l!l
00
MC/DC0%0/1
$args{'hash'} || (return ())
$args{'hash'}
Truth table0%0/2
A
A= $args{'hash'}
Aresult
11
00
413  
414 0 my $db = { };
415 0 ▶ if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{full_hashes} })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sticky'} and exists $self->{'data'}{$self->{'files'}{'full_hashes'}}) { } 0 0 0
Condition0%0/3
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"full_hashes"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sticky"} and exists $self->{"data"}{$self->{"files"}{"full_hashes"}}
$self->{"sticky"} exists $self->{"data"}{$self->{"files"}{"full_hashes"}}
Truth table0%0/3
A and B
A= $self->{"sticky"}B= exists $self->{"data"}{$self->{"files"}{"full_hashes"}}
ABresult
0X0
100
111
416 0 $db = $self->{data}->{ $self->{files}->{full_hashes} };
417 }
418 else {
419 0 my $file = path(join("/", $self->{path}, $self->{files}->{full_hashes}));
420 0 ▶ if (! -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
421 0 return ();
422 }
423  
424 0 ▶ $self->{logger} && $self->{logger}->debug("Load $file");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Load $file") 0 0 0
425 0 $db = retrieve($file);
426 }
427  
428 0 my @hashes = ();
429 0 ▶ $self->{logger} && $self->{logger}->debug("Number of full hashes on file: ", scalar(@{ $db->{hashes} }));
Branch: 1 branch
BranchTrueFalseTotal
$self->{"logger"} and $self->{"logger"}->debug("Number of full hashes on file: ", scalar @{$$db{"hashes"};}) 0 0 0
430 0 foreach my $list (@lists) {
431 my $result = first {
432 $_->{hash} eq $hash &&
433 $_->{list}->{threatEntryType} eq $list->{threatEntryType} &&
434 $_->{list}->{threatType} eq $list->{threatType} &&
435 $_->{list}->{platformType} eq $list->{platformType} &&
436 $_->{expire} > time()
437 0 ▶ } @{ $db->{hashes} };
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
$_->{"hash"} eq $hash and $_->{"list"}{"threatEntryType"} eq $list->{"threatEntryType"} and $_->{"list"}{"threatType"} eq $list->{"threatType"} and $_->{"list"}{"platformType"} eq $list->{"platformType"} and $_->{"expire"} > time 0 0 0
Condition0%0/9
$_->{"hash"} eq $hash and $_->{"list"}{"threatEntryType"} eq $list->{"threatEntryType"} and $_->{"list"}{"threatType"} eq $list->{"threatType"} and $_->{"list"}{"platformType"} eq $list->{"platformType"}
!ll&&!rl&&r
000
$_->{"hash"} eq $hash and $_->{"list"}{"threatEntryType"} eq $list->{"threatEntryType"} and $_->{"list"}{"threatType"} eq $list->{"threatType"}
!ll&&!rl&&r
000
$_->{"hash"} eq $hash and $_->{"list"}{"threatEntryType"} eq $list->{"threatEntryType"}
!ll&&!rl&&r
000
MC/DC0%0/4
$_->{"hash"} eq $hash and $_->{"list"}{"threatEntryType"} eq $list->{"threatEntryType"} and $_->{"list"}{"threatType"} eq $list->{"threatType"} and $_->{"list"}{"platformType"} eq $list->{"platformType"}
$_->{"hash"} eq $hash $_->{"list"}{"threatEntryType"} eq $list->{"threatEntryType"} $_->{"list"}{"threatType"} eq $list->{"threatType"} $_->{"list"}{"platformType"} eq $list->{"platformType"}
Truth table0%0/5
A and B and C and D
A= $_->{"hash"} eq $hashB= $_->{"list"}{"threatEntryType"} eq $list->{"threatEntryType"}C= $_->{"list"}{"threatType"} eq $list->{"threatType"}D= $_->{"list"}{"platformType"} eq $list->{"platformType"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
438  
439 0 ▶ push(@hashes, $result) if (defined($result));
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 0 0 0
440 }
441  
442 0 return @hashes;
443 }
444  
445  
446 sub list_to_file {
447 0 ▶ my ($self, $list) = @_;
Subroutine list_to_file: not called CC=1 SCAR=6.9
Pod undocumented
448  
449 0 return join("_", $list->{threatType}, $list->{platformType}, $list->{threatEntryType}) . ".gsb4";
450 }
451  
452  
453 sub close {
454 0 ▶ my ($self, %args) = @_;
Subroutine close: not called CC=5 SCAR=34.0
Pod documented
455  
456 0 ▶ if ($self->{keep_all} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_all"} == 0) 0 0 0
457 0 return;
458 }
459  
460 0 my $file = path(join("/", $self->{path}, $self->{files}->{full_hashes}));
461 0 ▶ if (! -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
462 0 return;
463 }
464  
465 0 my $db = retrieve($file);
466  
467 0 my @results = grep { $_->{expire} > time() } @{ $db->{hashes} };
468 0 ▶ if (scalar(@results) < scalar(@{ $db->{hashes} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @results < scalar @{$db->{"hashes"};}) 0 0 0
469 0 $db->{hashes} = [@results];
470 0 ▶ nstore($db, $file) or croak("Cannot save data to $file: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
nstore($db, $file) or croak("Cannot save data to ${file}: $!\n") 0 0 0
471 }
472  
473 0 $self->{data} = { };
474 }
475  
476 =head1 CHANGELOG
477  
478 =over 4
479  
480  
481 =item 0.1
482  
483 Initial release
484  
485 =back
486  
487 =head1 SEE ALSO
488  
489 See L<Net::Google::SafeBrowsing4> for handling Google Safe Browsing v4.
490  
491 See L<Net::Google::SafeBrowsing4::Storage> for the list of public functions.
492  
493 Google Safe Browsing v4 API: L<https://developers.google.com/safe-browsing/v4/>
494  
495  
496 =head1 AUTHOR
497  
498 Julien Sobrier, E<lt>julien@sobrier.netE<gt>
499  
500 =head1 COPYRIGHT AND LICENSE
501  
502 Copyright (C) 2016 by Julien Sobrier
503  
504 This library is free software; you can redistribute it and/or modify
505 it under the same terms as Perl itself, either Perl version 5.8.8 or,
506 at your option, any later version of Perl 5 you may have available.
507  
508  
509 =cut
510  
511 3 1;
« SafeBrowsing4.pm ↑ Summary SafeBrowsing4/URI.pm »