summaryrefslogtreecommitdiff
path: root/testprogs/ejs/ldb.js
blob: 36b175f2c12bb516da0f77106a7adc7b17b1e994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/sh
exec smbscript "$0" ${1+"$@"}
/*
	demonstrate access to ldb databases from ejs
*/


var ldb = ldb_init();
var sys;
var options = GetOptions(ARGV, 
		"POPT_AUTOHELP",
		"POPT_COMMON_SAMBA");
if (options == undefined) {
   println("Failed to parse options");
   return -1;
}

libinclude("base.js");

if (options.ARGV.length != 1) {
   println("Usage: ldb.js <prefix>");
   return -1;
}

prefix = options.ARGV[0];

function basic_tests(ldb)
{
	println("Running basic tests");
	ok = ldb.add("
dn: cn=x,cn=test
objectClass: foo
x: 3
");
	assert(ok.error == 0);

	println("Testing ldb.search");
	var res = ldb.search("(objectClass=*)");
	assert(res.msgs[0].objectClass[0] == "foo");
	assert(res.msgs[0].dn == "cn=x,cn=test");
	assert(res.msgs[0].x == 3);

	ok = ldb.add("
dn: cn=x2,cn=test
objectClass: foo
x: 4
");
	assert(ok.error == 0);
	var attrs = new Array("x");
	res = ldb.search("x=4", NULL, ldb.SCOPE_DEFAULT, attrs);
	assert(res.msgs[0].x == 4);
	assert(res.msgs[0].objectClass == undefined);
	assert(res.msgs[0].dn == "cn=x2,cn=test");

	ok = ldb.del("cn=x,cn=test");
	assert(ok.error == 0);

	ok = ldb.rename("cn=x2,cn=test", "cn=x3,cn=test");
	assert(ok.error == 0);
	res = ldb.search("x=4", NULL, ldb.SCOPE_DEFAULT, attrs);
	assert(res.msgs[0].dn == "cn=x3,cn=test");

	ok = ldb.rename("cn=x3,cn=test", "cn=X3,cn=test");
	assert(ok.error == 0);
	res = ldb.search("x=4", NULL, ldb.SCOPE_DEFAULT, attrs);
	assert(res.msgs[0].dn == "cn=X3,cn=test");

	ok = ldb.modify("
dn: cn=x3,cn=test
changetype: modify
add: x
x: 7
");

	res = ldb.search("x=7");
	assert(res.msgs.length == 1);
	assert(res.msgs[0].x.length == 2);

	/* Check a few things before we add modules */
	assert(res.msgs[0].objectGUID == undefined);
	assert(res.msgs[0].createTimestamp == undefined);
	assert(res.msgs[0].whenCreated == undefined);

}
	
function setup_modules(ldb)
{
	ok = ldb.add("
dn: @MODULES
@LIST: rootdse,operational,rdn_name,partition

dn: cn=ROOTDSE
defaultNamingContext: cn=Test

dn: @PARTITION
partition: cn=SideTest:" + prefix + "/" + "testside.ldb
partition: cn=Sub,cn=PartTest:" + prefix + "/" + "testsub.ldb
partition: cn=PartTest:" + prefix + "/" + "testpartition.ldb
partition: cn=Sub,cn=Sub,cn=PartTest:" + prefix + "/" + "testsubsub.ldb
replicateEntries: @ATTRIBUTES
replicateEntries: @INDEXLIST
modules: cn=PartTest:objectguid
");
}

/* Test the basic operation of the timestamps,objectguid and name_rdn
   modules */

function modules_test(ldb, parttestldb) 
{
        println("Running modules tests");

        ok = ldb.add("
dn: @ATTRIBUTES
cn: CASE_INSENSITIVE
caseattr: CASE_INSENSITIVE
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	/* Confirm that the attributes were replicated */
	var res_attrs =  parttestldb.search("cn=*", "@ATTRIBUTES",  parttestldb.SCOPE_BASE);
	assert(res_attrs.msgs[0].cn == "CASE_INSENSITIVE");

	ok = ldb.add("
dn: cn=x8,cn=PartTest
objectClass: foo
x: 8
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	ok = ldb.add("
dn: cn=x9,cn=PartTest
objectClass: foo
x: 9
cn: X9
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	ok = ldb.add("
dn: cn=X9,cn=PartTest
objectClass: foo
x: 9
cn: X9
");
	if (ok.error == 0) {
		println("Should have failed to add cn=X9,cn=PartTest");
		assert(ok.error != 0);
	}

	var res = ldb.search("x=8", "cn=PartTest", ldb.SCOPE_DEFAULT);
	assert(res.msgs[0].objectGUID != undefined);
	assert(res.msgs[0].uSNCreated != undefined);
	assert(res.msgs[0].uSNChanged != undefined);
	assert(res.msgs[0].createTimestamp == undefined);
	assert(res.msgs[0].whenCreated != undefined);
	assert(res.msgs[0].name == "x8");
	assert(res.msgs[0].cn == "x8");

	/* Confirm that this ended up in the correct LDB */
	var res_otherldb =  parttestldb.search("x=8", "cn=PartTest",  parttestldb.SCOPE_DEFAULT);
	assert(res_otherldb.msgs[0].objectGUID != undefined);
	assert(res_otherldb.msgs[0].createTimestamp == undefined);
	assert(res_otherldb.msgs[0].whenCreated != undefined);
	assert(res_otherldb.msgs[0].name == "x8");
	assert(res_otherldb.msgs[0].cn == "x8");

	var attrs = new Array("*", "createTimestamp");
	var res2 = ldb.search("x=9", "cn=PartTest", ldb.SCOPE_DEFAULT, attrs);
	assert(res2.msgs[0].objectGUID != undefined);
	assert(res2.msgs[0].createTimestamp != undefined);
	assert(res2.msgs[0].whenCreated != undefined);
	assert(res2.msgs[0].name == "x9");
	assert(res2.msgs[0].cn == "x9");

	assert(res.msgs[0].objectGUID != res2.msgs[0].objectGUID);

	var attrs = new Array("*");
	var res3 = ldb.search("", "", ldb.SCOPE_BASE, attrs);
	assert(res3.msgs[0].cn == undefined);
	assert(res3.msgs[0].distinguishedName == undefined);
	assert(res3.msgs[0].name == undefined);
	assert(res3.msgs[0].currentTime != undefined);
	assert(res3.msgs[0].highestCommittedUSN != undefined);

	assert(res3.msgs[0].namingContexts[0] == "cn=Sub,cn=Sub,cn=PartTest");
	assert(res3.msgs[0].namingContexts[1] == "cn=Sub,cn=PartTest");
	assert(res3.msgs[0].namingContexts[2] == "cn=PartTest");
	assert(res3.msgs[0].namingContexts[3] == "cn=SideTest");
	var usn = res3.msgs[0].highestCommittedUSN;

	/* Start a transaction.  We are going to abort it later, to
	 * show we clean up all partitions */

	ok = ldb.transaction_start()
	if (!ok) {
		println("Failed to start a transaction: " + ok.errstr);
		assert(ok.error == 0);
	}

	
	ok = ldb.add("
dn: cn=x10,cn=parttest
objectClass: foo
x: 10
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	var attrs = new Array("highestCommittedUSN");
	var res4 = ldb.search("", "", ldb.SCOPE_BASE, attrs);
	var usn2 = res4.msgs[0].highestCommittedUSN;
	assert(usn < res4.msgs[0].highestCommittedUSN);

	ok = ldb.add("
dn: cn=x11,cn=sub,cn=parttest
objectClass: foo
x: 11
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	var attrs = new Array("highestCommittedUSN");
	var res5 = ldb.search("", "", ldb.SCOPE_BASE, attrs);
	assert(usn2 < res5.msgs[0].highestCommittedUSN);
	
	var attrs = new Array("*", "createTimestamp");
	var res6 = ldb.search("x=11", "cn=parttest", ldb.SCOPE_SUB, attrs);
	assert(res6.msgs.length == 0);

	var attrs = new Array("*", "createTimestamp");
	var res7 = ldb.search("x=10", "cn=sub,cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	assert(res7.msgs.length == 0);

	var res8 = ldb.search("x=11", "cn=sub,cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	
	assert(res8.msgs[0].objectGUID == undefined); /* The objectGUID module is not loaded here */
	assert(res8.msgs[0].uSNCreated == undefined); /* The objectGUID module is not loaded here */
	assert(res8.msgs[0].name == "x11");
	assert(res8.msgs[0].cn == "x11");

	ok = ldb.add("
dn: caseattr=XY,cn=PartTest
objectClass: foo
x: Y
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	ok = ldb.add("
dn: caseattr=XZ,cn=PartTest
objectClass: foo
x: Z
caseattr: XZ
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	ok = ldb.add("
dn: caseattr=xz,cn=PartTest
objectClass: foo
x: Z
caseattr: xz
");
	if (ok.error == 0) {
		println("Should have failed to add caseattr=xz,cn=PartTest");
		assert(ok.error != 0);
	}

	ok = ldb.add("
dn: caseattr2=XZ,cn=PartTest
objectClass: foo
x: Z
caseattr2: XZ
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	ok = ldb.add("
dn: caseattr2=Xz,cn=PartTest
objectClass: foo
x: Z
caseattr2: Xz
");
	if (ok.error != 0) {
		println("Failed to add: " + ok.errstr);
		assert(ok.error == 0);
	}

	var resX = ldb.search("caseattr=xz", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	assert(resX.msgs.length == 1); 
	assert(resX.msgs[0].objectGUID != undefined);
	assert(resX.msgs[0].createTimestamp != undefined);
	assert(resX.msgs[0].whenCreated != undefined);
	assert(resX.msgs[0].name == "XZ");

	var rescount = ldb.search("(|(caseattr=*)(cn=*))", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	assert(rescount.msgs.length == 5); 

	/* Check this attribute is *not* case sensitive */
	var resXcount = ldb.search("caseattr=x*", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	assert(resXcount.msgs.length == 2); 
	
	/* Check that this attribute *is* case sensitive */
	var resXcount2 = ldb.search("caseattr2=xz", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	assert(resXcount2.msgs.length == 0); 
	

	/* Now abort the transaction to show that even with
	 * partitions, it is aborted everywhere */
	ok = ldb.transaction_cancel();
	if (!ok) {
		println("Failed to cancel a transaction: " + ok.errstr);
		assert(ok);
	}

	/* now check it all went away */

	var attrs = new Array("highestCommittedUSN");
	var res9 = ldb.search("", "", ldb.SCOPE_BASE, attrs);
	assert(usn == res9.msgs[0].highestCommittedUSN);
	
	var attrs = new Array("*");
	var res10 = ldb.search("x=11", "cn=sub,cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	assert(res10.msgs.length == 0);

	var attrs = new Array("*");
	var res11 = ldb.search("x=10", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	assert(res11.msgs.length == 0);

	var attrs = new Array("*");
	var res12 = ldb.search("caseattr=*", "cn=parttest", ldb.SCOPE_DEFAULT, attrs);
	assert(res12.msgs.length == 0);

}

sys = sys_init();
var dbfile = "test.ldb";

sys.unlink(prefix + "/" + dbfile);
sys.unlink(prefix + "/" + "testpartition.ldb");
sys.unlink(prefix + "/" + "testsub.ldb");
sys.unlink(prefix + "/" + "testsubsub.ldb");
sys.unlink(prefix + "/" + "testside.ldb");

var ok = ldb.connect("tdb://" + prefix + "/" + dbfile);
assert(ok);

basic_tests(ldb);

setup_modules(ldb);
ldb = ldb_init();
var ok = ldb.connect("tdb://" + prefix + "/" + dbfile);
assert(ok);

parttestldb = ldb_init();
var ok = parttestldb.connect("tdb://" + prefix + "/" + "testpartition.ldb");
assert(ok);

modules_test(ldb, parttestldb);

sys.unlink(prefix + "/" + dbfile);
sys.unlink(prefix + "/" + "testpartition.ldb");
sys.unlink(prefix + "/" + "testsub.ldb");
sys.unlink(prefix + "/" + "testsubsub.ldb");
sys.unlink(prefix + "/" + "testside.ldb");
return 0;