Permalink
Browse files

Add a test for sort control

  • Loading branch information...
1 parent 58b0c38 commit b3b8f0c4dac6e9761ae2870d67bac0578ec63d9e @gbarr gbarr committed Jun 14, 2001
Showing with 104 additions and 0 deletions.
  1. +95 −0 t/70sortctrl.t
  2. +9 −0 test.cfg
View
@@ -0,0 +1,95 @@
+#!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.
+
+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
+);
+
+my($host, $base, $filter, $order) = @sortctrl{qw(host base filter order)};
+
+my $ldap = $EXTERNAL_TESTS && $host && Net::LDAP->new($host, version => 3);
+my $dse = $ldap && $ldap->root_dse;
+
+unless ($dse and grep { $_ eq LDAP_CONTROL_SORTREQUEST } $dse->get_value('supportedControl')) {
+ print "1..0\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";
+
View
@@ -11,3 +11,12 @@ $SERVER_EXE = "<path to ldap server executable>";
$SERVER_TYPE = "openldap2";
# $HOST = "localhost";
+
+$EXTERNAL_TESTS = 0;
+
+# %sortctrl = (
+# host => '<ldap server hostname>',
+# base => '<search base>',
+# filter => '<search filter>',
+# order => '<sort attribute name>',
+# );

0 comments on commit b3b8f0c

Please sign in to comment.