blib/lib/Test/Unix/User.pm

filter: Statementstmt 72.1% 96 / 133 Branchbran 62.9% 34 / 54 Conditioncond 41.6% 5 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 67.7% 147 / 217 CC 35 SCAR 36.3
CC
35
Cov
68%
user_ok
110.0
homedir_ok
19.0
_check_spec
19.0
CRAP 37.6
↑ Summary
1 package Test::Unix::User;
2  
3 # Copyright (c) 2005 Nik Clayton
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26  
27 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 require Exporter;
33 3 our @ISA = qw(Exporter);
34 3 our @EXPORT = qw(user_ok homedir_ok);
35  
36 3 my $Test = Test::Builder->new;
37  
38 3 ▶use User::pwent;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 3 my @USER_FIELDS = qw(name passwd uid gid change age quota
42 comment class gecos dir shell expire);
43 3 my %USER_FIELDS = map {$_ => 1} @USER_FIELDS;
44 3 my @HDIR_FIELDS = qw(name uid gid perm owner group);
45 3 my %HDIR_FIELDS = map {$_ => 1} @HDIR_FIELDS;
46  
47 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
48 2 my($self) = shift;
49 2 my $pack = caller;
50  
51 2 $Test->exported_to($pack);
52 2 $Test->plan(@_);
53  
54 2 $self->export_to_level(1, $self, qw(user_ok homedir_ok));
55 }
56  
57 =head1 NAME
58  
59 Test::Unix::User - Test::Builder based tests for Unix users and home directories
60  
61 =head1 VERSION
62  
63 Version 0.02
64  
65 =cut
66  
67 3 our $VERSION = '0.02';
68  
69 =head1 SYNOPSIS
70  
71 use Test::Unix::User tests => 2;
72  
73 user_ok({ name => 'nik', uid => 1000, ... },
74 "Verify nik's account");
75  
76 homedir_ok({ name => 'nik', perm => 0755, ... },
77 "Verify nik's home directory");
78  
79 Test::Unix::User B<automatically> exports C<user_ok()> and C<homedir_ok()>
80 to make it easier to test whether or not the Unix users and home
81 directories on the system have been correctly configured.
82  
83 Test::Unix::User uses Test::Builder, so plays nicely with Test::Simple,
84 Test::More, and other Test::Builder based modules.
85  
86 =head1 FUNCTIONS
87  
88 =head2 user_ok($spec, [ $test_name ]);
89  
90 user_ok() tests that an account exists that matches the given
91 specification.
92  
93 The specification is a hashref that consists of one or more keys. Keys
94 are taken from the L<User::pwent> module, and are C<name>, C<passwd>, C<uid>,
95 C<gid>, C<change>, C<age>, C<quota>, C<comment>, C<class>, C<gecos>,
96 C<dir>, C<shell>, and C<expire>. Some of these may not be supported on
97 your platform. See User::pwent for more details.
98  
99 Each value associated with a key is the value that that entry is supposed
100 to have.
101  
102 Only the C<name> key is mandatory, the others are optional. If they are
103 not present in the specification then they are not checked.
104  
105 The C<$test_name> is optional. If it is not present then a sensible one
106 is generated following the form
107  
108 Checking user '$user' ($key, $key, $key, ...)
109  
110 =cut
111  
112 0 ▶sub user_ok {
Subroutine user_ok: not called CC=10 SCAR=47.0
Pod documented
113 0 ▶ return unless _check_spec(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_spec(@_) 0 0 0
114  
115 0 my($spec, $test_name) = @_;
116  
117 0 ▶ if(! defined $test_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $test_name) 0 0 0
118 0 $test_name = "Checking user '$spec->{name}'";
119 0 $test_name .= ' (' . join(', ', sort keys %$spec) . ')';
120 }
121  
122 0 my($u, @diag);
123  
124 0 $u = getpwnam($spec->{name});
125  
126 0 ▶ if(! defined $u) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $u) 0 0 0
127 0 my $ok = $Test->ok(0, $test_name);
128 0 $Test->diag(" User '$spec->{name}' does not exist");
129 0 return $ok;
130 }
131  
132 0 foreach my $field (keys %$spec) {
133 0 ▶ if(! exists $USER_FIELDS{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $USER_FIELDS{$field}) 0 0 0
134 0 push @diag, " Invalid field '$field' given";
135 0 next;
136 }
137  
138 0 ▶ if(! defined $spec->{$field} or $spec->{$field} =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $spec->{$field} or $spec->{$field} =~ /^\s*$/) 0 0 0
Condition0%0/3
not defined $spec->{$field} or $spec->{$field} =~ /^\s*$/
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $spec->{$field} or $spec->{$field} =~ /^\s*$/
not defined $spec->{$field} $spec->{$field} =~ /^\s*$/
Truth table0%0/3
not(A) or B
A= not defined $spec->{$field}B= $spec->{$field} =~ /^\s*$/
ABresult
1X1
011
000
139 0 push @diag, " Empty field '$field' given";
140 0 next;
141 }
142  
143 0 ▶ if($u->$field ne $spec->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u->$field ne $spec->{$field}) 0 0 0
144 0 push @diag, " Field: $field\n";
145 0 push @diag, " expected: $spec->{$field}\n";
146 0 push @diag, " got: " . $u->$field . "\n";
147 }
148 }
149  
150 0 ▶ if(@diag) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diag) 0 0 0
151 0 my $ok = $Test->ok(0, $test_name);
152 0 $Test->diag(@diag);
153 0 return $ok;
154 }
155  
156 0 return $Test->ok(1, $test_name);
157 }
158  
159 =head2 homedir_ok($spec, [ $test_name ]);
160  
161 C<homedir_ok()> checks that the home directory for a given user exists and
162 matches the specification.
163  
164 The specification is a hashref that consists of one or more keys. Valid
165 keys are C<name>, C<uid>, C<gid>, C<owner>, C<group>, and C<perm>.
166  
167 The C<name> key is mandatory, the other keys are optional.
168  
169 The C<$test_name> is optional. If it is not present then a sensible one
170 is generated following the form.
171  
172 Home directory for user '$user' ($key, $key, $key, ...)
173  
174 Use C<uid> when you want to check the numeric user id assigned to the
175 directory, irrespective of the user name that is assigned to that uid.
176 Use C<owner> when you are interested in the name of the owner, without
177 being concerned about the numeric UID. Use both of these together to
178 ensure that the UID and the owner name match.
179  
180 C<gid> is to C<group> as C<uid> is to C<owner>.
181  
182 =cut
183  
184 15 ▶sub homedir_ok {
Subroutine homedir_ok: called CC=19 SCAR=29.4
Pod documented
185 15 ▶ return unless _check_spec(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_spec(@_) 1 14 15
186  
187 14 my($spec, $test_name) = @_;
188  
189 14 ▶ if(! defined $test_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $test_name) 13 1 14
190 13 $test_name = "Home directory for user '$spec->{name}'";
191 13 $test_name .= ' (' . join(', ', sort keys %$spec) . ')';
192 }
193  
194 14 my @diag;
195  
196 14 foreach my $field (keys %$spec) {
197 30 ▶ if(! exists $HDIR_FIELDS{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $HDIR_FIELDS{$field}) 1 29 30
198 1 push @diag, " Invalid field '$field' given";
199 1 delete $spec->{$field};
200 1 next;
201 }
202  
203 29 ▶ if(! defined $spec->{$field} or $spec->{$field} =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $spec->{$field} or $spec->{$field} =~ /^\s*$/) 2 27 29
Condition100%3/3
not defined $spec->{$field} or $spec->{$field} =~ /^\s*$/
l!l&&r!l&&!r
1127
MC/DC100%2/2
not defined $spec->{$field} or $spec->{$field} =~ /^\s*$/
not defined $spec->{$field} $spec->{$field} =~ /^\s*$/
Truth table100%3/3
not(A) or B
A= not defined $spec->{$field}B= $spec->{$field} =~ /^\s*$/
ABresult
1X1
011
000
204 2 push @diag, " Empty field '$field' given";
205 2 delete $spec->{$field};
206 2 next;
207 }
208 }
209
210 14 my $u = getpwnam($spec->{name});
211  
212 14 ▶ if(! defined $u) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $u) 1 13 14
213 1 my $ok = $Test->ok(0, $test_name);
214 1 $Test->diag(" User '$spec->{name}' does not exist");
215 1 return $ok;
216 }
217  
218 13 ▶ if(! -d $u->dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $u->dir) 1 12 13
219 1 my $ok = $Test->ok(0, $test_name);
220 1 $Test->diag(" Home directory '" . $u->dir . "' for '$spec->{name}' is not a directory");
221 1 return $ok;
222 }
223  
224 12 my $sb = stat($u->dir);
225  
226 12 foreach my $field (qw(uid gid)) {
227 24 ▶ if(exists $spec->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{$field}) 6 18 24
228 6 ▶ if($sb->$field != $spec->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sb->$field != $spec->{$field}) 1 5 6
229 1 push @diag, " Field: $field\n";
230 1 push @diag, " expected: $spec->{$field}\n";
231 1 push @diag, " got: " . $sb->$field . "\n";
232 }
233 }
234 }
235  
236 12 ▶ if(exists $spec->{owner}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"owner"}) 2 10 12
237 2 my $owner = getpwuid($sb->uid)->name();
238 2 ▶ if($spec->{owner} ne $owner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"owner"} ne $owner) 1 1 2
239 1 push @diag, " Field: owner\n";
240 1 push @diag, " expected: $spec->{owner}\n";
241 1 push @diag, " got: $owner\n";
242 }
243 }
244  
245 12 ▶ if(exists $spec->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"group"}) 2 10 12
246 2 my $group = getgrgid($sb->gid);
247 2 ▶ if($spec->{group} ne $group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"group"} ne $group) 1 1 2
248 1 push @diag, " Field: group\n";
249 1 push @diag, " expected: $spec->{group}\n";
250 1 push @diag, " got: $group\n";
251 }
252 }
253  
254 12 ▶ if(exists $spec->{perm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"perm"}) 3 9 12
255 3 ▶ if(($sb->mode & 07777) != $spec->{perm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($sb->mode & 4095) != $spec->{"perm"}) 1 2 3
256 1 push @diag, " Field: perm\n";
257 1 push @diag, sprintf(" expected: %04o\n", $spec->{perm});
258 1 push @diag, sprintf(" got: %04o\n", $sb->mode & 07777);
259 }
260 }
261  
262 12 ▶ if(@diag) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diag) 7 5 12
263 7 my $ok = $Test->ok(0, $test_name);
264 7 $Test->diag(@diag);
265 7 return $ok;
266 }
267  
268 5 return $Test->ok(1, $test_name);
269 }
270  
271 15 ▶sub _check_spec {
Subroutine _check_spec: called CC=8 SCAR=29.4
272 15 my($spec, $test_name) = @_;
273 15 my $sub = (caller(1))[3];
274  
275 15 $sub =~ s/Test::Unix::User:://;
276  
277 15 ▶ if(! defined $spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $spec) 1 14 15
278 1 my $ok = $Test->ok(0, "$sub()");
279 1 $Test->diag(" $sub() called with no arguments");
280 1 return $ok;
281 }
282  
283 14 ▶ if(ref($spec) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec ne "HASH") 0 14 14
284 0 my $t = $test_name;
285 0 ▶ $t = "$sub(...)" unless defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 0 0
286 0 my $ok = $Test->ok(0, $t);
287 0 $Test->diag(" First argument to $sub() must be a hash ref");
288 0 return $ok;
289 }
290  
291 14 ▶ if(! exists $spec->{name} or
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $spec->{"name"} && defined $spec->{"name"} or $spec->{"name"} =~ /^\s*$/) 0 14 14
Condition33%2/6
not exists $spec->{"name"} && defined $spec->{"name"} or $spec->{"name"} =~ /^\s*$/
l!l&&r!l&&!r
0014
exists $spec->{'name'} && defined $spec->{'name'}
!ll&&!rl&&r
0014
MC/DC0%0/3
not exists $spec->{"name"} && defined $spec->{"name"} or $spec->{"name"} =~ /^\s*$/
exists $spec->{'name'} defined $spec->{'name'} $spec->{"name"} =~ /^\s*$/
Truth table25%1/4
not(A && B) or C
A= exists $spec->{'name'}B= defined $spec->{'name'}C= $spec->{"name"} =~ /^\s*$/
ABCresult
0XX1
10X1
1111
1100
292 ! defined $spec->{name} or
293 $spec->{name} =~ /^\s*$/) {
294 0 my $t = $test_name;
295 0 ▶ $t = "$sub(...)" unless defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 0 0
296 0 my $ok = $Test->ok(0, $t);
297 0 $Test->diag(" $sub() called with no user name");
298 0 return $ok;
299 }
300  
301 14 return 1;
302 }
303  
304 3 1;
305  
306 =head1 EXAMPLES
307  
308 Verify that an account exists
309  
310 user_ok({ name => 'nik' }, "'nik' exists as a user");
311  
312 Verify that the account exists, that it has a given UID, and
313 that the home directory and shell match. Omit the test name,
314 rely on the default.
315  
316 user_ok({ name => 'nik', uid => 1001, dir => '/home/nik',
317 shell => '/bin/sh');
318  
319 Check that the home directory for 'nik' exists. Use an automatically
320 generated test name.
321  
322 homedir_ok({ name => 'nik' });
323  
324 Test that nik's home directory is owned by the 'nik' user, without
325 worrying what UID is assigned to that user.
326  
327 homedir_ok({ name => 'nik', owner => 'nik' });
328  
329 Ensure that nik's home directory is owned by uid 1000, and that
330 uid 1000 maps back to the 'nik' user
331  
332 homedir_ok({ name => 'nik', uid => 1000, owner => 'nik');
333  
334 Check the permissions on the home directory, and supply our own test
335 name.
336  
337 homedir_ok({ name => 'nik', perm => 0755 },
338 "Nik's home directory is correctly set");
339  
340 =head1 SEE ALSO
341  
342 Test::Simple, Test::Builder, User::pwent.
343  
344 =head1 AUTHOR
345  
346 Nik Clayton, C<nik@FreeBSD.org>
347  
348 =head1 BUGS
349  
350 Please report any bugs or feature requests to
351 C<bug-test-unix-user@rt.cpan.org>, or through the web interface at
352 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Unix-User>.
353 I will be notified, and then you'll automatically be notified of progress on
354 your bug as I make changes.
355  
356 =head1 COPYRIGHT & LICENSE
357  
358 Copyright (c) 2005 Nik Clayton
359 All rights reserved.
360  
361 Redistribution and use in source and binary forms, with or without
362 modification, are permitted provided that the following conditions
363 are met:
364  
365 1. Redistributions of source code must retain the above copyright
366 notice, this list of conditions and the following disclaimer.
367 2. Redistributions in binary form must reproduce the above copyright
368 notice, this list of conditions and the following disclaimer in the
369 documentation and/or other materials provided with the distribution.
370  
371 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
372 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
373 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
374 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
375 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
376 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
377 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
378 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
379 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
380 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381 SUCH DAMAGE.
382  
383 =cut
384  
385 3 1; # End of Test::Unix::User
↑ Summary