2
test_myldap.c - simple test for the myldap module
3
This file is part of the nss-pam-ldapd library.
5
Copyright (C) 2007 Arthur de Jong
7
This library is free software; you can redistribute it and/or
8
modify it under the terms of the GNU Lesser General Public
9
License as published by the Free Software Foundation; either
10
version 2.1 of the License, or (at your option) any later version.
12
This library is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
Lesser General Public License for more details.
17
You should have received a copy of the GNU Lesser General Public
18
License along with this library; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32
#include "nslcd/log.h"
33
#include "nslcd/cfg.h"
34
#include "nslcd/myldap.h"
40
static const char *foo="";
42
/* this is a simple way to get this into an executable,
43
we should probably read a valid config instead */
44
const char **base_get_var(int UNUSED(map)) {return NULL;}
45
int *scope_get_var(int UNUSED(map)) {return NULL;}
46
const char **filter_get_var(int UNUSED(map)) {return &foo;}
47
const char **attmap_get_var(int UNUSED(map),const char UNUSED(*name)) {return &foo;}
49
/* the maxium number of results to print (all results are retrieved) */
52
/* This is a very basic search test, it performs a test to get certain
53
entries from the database. It currently just prints out the DNs for
55
static void test_search(void)
57
MYLDAP_SESSION *session;
58
MYLDAP_SEARCH *search;
60
const char *attrs[] = { "uid", "cn", "gid", NULL };
63
/* initialize session */
64
printf("test_myldap: test_search(): getting session...\n");
65
session=myldap_create_session();
66
assert(session!=NULL);
68
printf("test_myldap: test_search(): doing search...\n");
69
search=myldap_search(session,nslcd_cfg->ldc_bases[0],
71
"(objectclass=posixAccount)",
75
printf("test_myldap: test_search(): get results...\n");
76
for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++)
79
printf("test_myldap: test_search(): [%d] DN %s\n",i,myldap_get_dn(entry));
80
else if (i==MAXRESULTS)
81
printf("test_myldap: test_search(): ...\n");
83
printf("test_myldap: test_search(): %d entries returned: %s\n",i,ldap_err2string(rc));
84
/* perform another search */
85
printf("test_myldap: test_search(): doing search...\n");
86
search=myldap_search(session,nslcd_cfg->ldc_bases[0],
88
"(objectclass=posixGroup)",
92
printf("test_myldap: test_search(): get results...\n");
93
for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++)
96
printf("test_myldap: test_search(): [%d] DN %s\n",i,myldap_get_dn(entry));
97
else if (i==MAXRESULTS)
98
printf("test_myldap: test_search(): ...\n");
100
printf("test_myldap: test_search(): %d entries returned: %s\n",i,ldap_err2string(rc));
102
myldap_session_close(session);
105
static void test_get(void)
107
MYLDAP_SESSION *session;
108
MYLDAP_SEARCH *search1,*search2;
110
const char *attrs1[] = { "cn", "userPassword", "memberUid", "gidNumber", "uniqueMember", NULL };
111
const char *attrs2[] = { "uid", NULL };
113
/* initialize session */
114
printf("test_myldap: test_get(): getting session...\n");
115
session=myldap_create_session();
116
assert(session!=NULL);
118
printf("test_myldap: test_get(): doing search...\n");
119
search1=myldap_search(session,nslcd_cfg->ldc_bases[0],
121
"(&(|(objectClass=posixGroup)(objectClass=groupOfUniqueNames))(cn=testgroup2))",
123
assert(search1!=NULL);
125
entry=myldap_get_entry(search1,&rc);
127
printf("test_myldap: test_get(): got DN %s\n",myldap_get_dn(entry));
128
/* get some attribute values */
129
(void)myldap_get_values(entry,"gidNumber");
130
(void)myldap_get_values(entry,"userPassword");
131
(void)myldap_get_values(entry,"memberUid");
132
(void)myldap_get_values(entry,"uniqueMember");
133
/* perform another search */
134
printf("test_myldap: test_get(): doing get...\n");
135
search2=myldap_search(session,"cn=Test User2,ou=people,dc=test,dc=tld",
137
"(objectclass=posixAccount)",
139
assert(search2!=NULL);
141
entry=myldap_get_entry(search2,&rc);
143
printf("test_myldap: test_get(): got DN %s\n",myldap_get_dn(entry));
144
/* test if searches are ok */
145
assert(myldap_get_entry(search1,&rc)==NULL);
146
assert(myldap_get_entry(search2,&rc)==NULL);
148
myldap_session_close(session);
151
/* This search prints a number of attributes from a search */
152
static void test_get_values(void)
154
MYLDAP_SESSION *session;
155
MYLDAP_SEARCH *search;
157
const char *attrs[] = { "uidNumber", "cn", "gidNumber", "uid", "objectClass", NULL };
161
/* initialize session */
162
printf("test_myldap: test_get_values(): getting session...\n");
163
session=myldap_create_session();
164
assert(session!=NULL);
166
search=myldap_search(session,nslcd_cfg->ldc_bases[0],
168
"(&(objectClass=posixAccount)(uid=*))",
170
assert(search!=NULL);
171
/* go over results */
172
for (i=0;(entry=myldap_get_entry(search,NULL))!=NULL;i++)
175
printf("test_myldap: test_get_values(): [%d] DN %s\n",i,myldap_get_dn(entry));
176
else if (i==MAXRESULTS)
177
printf("test_myldap: test_get_values(): ...\n");
178
/* try to get uid from attribute */
179
vals=myldap_get_values(entry,"uidNumber");
180
assert((vals!=NULL)&&(vals[0]!=NULL));
182
printf("test_myldap: test_get_values(): [%d] uidNumber=%s\n",i,vals[0]);
183
/* try to get gid from attribute */
184
vals=myldap_get_values(entry,"gidNumber");
185
assert((vals!=NULL)&&(vals[0]!=NULL));
187
printf("test_myldap: test_get_values(): [%d] gidNumber=%s\n",i,vals[0]);
188
/* write LDF_STRING(PASSWD_NAME) */
189
vals=myldap_get_values(entry,"uid");
190
assert((vals!=NULL)&&(vals[0]!=NULL));
192
printf("test_myldap: test_get_values(): [%d] uid=%s\n",i,vals[0]);
194
rdnval=myldap_get_rdn_value(entry,"cn");
196
printf("test_myldap: test_get_values(): [%d] cdrdn=%s\n",i,rdnval==NULL?"NULL":rdnval);
197
rdnval=myldap_get_rdn_value(entry,"uid");
199
printf("test_myldap: test_get_values(): [%d] uidrdn=%s\n",i,rdnval==NULL?"NULL":rdnval);
200
/* check objectclass */
201
assert(myldap_has_objectclass(entry,"posixAccount"));
204
myldap_session_close(session);
207
static void test_get_rdnvalues(void)
209
MYLDAP_SESSION *session;
210
MYLDAP_SEARCH *search;
212
const char *attrs[] = { "cn", "uid", NULL };
215
/* initialize session */
216
printf("test_myldap: test_get_rdnvalues(): getting session...\n");
217
session=myldap_create_session();
218
assert(session!=NULL);
220
printf("test_myldap: test_get_rdnvalues(): doing search...\n");
221
search=myldap_search(session,"cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld",
225
assert(search!=NULL);
227
entry=myldap_get_entry(search,&rc);
229
printf("test_myldap: test_get_rdnvalues(): got DN %s\n",myldap_get_dn(entry));
230
/* get some values from DN */
231
printf("test_myldap: test_get_rdnvalues(): DN.uid=%s\n",myldap_get_rdn_value(entry,"uid"));
232
printf("test_myldap: test_get_rdnvalues(): DN.cn=%s\n",myldap_get_rdn_value(entry,"cn"));
233
printf("test_myldap: test_get_rdnvalues(): DN.uidNumber=%s\n",myldap_get_rdn_value(entry,"uidNumber"));
235
myldap_session_close(session);
237
printf("test_myldap: test_get_rdnvalues(): DN.uid=%s\n",myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld","uid",buf,sizeof(buf)));
238
printf("test_myldap: test_get_rdnvalues(): DN.cn=%s\n",myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld","cn",buf,sizeof(buf)));
239
printf("test_myldap: test_get_rdnvalues(): DN.uidNumber=%s\n",myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld","uidNumber",buf,sizeof(buf)));
242
/* this method tests to see if we can perform two searches within
244
static void test_two_searches(void)
246
MYLDAP_SESSION *session;
247
MYLDAP_SEARCH *search1,*search2;
249
const char *attrs[] = { "uidNumber", "cn", "gidNumber", "uid", "objectClass", NULL };
251
/* initialize session */
252
printf("test_myldap: test_two_searches(): getting session...\n");
253
session=myldap_create_session();
254
assert(session!=NULL);
255
/* perform search1 */
256
search1=myldap_search(session,nslcd_cfg->ldc_bases[0],
258
"(&(objectClass=posixAccount)(uid=*))",
260
assert(search1!=NULL);
261
/* get a result from search1 */
262
entry=myldap_get_entry(search1,NULL);
264
printf("test_myldap: test_two_searches(): [search1] DN %s\n",myldap_get_dn(entry));
265
vals=myldap_get_values(entry,"cn");
266
assert((vals!=NULL)&&(vals[0]!=NULL));
267
printf("test_myldap: test_two_searches(): [search1] cn=%s\n",vals[0]);
268
/* start a second search */
269
search2=myldap_search(session,nslcd_cfg->ldc_bases[0],
271
"(&(objectclass=posixGroup)(gidNumber=*))",
273
assert(search2!=NULL);
274
/* get a result from search2 */
275
entry=myldap_get_entry(search2,NULL);
277
printf("test_myldap: test_two_searches(): [search2] DN %s\n",myldap_get_dn(entry));
278
vals=myldap_get_values(entry,"cn");
279
assert((vals!=NULL)&&(vals[0]!=NULL));
280
printf("test_myldap: test_two_searches(): [search2] cn=%s\n",vals[0]);
281
/* get another result from search1 */
282
entry=myldap_get_entry(search1,NULL);
284
printf("test_myldap: test_two_searches(): [search1] DN %s\n",myldap_get_dn(entry));
285
vals=myldap_get_values(entry,"cn");
286
assert((vals!=NULL)&&(vals[0]!=NULL));
287
printf("test_myldap: test_two_searches(): [search1] cn=%s\n",vals[0]);
289
myldap_search_close(search1);
290
/* get another result from search2 */
291
entry=myldap_get_entry(search2,NULL);
293
printf("test_myldap: test_two_searches(): [search2] DN %s\n",myldap_get_dn(entry));
294
vals=myldap_get_values(entry,"cn");
295
assert((vals!=NULL)&&(vals[0]!=NULL));
296
printf("test_myldap: test_two_searches(): [search2] cn=%s\n",vals[0]);
298
myldap_session_close(session);
301
/* perform a simple search */
302
static void *worker(void *arg)
304
MYLDAP_SESSION *session;
305
MYLDAP_SEARCH *search;
307
const char *attrs[] = { "uid", "cn", "gid", NULL };
308
struct worker_args *args=(struct worker_args *)arg;
311
/* initialize session */
312
session=myldap_create_session();
313
assert(session!=NULL);
315
search=myldap_search(session,nslcd_cfg->ldc_bases[0],
317
"(objectclass=posixAccount)",
319
assert(search!=NULL);
320
/* go over results */
321
for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++)
324
printf("test_myldap: test_threads(): [worker %d] [%d] DN %s\n",args->id,i,myldap_get_dn(entry));
325
else if (i==MAXRESULTS)
326
printf("test_myldap: test_threads(): [worker %d] ...\n",args->id);
328
printf("test_myldap: test_threads(): [worker %d] DONE: %s\n",args->id,ldap_err2string(rc));
329
assert(rc==LDAP_SUCCESS);
331
myldap_session_close(session);
335
/* thread ids of all running threads */
336
#define NUM_THREADS 5
337
pthread_t my_threads[NUM_THREADS];
339
static void test_threads(void)
342
struct worker_args args[NUM_THREADS];
343
/* start worker threads */
344
for (i=0;i<NUM_THREADS;i++)
347
assert(pthread_create(&my_threads[i],NULL,worker,&(args[i]))==0);
349
/* wait for all threads to die */
350
for (i=0;i<NUM_THREADS;i++)
352
assert(pthread_join(my_threads[i],NULL)==0);
356
static void test_connections(void)
358
MYLDAP_SESSION *session;
359
MYLDAP_SEARCH *search;
360
const char *attrs[] = { "uid", "cn", "gid", NULL };
361
char *old_uris[NSS_LDAP_CONFIG_URI_MAX+1];
363
/* save the old URIs */
364
for (i=0;i<(NSS_LDAP_CONFIG_URI_MAX+1);i++)
366
old_uris[i]=nslcd_cfg->ldc_uris[i].uri;
367
nslcd_cfg->ldc_uris[i].uri=NULL;
371
nslcd_cfg->ldc_uris[i++].uri="ldapi://%2fdev%2fnull/";
372
nslcd_cfg->ldc_uris[i++].uri="ldap://10.10.10.10/";
373
nslcd_cfg->ldc_uris[i++].uri="ldapi://%2fdev%2fnonexistent/";
374
nslcd_cfg->ldc_uris[i++].uri="ldap://nosuchhost/";
375
nslcd_cfg->ldc_uris[i++].uri=NULL;
376
/* initialize session */
377
printf("test_myldap: test_connections(): getting session...\n");
378
session=myldap_create_session();
379
assert(session!=NULL);
381
printf("test_myldap: test_connections(): doing search...\n");
382
search=myldap_search(session,nslcd_cfg->ldc_bases[0],
384
"(objectclass=posixAccount)",
386
assert(search==NULL);
388
myldap_session_close(session);
389
/* restore the old URIs */
390
for (i=0;i<(NSS_LDAP_CONFIG_URI_MAX+1);i++)
391
nslcd_cfg->ldc_uris[i].uri=old_uris[i];
394
/* the main program... */
395
int main(int argc,char *argv[])
399
struct sigaction act;
400
/* build the name of the file */
401
srcdir=getenv("srcdir");
404
snprintf(fname,sizeof(fname),"%s/nslcd-test.conf",srcdir);
405
fname[sizeof(fname)-1]='\0';
406
/* initialize configuration */
408
/* partially initialize logging */
409
log_setdefaultloglevel(LOG_DEBUG);
411
memset(&act,0,sizeof(struct sigaction));
412
act.sa_handler=SIG_IGN;
413
sigemptyset(&act.sa_mask);
414
act.sa_flags=SA_RESTART|SA_NOCLDSTOP;
415
assert(sigaction(SIGPIPE,&act,NULL)==0);
420
test_get_rdnvalues();