blib/lib/DBIx/Class/RandomColumns.pm

filter: Statementstmt 100% 79 / 79 Branchbran 81.2% 26 / 32 Conditioncond 55.5% 15 / 27 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 10 / 10 Podpod 83.3% 5 / 6 totaltotal 81.5% 137 / 168 CC 31 SCAR 22.8
CC
31
Cov
87%
random_columns
14.4
get_random_value
13.4
insert
4.1
CRAP 9.8
↑ Summary
1 package DBIx::Class::RandomColumns;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.005001';
7  
8 1 ▶use DBIx::Class 0.08009;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use parent qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 __PACKAGE__->mk_group_accessors(
13 inherited => qw(
14 _random_columns
15 max_dup_checks
16 default_field_size
17 default_text_set
18 default_int_max
19 default_int_min
20 )
21 );
22  
23 1 __PACKAGE__->max_dup_checks(100);
24 1 __PACKAGE__->default_field_size(32);
25 1 __PACKAGE__->default_text_set(['0'..'9', 'a'..'z']);
26 1 __PACKAGE__->default_int_max(2*31-1);
27 1 __PACKAGE__->default_int_min(0);
28  
29 =head1 NAME
30  
31 DBIx::Class::RandomColumns - Implicit Random Columns
32  
33 =head1 SYNOPSIS
34  
35 package My::Schema::Result::Utz;
36 use parent 'DBIx::Class::Core';
37  
38 __PACKAGE__->load_components(qw/RandomColumns/);
39 __PACKAGE__->table('utz');
40 __PACKAGE__->add_columns(qw(id foo bar baz));
41 __PACKAGE__->random_columns('id', bar => {size => 10});
42  
43 package My::Schema::Result::Gnarf;
44 use parent 'DBIx::Class::Core';
45  
46 __PACKAGE__->load_components(qw/RandomColumns/);
47 __PACKAGE__->table('gnarf');
48 __PACKAGE__->add_columns(
49 id => {
50 datatype => 'integer',
51 extra => {unsigned => 1},
52 is_random => {max => 2**32-1, min => 0}
53 },
54 foo => {
55 datatype => 'int',
56 size => 10,
57 },
58 bar => {
59 datatype => 'varchar',
60 is_random => {size => 10},
61 size => 32,
62 },
63 baz => {
64 datatype => 'varchar',
65 size => 255,
66 },
67 );
68  
69 =head1 VERSION
70  
71 This is version 0.004000
72  
73 =head1 DESCRIPTION
74  
75 This DBIx::Class component makes columns implicitly create random values.
76  
77 The main reason why this module exists is to generate unpredictable primary
78 keys to add some additional security to web applications. Most forms of
79 char and integer field types are supported.
80  
81 =head1 METHODS
82  
83 =cut
84  
85 2 ▶sub add_columns {
Subroutine add_columns: called CC=4 SCAR=13.9
Pod undocumented
86 2 my $class = shift;
87 2 my @random_columns;
88 2 my ($info, $opt);
89  
90 2 $class->next::method(@_);
91  
92 2 for my $column ($class->columns) {
93 24 $info = $class->column_info($column);
94 $opt = $info->{is_random}
95 24 ▶ or next;
Branch: 1 branch
BranchTrueFalseTotal
$opt = $info->{"is_random"} or next 11 13 24
96 11 push @random_columns, $column;
97 11 ▶ push @random_columns, $opt
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt eq "HASH" 5 6 11
98 if ref($opt) eq 'HASH';
99 }
100 2 $class->random_columns(@random_columns);
101  
102 2 return; # nothing
103 }
104  
105 =head2 remove_column
106  
107 Hooks into L<DBIx::Class::ResultSource/remove_column> to remove the
108 random column configuration for the given column.
109  
110 =cut
111  
112 2 ▶sub remove_column {
Subroutine remove_column: called CC=1 SCAR=0.0
Pod documented
113 2 my $class = shift;
114  
115 2 delete $class->random_columns->{$_[0]};
116  
117 2 return $class->next::method(@_);
118 }
119  
120 =head2 remove_columns
121  
122 Hooks into L<DBIx::Class::ResultSource/remove_columns> to remove the
123 random column configuration for the given columns.
124  
125 =cut
126  
127 2 ▶sub remove_columns {
Subroutine remove_columns: called CC=2 SCAR=6.9
Pod documented
128 2 my $class = shift;
129 2 my $random_columns = $class->random_columns;
130  
131 2 delete $random_columns->{$_} for @_;
132  
133 2 return $class->next::method(@_);
134 }
135  
136 =head2 random_columns
137  
138 __PACKAGE__->random_columns(@column_names);
139 __PACKAGE__->random_columns(name1 => \%options1, name2 => \%options2);
140 __PACKAGE__->random_columns(name1, name2 => \%options2);
141 $random_columns = __PACKAGE__->random_columns;
142  
143 Define or query fields that get random strings at creation. Each column
144 name may be followed by a hash reference containing options. In case no
145 explicit options are given, the method tries to find reasonable values.
146  
147 Valid options are:
148  
149 =over
150  
151 =item C<max>:
152  
153 Maximum number for integer fields. Defaults to C<2**31-1>. Must be an
154 integer number, that is greater than C<min> and should be positive.
155  
156 =item C<min>:
157  
158 Minimum number for integer fields. Defaults to C<0>. Must be an integer
159 number, that is lower than C<max> and can be negative unless the
160 corresponding field is not an unsigned integer.
161  
162 =item C<set>:
163  
164 A string or an array reference that contains the set of characters to use
165 for building a random content for string fields. The default set is
166 C<['0'..'9', 'a'..'z']>.
167  
168 =item C<size>:
169  
170 Length of the random string to create. Defaults to the size of the column or - if this
171 cannot be determined for whatever reason - to 32.
172  
173 =item C<check>:
174  
175 Search table before insert until generated column value is not found.
176 Defaults to false and must be set to a true value to activate.
177 Provided Perl's rand() function has sufficient entropy this lookup is only
178 usefull for short fields, because with the default set there are
179 C<36^field-size> possible combinations.
180  
181 =back
182  
183 Returns a hash reference, with column names of the random columns as keys and
184 hash references as values, that contain the random column settings.
185  
186 =cut
187  
188 45 ▶sub random_columns {
Subroutine random_columns: called CC=14 SCAR=26.7
Pod documented
189 45 my $class = shift;
190 45 ▶ my $random_auto_columns = $class->_random_columns || {};
Condition100%2/2
$class->_random_columns || {}
l!l
423
MC/DC100%1/1
$class->_random_columns || {}
$class->_random_columns
Truth table100%2/2
A
A= $class->_random_columns
Aresult
11
00
191  
192 # act as read accessor when no arguments are given
193 45 ▶ return $random_auto_columns unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 43 2 45
194  
195 2 my ($col, $info, $opt);
196  
197 # loop over argument list
198 2 ▶ while ($col = shift @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col = shift @_) 22 2 24
199 22 $info = $class->column_info($col);
200 22 ▶ $class->throw_exception(qq{column "$col" doesn't exist})
Branch: 1 branch
BranchTrueFalseTotal
unless $class->has_column($col) 0 22 22
201 unless $class->has_column($col);
202 22 ▶ $opt = ref $_[0] eq 'HASH' ? shift(@_) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 10 12 22
203  
204 # set auto column settings of current column
205 # as a hash reference in $class->_random_columns;
206 # the hash may contain:
207 # min: minimum value for integer fields
208 # max: maximum value for integer fields
209 # set: set of character to build a random string
210 # size: size of string fields
211 # check: true=on / false=off
212 22 my %conf = (check => $opt->{check});
213  
214 22 ▶ if (defined $opt->{max}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opt->{'max'}) { } 8 14 22
elsif (lc($info->{'data_type'} or '') =~ /^ (?: var(?:char2?|binary) | (?:char(?:acter(?:\s+varying)?)?) | binary | (?:tiny|medium|long)?blob | (?:tiny|medium|long)?text | clob | comment | bytea ) $/x) { } 12 2 14
215 8 $conf{max} = $opt->{max};
216 8 ▶ $conf{min} = $opt->{min} || 0;
Condition100%2/2
$opt->{'min'} || 0
l!l
62
MC/DC100%1/1
$opt->{'min'} || 0
$opt->{'min'}
Truth table100%2/2
A
A= $opt->{'min'}
Aresult
11
00
217 }
218 ▶ elsif (
Condition50%1/2
$info->{'data_type'} || ''
l!l
140
MC/DC0%0/1
$info->{'data_type'} || ''
$info->{'data_type'}
Truth table50%1/2
A
A= $info->{'data_type'}
Aresult
11
00
219 lc($info->{data_type} || '') =~ /^
220 (?:
221 var(?:char2?|binary) |
222 (?:char(?:acter(?:\s+varying)?)?) |
223 binary |
224 (?:tiny|medium|long)?blob |
225 (?:tiny|medium|long)?text |
226 clob |
227 comment |
228 bytea
229 )
230 $/x
231 ) {
232 $conf{set} = defined($opt->{set}) ?
233 ref($opt->{set}) ?
234 12 ▶ $opt->{set} : [split //, $opt->{set}] :
Branch: 2 branches
BranchTrueFalseTotal
defined $opt->{'set'} ? : 2 10 12
ref $opt->{'set'} ? : 2 0 2
235 $class->default_text_set;
236 $conf{size} = $opt->{size} ||
237 $info->{size} ||
238 12 ▶ $class->default_field_size;
Condition50%3/6
$opt->{'size'} || $info->{'size'} || $class->default_field_size
l!l&&r!l&&!r
1200
$opt->{"size"} or $info->{"size"}
l!l&&r!l&&!r
2100
MC/DC0%0/3
$opt->{'size'} || $info->{'size'} || $class->default_field_size
$opt->{"size"} $info->{"size"} $class->default_field_size
Truth table50%2/4
A or B || C
A= $opt->{"size"}B= $info->{"size"}C= $class->default_field_size
ABCresult
1XX1
01X1
0011
0000
239 }
240 else {
241 2 $conf{max} = 2**31-1;
242 2 $conf{min} = 0;
243 }
244 22 $random_auto_columns->{$col} = \%conf;
245 }
246  
247 # set internal class variable _random_columns
248 2 return $class->_random_columns($random_auto_columns);
249 }
250  
251 =head2 insert
252  
253 Hooks into L<DBIx::Class::Row/insert> to create a random value for each
254 L<random column|/random_columns> that is not defined.
255  
256 =cut
257  
258 4 ▶sub insert {
Subroutine insert: called CC=4 SCAR=14.0
Pod documented
259 4 my $self = shift;
260  
261 4 my $accessor;
262 4 for (keys %{$self->random_columns}) {
263 32 ▶ next if defined $self->get_column($_); # skip if defined
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get_column($_) 4 28 32
264  
265 28 ▶ $accessor = $self->column_info($_)->{accessor} || $_;
Condition33%1/3
$self->column_info($_)->{'accessor'} || $_
l!l&&r!l&&!r
0280
MC/DC0%0/2
$self->column_info($_)->{'accessor'} || $_
$self->column_info($_)->{'accessor'} $_
Truth table33%1/3
A || B
A= $self->column_info($_)->{'accessor'}B= $_
ABresult
1X1
011
000
266 28 $self->$accessor($self->get_random_value($_));
267 }
268 4 return $self->next::method;
269 }
270  
271 =head2 get_random_value
272  
273 $value = $instance->get_random_value($column_name);
274  
275 Compute a random value for the given C<$column_name>.
276  
277 Throws an exception if the concerning column has not been declared
278 as a random column.
279  
280 =cut
281  
282 32 ▶sub get_random_value {
Subroutine get_random_value: called CC=11 SCAR=25.9
Pod documented
283 32 my $self = shift;
284 32 my $column = shift;
285 32 ▶ my $conf = $self->random_columns->{$column}
Branch: 1 branch
BranchTrueFalseTotal
my $conf = $self->random_columns->{$column} or $self->throw_exception(qq[column "$column" is not a random column]) 32 0 32
286 or $self->throw_exception(qq{column "$column" is not a random column});
287 32 my $check = $conf->{check};
288 32 my $tries = $self->max_dup_checks;
289 32 my $id;
290  
291 32 ▶ if ($conf->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{'max'}) { } 16 16 32
292 # it's an integer column
293 16 ▶ do { # check uniqueness if check => 1 for this column
Branch: 1 branch
BranchTrueFalseTotal
$check and $tries-- and $self->result_source->resultset->search({$column, $id})->count and do { do { $id = int(rand $conf->{"max"} - $conf->{"min"} + 1) + $conf->{"min"} } } 0 16 16
Condition33%2/6
$check and $tries-- and $self->result_source->resultset->search({$column, $id})->count
!ll&&!rl&&r
1600
$check and $tries--
!ll&&!rl&&r
1600
MC/DC0%0/3
$check and $tries-- and $self->result_source->resultset->search({$column, $id})->count
$check $tries-- $self->result_source->resultset->search({$column, $id})->count
Truth table25%1/4
A and B and C
A= $checkB= $tries--C= $self->result_source->resultset->search({$column, $id})->count
ABCresult
0XX0
10X0
1100
1111
294 $id = int(rand($conf->{max} - $conf->{min} + 1)) + $conf->{min}
295 16 } while $check and
296 $tries-- and
297 $self->result_source->resultset->search({$column => $id})->count;
298 }
299 else {
300 16 my $set = $conf->{set};
301 16 ▶ do { # check uniqueness if check => 1 for this column
Branch: 1 branch
BranchTrueFalseTotal
$check and $tries-- and $self->result_source->resultset->search({$column, $id})->count and do { do { $id = ""; $id .= $$set[int rand @$set] foreach (1 .. $conf->{"size"}) } } 0 16 16
Condition66%4/6
$check and $tries-- and $self->result_source->resultset->search({$column, $id})->count
!ll&&!rl&&r
1240
$check and $tries--
!ll&&!rl&&r
1204
MC/DC0%0/3
$check and $tries-- and $self->result_source->resultset->search({$column, $id})->count
$check $tries-- $self->result_source->resultset->search({$column, $id})->count
Truth table50%2/4
A and B and C
A= $checkB= $tries--C= $self->result_source->resultset->search({$column, $id})->count
ABCresult
0XX0
10X0
1100
1111
302 16 $id = '';
303 # random id is as good as Perl's rand()
304 16 $id .= $set->[int(rand(@$set))] for (1 .. $conf->{size});
305 } while $check and
306 $tries-- and
307 $self->result_source->resultset->search({$column => $id})->count;
308 }
309  
310 32 ▶ $self->throw_exception("escaped from busy loop in DBIx::Class::RandomColumns::get_random_column_id()")
Branch: 1 branch
BranchTrueFalseTotal
unless $tries 0 32 32
311 unless $tries;
312  
313 32 return $id;
314 }
315  
316 1 1;
317  
318 __END__
↑ Summary