FormMagick/Validator/Business.pm untested

filter: Statementstmt 0.0% 0 / 29 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 79 CC 11 SCAR 37.4
CC
11
Cov
0%
credit_card_number
42.0
credit_card_expiry
42.0
CRAP 42.0
1 #!/usr/bin/perl -w
2  
3 #
4 #
5 # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org>
6 # Copyright (c) 2000-2002 Mitel Networks Corporation
7 # This software is distributed under the same licenses as Perl itself;
8 # see the file COPYING for details.
9 #
10 # $Id: Business.pm,v 1.10 2003/02/05 17:18:39 anoncvs_gtkglext Exp $
11 #
12  
13 package CGI::FormMagick::Validator;
14  
15 =pod
16  
17 =head1 NAME
18  
19 CGI::FormMagick::Validator::Business - business-related validation routines
20  
21 =head1 SYNOPSIS
22  
23 use CGI::FormMagick;
24  
25 =head1 DESCRIPTION
26  
27 See CGI::FormMagick::Validator for a general description.
28  
29 =begin testing
30 BEGIN: {
31 use CGI::FormMagick::Validator;
32 }
33  
34  
35 =end testing
36  
37 =head2 Validation routines provided:
38  
39 =over 4
40  
41  
42  
43 =item credit_card_number
44  
45 The data looks like a valid credit card number. Checks the input for
46 * numeric characters, spaces, and dashes, only
47 * length
48 * and a checksum algorithm used by most (all?) credit cards.
49  
50 =for testing
51 ok( credit_card_number() ne "OK" , "undef is not a credit card number");
52 ok( credit_card_number(undef, "") ne "OK" , "empty string is not a credit card number");
53 ok( credit_card_number(undef, "a") ne "OK" , "a is not a credit card number");
54 ok( credit_card_number(undef, "12")ne "OK" , "12 is not a credit card number");
55 ok( credit_card_number(undef, "4111 1111 1111 1111")
56 eq "OK" , "4111 1111 1111 1111 is a credit card numer");
57 ok( credit_card_number(undef, "4111-1111-1111-1111")
58 eq "OK" , "4111-1111-1111-1111 is a credit card number");
59 ok( credit_card_number(undef, "4111111111111111")
60 eq "OK" , "4111111111111111 is a credit card number");
61 ok( credit_card_number(undef, "4111111111111112")
62 ne "OK" , "4111111111111112 is not a credit card number (Bad checksum)");
63 ok( credit_card_number(undef, "411111111111111")
64 ne "OK" , "411111111111111 is not a credit card number (Too short)");
65  
66 =cut
67  
68 sub credit_card_number {
69 my ($fm, $number) = @_;
70 my ($i, $sum, $weight);
71  
72 return "You must enter a credit card number" unless $number;
73  
74 return "Credit card numbers shouldn't have anything but "
75 . "numbers, spaces or dashes" if $number =~ /[^\d\s-]/;
76  
77 $number =~ s/\D//g;
78  
79 return "Must be at least 14 characters in length"
80 unless length($number) >= 13 && 0+$number;
81  
82 for ($i = 0; $i < length($number) - 1; $i++) {
83 $weight = substr($number, -1 * ($i + 2), 1) * (2 - ($i % 2));
84 $sum += (($weight < 10) ? $weight : ($weight - 9));
85 }
86  
87 return "OK" if substr($number, -1) == (10 - $sum % 10) % 10;
88 return "Doesn't appear to be a valid credit card number";
89  
90 }
91  
92 =pod
93  
94 =item credit_card_expiry
95  
96 The data looks like a valid credit card expiry date. Checks MM/YY and
97 MM/YYYY format dates and fails if the date is in the past or is more than
98 ten years in the future.
99  
100 =begin testing
101  
102 my ($m, $y) = (localtime)[4,5];
103 $m++;
104 $y += 1900;
105  
106 sub current_m_y_plus {
107 return sprintf "%02d/%d", ($m + $_[0]), ($y + $_[1]);
108 }
109  
110 my @four_digit_year_expecations = (
111 { is_valid => 1, input => current_m_y_plus(1,0) },
112 { is_valid => 1, input => current_m_y_plus(0,1) },
113 { is_valid => 1, input => current_m_y_plus(1,1) },
114 { is_valid => 0, input => current_m_y_plus(-1,0),
115 reason => 'Expired already' },
116 { is_valid => 1, input => current_m_y_plus(0,0),
117 reason => 'Doesn\'t expire until next month after' },
118 { is_valid => 0, input => current_m_y_plus(0,10),
119 reason => 'Ten years is too far in the future' },
120 );
121  
122 my @two_digit_year_expectations;
123 foreach (@four_digit_year_expecations) {
124 my %two_digit = %$_;
125 $two_digit{input} =~ s/..(..)$/$1/;
126 push @two_digit_year_expectations, \%two_digit;
127 }
128  
129 (my $three_digit_year = "$m/$y") =~ s/.(...)$/$1/;
130 (my $one_digit_year = "$m/$y") =~ s/(...).$/$1/;
131  
132 foreach my $case (
133 @four_digit_year_expecations,
134 @two_digit_year_expectations,
135 { is_valid => 0, input => $three_digit_year,
136 reason => 'Three digits in the year?' },
137 { is_valid => 0, input => $one_digit_year,
138 reason => 'One digit in the year?' },
139 { is_valid => 0, input => '' },
140 ) {
141 my ($expected, $input, $reason) = @{$case}{qw(is_valid input reason)};
142 my $actual = 'OK' eq credit_card_expiry(undef, $input);
143 my $should = 'should' . ($expected ? '' : "n't");
144 my $description = "credit_card_expiry('$input') $should be valid";
145 $description .= " ($reason)" if defined $reason;
146 ok($actual == $expected, $description);
147 }
148  
149 ok(credit_card_expiry(), "credit_card_expiry(undef) shouldn't be valid.");
150  
151 =end testing
152  
153 =cut
154  
155 sub credit_card_expiry {
156 my ($fm, $data) = @_;
157  
158 return "No expiry date entered." unless defined $data;
159  
160 my ($m, $y) = split(/\D/, $data); # split on first non-numeric char
161  
162 return "Expiry date must be in the format MM/YY or MM/YYYY"
163 unless (
164 defined ($y)
165 and defined($m)
166 and $y =~ /\d{2}|\d{4}/
167 and $m =~ /\d{2}/
168 );
169  
170 my ($this_m, $this_y) = (localtime())[4,5];
171 $this_m++;
172 $this_y += 1900;
173  
174 $this_y =~ s/^..// if ($y =~ /^..$/);
175  
176 return "This expiry date appears to have already passed"
177 if (
178 $y < $this_y
179 or ($y == $this_y and $m < $this_m)
180 );
181  
182 return "This expiry date appears to be too far in the future"
183 if (($y - 10) >= $this_y);
184  
185 return "OK";
186 }
187  
188  
189 =pod
190  
191 =head1 SEE ALSO
192  
193 The main perldoc for CGI::FormMagick
194  
195 =head1 AUTHOR
196  
197 Kirrily "Skud" Robert <skud@infotrope.net>
198  
199 More information about FormMagick may be found at
200 http://sourceforge.net/projects/formmagick/
201  
202 =cut
203  
204 return 1;