SafeBrowsing2/Storage.pm

filter: Statementstmt 16.3% 15 / 92 Branchbran 16.6% 1 / 6 Conditioncond 0.0% 0 / 76 MC/DCmcdc 0.0% 0 / 41 Subroutinesub 15.3% 4 / 26 Podpod 86.9% 20 / 23 totaltotal 15.1% 40 / 264 SCAR 72.6
CC
43
Cov
9%
add_chunks
30.0
update_error
30.0
delete_add_ckunks
20.0
CRAP 1427.4
« SafeBrowsing2.pm ↑ Summary SafeBrowsing2/DBI.pm »
1 package Net::Google::SafeBrowsing2::Storage;
2  
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 1 our $VERSION = '0.4';
11  
12 =head1 NAME
13  
14 Net::Google::SafeBrowsing2::Storage - Base class for storing the Google Safe Browsing v2 database
15  
16 =head1 SYNOPSIS
17  
18 package Net::Google::SafeBrowsing2::Sqlite;
19  
20 use base 'Net::Google::SafeBrowsing2::Storage';
21  
22 =head1 DESCRIPTION
23  
24 This is the base class for implementing a storage mechanism for the Google Safe Browsing v2 database. See L<Net::Google::SafeBrowsing2::Sqlite> for an example of implementation.
25  
26 This module cannot be used on its own as it does not actually store anything. All methods should redefined. Check the code to see which arguments are used, and what should be returned.
27  
28 =cut
29  
30  
31 =head1 CONSTRUCTOR
32  
33 =over 4
34  
35 =back
36  
37 =head2 new()
38  
39 Create a Net::Google::SafeBrowsing2::Storage object
40  
41 my $storage => Net::Google::SafeBrowsing2::Storage->new();
42  
43 =cut
44  
45 sub new {
46 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
47  
48 1 my $self = {
49 %args,
50 };
51  
52 1 ▶ bless $self, $class or croak "Can't bless $class: $!";
Branch: 1 branch
BranchTrueFalseTotal
bless $self, $class or croak("Can't bless ${class}: $!") 0 1 1
53 1 return $self;
54 }
55  
56 =head1 PUBLIC FUNCTIONS
57  
58 =over 4
59  
60 =back
61  
62 =head2 add_chunks()
63  
64 Add chunk information to the local database
65  
66 $storage->add_chunks(type => 'a', chunknum => 2154, chunks => [{host => HEX, prefix => ''}], list => 'goog-malware-shavar');
67  
68 Does not return anything.
69  
70  
71 Arguments
72  
73 =over 4
74  
75 =item type
76  
77 Required. Type of chunk: 'a' (add chunk) or 's' (sub chunk).
78  
79 =item chunknum
80  
81 Required. Chunk number.
82  
83 =item chunks
84  
85 Required. Array of chunks
86  
87 For add chunks, each element of the array is an hash reference in the following format:
88  
89 {
90 host => HEX,
91 prefix => HEX
92 }
93  
94 For sub chunks, each element of the array is an hash reference in the following format:
95  
96 {
97 host => HEX,
98 prefix => HEX,
99 add_chunknum => INTEGER
100 }
101  
102 =item list
103  
104 Required. Google Safe Browsing list name.
105  
106  
107 =back
108  
109 =cut
110  
111 sub add_chunks {
112 0 ▶ my ($self, %args) = @_;
Subroutine add_chunks: not called CC=5 SCAR=34.0
Pod documented
113 0 ▶ my $type = $args{type} || 'a';
Condition0%0/2
$args{'type'} || 'a'
l!l
00
MC/DC0%0/1
$args{'type'} || 'a'
$args{'type'}
Truth table0%0/2
A
A= $args{'type'}
Aresult
11
00
114 0 ▶ my $chunknum = $args{chunknum} || 0;
Condition0%0/2
$args{'chunknum'} || 0
l!l
00
MC/DC0%0/1
$args{'chunknum'} || 0
$args{'chunknum'}
Truth table0%0/2
A
A= $args{'chunknum'}
Aresult
11
00
115 0 ▶ my $chunks = $args{chunks} || [];
Condition0%0/2
$args{'chunks'} || []
l!l
00
MC/DC0%0/1
$args{'chunks'} || []
$args{'chunks'}
Truth table0%0/2
A
A= $args{'chunks'}
Aresult
11
00
116 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
117  
118  
119 # Save { type => $type, host => $chunk->{host}, prefix => $chunk->{prefix}, chunknum => $chunknum, list => $list }
120 }
121  
122 =head2 get_add_chunks()
123  
124 Returns a list of chunks for a given host key for all lists.
125  
126 my @chunks = $storage->get_add_chunks(hostkey => HEX);
127  
128  
129 Arguments
130  
131 =over 4
132  
133 =item hostkey.
134  
135 Required. Host key.
136  
137 =back
138  
139  
140 Return value
141  
142 =over 4
143  
144 Array of add chunks in the same format as described above:
145  
146 (
147 {
148 chunknum => 25121,
149 hostkey => hex('12345678'),
150 prefix => '',
151 list => 'goog-malware-shavar'
152 },
153 {
154 chunknum => '25121',
155 hostkey => hex('12345678'),
156 prefix => hex('2fc96b9f'),
157 list => 'goog-malware-shavar'
158 },
159 );
160  
161 =back
162  
163 =cut
164  
165 sub get_add_chunks {
166 0 ▶ my ($self, %args) = @_;
Subroutine get_add_chunks: not called CC=2 SCAR=17.9
Pod documented
167 0 ▶ my $hostkey = $args{hostkey} || '';
Condition0%0/2
$args{'hostkey'} || ''
l!l
00
MC/DC0%0/1
$args{'hostkey'} || ''
$args{'hostkey'}
Truth table0%0/2
A
A= $args{'hostkey'}
Aresult
11
00
168  
169 return (
170 {
171 0 chunknum => 25121,
172 prefix => '',
173 hostkey => $hostkey,
174 list => 'goog-malware-shavar'
175 },
176 {
177 chunknum => '25121',
178 prefix => $self->ascii_to_hex('2fc96b9f'),
179 hostkey => $hostkey,
180 list => 'goog-malware-shavar'
181 },
182 );
183 }
184  
185 =head2 get_sub_chunks()
186  
187 Returns a list of sub chunks for a given host key for all lists.
188  
189 my @chunks = $storage->get_sub_chunks(hostkey => HEX);
190  
191  
192 Arguments
193  
194 =over 4
195  
196 =item hostkey
197  
198 Required. Host key.
199  
200 =back
201  
202  
203 Return value
204  
205 =over 4
206  
207 Array of add chunks in the same format as described above:
208  
209 (
210 {
211 chunknum => 37441,
212 prefix => '',
213 addchunknum => 23911,
214 list => 'goog-malware-shavar'
215 },
216 {
217 chunknum => 37441,
218 prefix => '',
219 addchunknum => 22107,
220 list => 'goog-malware-shavar'
221 },
222 );
223  
224 =back
225  
226 =cut
227  
228 sub get_sub_chunks {
229 0 ▶ my ($self, %args) = @_;
Subroutine get_sub_chunks: not called CC=2 SCAR=17.9
Pod documented
230 0 ▶ my $hostkey = $args{hostkey} || '';
Condition0%0/2
$args{'hostkey'} || ''
l!l
00
MC/DC0%0/1
$args{'hostkey'} || ''
$args{'hostkey'}
Truth table0%0/2
A
A= $args{'hostkey'}
Aresult
11
00
231  
232  
233 return (
234 {
235 0 chunknum => 37441,
236 prefix => '',
237 addchunknum => 23911,
238 list => 'goog-malware-shavar'
239 },
240 {
241 chunknum => 37441,
242 prefix => '',
243 addchunknum => 22107,
244 list => 'goog-malware-shavar'
245 },
246 );
247 }
248  
249 =head2 get_add_chunks_nums()
250  
251 Returns a list of unique add chunk numbers for a specific list.
252  
253 B<IMPORTANT>: this list should be sorted in ascendant order.
254  
255 my @ids = $storage->get_add_chunks_nums(list => 'goog-malware-shavar');
256  
257  
258 Arguments
259  
260 =over 4
261  
262 =item list
263  
264 Required. Google Safe Browsing list name
265  
266 =back
267  
268  
269 Return value
270  
271 =over 4
272  
273 Array of integers sorted in ascendant order:
274  
275 qw(25121 25122 25123 25124 25125 25126)
276  
277 =back
278  
279 =cut
280  
281 sub get_add_chunks_nums {
282 0 ▶ my ($self, %args) = @_;
Subroutine get_add_chunks_nums: not called CC=2 SCAR=17.9
Pod documented
283 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
284  
285 0 return qw(25121 25122 25123 25124 25125 25126);
286 }
287  
288 =head2 get_sub_chunks_nums()
289  
290 Returns a list of unique sub chunk numbers for a specific list.
291  
292 B<IMPORTANT>: this list should be sorted in ascendant order.
293  
294 my @ids = $storage->get_sub_chunks_nums(list => 'goog-malware-shavar');
295  
296  
297 Arguments
298  
299 =over 4
300  
301 =item list
302  
303 Required. Google Safe Browsing list name
304  
305 =back
306  
307  
308 Return value
309  
310 =over 4
311  
312 Array of integers sorted in ascendant order:
313  
314 qw(37441 37442 37443 37444 37445 37446 37447 37448 37449 37450)
315  
316 =back
317  
318 =cut
319  
320 sub get_sub_chunks_nums {
321 0 ▶ my ($self, %args) = @_;
Subroutine get_sub_chunks_nums: not called CC=2 SCAR=17.9
Pod documented
322 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
323
324 0 return qw(37441 37442 37443 37444 37445 37446 37447 37448 37449 37450);
325 }
326  
327 =head2 delete_add_chunks()
328  
329 Delete add chunks from the local database
330  
331 $storage->delete_add_chunks(chunknums => [qw/37444 37445 37446/], list => 'goog-malware-shavar');
332  
333  
334 Arguments
335  
336 =over 4
337  
338 =item chunknums
339  
340 Required. Array of chunk numbers
341  
342 =item list
343  
344 Required. Google Safe Browsing list name
345  
346 =back
347  
348  
349 No return value
350  
351  
352 =cut
353  
354 sub delete_add_ckunks {
355 0 ▶ my ($self, %args) = @_;
Subroutine delete_add_ckunks: not called CC=4 SCAR=30.0
Pod undocumented
356 0 ▶ my $chunknums = $args{chunknums} || [];
Condition0%0/2
$args{'chunknums'} || []
l!l
00
MC/DC0%0/1
$args{'chunknums'} || []
$args{'chunknums'}
Truth table0%0/2
A
A= $args{'chunknums'}
Aresult
11
00
357 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
358  
359 0 foreach my $num (@$chunknums) {
360 # DELETE FROM [...] WHERE chunknumber = $num AND list = $list
361 }
362 }
363  
364 =head2 delete_sub_chunks()
365  
366 Delete sub chunks from the local database
367  
368 $storage->delete_sub_chunks(chunknums => [qw/37444 37445 37446/], list => 'goog-malware-shavar');
369  
370  
371 Arguments
372  
373 =over 4
374  
375 =item chunknums
376  
377 Required. Array of chunk numbers
378  
379 =item list
380  
381 Required. Google Safe Browsing list name
382  
383 =back
384  
385  
386 No return value
387  
388  
389 =cut
390  
391 sub delete_sub_ckunks {
392 0 ▶ my ($self, %args) = @_;
Subroutine delete_sub_ckunks: not called CC=4 SCAR=30.0
Pod undocumented
393 0 ▶ my $chunknums = $args{chunknums} || [];
Condition0%0/2
$args{'chunknums'} || []
l!l
00
MC/DC0%0/1
$args{'chunknums'} || []
$args{'chunknums'}
Truth table0%0/2
A
A= $args{'chunknums'}
Aresult
11
00
394 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
395  
396 0 foreach my $num (@$chunknums) {
397 # DELETE FROM [...] WHERE chunknumber = $num AND list = $list
398 }
399 }
400  
401 =head2 get_full_hashes()
402  
403 Return a list of full hashes
404  
405 $storage->get_full_hashes(chunknum => 37444, timestamp => time() - 45 * 60 * 60, list => 'goog-malware-shavar');
406  
407  
408 Arguments
409  
410 =over 4
411  
412 =item chunknum
413  
414 Required. Add chunk number
415  
416 =item timestamp
417  
418 Required. Request hashes retrieved after this timestamp value.
419  
420 =item list
421  
422 Required. Google Safe Browsing list name
423  
424 =back
425  
426 Return value
427  
428 =over 4
429  
430 Array of full hashes:
431  
432 (HEX, HEX, HEX)
433  
434 =back
435  
436  
437 =cut
438  
439 sub get_full_hashes {
440 0 ▶ my ($self, %args) = @_;
Subroutine get_full_hashes: not called CC=4 SCAR=30.0
Pod documented
441 0 ▶ my $chunknum = $args{chunknum} || 0;
Condition0%0/2
$args{'chunknum'} || 0
l!l
00
MC/DC0%0/1
$args{'chunknum'} || 0
$args{'chunknum'}
Truth table0%0/2
A
A= $args{'chunknum'}
Aresult
11
00
442 0 ▶ my $timestamp = $args{timestamp} || 0;
Condition0%0/2
$args{'timestamp'} || 0
l!l
00
MC/DC0%0/1
$args{'timestamp'} || 0
$args{'timestamp'}
Truth table0%0/2
A
A= $args{'timestamp'}
Aresult
11
00
443 0 ▶ my $list = $args{list} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
444  
445 return (
446 0 $self->ascii_to_hex('eb9744c011d332ad9c92442d18d5a0f913328ad5623983822fc86fad1aab649d'),
447 $self->ascii_to_hex('2ae11a967a5517e24c7be3fa0b8f56e7a13358ce3b07556dc251bc6b650f0f59')
448 );
449 }
450  
451 =head2 updated()
452  
453 Save information about a successful database update
454  
455 $storage->updated('time' => time(), wait => 1800, list => 'goog-malware-shavar');
456  
457  
458 Arguments
459  
460 =over 4
461  
462 =item time
463  
464 Required. Time of the update.
465  
466 =item wait
467  
468 Required. Number o seconds to wait before doing the next update.
469  
470 =item list
471  
472 Required. Google Safe Browsing list name.
473  
474 =back
475  
476  
477 No return value
478  
479 =cut
480  
481 sub updated {
482 0 ▶ my ($self, %args) = @_;
Subroutine updated: not called CC=4 SCAR=30.0
Pod documented
483 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
484 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
485 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
486  
487 # INSERT INTO [...] (last, wait, errors, list) VALUES (?, ?, 0, ?)", $time, $wait, $list);
488 }
489  
490 =head2 update_error()
491  
492 Save information about a failed database update
493  
494 $storage->update_error('time' => time(), wait => 60, list => 'goog-malware-shavar', errors => 1);
495  
496  
497 Arguments
498  
499 =over 4
500  
501 =item time
502  
503 Required. Time of the update.
504  
505 =item wait
506  
507 Required. Number o seconds to wait before doing the next update.
508  
509 =item list
510  
511 Required. Google Safe Browsing list name.
512  
513 =item errors
514  
515 Required. Number of errors.
516  
517 =back
518  
519  
520 No return value
521  
522 =cut
523  
524 sub update_error {
525 0 ▶ my ($self, %args) = @_;
Subroutine update_error: not called CC=5 SCAR=34.0
Pod documented
526 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
527 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
528 0 ▶ my $wait = $args{'wait'} || 60;
Condition0%0/2
$args{'wait'} || 60
l!l
00
MC/DC0%0/1
$args{'wait'} || 60
$args{'wait'}
Truth table0%0/2
A
A= $args{'wait'}
Aresult
11
00
529 0 ▶ my $errors = $args{errors} || 1;
Condition0%0/2
$args{'errors'} || 1
l!l
00
MC/DC0%0/1
$args{'errors'} || 1
$args{'errors'}
Truth table0%0/2
A
A= $args{'errors'}
Aresult
11
00
530  
531 # UPDATE updates SET last = $time, wait = $wait, errors = $errors, list = $list
532 }
533  
534 =head2 last_update()
535  
536 Return information about the last database update
537  
538 my $info = $storage->last_update(list => 'goog-malware-shavar');
539  
540  
541 Arguments
542  
543 =over 4
544  
545 =item list
546  
547 Required. Google Safe Browsing list name.
548  
549 =back
550  
551  
552 Return value
553  
554 =over 4
555  
556 Hash reference
557  
558 {
559 time => time(),
560 wait => 1800,
561 errors => 0
562 }
563  
564 =back
565  
566 =cut
567  
568 sub last_update {
569 0 ▶ my ($self, %args) = @_;
Subroutine last_update: not called CC=2 SCAR=17.9
Pod documented
570 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
571  
572 0 return {'time' => time(), 'wait' => 1800, errors => 0};
573 }
574  
575 =head2 add_full_hashes()
576  
577 Add full hashes to the local database
578  
579 $storage->add_full_hashes(timestamp => time(), full_hashes => [{chunknum => 2154, hash => HEX, list => 'goog-malware-shavar'}]);
580  
581  
582 Arguments
583  
584 =over 4
585  
586 =item timestamp
587  
588 Required. Time when the full hash was retrieved.
589  
590 =item full_hashes
591  
592 Required. Array of full hashes. Each element is an hash reference in the following format:
593  
594 {
595 chunknum => INTEGER,
596 hash => HEX,
597 list => 'goog-malware-shavar'
598 }
599  
600 =back
601  
602  
603 No return value
604  
605  
606 =cut
607  
608 sub add_full_hashes {
609 0 ▶ my ($self, %args) = @_;
Subroutine add_full_hashes: not called CC=4 SCAR=30.0
Pod documented
610 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
611 0 ▶ my $full_hashes = $args{full_hashes} || [];
Condition0%0/2
$args{'full_hashes'} || []
l!l
00
MC/DC0%0/1
$args{'full_hashes'} || []
$args{'full_hashes'}
Truth table0%0/2
A
A= $args{'full_hashes'}
Aresult
11
00
612  
613 0 foreach my $hash (@$full_hashes) {
614 # INSERT INTO [...] (num, hash, list, timestamp) VALUES ($hash->{chunknum}, $hash->{hash}, $hash->{list}, $timestamp);
615 }
616 }
617  
618 =head2 delete_full_hashes()
619  
620 Delete full hashes from the local database
621  
622 $storage->delete_full_hashes(chunknums => [qw/2154 2156 2158/], list => 'goog-malware-shavar');
623  
624  
625 Arguments
626  
627 =over 4
628  
629 =item chunknums
630  
631 Required. Array of chunk numbers.
632  
633 =item list
634  
635 Required. Google Safe Browsing list name.
636  
637 =back
638  
639  
640 No return value
641  
642  
643 =cut
644  
645 sub delete_full_hashes {
646 0 ▶ my ($self, %args) = @_;
Subroutine delete_full_hashes: not called CC=4 SCAR=30.0
Pod documented
647 0 ▶ my $chunknums = $args{chunknums} || [];
Condition0%0/2
$args{'chunknums'} || []
l!l
00
MC/DC0%0/1
$args{'chunknums'} || []
$args{'chunknums'}
Truth table0%0/2
A
A= $args{'chunknums'}
Aresult
11
00
648 0 ▶ my $list = $args{list} || croak "Missing list name\n";
Condition0%0/3
$args{'list'} || croak("Missing list name\n")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'list'} || croak("Missing list name\n")
$args{'list'} croak("Missing list name\n")
Truth table0%0/3
A || B
A= $args{'list'}B= croak("Missing list name\n")
ABresult
1X1
011
000
649  
650 0 foreach my $num (@$chunknums) {
651 # "DELETE FROM [...] WHERE num = $num AND list = $list
652 }
653 }
654  
655 =head2 full_hash_error()
656  
657 Save information about failed attempt to retrieve a full hash
658  
659 $storage->full_hash_error(timestamp => time(), prefix => HEX);
660  
661  
662 Arguments
663  
664 =over 4
665  
666 =item timestamp
667  
668 Required. Time when the Google returned an error.
669  
670 =item prefix
671  
672 Required. Host prefix.
673  
674 =back
675  
676  
677 No return value
678  
679  
680 =cut
681  
682 sub full_hash_error {
683 0 ▶ my ($self, %args) = @_;
Subroutine full_hash_error: not called CC=3 SCAR=24.8
Pod documented
684 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
685 0 ▶ my $prefix = $args{prefix} || '';
Condition0%0/2
$args{'prefix'} || ''
l!l
00
MC/DC0%0/1
$args{'prefix'} || ''
$args{'prefix'}
Truth table0%0/2
A
A= $args{'prefix'}
Aresult
11
00
686  
687 # Add 1 to existing error count
688 }
689  
690 =head2 full_hash_ok()
691  
692 Save information about a successful attempt to retrieve a full hash
693  
694 $storage->full_hash_ok(timestamp => time(), prefix => HEX);
695  
696  
697 Arguments
698  
699 =over 4
700  
701 =item timestamp
702  
703 Required. Time when the Google returned an error.
704  
705 =item prefix
706  
707 Required. Host prefix.
708  
709 =back
710  
711  
712 No return value
713  
714  
715 =cut
716  
717 sub full_hash_ok {
718 0 ▶ my ($self, %args) = @_;
Subroutine full_hash_ok: not called CC=3 SCAR=24.8
Pod documented
719 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
720 0 ▶ my $prefix = $args{prefix} || '';
Condition0%0/2
$args{'prefix'} || ''
l!l
00
MC/DC0%0/1
$args{'prefix'} || ''
$args{'prefix'}
Truth table0%0/2
A
A= $args{'prefix'}
Aresult
11
00
721  
722 # UPDATE full_hashes_errors SET errors = 0, timestamp = $timestamp WHERE prefix = $prefix
723 }
724  
725 =head2 get_full_hash_error()
726  
727 Save information about a successful attempt to retrieve a full hash
728  
729 my $info = $storage->get_full_hash_error(prefix => HEX);
730  
731  
732 Arguments
733  
734 =over 4
735  
736 =item prefix
737  
738 Required. Host prefix.
739  
740 =back
741  
742  
743 Return value
744  
745 =over 4
746  
747 undef if there was no error
748  
749 Hash reference in the following format if there was an error:
750  
751 {
752 timestamp => time(),
753 errors => 3
754 }
755  
756 =back
757  
758  
759 =cut
760  
761 sub get_full_hash_error {
762 0 ▶ my ($self, %args) = @_;
Subroutine get_full_hash_error: not called CC=2 SCAR=17.9
Pod documented
763 0 ▶ my $prefix = $args{prefix} || '';
Condition0%0/2
$args{'prefix'} || ''
l!l
00
MC/DC0%0/1
$args{'prefix'} || ''
$args{'prefix'}
Truth table0%0/2
A
A= $args{'prefix'}
Aresult
11
00
764  
765  
766 # no error
767 0 return undef;
768  
769 # some error
770 # return { timestamp => time(), errors => 3 }
771 }
772  
773 =head2 get_mac_keys()
774  
775 Retrieve the Message Authentication Code (MAC) keys.
776  
777 my $keys = $storage->get_mac_keys();
778  
779  
780 No arguments
781  
782  
783 Return value
784  
785 =over 4
786  
787  
788 Hash reference in the following format:
789  
790 {
791 client_key => '',
792 wrapped_key => ''
793 }
794  
795 =back
796  
797 =cut
798  
799 sub get_mac_keys {
800 0 ▶ my ($self, %args) = @_;
Subroutine get_mac_keys: not called CC=1 SCAR=6.9
Pod documented
801  
802  
803 0 return { client_key => '', wrapped_key => '' }
804 }
805  
806 =head2 delete_add_keys()
807  
808 Add the Message Authentication Code (MAC) keys.
809  
810 $storage->delete_mac_keys(client_key => 'KEY', wrapped_key => 'KEY');
811  
812  
813 Arguments
814  
815 =over 4
816  
817 =item client_key
818  
819 Required. Client key.
820  
821 =item wrapped_key
822  
823 Required. Wrapped key.
824  
825 =back
826  
827 No return value
828  
829 =cut
830  
831 sub add_mac_keys {
832 0 ▶ my ($self, %args) = @_;
Subroutine add_mac_keys: not called CC=3 SCAR=24.8
Pod undocumented
833 0 ▶ my $client_key = $args{client_key} || '';
Condition0%0/2
$args{'client_key'} || ''
l!l
00
MC/DC0%0/1
$args{'client_key'} || ''
$args{'client_key'}
Truth table0%0/2
A
A= $args{'client_key'}
Aresult
11
00
834 0 ▶ my $wrapped_key = $args{wrapped_key} || '';
Condition0%0/2
$args{'wrapped_key'} || ''
l!l
00
MC/DC0%0/1
$args{'wrapped_key'} || ''
$args{'wrapped_key'}
Truth table0%0/2
A
A= $args{'wrapped_key'}
Aresult
11
00
835  
836 # INSERT INTO ...
837  
838 }
839  
840 =head2 delete_mac_keys()
841  
842 Delete the Message Authentication Code (MAC) keys.
843  
844 $storage->delete_mac_keys();
845  
846  
847 No arguments
848  
849 No return value
850  
851 =cut
852  
853 sub delete_mac_keys {
854 0 ▶ my ($self, %args) = @_;
Subroutine delete_mac_keys: not called CC=1 SCAR=6.9
Pod documented
855  
856 # # DELETE FROM mac_keys WHERE 1
857 }
858  
859  
860 =head2 reset()
861  
862 Remove all local data
863  
864 $storage->delete_mac_keys();
865  
866  
867 Arguments
868  
869 =over 4
870  
871 =item list
872  
873 Required. Google Safe Browsing list name.
874  
875 =back
876  
877 No return value
878  
879 =cut
880  
881 sub reset {
882 0 ▶ my ($self, %args) = @_;
Subroutine reset: not called CC=2 SCAR=17.9
Pod documented
883 0 ▶ my $list = $args{'list'} || '';
Condition0%0/2
$args{'list'} || ''
l!l
00
MC/DC0%0/1
$args{'list'} || ''
$args{'list'}
Truth table0%0/2
A
A= $args{'list'}
Aresult
11
00
884  
885 # DELETE FROM s_chunks WHERE list = $list
886 # DELETE FROM a_chunks WHERE list = $list
887 # DELETE FROM full_hashes WHERE list = $list
888 # DELETE FROM full_hashes_errors WHERE list = $list
889 # DELETE FROM updates WHERE list = $list
890 }
891  
892  
893 =head1 PRIVATE FUNCTIONS
894  
895 These functions are not intended for debugging purpose.
896  
897 =over 4
898  
899 =back
900  
901 =head2 hex_to_ascii()
902  
903 Transform hexadecimal strings to printable ASCII strings. Used mainly for debugging.
904  
905 print $storage->hex_to_ascii('hex value');
906  
907 =cut
908  
909 sub hex_to_ascii {
910 0 ▶ my ($self, $hex) = @_;
Subroutine hex_to_ascii: not called CC=2 SCAR=17.9
Pod documented
911  
912  
913 0 my $ascii = '';
914  
915 0 ▶ while (length $hex > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $hex > 0) 0 0 0
916 0 $ascii .= sprintf("%02x", ord( substr($hex, 0, 1, '') ) );
917 }
918  
919 0 return $ascii;
920 }
921  
922 =head2 ascii_to_hex()
923  
924 Transform ASCII strings to hexadecimal strings.
925  
926 print $storage->ascii_to_hex('ascii value');
927  
928 =cut
929  
930 sub ascii_to_hex {
931 0 ▶ my ($self, $ascii) = @_;
Subroutine ascii_to_hex: not called CC=2 SCAR=17.9
Pod documented
932  
933 0 my $hex = '';
934 0 ▶ for (my $i = 0; $i < int(length($ascii) / 2); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < int length($ascii) / 2) 0 0 0
935 0 $hex .= chr hex( substr($ascii, $i * 2, 2) );
936 }
937  
938 0 return $hex;
939 }
940  
941 =head1 CHANGELOG
942  
943 =over 4
944  
945 =item 0.4
946  
947 Add reset mehtod to empty local database.
948  
949 =item 0.3
950  
951 Return the hostkey as part of the add chunks (get_add_chunks).
952  
953 =item 0.2
954  
955 Add functions to store and retrieve Message Authentication Code (MAC) keys.
956  
957 =back
958  
959 =head1 SEE ALSO
960  
961 See L<Net::Google::SafeBrowsing2> for handling Google Safe Browsing v2.
962  
963 See L<Net::Google::SafeBrowsing2::Sqlite> or L<Net::Google::SafeBrowsing2::MySQL> for an example of storing and managing the Google Safe Browsing database.
964  
965 Google Safe Browsing v2 API: L<http://code.google.com/apis/safebrowsing/developers_guide_v2.html>
966  
967 =head1 AUTHOR
968  
969 Julien Sobrier, E<lt>jsobrier@zscaler.comE<gt> or E<lt>julien@sobrier.netE<gt>
970  
971 =head1 COPYRIGHT AND LICENSE
972  
973 Copyright (C) 2011 by Julien Sobrier
974  
975 This library is free software; you can redistribute it and/or modify
976 it under the same terms as Perl itself, either Perl version 5.8.8 or,
977 at your option, any later version of Perl 5 you may have available.
978  
979  
980 =cut
981  
982 1 1;
« SafeBrowsing2.pm ↑ Summary SafeBrowsing2/DBI.pm »