File: 04attributes.t

package info (click to toggle)
libnet-ldap-server-test-perl 0.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: perl: 1,038; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 1,658 bytes parent folder | download | duplicates (3)
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
use Test::More tests => 11;

use strict;
use warnings;
use Carp;

use Net::LDAP;
use Net::LDAP::Server::Test;
use Net::LDAP::Entry;

#
# these tests pulled nearly verbatim from the Net::LDAP synopsis
#

my %opts = (port => '10636');

my $host = 'ldap://localhost:' . $opts{port};

ok( my $server = Net::LDAP::Server::Test->new( $opts{port}, auto_schema => 1 ),
    "spawn new server" );

ok( my $ldap = Net::LDAP->new( $host, %opts, ), "new LDAP connection" );

unless ($ldap) {
    my $error = $@;
    diag("stop() server");
    $server->stop();
    croak "Unable to connect to LDAP server $host: $error";
}

ok( my $rc = $ldap->bind(), "LDAP bind()" );

$ldap->add(
    dn => 'ou=test,dc=test,dc=example',
    attrs => [
        id => 'test',
        cn => [qw(cn1 cn2)],
    ],
);

sub fetch_entry {
    my $mesg;
    ok( $mesg = $ldap->search(    # perform a search
            base  => "ou=test,dc=test,dc=example",
            scope => 'base',
            filter => "id=test"
        ),
        "LDAP search()"
    );

    $mesg->code && croak $mesg->error;
    return ($mesg->entries)[0];
}

# test deleting of attribute cns
my $entry = fetch_entry;
$entry->delete(cn => ['cn2']);
$entry->update($ldap);

$entry = fetch_entry;
is_deeply([ $entry->get_value('cn') ], ['cn1']);

# test adding of attribute cns
$entry->add(cn => ['cn2']);
$entry->update($ldap);

$entry = fetch_entry;
is_deeply([ $entry->get_value('cn') ], [qw(cn1 cn2)]);

# test replacing of attribute cns
$entry->replace(cn => [qw(cn3 cn4)]);
$entry->update($ldap);

$entry = fetch_entry;
is_deeply([ $entry->get_value('cn') ], [qw(cn3 cn4)]);

ok( $ldap->unbind, "LDAP unbind()" );