1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#!perl -T
use strict;
use warnings;
use Test::More tests => 35;
use Net::LDAP::Constant qw(
LDAP_SUCCESS
LDAP_PARAM_ERROR
LDAP_INVALID_DN_SYNTAX
LDAP_ALREADY_EXISTS
);
use Net::LDAP::Entry;
use Net::LDAP::Util qw(canonical_dn);
use Test::Net::LDAP::Mock::Data;
use Test::Net::LDAP::Util qw(ldap_result_is ldap_dn_is);
my $data = Test::Net::LDAP::Mock::Data->new;
my $search;
# Add an entry
$data->add_ok('uid=user1, dc=example, dc=com', attrs => [
sn => 'User',
cn => 'One',
]);
$search = $data->search_ok(
base => 'dc=example, dc=com', scope => 'one',
filter => '(uid=*)', attrs => [qw(uid sn cn)],
);
is(scalar($search->entries), 1);
ldap_dn_is($search->entry->dn, 'uid=user1,dc=example,dc=com');
is($search->entry->get_value('uid'), 'user1');
is($search->entry->get_value('sn'), 'User');
is($search->entry->get_value('cn'), 'One');
# Add more entries
$data->add_ok('uid=user2, dc=example, dc=com', attrs => [
sn => 'User',
cn => 'Two',
]);
$data->add_ok('uid=user3, dc=example, dc=com', attrs => [
sn => 'User',
cn => 'Three',
]);
$search = $data->search_ok(
base => 'dc=example, dc=com', scope => 'one',
filter => '(uid=*)', attrs => [qw(uid sn cn)],
);
is(scalar($search->entries), 3);
my @entries = sort {$a->get_value('uid') cmp $b->get_value('uid')} $search->entries;
ldap_dn_is($entries[0]->dn, 'uid=user1,dc=example,dc=com');
is($entries[0]->get_value('uid'), 'user1');
is($entries[0]->get_value('sn'), 'User');
is($entries[0]->get_value('cn'), 'One');
ldap_dn_is($entries[1]->dn, 'uid=user2,dc=example,dc=com');
is($entries[1]->get_value('uid'), 'user2');
is($entries[1]->get_value('sn'), 'User');
is($entries[1]->get_value('cn'), 'Two');
ldap_dn_is($entries[2]->dn, 'uid=user3,dc=example,dc=com');
is($entries[2]->get_value('uid'), 'user3');
is($entries[2]->get_value('sn'), 'User');
is($entries[2]->get_value('cn'), 'Three');
# Callback
my @callback_args;
my $mesg = $data->add_ok('uid=user4, dc=example, dc=com',
callback => sub {
push @callback_args, \@_;
}
);
is(scalar(@callback_args), 1);
is(scalar(@{$callback_args[0]}), 1);
cmp_ok($callback_args[0][0], '==', $mesg);
# Preserve DN exactly as specified at the time of add()
$data->add_ok('UID=User5, DC=Example,DC=COM');
$search = $data->search_ok(
base => 'dc=example, dc=com', scope => 'one',
filter => '(uid=user5)', attrs => [qw(uid)],
);
is $search->entry->dn, 'UID=User5, DC=Example,DC=COM';
is $search->entry->get_value('uid'), 'User5';
# Error: dn is missing
$data->add_is([attrs => [
cn => 'Test']
], LDAP_PARAM_ERROR);
# Error: dn is invalid
$data->add_is(['invalid', attrs => [
cn => 'Test'
]], LDAP_INVALID_DN_SYNTAX);
$data->add_is([dn => 'invalid', attrs => [
cn => 'Test'
]], LDAP_INVALID_DN_SYNTAX);
# Error: Attempt to add a duplicate
$data->add_is(['uid=user1, dc=example, dc=com', attrs => [
cn => 'Test'
]], LDAP_ALREADY_EXISTS);
|