r25781: Handle and test linked attribute renames.
[samba.git] / testprogs / ejs / ldap.js
1 #!/bin/sh
2 exec smbscript "$0" ${1+"$@"}
3 /*
4         test certin LDAP behaviours
5 */
6
7 var ldb = ldb_init();
8 var gc_ldb = ldb_init();
9
10 var options = GetOptions(ARGV, 
11                 "POPT_AUTOHELP",
12                 "POPT_COMMON_SAMBA",
13                 "POPT_COMMON_CREDENTIALS");
14 if (options == undefined) {
15    println("Failed to parse options");
16    return -1;
17 }
18
19 libinclude("base.js");
20
21 if (options.ARGV.length != 1) {
22    println("Usage: ldap.js <HOST>");
23    return -1;
24 }
25
26 var host = options.ARGV[0];
27
28 function basic_tests(ldb, gc_ldb, base_dn, configuration_dn, schema_dn)
29 {
30         println("Running basic tests");
31
32         ldb.del("cn=ldaptestuser,cn=users," + base_dn);
33
34         var ok = ldb.add("
35 dn: cn=ldaptestuser,cn=uSers," + base_dn + "
36 objectclass: user
37 objectclass: person
38 cN: LDAPtestUSER
39 ");
40         if (ok.error != 0) {
41                 ok = ldb.del("cn=ldaptestuser,cn=users," + base_dn);
42                 if (ok.error != 0) {
43                         println(ok.errstr);
44                         assert(ok.error == 0);
45                 }
46                 ok = ldb.add("
47 dn: cn=ldaptestuser,cn=uSers," + base_dn + "
48 objectclass: user
49 objectclass: person
50 cN: LDAPtestUSER
51 ");
52                 if (ok.error != 0) {
53                         println(ok.errstr);
54                         assert(ok.error == 0);
55                 }
56         }
57
58         ldb.del("cn=ldaptestgroup,cn=users," + base_dn);
59
60         var ok = ldb.add("
61 dn: cn=ldaptestgroup,cn=uSers," + base_dn + "
62 objectclass: group
63 member: cn=ldaptestuser,cn=useRs," + base_dn + "
64 ");
65         if (ok.error != 0) {
66                 ok = ldb.del("cn=ldaptestgroup,cn=users," + base_dn);
67                 if (ok.error != 0) {
68                         println(ok.errstr);
69                         assert(ok.error == 0);
70                 }
71                 ok = ldb.add("
72 dn: cn=ldaptestgroup,cn=uSers," + base_dn + "
73 objectclass: group
74 member: cn=ldaptestuser,cn=useRs," + base_dn + "
75 ");
76                 if (ok.error != 0) {
77                         println(ok.errstr);
78                         assert(ok.error == 0);
79                 }
80         }
81
82         var ok = ldb.add("
83 dn: cn=ldaptestcomputer,cn=computers," + base_dn + "
84 objectclass: computer
85 cN: LDAPtestCOMPUTER
86 ");
87         if (ok.error != 0) {
88                 ok = ldb.del("cn=ldaptestcomputer,cn=computers," + base_dn);
89                 if (ok.error != 0) {
90                         println(ok.errstr);
91                         assert(ok.error == 0);
92                 }
93                 ok = ldb.add("
94 dn: cn=ldaptestcomputer,cn=computers," + base_dn + "
95 objectClass: computer
96 cn: LDAPtestCOMPUTER
97 ");
98                 if (ok.error != 0) {
99                         println(ok.errstr);
100                         assert(ok.error == 0);
101                 }
102         }
103
104         if (ok.error != 0) {
105                 println(ok.errstr);
106                 assert(ok.error == 0);
107         }
108
109         var ok = ldb.add("
110 dn: cn=ldaptest2computer,cn=computers," + base_dn + "
111 objectClass: computer
112 cn: LDAPtest2COMPUTER
113 userAccountControl: 4096
114 ");
115         if (ok.error != 0) {
116                 ok = ldb.del("cn=ldaptest2computer,cn=computers," + base_dn);
117                 if (ok.error != 0) {
118                         println(ok.errstr);
119                         assert(ok.error == 0);
120                 }
121                 ok = ldb.add("
122 dn: cn=ldaptest2computer,cn=computers," + base_dn + "
123 objectClass: computer
124 cn: LDAPtest2COMPUTER
125 userAccountControl: 4096
126 ");
127                 if (ok.error != 0) {
128                         println(ok.errstr);
129                         assert(ok.error == 0);
130                 }
131         }
132
133             ok = ldb.modify("
134 dn: cn=ldaptest2computer,cn=computers," + base_dn + "
135 changetype: modify
136 replace: servicePrincipalName
137 servicePrincipalName: host/ldaptest2computer
138 servicePrincipalName: host/ldaptest2computer
139 servicePrincipalName: cifs/ldaptest2computer
140 ");
141
142 //LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
143                 if (ok.error != 20) {
144                         println("Expected error LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS, got :" + ok.errstr);
145                         assert(ok.error == 20);
146                 }
147
148         ok = ldb.add("
149 dn: cn=ldaptestuser2,cn=useRs," + base_dn + "
150 objectClass: person
151 objectClass: user
152 cn: LDAPtestUSER2
153 ");
154         if (ok.error != 0) {
155                 ok = ldb.del("cn=ldaptestuser2,cn=users," + base_dn);
156                 if (ok.error != 0) {
157                         println(ok.errstr);
158                         assert(ok.error == 0);
159                 }
160                 ok = ldb.add("
161 dn: cn=ldaptestuser2,cn=useRs," + base_dn + "
162 objectClass: person
163 objectClass: user
164 cn: LDAPtestUSER2
165 ");
166                 if (ok.error != 0) {
167                         println(ok.errstr);
168                         assert(ok.error == 0);
169                 }
170         }
171
172         println("Testing Group Modifies");
173         ok = ldb.modify("
174 dn: cn=ldaptestgroup,cn=users," + base_dn + "
175 changetype: modify
176 add: member
177 member: cn=ldaptestuser2,cn=users," + base_dn + "
178 member: cn=ldaptestcomputer,cn=computers," + base_dn + "
179 ");
180
181         if (ok.error != 0) {
182                 println(ok.errstr);
183                 assert(ok.error == 0);
184         }
185
186         ok = ldb.del("cn=ldaptestuser3,cn=users," + base_dn);
187
188         println("Testing Renames");
189
190         ok = ldb.rename("cn=ldaptestuser2,cn=users," + base_dn, "cn=ldaptestuser3,cn=users," + base_dn);
191         if (ok.error != 0) {
192                 println("Could not rename cn=ldaptestuser2,cn=users," + base_dn + " into cn=ldaptestuser3,cn=users," + base_dn + ": " + ok.errstr);
193                 assert(ok.error == 0);
194         }
195
196         ok = ldb.rename("cn=ldaptestuser3,cn=users," + base_dn, "cn=ldaptestuser3,cn=users," + base_dn);
197         if (ok.error != 0) {
198                 println("Could not rename cn=ldaptestuser3,cn=users," + base_dn + " onto itself: " + ok.errstr);
199                 assert(ok.error == 0);
200         }
201
202         ok = ldb.rename("cn=ldaptestuser3,cn=users," + base_dn, "cn=ldaptestUSER3,cn=users," + base_dn);
203         if (ok.error != 0) {
204                 println("Could not rename cn=ldaptestuser3,cn=users," + base_dn + " into cn=ldaptestUSER3,cn=users," + base_dn + ": " + ok.errstr);
205                 assert(ok.error == 0);
206         }
207
208         println("Testing ldb.search for (&(cn=ldaptestuser3)(objectClass=user))");
209         var res = ldb.search("(&(cn=ldaptestuser3)(objectClass=user))");
210         if (res.error != 0 || res.msgs.length != 1) {
211                 println("Could not find (&(cn=ldaptestuser3)(objectClass=user))");
212                 assert(res.error == 0);
213                 assert(res.msgs.length == 1);
214         }
215
216         assert(res.msgs[0].dn == ("CN=ldaptestUSER3,CN=Users," + base_dn));
217         assert(res.msgs[0].cn == "ldaptestUSER3");
218         assert(res.msgs[0].name == "ldaptestUSER3");
219
220         // ensure we cannot add it again
221         ok = ldb.add("
222 dn: cn=ldaptestuser3,cn=userS," + base_dn + "
223 objectClass: person
224 objectClass: user
225 cn: LDAPtestUSER3
226 ");
227 //LDB_ERR_ENTRY_ALREADY_EXISTS
228         if (ok.error != 68) {
229                 println("expected error LDB_ERR_ENTRY_ALREADY_EXISTS, got: " + ok.errstr);
230                 assert(ok.error == 68);
231         }
232
233         // rename back
234         ok = ldb.rename("cn=ldaptestuser3,cn=users," + base_dn, "cn=ldaptestuser2,cn=users," + base_dn);
235         if (ok.error != 0) {
236                 println(ok.errstr);
237                 assert(ok.error == 0);
238         }
239
240         // ensure we cannnot rename it twice
241         ok = ldb.rename("cn=ldaptestuser3,cn=users," + base_dn, "cn=ldaptestuser2,cn=users," + base_dn);
242 //LDB_ERR_NO_SUCH_OBJECT
243         assert(ok.error == 32);
244
245         // ensure can now use that name
246         ok = ldb.add("
247 dn: cn=ldaptestuser3,cn=users," + base_dn + "
248 objectClass: person
249 objectClass: user
250 cn: LDAPtestUSER3
251 ");
252         
253         // ensure we now cannnot rename
254         ok = ldb.rename("cn=ldaptestuser2,cn=users," + base_dn, "cn=ldaptestuser3,cn=users," + base_dn);
255 //LDB_ERR_ENTRY_ALREADY_EXISTS
256         if (ok.error != 68) {
257                 println("expected error LDB_ERR_ENTRY_ALREADY_EXISTS, got: " + ok.errstr);
258                 assert(ok.error == 68);
259         }
260         assert(ok.error == 68);
261         ok = ldb.rename("cn=ldaptestuser3,cn=users," + base_dn, "cn=ldaptestuser3,cn=configuration," + base_dn);
262         if (ok.error != 71 && ok.error != 64) {
263                 println("expected error LDB_ERR_ENTRY_ALREADY_EXISTS or LDAP_NAMING_VIOLATION, got: " + ok.errstr);
264                 assert(ok.error == 71 || ok.error == 64);
265         }
266         assert(ok.error == 71 || ok.error == 64);
267
268         ok = ldb.rename("cn=ldaptestuser3,cn=users," + base_dn, "cn=ldaptestuser5,cn=users," + base_dn);
269         if (ok.error != 0) {
270                 println(ok.errstr);
271                 assert(ok.error == 0);
272         }
273
274         ok = ldb.del("cn=ldaptestuser5,cn=users," + base_dn);
275
276         ok = ldb.del("cn=ldaptestgroup2,cn=users," + base_dn);
277
278         ok = ldb.rename("cn=ldaptestgroup,cn=users," + base_dn, "cn=ldaptestgroup2,cn=users," + base_dn);
279         if (ok.error != 0) {
280                 println(ok.errstr);
281                 assert(ok.error == 0);
282         }
283
284         println("Testing subtree Renames");
285
286         ok = ldb.add("
287 dn: cn=ldaptestcontainer," + base_dn + "
288 objectClass: container
289 ");
290         
291         ok = ldb.add("
292 dn: CN=ldaptestuser4,CN=ldaptestcontainer," + base_dn + "
293 objectClass: person
294 objectClass: user
295 cn: LDAPtestUSER4
296 ");
297         if (ok.error != 0) {
298                 ok = ldb.del("cn=ldaptestuser4,cn=ldaptestcontainer," + base_dn);
299                 if (ok.error != 0) {
300                         println(ok.errstr);
301                         assert(ok.error == 0);
302                 }
303                 ok = ldb.add("
304 dn: CN=ldaptestuser4,CN=ldaptestcontainer," + base_dn + "
305 objectClass: person
306 objectClass: user
307 cn: LDAPtestUSER4
308 ");
309                 if (ok.error != 0) {
310                         println(ok.errstr);
311                         assert(ok.error == 0);
312                 }
313         }
314
315         println("Testing ldb.rename of cn=ldaptestcontainer," + base_dn + " to cn=ldaptestcontainer2," + base_dn);
316         ok = ldb.rename("CN=ldaptestcontainer," + base_dn, "CN=ldaptestcontainer2," + base_dn);
317         if (ok.error != 0) {
318                 println(ok.errstr);
319                 assert(ok.error == 0);
320         }
321
322         println("Testing ldb.search for (&(cn=ldaptestuser4)(objectClass=user))");
323         var res = ldb.search("(&(cn=ldaptestuser4)(objectClass=user))");
324         if (res.error != 0 || res.msgs.length != 1) {
325                 println("Could not find (&(cn=ldaptestuser4)(objectClass=user))");
326                 assert(res.error == 0);
327                 assert(res.msgs.length == 1);
328         }
329
330         assert(res.msgs[0].dn == ("CN=ldaptestuser4,CN=ldaptestcontainer2," + base_dn));
331
332         println("Testing ldb.search for (&(cn=ldaptestuser4)(objectClass=user)) in renamed container");
333         var res = ldb.search("(&(cn=ldaptestuser4)(objectClass=user))", "cn=ldaptestcontainer2," + base_dn, ldb.SCOPE_SUBTREE);
334         if (res.error != 0 || res.msgs.length != 1) {
335                 println("Could not find (&(cn=ldaptestuser4)(objectClass=user)) under cn=ldaptestcontainer2," + base_dn);
336                 assert(res.error == 0);
337                 assert(res.msgs.length == 1);
338         }
339
340         assert(res.msgs[0].dn == ("CN=ldaptestuser4,CN=ldaptestcontainer2," + base_dn));
341
342         println("Testing ldb.rename (into itself) of cn=ldaptestcontainer2," + base_dn + " to cn=ldaptestcontainer,cn=ldaptestcontainer2," + base_dn);
343         ok = ldb.rename("cn=ldaptestcontainer2," + base_dn, "cn=ldaptestcontainer,cn=ldaptestcontainer2," + base_dn);
344         if (ok.error != 53) { /* LDAP_UNWILLING_TO_PERFORM */
345                 println(ok.errstr);
346                 assert(ok.error == 53);
347         }
348
349         println("Testing ldb.rename (into non-existent container) of cn=ldaptestcontainer2," + base_dn + " to cn=ldaptestcontainer,cn=ldaptestcontainer3," + base_dn);
350         ok = ldb.rename("cn=ldaptestcontainer2," + base_dn, "cn=ldaptestcontainer,cn=ldaptestcontainer3," + base_dn);
351         if (ok.error != 53 && ok.error != 80) { /* LDAP_UNWILLING_TO_PERFORM or LDAP_OTHER*/
352                 println(ok.errstr);
353                 assert(ok.error == 53 || ok.error == 80);
354         }
355
356         println("Testing delete (should fail, not a leaf node) of renamed cn=ldaptestcontainer2," + base_dn);
357         ok = ldb.del("cn=ldaptestcontainer2," + base_dn);
358         if (ok.error != 66) { /* LDB_ERR_NOT_ALLOWED_ON_NON_LEAF */
359                 println(ok.errstr);
360                 assert(ok.error == 66);
361         }
362         println("Testing delete of subtree renamed "+res.msgs[0].dn);
363         ok = ldb.del(res.msgs[0].dn);
364         if (ok.error != 0) {
365                 println(ok.errstr);
366                 assert(ok.error == 0);
367         }
368         println("Testing delete of renamed cn=ldaptestcontainer2," + base_dn);
369         ok = ldb.del("cn=ldaptestcontainer2," + base_dn);
370         if (ok.error != 0) {
371                 println(ok.errstr);
372                 assert(ok.error == 0);
373         }
374         
375         ok = ldb.add("
376 dn: cn=ldaptestutf8user èùéìòà ,cn=users," + base_dn + "
377 objectClass: user
378 ");
379         if (ok.error != 0) {
380                 ok = ldb.del("cn=ldaptestutf8user èùéìòà ,cn=users," + base_dn);
381                 if (ok.error != 0) {
382                         println(ok.errstr);
383                         assert(ok.error == 0);
384                 }
385         ok = ldb.add("
386 dn: cn=ldaptestutf8user èùéìòà ,cn=users," + base_dn + "
387 objectClass: user
388 ");
389                 if (ok.error != 0) {
390                         println(ok.errstr);
391                         assert(ok.error == 0);
392                 }
393         }
394
395         ok = ldb.add("
396 dn: cn=ldaptestutf8user2  èùéìòà ,cn=users," + base_dn + "
397 objectClass: user
398 ");
399         if (ok.error != 0) {
400                 ok = ldb.del("cn=ldaptestutf8user2  èùéìòà ,cn=users," + base_dn);
401                 if (ok.error != 0) {
402                         println(ok.errstr);
403                         assert(ok.error == 0);
404                 }
405         ok = ldb.add("
406 dn: cn=ldaptestutf8user2  èùéìòà ,cn=users," + base_dn + "
407 objectClass: user
408 ");
409                 if (ok.error != 0) {
410                         println(ok.errstr);
411                         assert(ok.error == 0);
412                 }
413         }
414
415         println("Testing ldb.search for (&(cn=ldaptestuser)(objectClass=user))");
416         var res = ldb.search("(&(cn=ldaptestuser)(objectClass=user))");
417         if (res.error != 0 || res.msgs.length != 1) {
418                 println("Could not find (&(cn=ldaptestuser)(objectClass=user))");
419                 assert(res.error == 0);
420                 assert(res.msgs.length == 1);
421         }
422
423         assert(res.msgs[0].dn == ("CN=ldaptestuser,CN=Users," + base_dn));
424         assert(res.msgs[0].cn == "ldaptestuser");
425         assert(res.msgs[0].name == "ldaptestuser");
426         assert(res.msgs[0].objectClass[0] == "top");
427         assert(res.msgs[0].objectClass[1] == "person");
428         assert(res.msgs[0].objectClass[2] == "organizationalPerson");
429         assert(res.msgs[0].objectClass[3] == "user");
430         assert(res.msgs[0].objectGUID != undefined);
431         assert(res.msgs[0].whenCreated != undefined);
432         assert(res.msgs[0].objectCategory == ("CN=Person,CN=Schema,CN=Configuration," + base_dn));
433         assert(res.msgs[0].sAMAccountType == 805306368);
434 //      assert(res[0].userAccountControl == 546);
435  
436         println("Testing ldb.search for (&(cn=ldaptestuser)(objectCategory=cn=person,cn=schema,cn=configuration," + base_dn + "))");
437         var res2 = ldb.search("(&(cn=ldaptestuser)(objectCategory=cn=person,cn=schema,cn=configuration," + base_dn + "))");
438         if (res2.error != 0 || res2.msgs.length != 1) {
439                 println("Could not find (&(cn=ldaptestuser)(objectCategory=cn=person,cn=schema,cn=configuration," + base_dn + "))");
440                 assert(res2.error == 0);
441                 assert(res2.msgs.length == 1);
442         }
443
444         assert(res.msgs[0].dn == res2.msgs[0].dn);
445
446         println("Testing ldb.search for (&(cn=ldaptestuser)(objectCategory=PerSon))");
447         var res3 = ldb.search("(&(cn=ldaptestuser)(objectCategory=PerSon))");
448         if (res3.error != 0) {
449                 println("Could not find (&(cn=ldaptestuser)(objectCategory=PerSon)): " + res3.errstr);
450                 assert(res3.error == 0);
451         } else if (res3.msgs.length != 1) {
452                 println("Could not find (&(cn=ldaptestuser)(objectCategory=PerSon)): matched " + res3.msgs.length);
453                 assert(res3.msgs.length == 1);
454         }
455
456         assert(res.msgs[0].dn == res3.msgs[0].dn);
457
458         if (gc_ldb != undefined) {
459                 println("Testing ldb.search for (&(cn=ldaptestuser)(objectCategory=PerSon)) in Global Catalog");
460                 var res3gc = gc_ldb.search("(&(cn=ldaptestuser)(objectCategory=PerSon))");
461                 if (res3gc.error != 0) {
462                         println("Could not find (&(cn=ldaptestuser)(objectCategory=PerSon)) in Global Catalog: " + res3gc.errstr);
463                         assert(res3gc.error == 0);
464                 } else if (res3gc.msgs.length != 1) {
465                         println("Could not find (&(cn=ldaptestuser)(objectCategory=PerSon)) in Global Catalog: matched " + res3gc.msgs.length);
466                         assert(res3gc.msgs.length == 1);
467                 }
468         
469                 assert(res.msgs[0].dn == res3gc.msgs[0].dn);
470         }
471
472         println("Testing ldb.search for (&(cn=ldaptestuser)(objectCategory=PerSon)) in with 'phantom root' control");
473         var attrs = new Array("cn");
474         var controls = new Array("search_options:1:2");
475         var res3control = gc_ldb.search("(&(cn=ldaptestuser)(objectCategory=PerSon))", base_dn, ldb.SCOPE_SUBTREE, attrs, controls);
476         if (res3control.error != 0 || res3control.msgs.length != 1) {
477                 println("Could not find (&(cn=ldaptestuser)(objectCategory=PerSon)) in Global Catalog");
478                 assert(res3control.error == 0);
479                 assert(res3control.msgs.length == 1);
480         }
481         
482         assert(res.msgs[0].dn == res3control.msgs[0].dn);
483
484         ok = ldb.del(res.msgs[0].dn);
485         if (ok.error != 0) {
486                 println(ok.errstr);
487                 assert(ok.error == 0);
488         }
489
490         println("Testing ldb.search for (&(cn=ldaptestcomputer)(objectClass=user))");
491         var res = ldb.search("(&(cn=ldaptestcomputer)(objectClass=user))");
492         if (res.error != 0 || res.msgs.length != 1) {
493                 println("Could not find (&(cn=ldaptestuser)(objectClass=user))");
494                 assert(res.error == 0);
495                 assert(res.msgs.length == 1);
496         }
497
498         assert(res.msgs[0].dn == ("CN=ldaptestcomputer,CN=Computers," + base_dn));
499         assert(res.msgs[0].cn == "ldaptestcomputer");
500         assert(res.msgs[0].name == "ldaptestcomputer");
501         assert(res.msgs[0].objectClass[0] == "top");
502         assert(res.msgs[0].objectClass[1] == "person");
503         assert(res.msgs[0].objectClass[2] == "organizationalPerson");
504         assert(res.msgs[0].objectClass[3] == "user");
505         assert(res.msgs[0].objectClass[4] == "computer");
506         assert(res.msgs[0].objectGUID != undefined);
507         assert(res.msgs[0].whenCreated != undefined);
508         assert(res.msgs[0].objectCategory == ("CN=Computer,CN=Schema,CN=Configuration," + base_dn));
509         assert(res.msgs[0].primaryGroupID == 513);
510 //      assert(res.msgs[0].sAMAccountType == 805306368);
511 //      assert(res.msgs[0].userAccountControl == 546);
512
513         println("Testing ldb.search for (&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + base_dn + "))");
514         var res2 = ldb.search("(&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + base_dn + "))");
515         if (res2.error != 0 || res2.msgs.length != 1) {
516                 println("Could not find (&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + base_dn + "))");
517                 assert(res2.error == 0);
518                 assert(res2.msgs.length == 1);
519         }
520
521         assert(res.msgs[0].dn == res2.msgs[0].dn);
522
523         if (gc_ldb != undefined) {
524                 println("Testing ldb.search for (&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + base_dn + ")) in Global Catlog");
525                 var res2gc = gc_ldb.search("(&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + base_dn + "))");
526                 if (res2gc.error != 0 || res2gc.msgs.length != 1) {
527                         println("Could not find (&(cn=ldaptestcomputer)(objectCategory=cn=computer,cn=schema,cn=configuration," + base_dn + ")) in Global Catlog");
528                         assert(res2gc.error == 0);
529                         assert(res2gc.msgs.length == 1);
530                 }
531
532                 assert(res.msgs[0].dn == res2gc.msgs[0].dn);
533         }
534
535         println("Testing ldb.search for (&(cn=ldaptestcomputer)(objectCategory=compuTER))");
536         var res3 = ldb.search("(&(cn=ldaptestcomputer)(objectCategory=compuTER))");
537         if (res3.error != 0 || res3.msgs.length != 1) {
538                 println("Could not find (&(cn=ldaptestcomputer)(objectCategory=compuTER))");
539                 assert(res3.error == 0);
540                 assert(res3.msgs.length == 1);
541         }
542
543         assert(res.msgs[0].dn == res3.msgs[0].dn);
544
545         if (gc_ldb != undefined) {
546                 println("Testing ldb.search for (&(cn=ldaptestcomputer)(objectCategory=compuTER)) in Global Catalog");
547                 var res3gc = gc_ldb.search("(&(cn=ldaptestcomputer)(objectCategory=compuTER))");
548                 if (res3gc.error != 0 || res3gc.msgs.length != 1) {
549                         println("Could not find (&(cn=ldaptestcomputer)(objectCategory=compuTER)) in Global Catalog");
550                         assert(res3gc.error == 0);
551                         assert(res3gc.msgs.length == 1);
552                 }
553
554                 assert(res.msgs[0].dn == res3gc.msgs[0].dn);
555         }
556
557         println("Testing ldb.search for (&(cn=ldaptestcomp*r)(objectCategory=compuTER))");
558         var res4 = ldb.search("(&(cn=ldaptestcomp*r)(objectCategory=compuTER))");
559         if (res4.error != 0 || res4.msgs.length != 1) {
560                 println("Could not find (&(cn=ldaptestcomp*r)(objectCategory=compuTER))");
561                 assert(res4.error == 0);
562                 assert(res4.msgs.length == 1);
563         }
564
565         assert(res.msgs[0].dn == res4.msgs[0].dn);
566
567         println("Testing ldb.search for (&(cn=ldaptestcomput*)(objectCategory=compuTER))");
568         var res5 = ldb.search("(&(cn=ldaptestcomput*)(objectCategory=compuTER))");
569         if (res5.error != 0 || res5.msgs.length != 1) {
570                 println("Could not find (&(cn=ldaptestcomput*)(objectCategory=compuTER))");
571                 assert(res5.error == 0);
572                 assert(res5.msgs.length == 1);
573         }
574
575         assert(res.msgs[0].dn == res5.msgs[0].dn);
576
577         println("Testing ldb.search for (&(cn=*daptestcomputer)(objectCategory=compuTER))");
578         var res6 = ldb.search("(&(cn=*daptestcomputer)(objectCategory=compuTER))");
579         if (res6.error != 0 || res6.msgs.length != 1) {
580                 println("Could not find (&(cn=*daptestcomputer)(objectCategory=compuTER))");
581                 assert(res6.error == 0);
582                 assert(res6.msgs.length == 1);
583         }
584
585         assert(res.msgs[0].dn == res6.msgs[0].dn);
586
587         ok = ldb.del(res.msgs[0].dn);
588         if (ok.error != 0) {
589                 println(ok.errstr);
590                 assert(ok.error == 0);
591         }
592
593         println("Testing ldb.search for (&(cn=ldaptest2computer)(objectClass=user))");
594         var res = ldb.search("(&(cn=ldaptest2computer)(objectClass=user))");
595         if (res.error != 0 || res.msgs.length != 1) {
596                 println("Could not find (&(cn=ldaptest2computer)(objectClass=user))");
597                 assert(res.error == 0);
598                 assert(res.msgs.length == 1);
599         }
600
601         assert(res.msgs[0].dn == ("CN=ldaptest2computer,CN=Computers," + base_dn));
602         assert(res.msgs[0].cn == "ldaptest2computer");
603         assert(res.msgs[0].name == "ldaptest2computer");
604         assert(res.msgs[0].objectClass[0] == "top");
605         assert(res.msgs[0].objectClass[1] == "person");
606         assert(res.msgs[0].objectClass[2] == "organizationalPerson");
607         assert(res.msgs[0].objectClass[3] == "user");
608         assert(res.msgs[0].objectClass[4] == "computer");
609         assert(res.msgs[0].objectGUID != undefined);
610         assert(res.msgs[0].whenCreated != undefined);
611         assert(res.msgs[0].objectCategory == "cn=Computer,cn=Schema,cn=Configuration," + base_dn);
612         assert(res.msgs[0].sAMAccountType == 805306369);
613 //      assert(res.msgs[0].userAccountControl == 4098);
614
615
616         ok = ldb.del(res.msgs[0].dn);
617         if (ok.error != 0) {
618                 println(ok.errstr);
619                 assert(ok.error == 0);
620         }
621
622         var attrs = new Array("cn", "name", "objectClass", "objectGUID", "whenCreated", "nTSecurityDescriptor", "memberOf");
623         println("Testing ldb.search for (&(cn=ldaptestUSer2)(objectClass=user))");
624         var res = ldb.search("(&(cn=ldaptestUSer2)(objectClass=user))", base_dn, ldb.SCOPE_SUBTREE, attrs);
625         if (res.error != 0 || res.msgs.length != 1) {
626                 println("Could not find (&(cn=ldaptestUSer2)(objectClass=user))");
627                 assert(res.error == 0);
628                 assert(res.msgs.length == 1);
629         }
630
631         assert(res.msgs[0].dn == ("CN=ldaptestuser2,CN=Users," + base_dn));
632         assert(res.msgs[0].cn == "ldaptestuser2");
633         assert(res.msgs[0].name == "ldaptestuser2");
634         assert(res.msgs[0].objectClass[0] == "top");
635         assert(res.msgs[0].objectClass[1] == "person");
636         assert(res.msgs[0].objectClass[2] == "organizationalPerson");
637         assert(res.msgs[0].objectClass[3] == "user");
638         assert(res.msgs[0].objectGUID != undefined);
639         assert(res.msgs[0].whenCreated != undefined);
640         assert(res.msgs[0].nTSecurityDescriptor != undefined);
641         assert(res.msgs[0].memberOf[0] == ("CN=ldaptestgroup2,CN=Users," + base_dn));
642
643         ok = ldb.del(res.msgs[0].dn);
644         if (ok.error != 0) {
645                 println(ok.errstr);
646                 assert(ok.error == 0);
647         }
648
649         println("Testing ldb.search for (&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))");
650         var res = ldb.search("(&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))");
651
652         if (res.error != 0 || res.msgs.length != 1) {
653                 println("Could not find (&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))");
654                 assert(res.error == 0);
655                 assert(res.msgs.length == 1);
656         }
657
658         assert(res.msgs[0].dn == ("CN=ldaptestutf8user èùéìòà,CN=Users," + base_dn));
659         assert(res.msgs[0].cn == "ldaptestutf8user èùéìòà");
660         assert(res.msgs[0].name == "ldaptestutf8user èùéìòà");
661         assert(res.msgs[0].objectClass[0] == "top");
662         assert(res.msgs[0].objectClass[1] == "person");
663         assert(res.msgs[0].objectClass[2] == "organizationalPerson");
664         assert(res.msgs[0].objectClass[3] == "user");
665         assert(res.msgs[0].objectGUID != undefined);
666         assert(res.msgs[0].whenCreated != undefined);
667
668         ok = ldb.del(res.msgs[0].dn);
669         if (ok.error != 0) {
670                 println(ok.errstr);
671                 assert(ok.error == 0);
672         }
673
674         ok = ldb.del(("CN=ldaptestgroup2,CN=Users," + base_dn))
675         if (ok.error != 0) {
676                 println(ok.errstr);
677                 assert(ok.error == 0);
678         }
679
680         println("Testing ldb.search for (&(cn=ldaptestutf8user2 ÈÙÉÌÒÀ)(objectClass=user))");
681         var res = ldb.search("(&(cn=ldaptestutf8user ÈÙÉÌÒÀ)(objectClass=user))");
682
683         if (res.error != 0 || res.msgs.length != 1) {
684                 println("Could not find (expect space collapse, win2k3 fails) (&(cn=ldaptestutf8user2 ÈÙÉÌÒÀ)(objectClass=user))");
685         } else {
686                 assert(res.msgs[0].dn == ("cn=ldaptestutf8user2 èùéìòà,cn=users," + base_dn));
687                 assert(res.msgs[0].cn == "ldaptestutf8user2 èùéìòà");
688         }
689
690         println("Testing that we can't get at the configuration DN from the main search base");
691         var attrs = new Array("cn");
692         var res = ldb.search("objectClass=crossRef", base_dn, ldb.SCOPE_SUBTREE, attrs);
693         assert(res.error == 0);
694         if (res.msgs.length != 0) {
695                 println("Got configuration DN " + res.msgs[0].dn + " which should not be able to be seen from main search base");
696         }
697         assert(res.msgs.length == 0);
698
699         println("Testing that we can get at the configuration DN from the main search base on the LDAP port with the 'phantom root' search_options control");
700         var attrs = new Array("cn");
701         var controls = new Array("search_options:1:2");
702         var res = ldb.search("objectClass=crossRef", base_dn, ldb.SCOPE_SUBTREE, attrs, controls);
703         assert(res.error == 0);
704         assert(res.msgs.length > 0);
705
706         if (gc_ldb != undefined) {
707                 println("Testing that we can get at the configuration DN from the main search base on the GC port with the search_options control == 0");
708                 var attrs = new Array("cn");
709                 var controls = new Array("search_options:1:0");
710                 var res = gc_ldb.search("objectClass=crossRef", base_dn, gc_ldb.SCOPE_SUBTREE, attrs, controls);
711                 assert(res.error == 0);
712                 assert(res.msgs.length > 0);
713
714                 println("Testing that we do find configuration elements in the global catlog");
715                 var attrs = new Array("cn");
716                 var res = gc_ldb.search("objectClass=crossRef", base_dn, ldb.SCOPE_SUBTREE, attrs);
717                 assert(res.error == 0);
718                 assert (res.msgs.length > 0);
719         
720                 println("Testing that we do find configuration elements and user elements at the same time");
721                 var attrs = new Array("cn");
722                 var res = gc_ldb.search("(|(objectClass=crossRef)(objectClass=person))", base_dn, ldb.SCOPE_SUBTREE, attrs);
723                 assert(res.error == 0);
724                 assert (res.msgs.length > 0);
725
726                 println("Testing that we do find configuration elements in the global catlog, with the configuration basedn");
727                 var attrs = new Array("cn");
728                 var res = gc_ldb.search("objectClass=crossRef", configuration_dn, ldb.SCOPE_SUBTREE, attrs);
729                 assert(res.error == 0);
730                 assert (res.msgs.length > 0);
731         }
732
733         println("Testing that we can get at the configuration DN on the main LDAP port");
734         var attrs = new Array("cn");
735         var res = ldb.search("objectClass=crossRef", configuration_dn, ldb.SCOPE_SUBTREE, attrs);
736         assert(res.error == 0);
737         assert (res.msgs.length > 0);
738
739         println("Testing objectCategory canonacolisation");
740         var attrs = new Array("cn");
741         var res = ldb.search("objectCategory=ntDsDSA", configuration_dn, ldb.SCOPE_SUBTREE, attrs);
742         assert(res.error == 0);
743         if (res.msgs.length == 0) {
744                 println("Didn't find any records with objectCategory=ntDsDSA");
745         }
746         assert(res.msgs.length != 0);
747         
748         var attrs = new Array("cn");
749         var res = ldb.search("objectCategory=CN=ntDs-DSA," + schema_dn, configuration_dn, ldb.SCOPE_SUBTREE, attrs);
750         assert(res.error == 0);
751         if (res.msgs.length == 0) {
752                 println("Didn't find any records with objectCategory=CN=ntDs-DSA," + schema_dn);
753         }
754         assert(res.msgs.length != 0);
755         
756         println("Testing objectClass attribute order on "+ base_dn);
757         var attrs = new Array("objectClass");
758         var res = ldb.search("objectClass=domain", base_dn, ldb.SCOPE_BASE, attrs);
759         assert(res.error == 0);
760         assert(res.msgs.length == 1);
761
762         assert(res.msgs[0].objectClass[0] == "top");
763         assert(res.msgs[0].objectClass[1] == "domain");
764         assert(res.msgs[0].objectClass[2] == "domainDNS");
765
766 //  check enumeration
767
768         var attrs = new Array("cn");
769         println("Testing ldb.search for objectCategory=person");
770         var res = ldb.search("objectCategory=person", base_dn, ldb.SCOPE_SUBTREE, attrs);
771         assert(res.error == 0);
772         assert(res.msgs.length > 0);
773
774         var attrs = new Array("cn");
775         var controls = new Array("domain_scope:1");
776         println("Testing ldb.search for objectCategory=person with domain scope control");
777         var res = ldb.search("objectCategory=person", base_dn, ldb.SCOPE_SUBTREE, attrs, controls);
778         assert(res.error == 0);
779         assert(res.msgs.length > 0);
780  
781         var attrs = new Array("cn");
782         println("Testing ldb.search for objectCategory=user");
783         var res = ldb.search("objectCategory=user", base_dn, ldb.SCOPE_SUBTREE, attrs);
784         assert(res.error == 0);
785         assert(res.msgs.length > 0);
786
787         var attrs = new Array("cn");
788         var controls = new Array("domain_scope:1");
789         println("Testing ldb.search for objectCategory=user with domain scope control");
790         var res = ldb.search("objectCategory=user", base_dn, ldb.SCOPE_SUBTREE, attrs, controls);
791         assert(res.error == 0);
792         assert(res.msgs.length > 0);
793         
794         var attrs = new Array("cn");
795         println("Testing ldb.search for objectCategory=group");
796         var res = ldb.search("objectCategory=group", base_dn, ldb.SCOPE_SUBTREE, attrs);
797         assert(res.error == 0);
798         assert(res.msgs.length > 0);
799
800         var attrs = new Array("cn");
801         var controls = new Array("domain_scope:1");
802         println("Testing ldb.search for objectCategory=group with domain scope control");
803         var res = ldb.search("objectCategory=group", base_dn, ldb.SCOPE_SUBTREE, attrs, controls);
804         assert(res.error == 0);
805         assert(res.msgs.length > 0);
806         
807 }
808
809 function basedn_tests(ldb, gc_ldb)
810 {
811         println("Testing for all rootDSE attributes");
812         var attrs = new Array();
813         var res = ldb.search("", "", ldb.SCOPE_BASE, attrs);
814         assert(res.error == 0);
815         assert(res.msgs.length == 1);
816
817         println("Testing for highestCommittedUSN");
818         var attrs = new Array("highestCommittedUSN");
819         var res = ldb.search("", "", ldb.SCOPE_BASE, attrs);
820         assert(res.error == 0);
821         assert(res.msgs.length == 1);
822         assert(res.msgs[0].highestCommittedUSN != undefined);
823         assert(res.msgs[0].highestCommittedUSN != 0);
824
825         println("Testing for netlogon via LDAP");
826         var attrs = new Array("netlogon");
827         var res = ldb.search("", "", ldb.SCOPE_BASE, attrs);
828         assert(res.error == 0);
829         assert(res.msgs.length == 0);
830
831         println("Testing for netlogon and highestCommittedUSN via LDAP");
832         var attrs = new Array("netlogon", "highestCommittedUSN");
833         var res = ldb.search("", "", ldb.SCOPE_BASE, attrs);
834         assert(res.error == 0);
835         assert(res.msgs.length == 0);
836 }
837
838 function find_basedn(ldb)
839 {
840     var attrs = new Array("defaultNamingContext");
841     var res = ldb.search("", "", ldb.SCOPE_BASE, attrs);
842     assert(res.error == 0);
843     assert(res.msgs.length == 1);
844     return res.msgs[0].defaultNamingContext;
845 }
846
847 function find_configurationdn(ldb)
848 {
849     var attrs = new Array("configurationNamingContext");
850     var res = ldb.search("", "", ldb.SCOPE_BASE, attrs);
851     assert(res.error == 0);
852     assert(res.msgs.length == 1);
853     return res.msgs[0].configurationNamingContext;
854 }
855
856 function find_schemadn(ldb)
857 {
858     var attrs = new Array("schemaNamingContext");
859     var res = ldb.search("", "", ldb.SCOPE_BASE, attrs);
860     assert(res.error == 0);
861     assert(res.msgs.length == 1);
862     return res.msgs[0].schemaNamingContext;
863 }
864
865 /* use command line creds if available */
866 ldb.credentials = options.get_credentials();
867 gc_ldb.credentials = options.get_credentials();
868
869 var ok = ldb.connect("ldap://" + host);
870 var base_dn = find_basedn(ldb);
871 var configuration_dn = find_configurationdn(ldb);
872 var schema_dn = find_schemadn(ldb);
873
874 printf("baseDN: %s\n", base_dn);
875
876 var ok = gc_ldb.connect("ldap://" + host + ":3268");
877 if (!ok) {
878         gc_ldb = undefined;
879 }
880
881 basic_tests(ldb, gc_ldb, base_dn, configuration_dn, schema_dn)
882
883 basedn_tests(ldb, gc_ldb)
884
885 return 0;