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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
/*
test_myldap.c - simple test for the myldap module
This file is part of the nss-pam-ldapd library.
Copyright (C) 2007-2014 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
#include "common.h"
#include "nslcd/log.h"
#include "nslcd/cfg.h"
#include "nslcd/myldap.h"
struct worker_args {
int id;
};
/* the maxium number of results to print (all results are retrieved) */
#define MAXRESULTS 10
/* This is a very basic search test, it performs a test to get certain
entries from the database. It currently just prints out the DNs for
the entries. */
static void test_search(void)
{
MYLDAP_SESSION *session;
MYLDAP_SEARCH *search;
MYLDAP_ENTRY *entry;
const char *attrs[] = { "uid", "cn", "gid", NULL };
int i;
int rc;
/* initialize session */
printf("test_myldap: test_search(): getting session...\n");
session = myldap_create_session();
assert(session != NULL);
/* perform search */
printf("test_myldap: test_search(): doing search...\n");
search = myldap_search(session, nslcd_cfg->bases[0], LDAP_SCOPE_SUBTREE,
"(objectclass=posixAccount)", attrs, NULL);
assert(search != NULL);
/* go over results */
printf("test_myldap: test_search(): get results...\n");
for (i = 0; (entry = myldap_get_entry(search, &rc)) != NULL; i++)
{
if (i < MAXRESULTS)
printf("test_myldap: test_search(): [%d] DN %s\n",
i, myldap_get_dn(entry));
else if (i == MAXRESULTS)
printf("test_myldap: test_search(): ...\n");
}
printf("test_myldap: test_search(): %d entries returned: %s\n",
i, ldap_err2string(rc));
assert(rc == LDAP_SUCCESS);
/* perform another search */
printf("test_myldap: test_search(): doing search...\n");
search = myldap_search(session, nslcd_cfg->bases[0], LDAP_SCOPE_SUBTREE,
"(objectclass=posixGroup)", attrs, NULL);
assert(search != NULL);
/* go over results */
printf("test_myldap: test_search(): get results...\n");
for (i = 0; (entry = myldap_get_entry(search, &rc)) != NULL; i++)
{
if (i < MAXRESULTS)
printf("test_myldap: test_search(): [%d] DN %s\n",
i, myldap_get_dn(entry));
else if (i == MAXRESULTS)
printf("test_myldap: test_search(): ...\n");
}
printf("test_myldap: test_search(): %d entries returned: %s\n",
i, ldap_err2string(rc));
assert(rc == LDAP_SUCCESS);
/* clean up */
myldap_session_close(session);
}
static void test_get(void)
{
MYLDAP_SESSION *session;
MYLDAP_SEARCH *search1, *search2;
MYLDAP_ENTRY *entry;
const char *attrs1[] = { "cn", "userPassword", "memberUid", "gidNumber", "member", NULL };
const char *attrs2[] = { "uid", NULL };
int rc;
/* initialize session */
printf("test_myldap: test_get(): getting session...\n");
session = myldap_create_session();
assert(session != NULL);
/* perform search */
printf("test_myldap: test_get(): doing search...\n");
search1 = myldap_search(session, nslcd_cfg->bases[0], LDAP_SCOPE_SUBTREE,
"(&(|(objectClass=posixGroup)(objectClass=groupOfNames))(cn=testgroup2))",
attrs1, NULL);
assert(search1 != NULL);
/* get one entry */
entry = myldap_get_entry(search1, &rc);
assert(entry != NULL);
printf("test_myldap: test_get(): got DN %s\n", myldap_get_dn(entry));
/* get some attribute values */
assert(myldap_get_values(entry, "gidNumber") != NULL);
assert(myldap_get_values(entry, "memberUid") == NULL);
assert(myldap_get_values(entry, "member") != NULL);
/* perform another search */
printf("test_myldap: test_get(): doing get...\n");
search2 = myldap_search(session, "cn=Test User2,ou=people,dc=test,dc=tld",
LDAP_SCOPE_BASE,
"(objectclass=posixAccount)", attrs2, NULL);
assert(search2 != NULL);
/* get one entry */
entry = myldap_get_entry(search2, &rc);
assert(entry != NULL);
printf("test_myldap: test_get(): got DN %s\n", myldap_get_dn(entry));
/* test if searches are ok */
assert(myldap_get_entry(search1, &rc) == NULL);
assert(myldap_get_entry(search2, &rc) == NULL);
/* clean up */
myldap_session_close(session);
}
/* This search prints a number of attributes from a search */
static void test_get_values(void)
{
MYLDAP_SESSION *session;
MYLDAP_SEARCH *search;
MYLDAP_ENTRY *entry;
const char *attrs[] = { "uidNumber", "cn", "gidNumber", "uid", "objectClass", NULL };
const char **vals;
const char *rdnval;
int i;
/* initialize session */
printf("test_myldap: test_get_values(): getting session...\n");
session = myldap_create_session();
assert(session != NULL);
/* perform search */
search = myldap_search(session, nslcd_cfg->bases[0], LDAP_SCOPE_SUBTREE,
"(&(objectClass=posixAccount)(uid=*))", attrs, NULL);
assert(search != NULL);
/* go over results */
for (i = 0; (entry = myldap_get_entry(search, NULL)) != NULL; i++)
{
if (i < MAXRESULTS)
printf("test_myldap: test_get_values(): [%d] DN %s\n",
i, myldap_get_dn(entry));
else if (i == MAXRESULTS)
printf("test_myldap: test_get_values(): ...\n");
/* try to get uid from attribute */
vals = myldap_get_values(entry, "uidNumber");
assert((vals != NULL) && (vals[0] != NULL));
if (i < MAXRESULTS)
printf("test_myldap: test_get_values(): [%d] uidNumber=%s\n",
i, vals[0]);
/* try to get gid from attribute */
vals = myldap_get_values(entry, "gidNumber");
assert((vals != NULL) && (vals[0] != NULL));
if (i < MAXRESULTS)
printf("test_myldap: test_get_values(): [%d] gidNumber=%s\n",
i, vals[0]);
/* write LDF_STRING(PASSWD_NAME) */
vals = myldap_get_values(entry, "uid");
assert((vals != NULL) && (vals[0] != NULL));
if (i < MAXRESULTS)
printf("test_myldap: test_get_values(): [%d] uid=%s\n", i, vals[0]);
/* get rdn values */
rdnval = myldap_get_rdn_value(entry, "cn");
if (i < MAXRESULTS)
printf("test_myldap: test_get_values(): [%d] cdrdn=%s\n",
i, rdnval == NULL ? "NULL" : rdnval);
rdnval = myldap_get_rdn_value(entry, "uid");
if (i < MAXRESULTS)
printf("test_myldap: test_get_values(): [%d] uidrdn=%s\n",
i, rdnval == NULL ? "NULL" : rdnval);
/* check objectclass */
assert(myldap_has_objectclass(entry, "posixAccount"));
}
/* clean up */
myldap_session_close(session);
}
static void test_get_rdnvalues(void)
{
MYLDAP_SESSION *session;
MYLDAP_SEARCH *search;
MYLDAP_ENTRY *entry;
const char *attrs[] = { "cn", "uid", NULL };
int rc;
char buf[80];
const char *rdnval;
/* initialize session */
printf("test_myldap: test_get_rdnvalues(): getting session...\n");
session = myldap_create_session();
assert(session != NULL);
/* perform search */
printf("test_myldap: test_get_rdnvalues(): doing search...\n");
search = myldap_search(session, "cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld",
LDAP_SCOPE_BASE, "(objectClass=*)", attrs, NULL);
assert(search != NULL);
/* get one entry */
entry = myldap_get_entry(search, &rc);
assert(entry != NULL);
printf("test_myldap: test_get_rdnvalues(): got DN %s\n",
myldap_get_dn(entry));
/* get some values from DN */
rdnval = myldap_get_rdn_value(entry, "uid");
printf("test_myldap: test_get_rdnvalues(): DN.uid=%s\n",
rdnval == NULL ? "NULL" : rdnval);
rdnval = myldap_get_rdn_value(entry, "cn");
printf("test_myldap: test_get_rdnvalues(): DN.cn=%s\n",
rdnval == NULL ? "NULL" : rdnval);
rdnval = myldap_get_rdn_value(entry, "uidNumber");
printf("test_myldap: test_get_rdnvalues(): DN.uidNumber=%s\n",
rdnval == NULL ? "NULL" : rdnval);
/* clean up */
myldap_session_close(session);
/* some tests */
rdnval = myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld",
"uid", buf, sizeof(buf));
printf("test_myldap: test_get_rdnvalues(): DN.uid=%s\n",
rdnval == NULL ? "NULL" : rdnval);
rdnval = myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld",
"cn", buf, sizeof(buf));
printf("test_myldap: test_get_rdnvalues(): DN.cn=%s\n",
rdnval == NULL ? "NULL" : rdnval);
rdnval = myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld",
"uidNumber", buf, sizeof(buf));
printf("test_myldap: test_get_rdnvalues(): DN.uidNumber=%s\n",
rdnval == NULL ? "NULL" : rdnval);
}
/* this method tests to see if we can perform two searches within
one session */
static void test_two_searches(void)
{
MYLDAP_SESSION *session;
MYLDAP_SEARCH *search1, *search2;
MYLDAP_ENTRY *entry;
const char *attrs[] = { "uidNumber", "cn", "gidNumber", "uid", "objectClass", NULL };
const char **vals;
/* initialize session */
printf("test_myldap: test_two_searches(): getting session...\n");
session = myldap_create_session();
assert(session != NULL);
/* perform search1 */
search1 = myldap_search(session, nslcd_cfg->bases[0], LDAP_SCOPE_SUBTREE,
"(&(objectClass=posixAccount)(uid=*))",
attrs, NULL);
assert(search1 != NULL);
/* get a result from search1 */
entry = myldap_get_entry(search1, NULL);
assert(entry != NULL);
printf("test_myldap: test_two_searches(): [search1] DN %s\n",
myldap_get_dn(entry));
vals = myldap_get_values(entry, "cn");
assert((vals != NULL) && (vals[0] != NULL));
printf("test_myldap: test_two_searches(): [search1] cn=%s\n", vals[0]);
/* start a second search */
search2 = myldap_search(session, nslcd_cfg->bases[0], LDAP_SCOPE_SUBTREE,
"(&(objectclass=posixGroup)(gidNumber=*))",
attrs, NULL);
assert(search2 != NULL);
/* get a result from search2 */
entry = myldap_get_entry(search2, NULL);
assert(entry != NULL);
printf("test_myldap: test_two_searches(): [search2] DN %s\n",
myldap_get_dn(entry));
vals = myldap_get_values(entry, "cn");
assert((vals != NULL) && (vals[0] != NULL));
printf("test_myldap: test_two_searches(): [search2] cn=%s\n", vals[0]);
/* get another result from search1 */
entry = myldap_get_entry(search1, NULL);
assert(entry != NULL);
printf("test_myldap: test_two_searches(): [search1] DN %s\n",
myldap_get_dn(entry));
vals = myldap_get_values(entry, "cn");
assert((vals != NULL) && (vals[0] != NULL));
printf("test_myldap: test_two_searches(): [search1] cn=%s\n", vals[0]);
/* stop search1 */
myldap_search_close(search1);
/* get another result from search2 */
entry = myldap_get_entry(search2, NULL);
assert(entry != NULL);
printf("test_myldap: test_two_searches(): [search2] DN %s\n",
myldap_get_dn(entry));
vals = myldap_get_values(entry, "cn");
assert((vals != NULL) && (vals[0] != NULL));
printf("test_myldap: test_two_searches(): [search2] cn=%s\n", vals[0]);
/* clean up */
myldap_session_close(session);
}
/* perform a simple search */
static void *worker(void *arg)
{
MYLDAP_SESSION *session;
MYLDAP_SEARCH *search;
MYLDAP_ENTRY *entry;
const char *attrs[] = { "uid", "cn", "gid", NULL };
struct worker_args *args = (struct worker_args *)arg;
int i;
int rc;
/* initialize session */
session = myldap_create_session();
assert(session != NULL);
/* perform search */
search = myldap_search(session, nslcd_cfg->bases[0], LDAP_SCOPE_SUBTREE,
"(objectclass=posixAccount)", attrs, NULL);
assert(search != NULL);
/* go over results */
for (i = 0; (entry = myldap_get_entry(search, &rc)) != NULL; i++)
{
if (i < MAXRESULTS)
printf("test_myldap: test_threads(): [worker %d] [%d] DN %s\n",
args->id, i, myldap_get_dn(entry));
else if (i == MAXRESULTS)
printf("test_myldap: test_threads(): [worker %d] ...\n", args->id);
}
printf("test_myldap: test_threads(): [worker %d] DONE: %s\n",
args->id, ldap_err2string(rc));
assert(rc == LDAP_SUCCESS);
/* clean up */
myldap_session_close(session);
return 0;
}
/* thread ids of all running threads */
#define NUM_THREADS 5
pthread_t my_threads[NUM_THREADS];
static void test_threads(void)
{
int i;
struct worker_args args[NUM_THREADS];
/* start worker threads */
for (i = 0; i < NUM_THREADS; i++)
{
args[i].id = i;
assert(pthread_create(&my_threads[i], NULL, worker, &(args[i])) == 0);
}
/* wait for all threads to die */
for (i = 0; i < NUM_THREADS; i++)
{
assert(pthread_join(my_threads[i], NULL) == 0);
}
}
static void test_connections(void)
{
MYLDAP_SESSION *session;
MYLDAP_SEARCH *search;
const char *attrs[] = { "uid", "cn", "gid", NULL };
char *old_uris[NSS_LDAP_CONFIG_MAX_URIS + 1];
int i;
/* save the old URIs */
for (i = 0; i < (NSS_LDAP_CONFIG_MAX_URIS + 1); i++)
{
old_uris[i] = nslcd_cfg->uris[i].uri;
nslcd_cfg->uris[i].uri = NULL;
}
/* set new URIs */
i = 0;
nslcd_cfg->uris[i++].uri = "ldapi://%2fdev%2fnull/";
nslcd_cfg->uris[i++].uri = "ldap://10.10.10.10/";
nslcd_cfg->uris[i++].uri = "ldapi://%2fdev%2fnonexistent/";
nslcd_cfg->uris[i++].uri = "ldap://nosuchhost/";
nslcd_cfg->uris[i++].uri = NULL;
/* initialize session */
printf("test_myldap: test_connections(): getting session...\n");
session = myldap_create_session();
assert(session != NULL);
/* perform search */
printf("test_myldap: test_connections(): doing search...\n");
search = myldap_search(session, nslcd_cfg->bases[0], LDAP_SCOPE_SUBTREE,
"(objectclass=posixAccount)", attrs, NULL);
assert(search == NULL);
/* clean up */
myldap_session_close(session);
/* restore the old URIs */
for (i = 0; i < (NSS_LDAP_CONFIG_MAX_URIS + 1); i++)
nslcd_cfg->uris[i].uri = old_uris[i];
}
/* test whether myldap_escape() handles buffer overlows correctly */
static void test_escape(void)
{
char buffer[1024];
assert(myldap_escape("test", buffer, 4) != 0);
assert(myldap_escape("t*st", buffer, 5) != 0);
assert(myldap_escape("t*st", buffer, 20) == 0);
assertstreq(buffer, "t\\2ast");
}
/* the main program... */
int main(int UNUSED(argc), char UNUSED(*argv[]))
{
char *srcdir;
char fname[100];
struct sigaction act;
/* build the name of the file */
srcdir = getenv("srcdir");
if (srcdir == NULL)
srcdir = ".";
snprintf(fname, sizeof(fname), "%s/nslcd-test.conf", srcdir);
fname[sizeof(fname) - 1] = '\0';
/* initialize configuration */
cfg_init(fname);
/* partially initialize logging */
log_setdefaultloglevel(LOG_DEBUG);
/* ignore SIGPIPE */
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART | SA_NOCLDSTOP;
assert(sigaction(SIGPIPE, &act, NULL) == 0);
/* do tests */
test_search();
test_get();
test_get_values();
test_get_rdnvalues();
test_two_searches();
test_threads();
test_connections();
test_escape();
return 0;
}
|