2 exec smbscript "$0" ${1+"$@"}
4 demonstrate access to ldb databases from ejs
10 var options = GetOptions(ARGV,
13 if (options == undefined) {
14 println("Failed to parse options");
18 libinclude("base.js");
20 if (options.ARGV.length != 1) {
21 println("Usage: ldb.js <prefix>");
25 prefix = options.ARGV[0];
27 function basic_tests(ldb)
29 println("Running basic tests");
35 assert(ok.error == 0);
37 println("Testing ldb.search");
38 var res = ldb.search("(objectClass=*)");
39 assert(res.msgs[0].objectClass[0] == "foo");
40 assert(res.msgs[0].dn == "cn=x,cn=test");
41 assert(res.msgs[0].x == 3);
48 assert(ok.error == 0);
49 var attrs = new Array("x");
50 res = ldb.search("x=4", NULL, ldb.SCOPE_DEFAULT, attrs);
51 assert(res.msgs[0].x == 4);
52 assert(res.msgs[0].objectClass == undefined);
53 assert(res.msgs[0].dn == "cn=x2,cn=test");
55 ok = ldb.del("cn=x,cn=test");
56 assert(ok.error == 0);
58 ok = ldb.rename("cn=x2,cn=test", "cn=x3,cn=test");
59 assert(ok.error == 0);
60 res = ldb.search("x=4", NULL, ldb.SCOPE_DEFAULT, attrs);
61 assert(res.msgs[0].dn == "cn=x3,cn=test");
63 ok = ldb.rename("cn=x3,cn=test", "cn=X3,cn=test");
64 assert(ok.error == 0);
65 res = ldb.search("x=4", NULL, ldb.SCOPE_DEFAULT, attrs);
66 assert(res.msgs[0].dn == "cn=X3,cn=test");
75 res = ldb.search("x=7");
76 assert(res.msgs.length == 1);
77 assert(res.msgs[0].x.length == 2);
79 /* Check a few things before we add modules */
80 assert(res.msgs[0].objectGUID == undefined);
81 assert(res.msgs[0].createTimestamp == undefined);
82 assert(res.msgs[0].whenCreated == undefined);
86 function setup_modules(ldb)
90 @LIST: rootdse,operational,rdn_name,partition
93 defaultNamingContext: cn=Test
96 partition: cn=SideTest:" + prefix + "/" + "testside.ldb
97 partition: cn=Sub,cn=PartTest:" + prefix + "/" + "testsub.ldb
98 partition: cn=PartTest:" + prefix + "/" + "testpartition.ldb
99 partition: cn=Sub,cn=Sub,cn=PartTest:" + prefix + "/" + "testsubsub.ldb
100 replicateEntries: @ATTRIBUTES
101 replicateEntries: @INDEXLIST
102 modules: cn=PartTest:objectguid
106 /* Test the basic operation of the timestamps,objectguid and name_rdn
109 function modules_test(ldb, parttestldb)
111 println("Running modules tests");
116 caseattr: CASE_INSENSITIVE
119 println("Failed to add: " + ok.errstr);
120 assert(ok.error == 0);
123 /* Confirm that the attributes were replicated */
124 var res_attrs = parttestldb.search("cn=*", "@ATTRIBUTES", parttestldb.SCOPE_BASE);
125 assert(res_attrs.msgs[0].cn == "CASE_INSENSITIVE");
128 dn: cn=x8,cn=PartTest
133 println("Failed to add: " + ok.errstr);
134 assert(ok.error == 0);
138 dn: cn=x9,cn=PartTest
144 println("Failed to add: " + ok.errstr);
145 assert(ok.error == 0);
149 dn: cn=X9,cn=PartTest
155 println("Should have failed to add cn=X9,cn=PartTest");
156 assert(ok.error != 0);
159 var res = ldb.search("x=8", "cn=PartTest", ldb.SCOPE_DEFAULT);
160 assert(res.msgs[0].objectGUID != undefined);
161 assert(res.msgs[0].uSNCreated != undefined);
162 assert(res.msgs[0].uSNChanged != undefined);
163 assert(res.msgs[0].createTimestamp == undefined);
164 assert(res.msgs[0].whenCreated != undefined);
165 assert(res.msgs[0].name == "x8");
166 assert(res.msgs[0].cn == "x8");
168 /* Confirm that this ended up in the correct LDB */
169 var res_otherldb = parttestldb.search("x=8", "cn=PartTest", parttestldb.SCOPE_DEFAULT);
170 assert(res_otherldb.msgs[0].objectGUID != undefined);
171 assert(res_otherldb.msgs[0].createTimestamp == undefined);
172 assert(res_otherldb.msgs[0].whenCreated != undefined);
173 assert(res_otherldb.msgs[0].name == "x8");
174 assert(res_otherldb.msgs[0].cn == "x8");
176 var attrs = new Array("*", "createTimestamp");
177 var res2 = ldb.search("x=9", "cn=PartTest", ldb.SCOPE_DEFAULT, attrs);
178 assert(res2.msgs[0].objectGUID != undefined);
179 assert(res2.msgs[0].createTimestamp != undefined);
180 assert(res2.msgs[0].whenCreated != undefined);
181 assert(res2.msgs[0].name == "x9");
182 assert(res2.msgs[0].cn == "x9");
184 assert(res.msgs[0].objectGUID != res2.msgs[0].objectGUID);
186 var attrs = new Array("*");
187 var res3 = ldb.search("", "", ldb.SCOPE_BASE, attrs);
188 assert(res3.msgs[0].cn == undefined);
189 assert(res3.msgs[0].distinguishedName == undefined);
190 assert(res3.msgs[0].name == undefined);
191 assert(res3.msgs[0].currentTime != undefined);
192 assert(res3.msgs[0].highestCommittedUSN != undefined);
194 assert(res3.msgs[0].namingContexts[0] == "cn=Sub,cn=Sub,cn=PartTest");
195 assert(res3.msgs[0].namingContexts[1] == "cn=Sub,cn=PartTest");
196 assert(res3.msgs[0].namingContexts[2] == "cn=PartTest");
197 assert(res3.msgs[0].namingContexts[3] == "cn=SideTest");
198 var usn = res3.msgs[0].highestCommittedUSN;
200 /* Start a transaction. We are going to abort it later, to
201 * show we clean up all partitions */
203 ok = ldb.transaction_start()
205 println("Failed to start a transaction: " + ok.errstr);
206 assert(ok.error == 0);
211 dn: cn=x10,cn=parttest
216 println("Failed to add: " + ok.errstr);
217 assert(ok.error == 0);
220 var attrs = new Array("highestCommittedUSN");
221 var res4 = ldb.search("", "", ldb.SCOPE_BASE, attrs);
222 var usn2 = res4.msgs[0].highestCommittedUSN;
223 assert(usn < res4.msgs[0].highestCommittedUSN);
226 dn: cn=x11,cn=sub,cn=parttest
231 println("Failed to add: " + ok.errstr);
232 assert(ok.error == 0);
235 var attrs = new Array("highestCommittedUSN");
236 var res5 = ldb.search("", "", ldb.SCOPE_BASE, attrs);
237 assert(usn2 < res5.msgs[0].highestCommittedUSN);
239 var attrs = new Array("*", "createTimestamp");
240 var res6 = ldb.search("x=11", "cn=parttest", ldb.SCOPE_SUB, attrs);
241 assert(res6.msgs.length == 0);
243 var attrs = new Array("*", "createTimestamp");
244 var res7 = ldb.search("x=10", "cn=sub,cn=parttest", ldb.SCOPE_DEFAULT, attrs);
245 assert(res7.msgs.length == 0);
247 var res8 = ldb.search("x=11", "cn=sub,cn=parttest", ldb.SCOPE_DEFAULT, attrs);
249 assert(res8.msgs[0].objectGUID == undefined); /* The objectGUID module is not loaded here */
250 assert(res8.msgs[0].uSNCreated == undefined); /* The objectGUID module is not loaded here */
251 assert(res8.msgs[0].name == "x11");
252 assert(res8.msgs[0].cn == "x11");
255 dn: caseattr=XY,cn=PartTest
260 println("Failed to add: " + ok.errstr);
261 assert(ok.error == 0);
265 dn: caseattr=XZ,cn=PartTest
271 println("Failed to add: " + ok.errstr);
272 assert(ok.error == 0);
276 dn: caseattr=xz,cn=PartTest
282 println("Should have failed to add caseattr=xz,cn=PartTest");
283 assert(ok.error != 0);
287 dn: caseattr2=XZ,cn=PartTest
293 println("Failed to add: " + ok.errstr);
294 assert(ok.error == 0);
298 dn: caseattr2=Xz,cn=PartTest
304 println("Failed to add: " + ok.errstr);
305 assert(ok.error == 0);
308 var resX = ldb.search("caseattr=xz", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
309 assert(resX.msgs.length == 1);
310 assert(resX.msgs[0].objectGUID != undefined);
311 assert(resX.msgs[0].createTimestamp != undefined);
312 assert(resX.msgs[0].whenCreated != undefined);
313 assert(resX.msgs[0].name == "XZ");
315 var rescount = ldb.search("(|(caseattr=*)(cn=*))", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
316 assert(rescount.msgs.length == 5);
318 /* Check this attribute is *not* case sensitive */
319 var resXcount = ldb.search("caseattr=x*", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
320 assert(resXcount.msgs.length == 2);
322 /* Check that this attribute *is* case sensitive */
323 var resXcount2 = ldb.search("caseattr2=xz", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
324 assert(resXcount2.msgs.length == 0);
327 /* Now abort the transaction to show that even with
328 * partitions, it is aborted everywhere */
329 ok = ldb.transaction_cancel();
331 println("Failed to cancel a transaction: " + ok.errstr);
335 /* now check it all went away */
337 var attrs = new Array("highestCommittedUSN");
338 var res9 = ldb.search("", "", ldb.SCOPE_BASE, attrs);
339 assert(usn == res9.msgs[0].highestCommittedUSN);
341 var attrs = new Array("*");
342 var res10 = ldb.search("x=11", "cn=sub,cn=parttest", ldb.SCOPE_DEFAULT, attrs);
343 assert(res10.msgs.length == 0);
345 var attrs = new Array("*");
346 var res11 = ldb.search("x=10", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
347 assert(res11.msgs.length == 0);
349 var attrs = new Array("*");
350 var res12 = ldb.search("caseattr=*", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
351 assert(res12.msgs.length == 0);
356 var dbfile = "test.ldb";
358 sys.unlink(prefix + "/" + dbfile);
359 sys.unlink(prefix + "/" + "testpartition.ldb");
360 sys.unlink(prefix + "/" + "testsub.ldb");
361 sys.unlink(prefix + "/" + "testsubsub.ldb");
362 sys.unlink(prefix + "/" + "testside.ldb");
364 var ok = ldb.connect("tdb://" + prefix + "/" + dbfile);
371 var ok = ldb.connect("tdb://" + prefix + "/" + dbfile);
374 parttestldb = ldb_init();
375 var ok = parttestldb.connect("tdb://" + prefix + "/" + "testpartition.ldb");
378 modules_test(ldb, parttestldb);
380 sys.unlink(prefix + "/" + dbfile);
381 sys.unlink(prefix + "/" + "testpartition.ldb");
382 sys.unlink(prefix + "/" + "testsub.ldb");
383 sys.unlink(prefix + "/" + "testsubsub.ldb");
384 sys.unlink(prefix + "/" + "testside.ldb");