FormMagick/Validator/Network.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 118 CC 14 SCAR 26.1
CC
14
Cov
0%
ip_number
30.0
email_simple
12.0
password
12.0
CRAP 13.5
1 #!/usr/bin/perl -w
2  
3 #
4 # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org>
5 # Copyright (c) 2000-2002 Mitel Networks Corporation
6 # This software is distributed under the same licenses as Perl itself;
7 # see the file COPYING for details.
8 #
9 # $Id: Network.pm,v 1.12 2003/02/05 17:18:41 anoncvs_gtkglext Exp $
10 #
11  
12 package CGI::FormMagick::Validator;
13 use strict;
14 use Mail::RFC822::Address;
15  
16 =pod
17  
18 =head1 NAME
19  
20 CGI::FormMagick::Validator::Network - network-related validation routines
21  
22 =head1 SYNOPSIS
23  
24 use CGI::FormMagick;
25  
26 =head1 DESCRIPTION
27  
28 See CGI::FormMagick::Validator for a general description.
29  
30 =head2 Validation routines provided:
31  
32 =over 4
33  
34 =item url
35  
36 The data looks like a (normalish) URL: C<$data =~ m!(http|ftp)://(\w/.-/+)!>
37  
38 =begin testing
39 BEGIN: {
40 use_ok("CGI::FormMagick::Validator");
41 }
42  
43 isnt( url(undef, 'http://'), "OK" , "http:// is not a complete URL");
44 isnt( url(undef, 'ftp://'), "OK" , "ftp:// is not a complete URL");
45 isnt( url(undef, 'abc'), "OK" , "abc is not a valid URL");
46 isnt( url(), "OK" , "undef is not a valid URL");
47 isnt( url(undef, ''), "OK" , "empty string is not a valid URL");
48 is( url(undef, 'http://a.bc'), "OK" , "http://a.bc is a valid URL");
49 is( url(undef, 'ftp://a.bc:21/'), "OK" , "ftp://a.bc:21 is a valid URL");
50 isnt( url(undef, 'http:///a.bc'), "OK" , "http:///a.bc has too many slashes");
51 isnt( url(undef, 'http://a_a.bc'), "OK" , "Underscores are not allowed in URLs");
52  
53 =end testing
54  
55 =cut
56  
57 sub url {
58 my ($fm, $data) = @_;
59 if ($data && $data =~ m!(http|ftp)://[a-zA-Z0-9][a-zA-Z0-9/.-/]!) {
60 return "OK";
61 } else {
62 return "This field must contain a URL starting with http:// or ftp://";
63 }
64 }
65  
66 =pod
67  
68 =item email_simple
69  
70 The data looks more or less like an internet email address:
71 C<$data =~ /.+\@.+\..+/>
72  
73 Note: not fully compliant with the entire gamut of RFC 822 addressing ;)
74  
75 =for testing
76 is( email_simple(undef, 'a@b.c'), "OK" , 'a@b.c is a valid email address');
77 is( email_simple(undef, 'a+b@c.d'), "OK" , 'a+b@c.d is a valid email address');
78 is( email_simple(undef, 'a-b=c@d.e'), "OK" , 'a-b=c@d.e is a valid email address');
79 isnt( email_simple(undef, 'abc'), "OK" , 'abc is not a valid email address');
80 isnt( email_simple(undef, 'a@b'), "OK" , 'a@b is not a valid email address');
81 isnt( email_simple(undef, '@b.c'), "OK" , '@b.c is not a valid email address');
82 isnt( email_simple(), "OK" , 'undef is not a valid email address');
83 isnt( email_simple(undef, ""), "OK" , 'empty string is not a valid email address');
84  
85 =cut
86  
87 sub email_simple {
88 my ($fm, $data) = @_;
89 if (not defined $data ) {
90 return "You must enter an email address.";
91 } elsif (Mail::RFC822::Address::valid($data)) {
92 return "OK";
93 } else {
94 return "This field doesn't look like an RFC822-compliant email address";
95 }
96 }
97  
98 =pod
99  
100 =item domain_name
101  
102 The data looks like an internet domain name or hostname.
103  
104 =for testing
105 is( domain_name(undef, "abc.com"), "OK" , "abc.com is a valid domain name");
106 isnt( domain_name(undef, "abc"), "OK" , "abc is not a valid domain name");
107 isnt( domain_name(), "OK" , "undef is not a valid domain name");
108 isnt( domain_name(undef, ""), "OK" , "empty string is not a valid domain name");
109  
110 =cut
111  
112 sub domain_name {
113 my ($fm, $data) = @_;
114 if ($data && $data =~ /^([a-z\d\-]+\.)+[a-z]{1,3}\.?$/o ) {
115 return "OK";
116 } else {
117 return "This field doesn't look like a valid Internet domain name or hostname.";
118 }
119 }
120  
121 =pod
122  
123 =item ip_number
124  
125 The data looks like a valid IP number.
126  
127 =for testing
128 is(ip_number(undef, '1.2.3.4'), 'OK', "ip_number('1.2.3.4') should be valid.");
129 is(ip_number(undef, '0.0.0.0'), 'OK', "ip_number('0.0.0.0') should be valid.");
130 is(ip_number(undef, '255.255.255.255'), 'OK', "ip_number('255.255.255.255') should be valid.");
131 isnt(ip_number(undef, '1.2.3'), 'OK', "ip_number('1.2.3') shouldn't be valid.");
132 isnt(ip_number(undef, '1000.2.3.4'), 'OK', "ip_number('1000.2.3.4') shouldn't be valid.");
133 isnt(ip_number(undef, '256.2.3.4'), 'OK', "ip_number('256.2.3.4') shouldn't be valid.");
134 isnt(ip_number(undef, 'a.2.3.4'), 'OK', "ip_number('a.2.3.4') shouldn't be valid.");
135 isnt(ip_number(undef, '1,2,3,4'), 'OK', "ip_number('1,2,3,4') shouldn't be valid.");
136 isnt(ip_number(undef, '1.2.3'), 'OK', "ip_number('1.2.3') shouldn't be valid.");
137 isnt(ip_number(undef, ''), 'OK', "ip_number('') shouldn't be valid.");
138 isnt(ip_number(), 'OK', "ip_number(undef) shouldn't be valid.");
139  
140 =cut
141  
142 sub ip_number {
143 my ($fm, $data) = @_;
144  
145 return undef unless defined $data;
146  
147 return 'Doesn\'t look like an IP' unless $data =~ /^[\d.]+$/;
148  
149 my @octets = split /\./, $data;
150  
151 return 'Not enough octets (expected X.X.X.X)' unless scalar @octets == 4;
152  
153 foreach my $octet (@octets) {
154 return "$octet is more than 255" if $octet > 255;
155 }
156  
157 return 'OK';
158 }
159  
160 =pod
161  
162 =item username
163  
164 The data looks like a good, valid username
165  
166 =for testing
167 is( username(undef, "abc"), "OK" , "abc is a valid username");
168 isnt( username(undef, "123"), "OK" , "123 is not a valid username");
169 isnt( username(), "OK" , "undef is not a valid username");
170 isnt( username(undef, ""), "OK" , "empty string is not a valid username");
171 isnt( username(undef, " "), "OK" , "spaces is not a valid username");
172  
173 =cut
174  
175 sub username {
176 my ($fm, $data) = @_;
177  
178 if ($data && $data =~ /[a-zA-Z]{3,8}/ ) {
179 return "OK";
180 } else {
181 return "This field must look like a valid username (3 to 8 letters and numbers)";
182 }
183 }
184  
185 =pod
186  
187 =item password
188  
189 The data looks like a good password
190  
191 =for testing
192 isnt( password(undef, "abc"), "OK" , "abc is not a good password");
193 isnt( password(), "OK" , "undef is not a good password");
194 isnt( password(undef, ""), "OK" , "empty string is not a good password");
195 is( password(undef, "ab1C23FouR!"), "OK" , "ab1C23FouR! is a good password");
196  
197 =cut
198  
199 sub password {
200 my ($fm, $data) = @_;
201 $_ = $data;
202 if (not defined $_) {
203 return "You must provide a password.";
204 } elsif (/\d/ and /[A-Z]/ and /[a-z]/ and /\W/ and length($_) > 6) {
205 return "OK";
206 } else {
207 return "The password you provided was not a good password. A good password should have a mixture of upper and lower case letters, numbers, and non-alphanumeric characters.";
208 }
209 }
210  
211 =pod
212  
213 =item mac_address
214  
215 The data looks like a good MAC address
216  
217 =for testing
218 isnt( mac_address(undef, "string"), "OK" , "string is not a good MAC address");
219 isnt( mac_address(), "OK" , "undef is not a good MAC address");
220 isnt( mac_address(undef, ""), "OK" , "empty string is not a good MAC address");
221 isnt( mac_address(undef, "01:23:45"), "OK" , "01:23:45 is too short for a MAC address");
222 isnt( mac_address(undef, "01:23:45:67:89:AB:CD"), "OK" , "01:23:45:67:89:AB:CD is too long for a MAC address");
223 is( mac_address(undef, "08:00:cf:2b:12:34"), "OK" , "08:00:cf:2b:12:34 is a good MAC address");
224 is( mac_address(undef, "08:00:CF:2B:12:34"), "OK" , "08:00:CF:2B:12:34 is a good MAC address");
225  
226 =cut
227  
228 sub mac_address {
229 my ($fm, $data) = @_;
230 $_ = lc $data; # easier to match on $_
231 if (not defined $_) {
232 return "You must provide a MAC address.";
233 } elsif (/^([0-9a-f][0-9a-f](:[0-9a-f][0-9a-f]){5})$/) {
234 return "OK";
235 } else {
236 return "The MAC address you provided was not valid.";
237 }
238 }
239  
240 return "FALSE";