SafeBrowsing2/Sqlite.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 32 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 176 SCAR 45.0
CC
9
Cov
0%
init
56.0
add_chunks_s
6.0
add_chunks_a
6.0
CRAP 90.0
1 package Net::Google::SafeBrowsing2::Sqlite;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Net::Google::SafeBrowsing2::DBI';
7  
8 use Carp;
9 use DBI;
10 use List::Util qw(first);
11  
12  
13 our $VERSION = '0.9';
14  
15  
16 =head1 NAME
17  
18 Net::Google::SafeBrowsing2::Sqlite - Sqlite as back-end storage for the Google Safe Browsing v2 database
19  
20 =head1 SYNOPSIS
21  
22 use Net::Google::SafeBrowsing2::Sqlite;
23  
24 my $storage = Net::Google::SafeBrowsing2::Sqlite->new(file => 'google-v2.db');
25 ...
26 $storage->close();
27  
28 =head1 DESCRIPTION
29  
30 This is an implementation of L<Net::Google::SafeBrowsing2::Storage> using Sqlite.
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::SafeBrowsing2::Sqlite object
44  
45 my $storage = Net::Google::SafeBrowsing2::Sqlite->new(file => 'google-v2.db');
46  
47 Arguments
48  
49 =over 4
50  
51 =item file
52  
53 Required. File to store the database.
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 cache_size
60  
61 Sqlite cache size. 20000 by default.
62  
63 =back
64  
65  
66 =cut
67  
68 sub new {
69 my ($class, %args) = @_;
70  
71 my $self = { # default arguments
72 keep_all => 0,
73 file => 'gsb2.db',
74 cache_size => 20000,
75  
76 %args,
77 };
78  
79 bless $self, $class or croak "Can't bless $class: $!";
80  
81  
82 $self->init();
83  
84 return $self;
85 }
86  
87 =head1 PUBLIC FUNCTIONS
88  
89 =over 4
90  
91 See L<Net::Google::SafeBrowsing2::Storage> for a complete list of public functions.
92  
93 =back
94  
95 =head2 close()
96  
97 Cleanup old full hashes, and close the connection to the database.
98  
99 $storage->close();
100  
101 =cut
102  
103 sub init {
104 my ($self, %args) = @_;
105  
106 $self->{dbh} = DBI->connect("dbi:SQLite:dbname=" . $self->{file}, "", "");
107 $self->{dbh}->do("PRAGMA journal_mode = OFF");
108 $self->{dbh}->do("PRAGMA synchronous = OFF");
109 $self->{dbh}->do("PRAGMA cache_size = " . $self->{cache_size});
110  
111 my @tables = $self->{dbh}->tables;
112  
113 if (! defined first { $_ eq '"main"."updates"' || $_ eq '"updates"' } @tables) {
114 $self->create_table_updates();
115 }
116 if (! defined first { $_ eq '"main"."a_chunks"' || $_ eq '"a_chunks"' } @tables) {
117 $self->create_table_a_chunks();
118 }
119 if (! defined first { $_ eq '"main"."s_chunks"' || $_ eq '"s_chunks"' } @tables) {
120 $self->create_table_s_chunks();
121 }
122 if (! defined first { $_ eq '"main"."full_hashes"' || $_ eq '"full_hashes"' } @tables) {
123 $self->create_table_full_hashes();
124 }
125 if (! defined first { $_ eq '"main"."full_hashes_errors"' || $_ eq '"full_hashes_errors"' } @tables) {
126 $self->create_table_full_hashes_errors();
127 }
128 if (! defined first { $_ eq '"main"."mac_keys"' || $_ eq '"mac_keys"' } @tables) {
129 $self->create_table_mac_keys();
130 }
131  
132 # Remove indexes created in previous versions
133 $self->{dbh}->do('DROP INDEX IF EXISTS a_chunks_hostkey;');
134 $self->{dbh}->do('DROP INDEX IF EXISTS a_chunks_num_list;');
135  
136 $self->{dbh}->do('DROP INDEX IF EXISTS s_chunks_hostkey;');
137 $self->{dbh}->do('DROP INDEX IF EXISTS s_chunks_num;');
138 }
139  
140  
141 sub create_table_updates {
142 my ($self, %args) = @_;
143  
144 my $schema = qq{
145 CREATE TABLE updates (
146 last INTEGER DEFAULT 0,
147 wait INTEGER DEFAULT 1800,
148 errors INTEGER DEFAULT 0,
149 list TEXT
150 );
151 }; # Need to handle errors
152  
153 $self->{dbh}->do($schema);
154 }
155  
156 sub create_table_a_chunks {
157 my ($self, %args) = @_;
158  
159 my $schema = qq{
160 CREATE TABLE a_chunks (
161 hostkey TEXT,
162 prefix TEXT,
163 num INTEGER,
164 list TEXT
165 );
166 };
167  
168 $self->{dbh}->do($schema);
169  
170 my $index = qq{
171 CREATE INDEX a_chunks_hostkey ON a_chunks (
172 hostkey
173 );
174 };
175 # $self->{dbh}->do($index);
176  
177 $index = qq{
178 CREATE INDEX a_chunks_num_list ON a_chunks (
179 num,
180 list
181 );
182 };
183 # $self->{dbh}->do($index);
184  
185 $index = qq{
186 CREATE UNIQUE INDEX a_chunks_unique ON a_chunks (
187 hostkey,
188 prefix,
189 num,
190 list
191 );
192 };
193 $self->{dbh}->do($index);
194 }
195  
196 sub create_table_s_chunks {
197 my ($self, %args) = @_;
198  
199 my $schema = qq{
200 CREATE TABLE s_chunks (
201 hostkey TEXT,
202 prefix TEXT,
203 num INTEGER,
204 add_num INTEGER,
205 list TEXT
206 );
207 };
208  
209 $self->{dbh}->do($schema);
210  
211 my $index = qq{
212 CREATE INDEX s_chunks_hostkey ON s_chunks (
213 hostkey
214 );
215 };
216 # $self->{dbh}->do($index);
217  
218 $index = qq{
219 CREATE INDEX s_chunks_num ON s_chunks (
220 num
221 );
222 };
223 # $self->{dbh}->do($index);
224  
225 $index = qq{
226 CREATE UNIQUE INDEX s_chunks_unique ON s_chunks (
227 hostkey,
228 prefix,
229 num,
230 add_num,
231 list
232 );
233 };
234 $self->{dbh}->do($index);
235 }
236  
237 sub create_table_full_hashes {
238 my ($self, %args) = @_;
239  
240 my $schema = qq{
241 CREATE TABLE full_hashes (
242 id INTEGER PRIMARY KEY AUTOINCREMENT,
243 num INTEGER,
244 hash TEXT,
245 list TEXT,
246 timestamp INTEGER
247 );
248 };
249  
250 $self->{dbh}->do($schema);
251  
252 my $index = qq{
253 CREATE UNIQUE INDEX hash ON full_hashes (
254 num,
255 hash,
256 list
257 );
258 };
259 $self->{dbh}->do($index);
260 }
261  
262 sub create_table_full_hashes_errors {
263 my ($self, %args) = @_;
264  
265 my $schema = qq{
266 CREATE TABLE full_hashes_errors (
267 id INTEGER PRIMARY KEY AUTOINCREMENT,
268 errors INTEGER,
269 prefix TEXT,
270 timestamp INTEGER
271 );
272 };
273  
274 $self->{dbh}->do($schema);
275 }
276  
277 sub create_table_mac_keys{
278 my ($self, %args) = @_;
279  
280 my $schema = qq{
281 CREATE TABLE mac_keys (
282 client_key TEXT Default '',
283 wrapped_key TEXT Default ''
284 );
285 };
286  
287 $self->{dbh}->do($schema);
288 }
289  
290  
291 sub add_chunks_s {
292 my ($self, %args) = @_;
293 my $chunknum = $args{chunknum} || 0;
294 my $chunks = $args{chunks} || [];
295 my $list = $args{'list'} || '';
296  
297 my $add = $self->{dbh}->prepare('INSERT OR IGNORE INTO s_chunks (hostkey, prefix, num, add_num, list) VALUES (?, ?, ?, ?, ?)');
298  
299 $self->{dbh}->begin_work;
300 foreach my $chunk (@$chunks) {
301 $add->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list );
302 }
303  
304 if (scalar @$chunks == 0) { # keep empty chunks
305 $add->execute( '', '', $chunknum, '', $list );
306 }
307 $self->{dbh}->commit;
308 }
309  
310 sub add_chunks_a {
311 my ($self, %args) = @_;
312 my $chunknum = $args{chunknum} || 0;
313 my $chunks = $args{chunks} || [];
314 my $list = $args{'list'} || '';
315  
316 my $add = $self->{dbh}->prepare('INSERT OR IGNORE INTO a_chunks (hostkey, prefix, num, list) VALUES (?, ?, ?, ?)');
317  
318 $self->{dbh}->begin_work;
319 foreach my $chunk (@$chunks) {
320 $add->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $list );
321 }
322  
323 if (scalar @$chunks == 0) { # keep empty chunks
324 $add->execute( '', '', $chunknum, $list );
325 }
326 $self->{dbh}->commit;
327 }
328  
329  
330 =head1 CHANGELOG
331  
332 =over 4
333  
334 =item 0.9
335  
336 Keep empty sub chunks. Improve performances (remove sone idexes, use trnsactions).
337  
338 New cache_size option.
339  
340 =item 0.8
341  
342 Index s_chunks_unique was created at the wrong place. Thanks to colinmkeith.
343  
344 =item 0.7
345  
346 Add option keep_all to keep expired full hashes. Useful for debugging.
347  
348 =item 0.6
349  
350 Use more efficient add_chunk_a and add_chunk_s functions.
351  
352 =item 0.5
353  
354 Use base class L<Net::Google::SafeBrowsing2::DBI>.
355  
356 =item 0.4
357  
358 Disable journalization. This speeds up updated by about 10x.
359  
360 =item 0.3
361  
362 Fix typos in the documentation.
363  
364 =item 0.2
365  
366 Add close() function to clean up old full hashes, and to close the connection to the database cleanly.
367  
368 Add table and function to store and retrieve the Message Authentication Code (MAC) key.
369  
370 In some environments, the module was trying to re-create existing tables. Fixed (Thank you to Luis Alberto Perez).
371  
372 =back
373  
374 =head1 SEE ALSO
375  
376 See L<Net::Google::SafeBrowsing2> for handling Google Safe Browsing v2.
377  
378 See L<Net::Google::SafeBrowsing2::Storage> for the list of public functions.
379  
380 See L<Net::Google::SafeBrowsing2::MySQL> for a back-end using Sqlite.
381  
382 Google Safe Browsing v2 API: L<http://code.google.com/apis/safebrowsing/developers_guide_v2.html>
383  
384  
385 =head1 AUTHOR
386  
387 Julien Sobrier, E<lt>jsobrier@zscaler.comE<gt> or E<lt>julien@sobrier.netE<gt>
388  
389 =head1 COPYRIGHT AND LICENSE
390  
391 Copyright (C) 2011 by Julien Sobrier
392  
393 This library is free software; you can redistribute it and/or modify
394 it under the same terms as Perl itself, either Perl version 5.8.8 or,
395 at your option, any later version of Perl 5 you may have available.
396  
397  
398 =cut
399  
400 1;