1#!perl
2
3use Test::More;
4
5use Net::LDAP;
6use Net::LDAP::Constant qw(LDAP_CONTROL_ASSERTION);
7use Net::LDAP::Control::Assertion;
8
9BEGIN { require "t/common.pl" }
10
11
12my @tests;
13
14{ # parse DATA into a list (= tests) of hashes (= test parameters) of lists (= parameter values)
15  local $/ = '';
16  while(my $para = <DATA> ) {
17    my @lines = split(/\n/, $para);
18    my %params;
19    chomp(@lines);
20    @lines = grep(!/^\s*(?:#.*?)?$/, @lines);
21    map { push(@{$params{$1}}, $2) if (/^(\w+):\s*(.*)$/) } @lines;
22    push(@tests, \%params)  if (%params);
23  }
24}
25
26start_server()
27? plan tests => 4 + 2 * scalar(@tests)
28: plan skip_all => 'no server';
29
30
31$ldap = client();
32isa_ok($ldap, Net::LDAP, "client");
33
34$rootdse = $ldap->root_dse;
35isa_ok($rootdse, Net::LDAP::RootDSE, "root_dse");
36
37
38SKIP: {
39  skip("RootDSE does not offer Assertion control", 2 + 2 * scalar(@tests))
40    unless($rootdse->supported_control(LDAP_CONTROL_ASSERTION));
41
42  #$mesg = $ldap->start_tls;
43  #ok(!$mesg->code, "start_tls: " . $mesg->code . ": " . $mesg->error);
44
45  $mesg = $ldap->bind($MANAGERDN, password => $PASSWD);
46  ok(!$mesg->code, "bind: " . $mesg->code . ": " . $mesg->error);
47
48  ok(ldif_populate($ldap, "data/40-in.ldif"), "data/40-in.ldif");
49
50  foreach my $test (@tests) {
51    $control = Net::LDAP::Control::Assertion->new(assertion => $test->{assertion}->[0]);
52    isa_ok($control, Net::LDAP::Control::Assertion, "control object");
53
54    if ($test->{action}->[0] eq 'search') {
55      $mesg = $ldap->search(base => $test->{dn}->[0],
56		filter => $test->{filter} ? $test->{filter}->[0] : '(objectclass=*)',
57		scope => $test->{scope} ? $test->{scope}->[0] : 'sub',
58		control => $control);
59      is($mesg->code, $test->{code}->[0] || 0,
60		($test->{code}->[0] ? "search [expecting ".$test->{code}->[0]."]: " : "search: ") .
61		$mesg->code . ": " . $mesg->error);
62    }
63    elsif ($test->{action}->[0] eq 'compare') {
64      $mesg = $ldap->compare($test->{dn}->[0],
65		attr => $test->{attr}->[0],
66		value => $test->{value}->[0],
67		control => $control);
68      is($mesg->code, $test->{code}->[0] || 6,
69		($test->{code}->[0] ? "compare [expecting ".$test->{code}->[0]."]: " : "search: ") .
70		$mesg->code . ": " . $mesg->error);
71    }
72    elsif ($test->{action}->[0] eq 'modify') {
73      $mesg = $ldap->modify($test->{dn}->[0],
74		$test->{changetype}->[0] => {
75			map { $_ => $test->{$_} } @{$test->{attrs}}
76		},
77		control => $control);
78      is($mesg->code, $test->{code}->[0] || 0,
79		($test->{code}->[0] ? "modify [expecting ".$test->{code}->[0]."]: " : "modify: ") .
80		$mesg->code . ": " . $mesg->error);
81    }
82    elsif ($test->{action}->[0] eq 'moddn') {
83      my %sup = $test->{newsuperior} ? ( newsuperior => $test->{newsuperior}->[0] ) : ();
84      $mesg = $ldap->moddn($test->{dn}->[0],
85		newrdn => $test->{newrdn}->[0],
86		%sup,
87		control => $control);
88      is($mesg->code, $test->{code}->[0] || 0,
89		($test->{code}->[0] ? "moddn [expecting ".$test->{code}->[0]."]: " : "moddn: ") .
90		$mesg->code . ": " . $mesg->error);
91    }
92    elsif ($test->{action}->[0] eq 'delete') {
93      $mesg = $ldap->delete($test->{dn}->[0],
94		control => $control);
95      is($mesg->code, $test->{code}->[0] || 0,
96		($test->{code}->[0] ? "delete [expecting ".$test->{code}->[0]."]: " : "delete: ") .
97		$mesg->code . ": " . $mesg->error);
98    }
99    else {
100      ok(0, "illegal action");
101      note("test: ", explain($test));
102    }
103  }
104}
105
106__DATA__
107
108## each section below represents one test; logic similar to , structure similar to LDIF
109# each tests needs at least the elements
110# - assertion: the assertion filter to use
111# - action:    the action on which the assertion is to be tested
112# - dn:        (base-)DN to use in the operation
113# - ...:       all other elements depend on the operation [see above]
114
115# search (expect failing assertion)
116action: search
117dn: ou=People, o=University of Michigan, c=US
118filter: (cn=Babs Jensen)
119assertion: (title=Mythical Manager, Research Systems)
120code: 122
121
122# search
123action: search
124dn: cn=Barbara Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
125filter: (cn=Babs Jensen)
126assertion: (title=Mythical Manager, Research Systems)
127
128# modify
129action: modify
130dn: cn=Barbara Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
131assertion: (title=MegaMythical Manager, Research Systems)
132changetype: replace
133attrs: title
134title: Uber-Mythical Manager, Research Systems
135title: Hyper-Mythical Manager, Research Systems
136code: 122
137
138# modify
139action: modify
140dn: cn=Barbara Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
141assertion: (title=Mythical Manager, Research Systems)
142changetype: replace
143attrs: title
144title: Uber-Mythical Manager, Research Systems
145title: Hyper-Mythical Manager, Research Systems
146
147# compare (expect failing assertion)
148action: compare
149dn: cn=Barbara Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
150filter: (cn=Babs Jensen)
151assertion: (title=HyperMythical Manager, Research Systems)
152attr: sn
153value: Jensen
154code: 122
155
156# compare
157action: compare
158dn: cn=Barbara Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
159filter: (cn=Babs Jensen)
160assertion: (title=Hyper-Mythical Manager, Research Systems)
161attr: sn
162value: Jensen
163
164# moddn
165action: moddn
166dn: cn=Barbara Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
167newrdn: cn=Babs Jensen
168assertion: (title=HyperMythical Manager, Research Systems)
169code: 122
170
171# moddn
172action: moddn
173dn: cn=Barbara Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
174newrdn: cn=Babs Jensen
175assertion: (title=Hyper-Mythical Manager, Research Systems)
176
177# delete
178action: delete
179dn: cn=Babs Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
180assertion: (title=HyperMythical Manager, Research Systems)
181code: 122
182
183# delete
184action: delete
185dn: cn=Babs Jensen, ou=Information Technology Division, ou=People, o=University of Michigan, c=US
186assertion: (title=Hyper-Mythical Manager, Research Systems)
187
188