| 1 |
lars |
1 |
<?php
|
|
|
2 |
// Call Net_LDAP2_UtilTest::main() if this source file is executed directly.
|
|
|
3 |
if (!defined("PHPUnit_MAIN_METHOD")) {
|
|
|
4 |
define("PHPUnit_MAIN_METHOD", "Net_LDAP2_UtilTest::main");
|
|
|
5 |
}
|
|
|
6 |
|
|
|
7 |
require_once "PHPUnit/Framework/TestCase.php";
|
|
|
8 |
require_once "PHPUnit/Framework/TestSuite.php";
|
|
|
9 |
|
|
|
10 |
require_once 'Net/LDAP2/Util.php';
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* Test class for Net_LDAP2_Util.
|
|
|
14 |
* Generated by PHPUnit_Util_Skeleton on 2007-10-09 at 10:33:22.
|
|
|
15 |
*/
|
|
|
16 |
class Net_LDAP2_UtilTest extends PHPUnit_Framework_TestCase {
|
|
|
17 |
/**
|
|
|
18 |
* Runs the test methods of this class.
|
|
|
19 |
*
|
|
|
20 |
* @access public
|
|
|
21 |
* @static
|
|
|
22 |
*/
|
|
|
23 |
public static function main() {
|
|
|
24 |
require_once "PHPUnit/TextUI/TestRunner.php";
|
|
|
25 |
|
|
|
26 |
$suite = new PHPUnit_Framework_TestSuite("Net_LDAP2_UtilTest");
|
|
|
27 |
$result = PHPUnit_TextUI_TestRunner::run($suite);
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Sets up the fixture, for example, open a network connection.
|
|
|
32 |
* This method is called before a test is executed.
|
|
|
33 |
*
|
|
|
34 |
* @access protected
|
|
|
35 |
*/
|
|
|
36 |
protected function setUp() {
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Tears down the fixture, for example, close a network connection.
|
|
|
41 |
* This method is called after a test is executed.
|
|
|
42 |
*
|
|
|
43 |
* @access protected
|
|
|
44 |
*/
|
|
|
45 |
protected function tearDown() {
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Test escape_dn_value()
|
|
|
50 |
*/
|
|
|
51 |
public function testEscape_dn_value() {
|
|
|
52 |
$dnval = ' '.chr(22).' t,e+s"t,\\v<a>l;u#e=! ';
|
|
|
53 |
$expected = '\20\20\16 t\,e\+s\"t\,\\\\v\<a\>l\;u\#e\=!\20\20\20\20';
|
|
|
54 |
|
|
|
55 |
// string call
|
|
|
56 |
$this->assertEquals(array($expected), Net_LDAP2_Util::escape_dn_value($dnval));
|
|
|
57 |
|
|
|
58 |
// array call
|
|
|
59 |
$this->assertEquals(array($expected), Net_LDAP2_Util::escape_dn_value(array($dnval)));
|
|
|
60 |
|
|
|
61 |
// multiple arrays
|
|
|
62 |
$this->assertEquals(array($expected, $expected, $expected), Net_LDAP2_Util::escape_dn_value(array($dnval,$dnval,$dnval)));
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Test unescape_dn_value()
|
|
|
67 |
*/
|
|
|
68 |
public function testUnescape_dn_value() {
|
|
|
69 |
$dnval = '\\20\\20\\16\\20t\\,e\\+s \\"t\\,\\\\v\\<a\\>l\\;u\\#e\\=!\\20\\20\\20\\20';
|
|
|
70 |
$expected = ' '.chr(22).' t,e+s "t,\\v<a>l;u#e=! ';
|
|
|
71 |
|
|
|
72 |
// string call
|
|
|
73 |
$this->assertEquals(array($expected), Net_LDAP2_Util::unescape_dn_value($dnval));
|
|
|
74 |
|
|
|
75 |
// array call
|
|
|
76 |
$this->assertEquals(array($expected), Net_LDAP2_Util::unescape_dn_value(array($dnval)));
|
|
|
77 |
|
|
|
78 |
// multiple arrays
|
|
|
79 |
$this->assertEquals(array($expected, $expected, $expected), Net_LDAP2_Util::unescape_dn_value(array($dnval,$dnval,$dnval)));
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Test escaping of filter values
|
|
|
84 |
*/
|
|
|
85 |
public function testEscape_filter_value() {
|
|
|
86 |
$expected = 't\28e,s\29t\2av\5cal\1eue';
|
|
|
87 |
$filterval = 't(e,s)t*v\\al'.chr(30).'ue';
|
|
|
88 |
|
|
|
89 |
// string call
|
|
|
90 |
$this->assertEquals(array($expected), Net_LDAP2_Util::escape_filter_value($filterval));
|
|
|
91 |
|
|
|
92 |
// array call
|
|
|
93 |
$this->assertEquals(array($expected), Net_LDAP2_Util::escape_filter_value(array($filterval)));
|
|
|
94 |
|
|
|
95 |
// multiple arrays
|
|
|
96 |
$this->assertEquals(array($expected, $expected, $expected), Net_LDAP2_Util::escape_filter_value(array($filterval,$filterval,$filterval)));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Test unescaping of filter values
|
|
|
101 |
*/
|
|
|
102 |
public function testUnescape_filter_value() {
|
|
|
103 |
$expected = 't(e,s)t*v\\al'.chr(30).'ue';
|
|
|
104 |
$filterval = 't\28e,s\29t\2av\5cal\1eue';
|
|
|
105 |
|
|
|
106 |
// string call
|
|
|
107 |
$this->assertEquals(array($expected), Net_LDAP2_Util::unescape_filter_value($filterval));
|
|
|
108 |
|
|
|
109 |
// array call
|
|
|
110 |
$this->assertEquals(array($expected), Net_LDAP2_Util::unescape_filter_value(array($filterval)));
|
|
|
111 |
|
|
|
112 |
// multiple arrays
|
|
|
113 |
$this->assertEquals(array($expected, $expected, $expected), Net_LDAP2_Util::unescape_filter_value(array($filterval,$filterval,$filterval)));
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Test asc2hex32()
|
|
|
118 |
*/
|
|
|
119 |
public function testAsc2hex32() {
|
|
|
120 |
|
|
|
121 |
$expected = '\00\01\02\03\04\05\06\07\08\09\0a\0b\0c\0d\0e\0f\10\11\12\13\14\15\16\17\18\19\1a\1b\1c\1d\1e\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
|
|
|
122 |
$str = '';
|
|
|
123 |
for ($i = 0; $i < 127; $i++) {
|
|
|
124 |
$str .= chr($i);
|
|
|
125 |
}
|
|
|
126 |
$this->assertEquals($expected, Net_LDAP2_Util::asc2hex32($str));
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Test HEX unescaping
|
|
|
131 |
*/
|
|
|
132 |
public function testHex2asc() {
|
|
|
133 |
$expected = '';
|
|
|
134 |
for ($i = 0; $i < 127; $i++) {
|
|
|
135 |
$expected .= chr($i);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
$str = '\00\01\02\03\04\05\06\07\08\09\0a\0b\0c\0d\0e\0f\10\11\12\13\14\15\16\17\18\19\1a\1b\1c\1d\1e\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
|
|
|
139 |
$this->assertEquals($expected, Net_LDAP2_Util::hex2asc($str));
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Tests split_rdn_multival()
|
|
|
144 |
*
|
|
|
145 |
* In addition to the above test of the basic split correction,
|
|
|
146 |
* we test here the functionality of mutlivalued RDNs
|
|
|
147 |
*/
|
|
|
148 |
public function testSplit_rdn_multival() {
|
|
|
149 |
// One value
|
|
|
150 |
$rdn = 'CN=J. Smith';
|
|
|
151 |
$expected = array('CN=J. Smith');
|
|
|
152 |
$split = Net_LDAP2_Util::split_rdn_multival($rdn);
|
|
|
153 |
$this->assertEquals($expected, $split);
|
|
|
154 |
|
|
|
155 |
// Two values
|
|
|
156 |
$rdn = 'OU=Sales+CN=J. Smith';
|
|
|
157 |
$expected = array('OU=Sales', 'CN=J. Smith');
|
|
|
158 |
$split = Net_LDAP2_Util::split_rdn_multival($rdn);
|
|
|
159 |
$this->assertEquals($expected, $split);
|
|
|
160 |
|
|
|
161 |
// Several multivals
|
|
|
162 |
$rdn = 'OU=Sales+CN=J. Smith+L=London+C=England';
|
|
|
163 |
$expected = array('OU=Sales', 'CN=J. Smith', 'L=London', 'C=England');
|
|
|
164 |
$split = Net_LDAP2_Util::split_rdn_multival($rdn);
|
|
|
165 |
$this->assertEquals($expected, $split);
|
|
|
166 |
|
|
|
167 |
// Unescaped "+" in value
|
|
|
168 |
$rdn = 'OU=Sa+les+CN=J. Smith';
|
|
|
169 |
$expected = array('OU=Sa+les', 'CN=J. Smith');
|
|
|
170 |
$split = Net_LDAP2_Util::split_rdn_multival($rdn);
|
|
|
171 |
$this->assertEquals($expected, $split);
|
|
|
172 |
|
|
|
173 |
// Unescaped "+" in attr name
|
|
|
174 |
$rdn = 'O+U=Sales+CN=J. Smith';
|
|
|
175 |
$expected = array('O+U=Sales', 'CN=J. Smith');
|
|
|
176 |
$split = Net_LDAP2_Util::split_rdn_multival($rdn);
|
|
|
177 |
$this->assertEquals($expected, $split);
|
|
|
178 |
|
|
|
179 |
// Unescaped "+" in attr name + value
|
|
|
180 |
$rdn = 'O+U=Sales+CN=J. Sm+ith';
|
|
|
181 |
$expected = array('O+U=Sales', 'CN=J. Sm+ith');
|
|
|
182 |
$split = Net_LDAP2_Util::split_rdn_multival($rdn);
|
|
|
183 |
$this->assertEquals($expected, $split);
|
|
|
184 |
|
|
|
185 |
// Unescaped "+" in attr name, but not first attr
|
|
|
186 |
// this documents a known bug. However, unfortunately we cant
|
|
|
187 |
// know wether the "C+" belongs to value "Sales" or attribute "C+N".
|
|
|
188 |
// To solve this, we must ask the schema which we do not right now.
|
|
|
189 |
// The problem is located in _correct_dn_splitting()
|
|
|
190 |
$rdn = 'OU=Sales+C+N=J. Smith';
|
|
|
191 |
$expected = array('OU=Sales+C', 'N=J. Smith'); // The "C+" is treaten as value of "OU"
|
|
|
192 |
$split = Net_LDAP2_Util::split_rdn_multival($rdn);
|
|
|
193 |
$this->assertEquals($expected, $split);
|
|
|
194 |
|
|
|
195 |
// Escaped "+" in attr name and value
|
|
|
196 |
$rdn = 'O\+U=Sales+CN=J. Sm\+ith';
|
|
|
197 |
$expected = array('O\+U=Sales', 'CN=J. Sm\+ith');
|
|
|
198 |
$split = Net_LDAP2_Util::split_rdn_multival($rdn);
|
|
|
199 |
$this->assertEquals($expected, $split);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Tests attribute splitting ('foo=bar' => array('foo', 'bar'))
|
|
|
204 |
*/
|
|
|
205 |
public function testSplit_attribute_string() {
|
|
|
206 |
$attr_str = "foo=bar";
|
|
|
207 |
|
|
|
208 |
// properly
|
|
|
209 |
$expected = array('foo', 'bar');
|
|
|
210 |
$split = Net_LDAP2_Util::split_attribute_string($attr_str);
|
|
|
211 |
$this->assertEquals($expected, $split);
|
|
|
212 |
|
|
|
213 |
// escaped "="
|
|
|
214 |
$attr_str = "fo\=o=b\=ar";
|
|
|
215 |
$expected = array('fo\=o', 'b\=ar');
|
|
|
216 |
$split = Net_LDAP2_Util::split_attribute_string($attr_str);
|
|
|
217 |
$this->assertEquals($expected, $split);
|
|
|
218 |
|
|
|
219 |
// escaped "=" and unescaped = later on
|
|
|
220 |
$attr_str = "fo\=o=b=ar";
|
|
|
221 |
$expected = array('fo\=o', 'b=ar');
|
|
|
222 |
$split = Net_LDAP2_Util::split_attribute_string($attr_str);
|
|
|
223 |
$this->assertEquals($expected, $split);
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Tests Ldap_explode_dn()
|
|
|
228 |
*/
|
|
|
229 |
public function testLdap_explode_dn() {
|
|
|
230 |
$dn = 'OU=Sales+CN=J. Smith,dc=example,dc=net';
|
|
|
231 |
$expected_casefold_none = array(
|
|
|
232 |
array('CN=J. Smith', 'OU=Sales'),
|
|
|
233 |
'dc=example',
|
|
|
234 |
'dc=net'
|
|
|
235 |
);
|
|
|
236 |
$expected_casefold_upper = array(
|
|
|
237 |
array('CN=J. Smith', 'OU=Sales'),
|
|
|
238 |
'DC=example',
|
|
|
239 |
'DC=net'
|
|
|
240 |
);
|
|
|
241 |
$expected_casefold_lower = array(
|
|
|
242 |
array('cn=J. Smith', 'ou=Sales'),
|
|
|
243 |
'dc=example',
|
|
|
244 |
'dc=net'
|
|
|
245 |
);
|
|
|
246 |
|
|
|
247 |
$expected_onlyvalues = array(
|
|
|
248 |
array( 'J. Smith', 'Sales'),
|
|
|
249 |
'example',
|
|
|
250 |
'net'
|
|
|
251 |
);
|
|
|
252 |
$expected_reverse = array_reverse($expected_casefold_upper);
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
$dn_exploded_cnone = Net_LDAP2_Util::ldap_explode_dn($dn, array('casefold' => 'none'));
|
|
|
256 |
$this->assertEquals($expected_casefold_none, $dn_exploded_cnone, 'Option casefold none failed');
|
|
|
257 |
|
|
|
258 |
$dn_exploded_cupper = Net_LDAP2_Util::ldap_explode_dn($dn, array('casefold' => 'upper'));
|
|
|
259 |
$this->assertEquals($expected_casefold_upper, $dn_exploded_cupper, 'Option casefold upper failed');
|
|
|
260 |
|
|
|
261 |
$dn_exploded_clower = Net_LDAP2_Util::ldap_explode_dn($dn, array('casefold' => 'lower'));
|
|
|
262 |
$this->assertEquals($expected_casefold_lower, $dn_exploded_clower, 'Option casefold lower failed');
|
|
|
263 |
|
|
|
264 |
$dn_exploded_onlyval = Net_LDAP2_Util::ldap_explode_dn($dn, array('onlyvalues' => true));
|
|
|
265 |
$this->assertEquals($expected_onlyvalues, $dn_exploded_onlyval, 'Option onlyval failed');
|
|
|
266 |
|
|
|
267 |
$dn_exploded_reverse = Net_LDAP2_Util::ldap_explode_dn($dn, array('reverse' => true));
|
|
|
268 |
$this->assertEquals($expected_reverse, $dn_exploded_reverse, 'Option reverse failed');
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Tests if canonical_dn() works
|
|
|
273 |
*
|
|
|
274 |
* Note: This tests depend on the default options of canonical_dn().
|
|
|
275 |
*/
|
|
|
276 |
public function testCanonical_dn() {
|
|
|
277 |
// test empty dn (is valid according to rfc)
|
|
|
278 |
$this->assertEquals('', Net_LDAP2_Util::canonical_dn(''));
|
|
|
279 |
|
|
|
280 |
// default options with common dn
|
|
|
281 |
$testdn = 'cn=beni,DC=php,c=net';
|
|
|
282 |
$expected = 'CN=beni,DC=php,C=net';
|
|
|
283 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($testdn));
|
|
|
284 |
|
|
|
285 |
// casefold tests with common dn
|
|
|
286 |
$expected_up = 'CN=beni,DC=php,C=net';
|
|
|
287 |
$expected_lo = 'cn=beni,dc=php,c=net';
|
|
|
288 |
$expected_no = 'cn=beni,DC=php,c=net';
|
|
|
289 |
$this->assertEquals($expected_up, Net_LDAP2_Util::canonical_dn($testdn, array('casefold' => 'upper')));
|
|
|
290 |
$this->assertEquals($expected_lo, Net_LDAP2_Util::canonical_dn($testdn, array('casefold' => 'lower')));
|
|
|
291 |
$this->assertEquals($expected_no, Net_LDAP2_Util::canonical_dn($testdn, array('casefold' => 'none')));
|
|
|
292 |
|
|
|
293 |
// reverse
|
|
|
294 |
$expected_rev = 'C=net,DC=php,CN=beni';
|
|
|
295 |
$this->assertEquals($expected_rev, Net_LDAP2_Util::canonical_dn($testdn, array('reverse' => true)), 'Option reverse failed');
|
|
|
296 |
|
|
|
297 |
// DN as arrays
|
|
|
298 |
$dn_index = array('cn=beni', 'dc=php', 'c=net');
|
|
|
299 |
$dn_assoc = array('cn' => 'beni', 'dc' => 'php', 'c' => 'net');
|
|
|
300 |
$expected = 'CN=beni,DC=php,C=net';
|
|
|
301 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($dn_index));
|
|
|
302 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($dn_assoc));
|
|
|
303 |
|
|
|
304 |
// DN with multiple rdn value
|
|
|
305 |
$testdn = 'ou=dev+cn=beni,DC=php,c=net';
|
|
|
306 |
$testdn_index = array(array('ou=dev','cn=beni'),'DC=php','c=net');
|
|
|
307 |
$testdn_assoc = array(array('ou' => 'dev','cn' => 'beni'),'DC' => 'php','c' => 'net');
|
|
|
308 |
$expected = 'CN=beni+OU=dev,DC=php,C=net';
|
|
|
309 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($testdn));
|
|
|
310 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($testdn_assoc));
|
|
|
311 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($expected));
|
|
|
312 |
|
|
|
313 |
// test DN with OID
|
|
|
314 |
$testdn = 'OID.2.5.4.3=beni,dc=php,c=net';
|
|
|
315 |
$expected = '2.5.4.3=beni,DC=php,C=net';
|
|
|
316 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($testdn));
|
|
|
317 |
|
|
|
318 |
// test with leading and ending spaces
|
|
|
319 |
$testdn = 'cn= beni ,DC=php,c=net';
|
|
|
320 |
$expected = 'CN=\20\20beni\20\20,DC=php,C=net';
|
|
|
321 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($testdn));
|
|
|
322 |
|
|
|
323 |
// test with to-be escaped characters in attr value
|
|
|
324 |
$specialchars = array(
|
|
|
325 |
',' => '\,',
|
|
|
326 |
'+' => '\+',
|
|
|
327 |
'"' => '\"',
|
|
|
328 |
'\\' => '\\\\',
|
|
|
329 |
'<' => '\<',
|
|
|
330 |
'>' => '\>',
|
|
|
331 |
';' => '\;',
|
|
|
332 |
'#' => '\#',
|
|
|
333 |
'=' => '\=',
|
|
|
334 |
chr(18) => '\12',
|
|
|
335 |
'/' => '\/'
|
|
|
336 |
);
|
|
|
337 |
foreach ($specialchars as $char => $escape) {
|
|
|
338 |
$test_string = 'CN=be'.$char.'ni,DC=ph'.$char.'p,C=net';
|
|
|
339 |
$test_index = array('CN=be'.$char.'ni', 'DC=ph'.$char.'p', 'C=net');
|
|
|
340 |
$test_assoc = array('CN' => 'be'.$char.'ni', 'DC' => 'ph'.$char.'p', 'C' => 'net');
|
|
|
341 |
$expected = 'CN=be'.$escape.'ni,DC=ph'.$escape.'p,C=net';
|
|
|
342 |
|
|
|
343 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($test_string), 'String escaping test ('.$char.') failed');
|
|
|
344 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($test_index), 'Indexed array escaping test ('.$char.') failed');
|
|
|
345 |
$this->assertEquals($expected, Net_LDAP2_Util::canonical_dn($test_assoc), 'Associative array encoding test ('.$char.') failed');
|
|
|
346 |
}
|
|
|
347 |
}
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
// Call Net_LDAP2_UtilTest::main() if this source file is executed directly.
|
|
|
351 |
if (PHPUnit_MAIN_METHOD == "Net_LDAP2_UtilTest::main") {
|
|
|
352 |
Net_LDAP2_UtilTest::main();
|
|
|
353 |
}
|
|
|
354 |
?>
|