1# Copied from Net::LDAP's t/common.pl - perl licence 2# 3# 4BEGIN { 5 6 $SERVER_EXE = '/usr/sbin/ldapd'; 7 $SERVER_TYPE = 'ldapd+ssl+ipc'; 8 9 undef $SERVER_EXE unless $SERVER_EXE and -x $SERVER_EXE; 10 11 # fallback for the host to connect - needs to support IPv4 & IPv6 12 $HOST ||= 'localhost'; 13 14 # Where to put temporary files while testing 15 # the Makefile is setup to delete temp/ when make clean is run 16 $TEMPDIR = "./temp"; 17 18 $PASSWD = 'secret'; 19 $BASEDN = "o=University of Michigan, c=US"; 20 $MANAGERDN= "cn=Manager, o=University of Michigan, c=US"; 21 $JAJDN = "cn=James A Jones 1, ou=Alumni Association, ou=People, o=University of Michigan, c=US"; 22 $BABSDN = "cn=Barbara Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US"; 23 $PORT = 6640; 24 @LDAPD = ($SERVER_EXE, "-r", "./temp", "-f", "./nldapd.conf", "-s" , "./ctrlsock", "-dv"), 25 26 $LDAP_VERSION ||= 3; 27 mkdir($TEMPDIR,0777); 28 die "$TEMPDIR is not a directory" unless -d $TEMPDIR; 29} 30 31use Test::More; 32use Net::LDAP; 33use Net::LDAP::LDIF; 34use Net::LDAP::Util qw(canonical_dn); 35use File::Path qw(rmtree); 36use File::Basename qw(basename); 37use File::Compare qw(compare_text); 38 39my $pid; 40 41sub start_server { 42 my %arg = (version => 3, @_); 43 44 return 0 45 unless ($LDAP_VERSION >= $arg{version} 46 and $LDAPD[0] and -x $LDAPD[0] 47 and (!$arg{ssl} or $SSL_PORT) 48 and (!$arg{ipc} or $IPC_SOCK)); 49 50 note("@LDAPD") if $ENV{TEST_VERBOSE}; 51 52 my $log = $TEMPDIR . "/" . basename($0,'.t'); 53 54 unless ($pid = fork) { 55 die "fork: $!" unless defined $pid; 56 57 open(STDERR, ">$log"); 58 open(STDOUT, ">&STDERR"); 59 close(STDIN); 60 61 exec(@LDAPD) or die "cannot exec @LDAPD"; 62 } 63 64 sleep 2; # wait for server to start 65 return 1; 66} 67 68sub kill_server { 69 if ($pid) { 70 kill 9, $pid; 71 sleep 2; 72 undef $pid; 73 } 74} 75 76END { 77 kill_server(); 78} 79 80sub client { 81 my %arg = @_; 82 my $ldap; 83 my $count; 84 local $^W = 0; 85 my %opt = map { $_ => $arg{$_} } grep { exists($arg{$_}) } qw/inet4 inet6 debug/; 86 87 if ($arg{ssl}) { 88 require Net::LDAPS; 89 until($ldap = Net::LDAPS->new($HOST, %opt, port => $SSL_PORT, version => 3)) { 90 die "ldaps://$HOST:$SSL_PORT/ $@" if ++$count > 10; 91 sleep 1; 92 } 93 } 94 elsif ($arg{ipc}) { 95 require Net::LDAPI; 96 until($ldap = Net::LDAPI->new($IPC_SOCK)) { 97 die "ldapi://$IPC_SOCK/ $@" if ++$count > 10; 98 sleep 1; 99 } 100 } 101 elsif ($arg{url}) { 102 print "Trying $arg{url}\n"; 103 until($ldap = Net::LDAP->new($arg{url}, %opt)) { 104 die "$arg{url} $@" if ++$count > 10; 105 sleep 1; 106 } 107 } 108 else { 109 until($ldap = Net::LDAP->new($HOST, %opt, port => $PORT, version => $LDAP_VERSION)) { 110 die "ldap://$HOST:$PORT/ $@" if ++$count > 10; 111 sleep 1; 112 } 113 } 114 $ldap; 115} 116 117sub compare_ldif { 118 my($test,$mesg) = splice(@_,0,2); 119 120 unless (ok(!$mesg->code, $mesg->error)) { 121 skip($mesg->error, 2); 122 return; 123 } 124 125 my $ldif = Net::LDAP::LDIF->new("$TEMPDIR/${test}-out.ldif","w", lowercase => 1); 126 unless (ok($ldif, "Read ${test}-out.ldif")) { 127 skip("Read error", 1); 128 return; 129 } 130 131 my @canon_opt = (casefold => 'lower', separator => ', '); 132 foreach $entry (@_) { 133 $entry->dn(canonical_dn($entry->dn, @canon_opt)); 134 foreach $attr ($entry->attributes) { 135 $entry->delete($attr) if $attr =~ /^(modifiersname|modifytimestamp|creatorsname|createtimestamp)$/i; 136 if ($attr =~ /^(seealso|member|owner)$/i) { 137 $entry->replace($attr => [ map { canonical_dn($_, @canon_opt) } $entry->get_value($attr) ]); 138 } 139 } 140 $ldif->write($entry); 141 } 142 143 $ldif->done; # close the file; 144 145 ok(!compare_text("$TEMPDIR/${test}-out.ldif", "data/${test}-cmp.ldif"), "data/${test}-cmp.ldif"); 146} 147 148sub ldif_populate { 149 my ($ldap, $file, $change) = @_; 150 my $ok = 1; 151 152 my $ldif = Net::LDAP::LDIF->new($file,"r", changetype => $change || 'add') 153 or return; 154 155 while (my $e = $ldif->read_entry) { 156 $mesg = $e->update($ldap); 157 if ($mesg->code) { 158 $ok = 0; 159 Net::LDAP::LDIF->new(qw(- w))->write_entry($e); 160 print "# ",$mesg->code,": ",$mesg->error,"\n"; 161 } 162 } 163 $ok; 164} 165 1661; 167