blib/lib/Net/LDAP/Server/Test.pm

filter: Statementstmt 69.0% 325 / 471 Branchbran 52.0% 104 / 200 Conditioncond 45.0% 9 / 20 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 83.3% 40 / 48 Podpod 100% 3 / 3 totaltotal 63.9% 483 / 755 SCAR 67.8
CC
124
Cov
63%
_search_auto_schema_data
340.2
_update_groups
110.0
_add_AD
42.0
CRAP 878.7
↑ Summary
1 package Net::LDAP::Server::Test;
2  
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Data::Dump ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Net::LDAP::SID;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 our $VERSION = '0.20';
12  
13 =head1 NAME
14  
15 Net::LDAP::Server::Test - test Net::LDAP code
16  
17 =head1 SYNOPSIS
18  
19 use Test::More tests => 10;
20 use Net::LDAP::Server::Test;
21
22 ok( my $server = Net::LDAP::Server::Test->new(8080),
23 "test LDAP server spawned");
24
25 # connect to port 8080 with your Net::LDAP code.
26 ok(my $ldap = Net::LDAP->new( 'localhost', port => 8080 ),
27 "new LDAP connection" );
28
29 # ... test stuff with $ldap ...
30
31 # server will exit when you call final LDAP unbind().
32 ok($ldap->unbind(), "LDAP server unbound");
33  
34 =head1 DESCRIPTION
35  
36 Now you can test your Net::LDAP code without having a real
37 LDAP server available.
38  
39 =head1 METHODS
40  
41 Only one user-level method is implemented: new().
42  
43 =cut
44  
45 {
46  
47 package # fool Pause
48 MyLDAPServer;
49  
50 16 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 16 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 16 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 16 use Net::LDAP::Constant qw(
54 LDAP_SUCCESS
55 LDAP_NO_SUCH_OBJECT
56 LDAP_CONTROL_PAGED
57 LDAP_OPERATIONS_ERROR
58 LDAP_UNWILLING_TO_PERFORM
59 LDAP_ALREADY_EXISTS
60 LDAP_TYPE_OR_VALUE_EXISTS
61 LDAP_NO_SUCH_ATTRIBUTE
62 16 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
63 16 ▶ use Net::LDAP::Util qw(ldap_explode_dn canonical_dn);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 16 ▶ use Net::LDAP::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 16 ▶ use Net::LDAP::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 16 ▶ use Net::LDAP::FilterMatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 16 ▶ use Net::LDAP::Control;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 16 ▶ use Net::LDAP::ASN qw(LDAPRequest LDAPResponse);
Subroutine BEGIN: called CC=1 SCAR=0.0
69 16 ▶ use Convert::ASN1 qw(asn_read);
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 16 ▶ use base 'Net::LDAP::Server';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 16 ▶ use fields qw( _flags );
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 16 use constant RESULT_OK => {
75 'matchedDN' => '',
76 'errorMessage' => '',
77 'resultCode' => LDAP_SUCCESS
78 16 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 16 use constant RESULT_NO_SUCH_OBJECT => {
81 'matchedDN' => '',
82 'errorMessage' => '',
83 'resultCode' => LDAP_NO_SUCH_OBJECT,
84 16 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 16 use constant RESULT_ALREADY_EXISTS => {
87 'matchedDN' => '',
88 'errorMessage' => '',
89 'resultCode' => LDAP_ALREADY_EXISTS,
90 16 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 16 use constant RESULT_TYPE_OR_VALUE_EXISTS => {
93 'matchedDN' => '',
94 'errorMessage' => '',
95 'resultCode' => LDAP_TYPE_OR_VALUE_EXISTS,
96 16 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 16 use constant RESULT_NO_SUCH_ATTRIBUTE => {
99 'matchedDN' => '',
100 'errorMessage' => '',
101 'resultCode' => LDAP_NO_SUCH_ATTRIBUTE,
102 16 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 16 our %Data; # package data lasts as long as $$ does.
105 16 our $Cookies = 0;
106 16 our %Searches;
107 16 my @Scopes = qw(base one sub);
108  
109 # constructor
110 sub new {
111 7 ▶ my ( $class, $sock, %args ) = @_;
Subroutine new: called CC=2 SCAR=7.0
112 7 my $self = $class->SUPER::new($sock);
113 warn sprintf "Accepted connection from: %s\n", $sock->peerhost()
114 7 ▶ if $ENV{LDAP_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LDAP_DEBUG"} 0 7 7
115 7 $self->{_flags} = \%args;
116 7 return $self;
117 }
118  
119 sub unbind {
120 0 ▶ my $self = shift;
Subroutine unbind: not called CC=1 SCAR=6.9
121 0 my $reqData = shift;
122 0 return RESULT_OK;
123 }
124  
125 # the bind operation
126 sub bind {
127 5 ▶ my $self = shift;
Subroutine bind: called CC=1 SCAR=0.0
128 5 my $reqData = shift;
129 5 return RESULT_OK;
130 }
131  
132 # the search operation
133 sub search {
134 17 ▶ my $self = shift;
Subroutine search: called CC=3 SCAR=11.0
135  
136 17 ▶ if ( defined $self->{_flags}->{data} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'_flags'}{'data'}) { } 1 16 17
elsif (defined $self->{'_flags'}{'auto_schema'}) { } 14 2 16
137 1 return $self->_search_user_supplied_data(@_);
138 }
139 elsif ( defined $self->{_flags}->{auto_schema} ) {
140 14 return $self->_search_auto_schema_data(@_);
141 }
142 else {
143 2 return $self->_search_default_test_data(@_);
144 }
145 }
146  
147 sub _search_user_supplied_data {
148 1 ▶ my ( $self, $reqData ) = @_;
Subroutine _search_user_supplied_data: called CC=1 SCAR=0.0
149  
150 # TODO??
151  
152 #warn 'SEARCH USER DATA: ' . Data::Dump::dump \@_;
153 1 return RESULT_OK, @{ $self->{_flags}->{data} };
154 }
155  
156 sub _search_auto_schema_data {
157 14 ▶ my ( $self, $reqData, $reqMsg ) = @_;
Subroutine _search_auto_schema_data: called CC=41 SCAR=58.3
158  
159 #warn 'SEARCH SCHEMA: ' . Data::Dump::dump \@_;
160  
161 14 my @results;
162 14 my $base = $reqData->{baseObject};
163  
164 # $reqData->{scope} is a enum but we want a word
165 my $scope
166 14 ▶ = $Scopes[ defined $reqData->{scope} ? $reqData->{scope} : 2 ];
Branch: 1 branch
BranchTrueFalseTotal
defined $reqData->{'scope'} ? : 14 0 14
167 14 ▶ my @attrs = @{ $reqData->{attributes} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$reqData->{"attributes"} or [] 0 14 14
168 14 my @filters = ();
169  
170 14 ▶ if ( exists $reqData->{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $reqData->{"filter"}) 14 0 14
171 push( @filters,
172 14 bless( $reqData->{filter}, 'Net::LDAP::Filter' ) );
173  
174 }
175  
176 14 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 14 14
177 0 warn "search for '$base' with scope '$scope' in Data: "
178 . Data::Dump::dump \%Data;
179 0 warn "filters: " . Data::Dump::dump \@filters;
180 }
181  
182 # support paged results
183 14 my ( $page_size, $cookie, $controls, $offset );
184 14 ▶ if ( exists $reqMsg->{controls} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $reqMsg->{"controls"}) 0 14 14
185 0 for my $control ( @{ $reqMsg->{controls} } ) {
186  
187 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
188 0 warn "control: " . Data::Dump::dump($control) . "\n";
189 }
190  
191 0 ▶ if ( $control->{type} eq LDAP_CONTROL_PAGED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($control->{"type"} eq "1.2.840.113556.1.4.319") 0 0 0
192 0 my $asn = Net::LDAP::Control->from_asn($control);
193  
194 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
195 0 warn "asn: " . Data::Dump::dump($asn) . "\n";
196 }
197 0 $page_size = $asn->size;
198  
199 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
200 0 warn "size == $page_size";
201 0 warn "cookie == " . $asn->cookie;
202 }
203  
204 # assign a cookie if this is the first page of paged search
205 0 ▶ if ( !$asn->cookie ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($asn->cookie) 0 0 0
206 0 $asn->cookie( ++$Cookies );
207 0 $asn->value; # IMPORTANT!! encode value with cookie
208  
209 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
210 0 warn "no cookie assigned. setting to $Cookies";
211 }
212  
213 # keep track of offset
214 0 $Searches{ $asn->cookie } = 0;
215 }
216  
217 0 $offset = $Searches{ $asn->cookie };
218 0 $cookie = $asn->cookie;
219  
220 0 push( @$controls, $asn );
221 }
222 }
223 }
224  
225 # loop over all keys looking for match
226 # we sort in order for paged control to work
227 14 ENTRY: for my $dn ( sort keys %Data ) {
228  
229 32 ▶ next unless $dn =~ m/$base$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dn =~ /$base$/ 8 24 32
230  
231 24 ▶ if ( $scope eq 'base' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scope eq 'base') { } 12 12 24
elsif ($scope eq 'one') { } 6 6 12
232 12 ▶ next unless $dn eq $base;
Branch: 1 branch
BranchTrueFalseTotal
unless $dn eq $base 4 8 12
233 }
234 elsif ( $scope eq 'one' ) {
235 6 my $dn_depth = scalar @{ ldap_explode_dn($dn) };
236 6 my $base_depth = scalar @{ ldap_explode_dn($base) };
237  
238 # We're guaranteed to be at or under $base thanks to the m// above
239 6 ▶ next unless $dn_depth == $base_depth + 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $dn_depth == $base_depth + 1 4 2 6
240 }
241  
242 16 my $entry = $Data{$dn};
243  
244 16 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 16 16
245 0 warn "trying to match $dn : " . Data::Dump::dump $entry;
246 }
247  
248 16 my $match = 0;
249 16 for my $filter (@filters) {
250  
251 16 ▶ if ( $filter->match($entry) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter->match($entry)) 11 5 16
252  
253 #warn "$f matches entry $dn";
254 11 $match++;
255 }
256 }
257  
258 #warn "matched $match";
259 16 ▶ if ( $match == scalar(@filters) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match == scalar @filters) 11 5 16
260  
261 # clone the entry so that client cannot modify %Data
262 11 my $result = $entry->clone;
263  
264 # filter returned attributes to those requested
265 11 ▶ if (@attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attrs) 0 11 11
266 0 my %wanted = map { $_ => 1 } @attrs;
267 $result->delete($_)
268 0 for grep { not $wanted{$_} } $result->attributes;
269 }
270  
271 11 push( @results, $result );
272  
273 }
274 }
275  
276 # for paged results we find everything then take a slice.
277 # this is less how a Real Server would do it but does
278 # work for the simple case where we want to make sure our offset
279 # and page size are accurate and we're not returning the same results
280 # in multiple pages.
281 # the $page_size -1 is because we're zero-based.
282  
283 14 my $total_found = scalar(@results);
284 14 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 14 14
285 0 warn "found $total_found total results for filters:"
286 . Data::Dump::dump( \@filters );
287  
288 #warn Data::Dump::dump( \@results );
289 0 ▶ if ($page_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_size) 0 0 0
290 0 warn "page_size == $page_size offset == $offset\n";
291 }
292 }
293  
294 14 ▶ if ( $scope eq 'base' and $total_found == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope eq "base" and $total_found == 0) 3 11 14
Condition100%3/3
$scope eq "base" and $total_found == 0
!ll&&!rl&&r
473
MC/DC100%2/2
$scope eq "base" and $total_found == 0
$scope eq "base" $total_found == 0
Truth table100%3/3
A and B
A= $scope eq "base"B= $total_found == 0
ABresult
0X0
100
111
295 3 return RESULT_NO_SUCH_OBJECT;
296 }
297  
298 11 ▶ if ( $page_size && $offset > $#results ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($page_size and $offset > $#results) { } 0 11 11
elsif ($page_size and @results) { } 0 11 11
elsif (defined $page_size and $page_size == 0) { } 0 22 22
Condition33%1/3
$page_size and $offset > $#results
!ll&&!rl&&r
1100
MC/DC0%0/2
$page_size and $offset > $#results
$page_size $offset > $#results
Truth table33%1/3
A and B
A= $page_sizeB= $offset > $#results
ABresult
0X0
100
111
299  
300 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
301 0 warn "exceeded end of results\n";
302 }
303 0 @results = ();
304  
305 # IMPORTANT!! must set pager cookie to false
306 # to indicate no more results
307 0 for my $control (@$controls) {
308 0 ▶ if ( $control->isa('Net::LDAP::Control::Paged') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($control->isa("Net::LDAP::Control::Paged")) 0 0 0
309 0 $control->cookie(undef);
310 0 $control->value; # IMPORTANT!! re-encode
311 }
312 }
313 }
314 ▶ elsif ( $page_size && @results ) {
Condition33%1/3
$page_size and @results
!ll&&!rl&&r
1100
MC/DC0%0/2
$page_size and @results
$page_size @results
Truth table33%1/3
A and B
A= $page_sizeB= @results
ABresult
0X0
100
111
315  
316 0 my $limit = $offset + $page_size - 1;
317 0 ▶ if ( $limit > $#results ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > $#results) 0 0 0
318 0 $limit = $#results;
319 }
320  
321 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
322 0 warn "slice \@results[ $offset .. $limit ]\n";
323 }
324 0 @results = @results[ $offset .. $limit ];
325  
326 # update our global marker
327 0 $Searches{$cookie} = $limit + 1;
328  
329 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
330 0 warn "returning " . scalar(@results) . " total results\n";
331 0 warn "next offset start is $Searches{$cookie}\n";
332  
333 #warn Data::Dump::dump( \@results );
334 }
335  
336 }
337  
338 # special case. client is telling server to abort.
339 ▶ elsif ( defined $page_size && $page_size == 0 ) {
Condition33%1/3
defined $page_size and $page_size == 0
!ll&&!rl&&r
1100
MC/DC0%0/2
defined $page_size and $page_size == 0
defined $page_size $page_size == 0
Truth table33%1/3
A and B
A= defined $page_sizeB= $page_size == 0
ABresult
0X0
100
111
340  
341 0 @results = ();
342  
343 }
344  
345 #warn "search results for " . Data::Dump::dump($reqData) . "\n: "
346 # . Data::Dump::dump \@results;
347  
348 11 return ( RESULT_OK, \@results, $controls );
349  
350 }
351  
352 sub _search_default_test_data {
353 2 ▶ my ( $self, $reqData ) = @_;
Subroutine _search_default_test_data: called CC=6 SCAR=18.4
354  
355 #warn 'SEARCH DEFAULT: ' . Data::Dump::dump \@_;
356  
357 2 my $base = $reqData->{'baseObject'};
358  
359 # plain die if dn contains 'dying'
360 2 ▶ die("panic") if $base =~ /dying/;
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ /dying/ 0 2 2
361  
362 # return a correct LDAPresult, but an invalid entry
363 2 ▶ return RESULT_OK, { test => 1 } if $base =~ /invalid entry/;
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ /invalid entry/ 0 2 2
364  
365 # return an invalid LDAPresult
366 2 ▶ return { test => 1 } if $base =~ /invalid result/;
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ /invalid result/ 0 2 2
367  
368 2 my @entries;
369 2 ▶ if ( $reqData->{'scope'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reqData->{'scope'}) { } 2 0 2
370  
371 # onelevel or subtree
372 2 ▶ for ( my $i = 1; $i < 11; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 11) 20 2 22
373 20 my $dn = "ou=test $i,$base";
374 20 my $entry = Net::LDAP::Entry->new;
375 20 $entry->dn($dn);
376 20 $entry->add(
377 dn => $dn,
378 sn => 'value1',
379 cn => [qw(value1 value2)]
380 );
381 20 push @entries, $entry;
382 }
383  
384 2 my $entry1 = Net::LDAP::Entry->new;
385 2 $entry1->dn("cn=dying entry,$base");
386 2 $entry1->add(
387 cn => 'dying entry',
388 description =>
389 'This entry will result in a dying error when queried'
390 );
391 2 push @entries, $entry1;
392  
393 2 my $entry2 = Net::LDAP::Entry->new;
394 2 $entry2->dn("cn=invalid entry,$base");
395 2 $entry2->add(
396 cn => 'invalid entry',
397 description =>
398 'This entry will result in ASN1 error when queried'
399 );
400 2 push( @entries, $entry2 );
401  
402 2 my $entry3 = Net::LDAP::Entry->new;
403 2 $entry3->dn("cn=invalid result,$base");
404 2 $entry3->add(
405 cn => 'invalid result',
406 description =>
407 'This entry will result in ASN1 error when queried'
408 );
409 2 push @entries, $entry3;
410 }
411 else {
412  
413 # base
414 0 my $entry = Net::LDAP::Entry->new;
415 0 $entry->dn($base);
416 0 $entry->add(
417 dn => $base,
418 sn => 'value1',
419 cn => [qw(value1 value2)]
420 );
421 0 push @entries, $entry;
422 }
423 2 return RESULT_OK, @entries;
424 }
425  
426 sub add {
427 10 ▶ my ( $self, $reqData, $reqMsg ) = @_;
Subroutine add: called CC=5 SCAR=16.6
428  
429 10 my $key = $reqData->{objectName};
430 10 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 10 10
431 0 warn 'ADD: ' . Data::Dump::dump \@_;
432 0 warn "key: $key";
433 }
434  
435 10 ▶ if ( exists $Data{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $MyLDAPServer::Data{$key}) 1 9 10
436 1 return RESULT_ALREADY_EXISTS;
437 }
438  
439 9 my $entry = Net::LDAP::Entry->new;
440 9 $entry->dn($key);
441 9 for my $attr ( @{ $reqData->{attributes} } ) {
442 18 $entry->add( $attr->{type} => \@{ $attr->{vals} } );
443 }
444  
445 9 $Data{$key} = $entry;
446  
447 9 ▶ if ( exists $self->{_flags}->{active_directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_flags"}{"active_directory"}) 0 9 9
448 0 $self->_add_AD( $reqData, $reqMsg, $key, $entry, \%Data );
449 }
450  
451 9 return RESULT_OK;
452 }
453  
454 sub modify {
455 8 ▶ my ( $self, $reqData, $reqMsg ) = @_;
Subroutine modify: called CC=12 SCAR=25.1
456  
457 8 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 8 8
458 0 warn 'MODIFY: ' . Data::Dump::dump \@_;
459 }
460  
461 8 my $key = $reqData->{object};
462 8 ▶ if ( !exists $Data{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $MyLDAPServer::Data{$key}) 1 7 8
463 1 return RESULT_NO_SUCH_OBJECT;
464 }
465  
466 7 my @mods = @{ $reqData->{modification} };
467 7 for my $mod (@mods) {
468 7 my $attr = $mod->{modification}->{type};
469 7 my $vals = $mod->{modification}->{vals};
470 7 my $entry = $Data{$key};
471  
472 7 my $current_value = $entry->get_value( $attr, asref => 1 );
473  
474 7 ▶ if ( $mod->{operation} == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mod->{'operation'} == 0) { } 3 4 7
elsif ($mod->{'operation'} == 1) { } 3 1 4
elsif ($mod->{'operation'} == 2) { } 1 0 1
475 3 ▶ if ( defined $current_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_value) 2 1 3
476 2 for my $v (@$current_value) {
477 2 ▶ if ( grep { $_ eq $v } @$vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $v;} @$vals) 1 1 2
478 1 return RESULT_TYPE_OR_VALUE_EXISTS;
479 }
480 }
481 }
482  
483 2 $entry->add( $attr => $vals );
484 }
485 elsif ( $mod->{operation} == 1 ) {
486 3 ▶ if ( !defined $current_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current_value) 1 2 3
487 1 return RESULT_NO_SUCH_ATTRIBUTE;
488 }
489 2 $entry->delete( $attr => $vals );
490 }
491 elsif ( $mod->{operation} == 2 ) {
492 1 $entry->replace( $attr => $vals );
493 }
494 else {
495 0 croak "unknown modify operation: $mod->{operation}";
496 }
497 }
498  
499 5 ▶ if ( $self->{_flags}->{active_directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_flags"}{"active_directory"}) 0 5 5
500 0 $self->_modify_AD( $reqData, $reqMsg, \%Data );
501 }
502  
503 5 return RESULT_OK;
504  
505 }
506  
507 sub delete {
508 0 ▶ my ( $self, $reqData, $reqMsg ) = @_;
Subroutine delete: not called CC=3 SCAR=24.8
509  
510 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
511 0 warn 'DELETE: ' . Data::Dump::dump \@_;
512 }
513  
514 0 my $key = $reqData;
515 0 ▶ if ( !exists $Data{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $MyLDAPServer::Data{$key}) 0 0 0
516 0 return RESULT_NO_SUCH_OBJECT;
517 }
518 0 delete $Data{$key};
519  
520 0 return RESULT_OK;
521  
522 }
523  
524 sub modifyDN {
525 3 ▶ my ( $self, $reqData, $reqMsg ) = @_;
Subroutine modifyDN: called CC=5 SCAR=16.2
526  
527 #warn "modifyDN: " . Data::Dump::dump \@_;
528 #warn "modifyDN: " . Data::Dump::dump($reqData);
529 #warn "existing: " . Data::Dump::dump( \%Data );
530 #warn "existing DNs: " . Data::Dump::dump([keys %Data]);
531  
532 3 my $oldkey = $reqData->{entry};
533 3 my $newkey = $reqData->{newrdn};
534 3 ▶ if ( defined $reqData->{newSuperior} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reqData->{'newSuperior'}) { } 0 3 3
535 0 $newkey .= ',' . $reqData->{newSuperior};
536 }
537 else {
538 # As we only have the new relative DN, we still
539 # need the base for it. We'll take it from $oldkey
540 3 my $exploded_dn = ldap_explode_dn( $oldkey, casefold => 'none' );
541 3 shift @$exploded_dn;
542 3 $newkey .= ',' . canonical_dn( $exploded_dn, casefold => 'none' );
543 }
544  
545 3 ▶ if ( !exists $Data{$oldkey} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $MyLDAPServer::Data{$oldkey}) 1 2 3
546 1 return RESULT_NO_SUCH_OBJECT;
547 }
548 2 ▶ if ( exists $Data{$newkey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $MyLDAPServer::Data{$newkey}) 1 1 2
549 1 return RESULT_ALREADY_EXISTS;
550 }
551 1 my $entry = $Data{$oldkey};
552 1 my $newentry = $entry->clone;
553 1 $newentry->dn($newkey);
554 1 $Data{$newkey} = $newentry;
555  
556 #warn "created new entry: $newkey";
557 1 ▶ if ( $reqData->{deleteoldrdn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reqData->{"deleteoldrdn"}) 1 0 1
558 1 delete $Data{$oldkey};
559  
560 #warn "deleted old entry: $oldkey";
561 }
562  
563 1 return RESULT_OK;
564 }
565  
566 sub compare {
567 0 ▶ my ( $self, $reqData, $reqMsg ) = @_;
Subroutine compare: not called CC=1 SCAR=6.9
568  
569 #warn "compare: " . Data::Dump::dump \@_;
570  
571 0 return RESULT_OK;
572 }
573  
574 sub abandon {
575 0 ▶ my ( $self, $reqData, $reqMsg ) = @_;
Subroutine abandon: not called CC=1 SCAR=6.9
576  
577 #warn "abandon: " . Data::Dump::dump \@_;
578  
579 0 return RESULT_OK;
580 }
581  
582 16 my $token_counter = 100;
583 16 my $sid_str = 'S-1-2-3-4-5-6-1234';
584  
585 0 ▶ sub _get_server_sid_string { return $sid_str }
Subroutine _get_server_sid_string: not called CC=1 SCAR=6.9
586  
587 sub _add_AD {
588 0 ▶ my ( $server, $reqData, $reqMsg, $key, $entry, $data ) = @_;
Subroutine _add_AD: not called CC=6 SCAR=37.4
589  
590 0 for my $attr ( @{ $reqData->{attributes} } ) {
591 0 ▶ if ( $attr->{type} eq 'objectClass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"type"} eq "objectClass") 0 0 0
592 0 ▶ if ( grep { $_ eq 'group' } @{ $attr->{vals} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq 'group';} @{$$attr{'vals'};}) { } 0 0 0
593  
594 # groups
595 0 $token_counter++;
596 0 ( my $group_sid_str = _get_server_sid_string() )
597 =~ s/-1234$/-$token_counter/;
598 0 ▶ if ( $ENV{LDAP_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LDAP_DEBUG"}) 0 0 0
599 0 carp "group_sid_str = $group_sid_str";
600 }
601 0 $entry->add( 'primaryGroupToken' => $token_counter );
602 0 $entry->add( 'objectSID' => "$group_sid_str" );
603 0 $entry->add( 'distinguishedName' => $key );
604  
605 }
606 else {
607  
608 # users
609 0 my $gid = $entry->get_value('primaryGroupID');
610 0 ▶ $gid = '1234' unless ( defined $gid );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gid 0 0 0
611 0 ( my $user_sid_str = _get_server_sid_string() )
612 =~ s/-1234$/-$gid/;
613  
614 0 my $user_sid = Net::LDAP::SID->new($user_sid_str);
615 0 $entry->add( 'objectSID' => $user_sid->as_binary );
616 0 $entry->add( 'distinguishedName' => $key );
617  
618 }
619 }
620  
621 }
622  
623 0 _update_groups($data);
624  
625 #dump $reqData;
626 #dump $data;
627  
628 }
629  
630 # AD stores group assignments in 'member' attribute
631 # of each group. 'memberOf' is linked internally to that
632 # attribute. We set 'memberOf' here if mimicing AD.
633 sub _update_groups {
634 0 ▶ my $data = shift;
Subroutine _update_groups: not called CC=10 SCAR=47.0
635  
636 # all groups
637 0 for my $key ( keys %$data ) {
638 0 my $entry = $data->{$key};
639  
640 #warn "groups: update groups for $key";
641 0 ▶ if ( !$entry->get_value('sAMAccountName') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->get_value("sAMAccountName")) 0 0 0
642  
643 #dump $entry;
644  
645 # group entry.
646 # are the users listed in member
647 # still assigned in their memberOf?
648 0 my %users = map { $_ => 1 } $entry->get_value('member');
649 0 for my $dn ( keys %users ) {
650  
651 #warn "User $dn is a member in $key";
652 0 my $user = $data->{$dn};
653 0 my %groups = map { $_ => 1 } $user->get_value('memberOf');
654  
655 # if $user does not list $key (group) as a memberOf,
656 # then add it.
657 0 ▶ if ( !exists $groups{$key} && exists $users{$dn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $groups{$key} and exists $users{$dn}) 0 0 0
Condition0%0/3
not exists $groups{$key} and exists $users{$dn}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $groups{$key} and exists $users{$dn}
not exists $groups{$key} exists $users{$dn}
Truth table0%0/3
not(A) and B
A= not exists $groups{$key}B= exists $users{$dn}
ABresult
0X0
100
111
658 0 $groups{$key}++;
659 0 $user->replace( memberOf => [ keys %groups ] );
660 }
661 }
662  
663 }
664  
665 }
666  
667 # all users
668  
669 0 for my $key ( keys %$data ) {
670 0 my $entry = $data->{$key};
671  
672 #warn "users: update groups for $key";
673 0 ▶ if ( $entry->get_value('sAMAccountName') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->get_value("sAMAccountName")) 0 0 0
674  
675 #dump $entry;
676  
677 # user entry
678 # get its groups and add this user to each of them.
679 0 my %groups = map { $_ => 1 } $entry->get_value('memberOf');
680 0 for my $dn ( keys %groups ) {
681 0 my $group = $data->{$dn};
682 my %users
683 0 = map { $_ => 1 } ( $group->get_value('member') );
684  
685 # if group no longer lists this user as a member,
686 # remove group from memberOf
687 0 ▶ if ( !exists $users{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $users{$key}) 0 0 0
688 0 delete $groups{$dn};
689 0 $entry->replace( memberOf => [ keys %groups ] );
690 }
691 }
692  
693 }
694 }
695  
696 }
697  
698 sub _modify_AD {
699 0 ▶ my ( $server, $reqData, $reqMsg, $data ) = @_;
Subroutine _modify_AD: not called CC=1 SCAR=6.9
700  
701 #dump $data;
702 0 _update_groups($data);
703  
704 #Data::Dump::dump $data;
705  
706 }
707  
708 # override the default behaviour to support controls
709 sub handle {
710 50 ▶ my $self = shift;
Subroutine handle: called CC=16 SCAR=29.7
711 50 my $socket;
712  
713 #warn "$Net::LDAP::Server::VERSION";
714 50 ▶ if ( $Net::LDAP::Server::VERSION ge '0.43' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::LDAP::Server::VERSION ge '0.43') { } 50 0 50
715 50 $socket = $self->{in};
716 }
717 else {
718 0 $socket = $self->{socket};
719 }
720  
721 50 asn_read( $socket, my $pdu );
722  
723 #print '-' x 80,"\n";
724 #print "Received:\n";
725 #Convert::ASN1::asn_dump(\*STDOUT,$pdu);
726 50 my $request = $LDAPRequest->decode($pdu);
727 50 ▶ my $mid = $request->{'messageID'}
Branch: 1 branch
BranchTrueFalseTotal
my $mid = $request->{"messageID"} or return 1 2 48 50
728 or return 1;
729  
730 #print "messageID: $mid\n";
731 #use Data::Dumper; print Dumper($request);
732  
733 48 my $reqType;
734 48 foreach my $type (@Net::LDAP::Server::reqTypes) {
735 255 ▶ if ( defined $request->{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request->{$type}) 48 207 255
736 48 $reqType = $type;
737 48 last;
738 }
739 }
740 48 ▶ my $respType = $Net::LDAP::Server::respTypes{$reqType}
Branch: 1 branch
BranchTrueFalseTotal
my $respType = $Net::LDAP::Server::respTypes{$reqType} or return 1 5 43 48
741 or
742 return 1; # if no response type is present hangup the connection
743  
744 43 my $reqData = $request->{$reqType};
745  
746 # here we can do something with the request of type $reqType
747 43 my $method = $Net::LDAP::Server::functions{$reqType};
748 43 my ( $result, $controls );
749 43 ▶ if ( $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 43 0 43
750 43 ▶ if ( $method eq 'search' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'search') { } 17 26 43
751 17 my @entries;
752 17 eval {
753 17 ( $result, @entries )
754 = $self->search( $reqData, $request );
755 17 ▶ if ( ref( $entries[0] ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entries[0] eq "ARRAY") 11 6 17
756 11 $controls = pop(@entries);
757 11 @entries = @{ shift(@entries) };
758  
759 #warn "got controls";
760 }
761 };
762  
763 # rethrow
764 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
765 0 croak $@;
766 }
767  
768 17 foreach my $entry (@entries) {
769 38 my $data;
770  
771 # default is to return a searchResEntry
772 38 my $sResType = 'searchResEntry';
773 38 ▶ if ( ref $entry eq 'Net::LDAP::Entry' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $entry eq 'Net::LDAP::Entry') { } 38 0 38
elsif (ref $entry eq 'Net::LDAP::Reference') { } 0 0 0
774 38 $data = $entry->{'asn'};
775 }
776 elsif ( ref $entry eq 'Net::LDAP::Reference' ) {
777 0 $data = $entry->{'asn'};
778 0 $sResType = 'searchResRef';
779 }
780 else {
781 0 $data = $entry;
782 }
783  
784 38 my $response;
785  
786 # is the full message specified?
787 38 ▶ if ( defined $data->{'protocolOp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{'protocolOp'}) { } 0 38 38
788 0 $response = $data;
789 0 $response->{'messageID'} = $mid;
790 }
791 else {
792 38 $response = {
793 'messageID' => $mid,
794 'protocolOp' => { $sResType => $data },
795 };
796 }
797 38 my $pdu = $LDAPResponse->encode($response);
798 38 ▶ if ($pdu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu) { } 38 0 38
799 38 print {$socket} $pdu;
800 }
801 else {
802 0 $result = undef;
803 0 last;
804 }
805 }
806 }
807 else {
808 26 eval { $result = $self->$method( $reqData, $request ) };
809 }
810 43 ▶ $result = Net::LDAP::Server::_operations_error() unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 43 43
811 }
812 else {
813 0 $result = {
814 'matchedDN' => '',
815 'errorMessage' => sprintf(
816 "%s operation is not supported by %s",
817 $method, ref $self
818 ),
819 'resultCode' => LDAP_UNWILLING_TO_PERFORM
820 };
821 }
822  
823 # and now send the result to the client
824 43 print {$socket} _encode_result( $mid, $respType, $result, $controls );
825  
826 43 return 0;
827 }
828  
829 sub _encode_result {
830 43 ▶ my ( $mid, $respType, $result, $controls ) = @_;
Subroutine _encode_result: called CC=3 SCAR=13.1
831  
832 43 my $response = {
833 'messageID' => $mid,
834 'protocolOp' => { $respType => $result },
835 };
836 43 ▶ if ( defined $controls ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $controls) 0 43 43
837 0 $response->{'controls'} = $controls;
838 }
839  
840 #warn "response: " . Data::Dump::dump($response) . "\n";
841  
842 43 my $pdu = $LDAPResponse->encode($response);
843  
844 # if response encoding failed return the error
845 43 ▶ if ( !$pdu ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pdu) 0 43 43
846 0 $response->{'protocolOp'}->{$respType}
847 = Net::LDAP::Server::_operations_error();
848 0 delete $response->{'controls'}; # just in case
849 0 $pdu = $LDAPResponse->encode($response);
850 }
851  
852 43 return $pdu;
853 }
854  
855 } # end MyLDAPServer
856  
857 =head2 new( I<port>, I<key_value_args> )
858  
859 Create a new server. Basically this just fork()s a child process
860 listing on I<port> and handling requests using Net::LDAP::Server.
861  
862 I<port> defaults to 10636.
863  
864 I<port> may be an IO::Socket::INET object listening to a local port.
865  
866 I<key_value_args> may be:
867  
868 =over
869  
870 =item data
871  
872 I<data> is optional data to return from the Net::LDAP search() function.
873 Typically it would be an array ref of Net::LDAP::Entry objects.
874  
875 =item auto_schema
876  
877 A true value means the add(), modify() and delete() methods will
878 store internal in-memory data based on DN values, so that search()
879 will mimic working on a real LDAP schema.
880  
881 =item active_directory
882  
883 Work in Active Directory mode. This means that entries are automatically
884 assigned a objectSID, and some effort is made to mimic the member/memberOf
885 linking between AD Users and Groups.
886  
887 =back
888  
889 new() will croak() if there was a problem fork()ing a new server.
890  
891 Returns a Net::LDAP::Server::Test object, which is just a
892 blessed reference to the PID of the forked server.
893  
894 =cut
895  
896 16 my %PORTS; # inside-out tracking of port-per-server
897  
898 sub new {
899 16 ▶ my $class = shift;
Subroutine new: called CC=18 SCAR=31.3
Pod documented
900 16 ▶ my $port = shift || 10636;
Condition50%1/2
shift() || 10636
l!l
160
MC/DC0%0/1
shift() || 10636
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
901 16 my %arg = @_;
902  
903 16 ▶ if ( $arg{data} and $arg{auto_schema} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"data"} and $arg{"auto_schema"}) 0 16 16
Condition66%2/3
$arg{"data"} and $arg{"auto_schema"}
!ll&&!rl&&r
1420
MC/DC0%0/2
$arg{"data"} and $arg{"auto_schema"}
$arg{"data"} $arg{"auto_schema"}
Truth table66%2/3
A and B
A= $arg{"data"}B= $arg{"auto_schema"}
ABresult
0X0
100
111
904 0 croak
905 "cannot handle both 'data' and 'auto_schema' features. Pick one.";
906 }
907  
908 16 pipe( my $r_fh, my $w_fh );
909  
910 16 my $pid = fork();
911  
912 16 ▶ if ( !defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 16 16
elsif ($pid == 0) { } 7 9 16
913 0 croak "can't fork a LDAP test server: $!";
914 }
915 elsif ( $pid == 0 ) {
916  
917 warn "Creating new LDAP server on port "
918 . ( ref $port ? $port->sockport : $port )
919 . " ... \n"
920 7 ▶ if $ENV{LDAP_DEBUG};
Branch: 2 branches
BranchTrueFalseTotal
if $ENV{"LDAP_DEBUG"} 0 7 7
ref $port ? : 0 0 0
921  
922 # the child (server)
923 7 ▶ my $sock = ref $port ? $port : IO::Socket::INET->new(
Branch: 2 branches
BranchTrueFalseTotal
my $sock = ref $port ? $port : "IO::Socket::INET"->new("Listen", 5, "Proto", "tcp", "Reuse", 1, "LocalPort", $port) or die "Unable to listen on port ${port}: $!" 0 7 7
ref $port ? : 0 7 7
924 Listen => 5,
925 Proto => 'tcp',
926 Reuse => 1,
927 LocalPort => $port
928 ) or die "Unable to listen on port $port: $!";
929  
930 # tickle the pipe to show we've opened ok
931 7 syswrite $w_fh, "Ready\n";
932 7 undef $w_fh;
933  
934 7 my $sel = IO::Select->new($sock);
935 7 my %Handlers;
936 7 ▶ while ( my @ready = $sel->can_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read) 57 0 57
937 57 foreach my $fh (@ready) {
938 57 ▶ if ( $fh == $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh == $sock) { } 7 50 57
939  
940 # let's create a new socket
941 7 my $psock = $sock->accept;
942 7 $sel->add($psock);
943 7 $Handlers{*$psock} = MyLDAPServer->new( $psock, %arg );
944  
945 #warn "new socket created";
946 }
947 else {
948 50 my $result = $Handlers{*$fh}->handle;
949 50 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) 7 43 50
950  
951 # we have finished with the socket
952 7 $sel->remove($fh);
953 7 $fh->close;
954 7 delete $Handlers{*$fh};
955  
956 # if there are no open connections,
957 # exit the child process.
958 7 ▶ if ( !keys %Handlers ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %Handlers) 7 0 7
959 warn " ... shutting down server\n"
960 7 ▶ if $ENV{LDAP_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LDAP_DEBUG"} 0 7 7
961 7 exit(0);
962 }
963 }
964 }
965 }
966 }
967  
968 # if we get here, we had some kinda problem.
969 0 croak "reached the end of while() loop prematurely";
970  
971 }
972 else {
973  
974 # this is the child
975 9 ▶ warn "child pid=$pid" if $ENV{LDAP_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LDAP_DEBUG"} 0 9 9
976  
977 9 ▶ return unless <$r_fh> =~ /Ready/; # newline varies
Branch: 1 branch
BranchTrueFalseTotal
unless readline($r_fh) =~ /Ready/ 0 9 9
978 9 close($r_fh);
979 9 my $self = bless( \$pid, $class );
980 9 $PORTS{"$self"} = $port;
981 9 return $self;
982 }
983  
984 }
985  
986 =head2 stop
987  
988 Calls waitpid() on the server's associated child process.
989 You may find it helpful to call this method explicitly,
990 especially if you are creating multiple
991 servers in the same test. Otherwise, this method is typically not
992 needed and may even cause your tests to hang indefinitely if
993 they die prematurely. YMMV.
994  
995 To prevent waitpid() from blocking and hanging your test server,
996 it is wrapped in an alarm() call, which will wait 2 seconds
997 and then call kill() on the reluctant pid. You have been warned.
998  
999 =cut
1000  
1001 sub stop {
1002 3 ▶ my $server = shift;
Subroutine stop: called CC=7 SCAR=21.4
Pod documented
1003 3 my $pid = $$server;
1004 3 ▶ warn "\$pid = $pid" if $ENV{LDAP_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LDAP_DEBUG"} 0 3 3
1005 3 eval {
1006 local $SIG{ALRM}
1007 3 ▶ = sub { die "waitpid($pid, 0) took too long\n" }; # NB: \n required
Subroutine __ANON__: called CC=1 SCAR=0.0
1008 3 alarm 2;
1009 3 my $ret = waitpid( $pid, 0 );
1010 2 ▶ warn "waitpid returned $ret" if $ENV{LDAP_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LDAP_DEBUG"} 0 2 2
1011 2 alarm 0;
1012 };
1013 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 2 3
1014 1 warn "$@";
1015 1 my $cnt = kill( 9, $pid );
1016 1 warn "kill(9,$pid) returned $cnt\n";
1017 }
1018 else {
1019 2 ▶ warn "waitpid($pid, 0) worked" if $ENV{LDAP_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"LDAP_DEBUG"} 0 2 2
1020 }
1021 3 my $tries = 0;
1022 3 ▶ while ( $server->port_is_open() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->port_is_open) 0 3 3
1023 0 warn "Waiting for port to close...\n";
1024 0 sleep(1);
1025 0 ▶ if ( $tries++ > 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries++ > 10) 0 0 0
1026 0 warn "Failed to determine that port closed. Giving up.\n";
1027 0 last;
1028 }
1029 }
1030 3 return $pid;
1031 }
1032  
1033 =head2 port_is_open
1034  
1035 Returns IO::Socket::INET->new for the current server port.
1036 If the port is already in use, this is a false value.
1037  
1038 =cut
1039  
1040 sub port_is_open {
1041 3 ▶ my $self = shift;
Subroutine port_is_open: called CC=1 SCAR=0.0
Pod documented
1042 3 my $port = $PORTS{"$self"};
1043 3 return IO::Socket::INET->new(
1044 PeerAddr => '127.0.0.1',
1045 PeerPort => $port,
1046 Proto => 'tcp',
1047 Type => SOCK_STREAM,
1048 );
1049 }
1050  
1051 =head1 AUTHOR
1052  
1053 Peter Karman, C<< <karman at cpan.org> >>
1054  
1055 =head1 BUGS
1056  
1057 Please report any bugs or feature requests to
1058 C<bug-net-ldap-server-test at rt.cpan.org>, or through the web interface at
1059 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-LDAP-Server-Test>.
1060 I will be notified, and then you'll automatically be notified of progress on
1061 your bug as I make changes.
1062  
1063 =head1 SUPPORT
1064  
1065 You can find documentation for this module with the perldoc command.
1066  
1067 perldoc Net::LDAP::Server::Test
1068  
1069 You can also look for information at:
1070  
1071 =over 4
1072  
1073 =item * AnnoCPAN: Annotated CPAN documentation
1074  
1075 L<http://annocpan.org/dist/Net-LDAP-Server-Test>
1076  
1077 =item * CPAN Ratings
1078  
1079 L<http://cpanratings.perl.org/d/Net-LDAP-Server-Test>
1080  
1081 =item * RT: CPAN's request tracker
1082  
1083 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-LDAP-Server-Test>
1084  
1085 =item * Search CPAN
1086  
1087 L<http://search.cpan.org/dist/Net-LDAP-Server-Test>
1088  
1089 =back
1090  
1091 =head1 ACKNOWLEDGEMENTS
1092  
1093 The Minnesota Supercomputing Institute C<< http://www.msi.umn.edu/ >>
1094 sponsored the development of this software.
1095  
1096 =head1 COPYRIGHT & LICENSE
1097  
1098 Copyright 2007 by the Regents of the University of Minnesota.
1099  
1100 This program is free software; you can redistribute it and/or modify it
1101 under the same terms as Perl itself.
1102  
1103 =head1 SEE ALSO
1104  
1105 Net::LDAP::Server
1106  
1107 =cut
1108  
1109 16 1;
↑ Summary