/
70sortctrl.t
108 lines (81 loc) · 2.56 KB
/
70sortctrl.t
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
#!perl
#
# For this test to run you must defined the following in test.cfg
# $EXTERNAL_TESTS = 1
# %sortctrl with the following entries
# host => name of ldap server
# base => the base for the search
# filter => the filter for the search
# order => the attribute name to order by
#
# The attribute given must have unique values over the entries
# returned from the search. This is because this test checks
# that the order of entries returned by 'attr' is the exact
# opposite of '-attr' this is not guaranteed if two entries have
# the same value for attr.
#
# Obviously the filter should be specific enough to ensure that
# a relatively small set of entries is returned
#
# TODO:
#
# This test should be expanded to test sort controls with
# more than one attribute specified.
use vars qw(%sortctrl);
BEGIN { require "t/common.pl" }
use Net::LDAP::LDIF;
use Net::LDAP::Control::Sort;
use Net::LDAP::Constant qw(
LDAP_CONTROL_SORTREQUEST
LDAP_CONTROL_SORTRESULT
LDAP_SUCCESS
);
unless ($EXTERNAL_TESTS) {
print "1..0 # Skip External tests disabled\n";
exit 0;
}
my($host, $base, $filter, $order) = @sortctrl{qw(host base filter order)};
my $ldap = $host && Net::LDAP->new($host, version => 3);
unless ($ldap) {
print "1..0 # Skip Cannot connect to host\n";
exit 0;
}
my $dse = $ldap && $ldap->root_dse;
unless ($dse and grep { $_ eq LDAP_CONTROL_SORTREQUEST } $dse->get_value('supportedControl')) {
print "1..0 # Skip server does not support LDAP_CONTROL_SORTREQUEST\n";
exit;
}
print "1..9\n";
Net::LDAP::LDIF->new(qw(- w))->write_entry($dse);
my $sort = Net::LDAP::Control::Sort->new(order => $order) or print "not ";
print "ok 1\n";
my $mesg = $ldap->search(
base => $base,
control => [$sort],
filter => $filter,
);
print "not " if $mesg->code;
print "ok 2\n";
my ($resp) = $mesg->control( LDAP_CONTROL_SORTRESULT ) or print "not ";
print "ok 3\n";
$resp && $resp->result == LDAP_SUCCESS or print "not ";
print "ok 4\n";
print "# ",$mesg->count,"\n";
my $dn1 = join ";", map { $_->dn } $mesg->entries;
$sort = Net::LDAP::Control::Sort->new(order => "-$order") or print "not ";
print "ok 5\n";
$mesg = $ldap->search(
base => $base,
control => [$sort],
filter => $filter,
);
print "not " if $mesg->code;
print "ok 6\n";
($resp) = $mesg->control( LDAP_CONTROL_SORTRESULT ) or print "not ";
print "ok 7\n";
$resp && $resp->result == LDAP_SUCCESS or print "not ";
print "ok 8\n";
print "# ",$mesg->count,"\n";
my $dn2 = join ";", map { $_->dn } reverse $mesg->entries;
print "not " unless $dn1 eq $dn2;
print "ok 9\n";