summaryrefslogtreecommitdiff
path: root/testprogs/ejs/ldb.js
blob: fe067f638ab031d9b4eb48dd1f4c1100df1c7ed0 (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
#!/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: ldap.js <HOST>");
   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);

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

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

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

	ok = ldb.rename("cn=x2,cn=test", "cn=x3,cn=test");
	assert(ok);
	res = ldb.search("x=4", NULL, ldb.SCOPE_DEFAULT, attrs);
	assert(res[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.length == 1);
	assert(res[0].x.length == 2);

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

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

dn: cn=ROOTDSE
defaultNamingContext: cn=Test

dn: @PARTITION
partition: cn=SideTest:" + prefix +  "testside.ldb
partition: cn=Sub,cn=Test:" + prefix +  "testsub.ldb
partition: cn=Test:" + prefix +  "testpartition.ldb
partition: cn=Sub,cn=Sub,cn=Test:" + prefix +  "testsubsub.ldb
");
}

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

function modules_test(ldb) 
{
        println("Running modules tests");
	ok = ldb.add("
dn: cn=x8,cn=test
objectClass: foo
x: 8
");
	if (!ok) {
		println("Failed to add: " + ldb.errstring());
		assert(ok);
	}

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

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

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

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

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

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

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);

modules_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");
return 0;