#!/bin/sh
# =============================================================================
# autopkgtest "ldap": self-test of the ldap module (a private slapd instance).
#
# Needs root only because slapd then drops privileges to the openldap user,
# exactly like in production; the module also works unprivileged.  The system
# slapd instance, /etc/ldap and /var/lib/ldap are never touched.
#
# THIS IS THE TEMPLATE FOR TESTING SOFTWARE THAT TALKS TO LDAP (sudo, sssd,
# nslcd, web applications, ...):
#   1. fx_ldap_start with your LDIF files and the options you need;
#   2. run your software against $LDAP_URI (or LDAPCONF / LDAPTLS_CACERT for
#      libldap clients; write $LDAP_CACERT into config files for others);
#   3. do nothing else, teardown stops slapd and deletes its data.
#
# HOW TO ADAPT THIS FILE
#  * Put your LDIFs in debian/tests/data and pass them with --ldif, parents
#    before children.  The suffix entry itself is generated unless you give
#    --no-base.
#  * Extra schemas: --schema NAME (from /etc/ldap/schema) or --schema FILE.
#  * Run from a checkout:  FX_LIBDIR=$PWD/lib sh debian/tests/ldap
# =============================================================================
set -eu

LIB="${FX_LIBDIR:-/usr/share/autopkgtest-helper}"
export FX_LIBDIR="$LIB"
. "$LIB/fixtures.sh"
fx_init
# Make the fx_ldap_* functions available.
fx_load ldap

# autopkgtest runs tests with the source tree as the current directory.
DATA="$(pwd)/debian/tests/data"

# ---------------------------------------------------------------------------
fx_step "plain ldap://, default suffix"
# Default suffix is dc=example,dc=org, admin DN cn=admin,<suffix>, password
# "secret".  people.ldif is loaded offline with slapadd BEFORE slapd starts,
# which is faster and more deterministic than ldapadd against a live server.
fx_ldap_start --ldif "$DATA/people.ldif"

# fx_ldap_search runs ldapsearch as the admin DN below $LDAP_BASE.
fx_ldap_search '(uid=alice)' dn >"$FX_ROOT/out"
fx_assert_contains "$FX_ROOT/out" "uid=alice,ou=people,dc=example,dc=org"

# A user bind with the correct password must work ...
ldapwhoami -x -H "$LDAP_URI" -D "uid=alice,ou=people,$LDAP_BASE" -w alicepw >"$FX_ROOT/out"
fx_assert_contains "$FX_ROOT/out" "uid=alice,ou=people"
# ... and with a wrong password it must not (otherwise the test proves nothing).
if ldapwhoami -x -H "$LDAP_URI" -D "uid=alice,ou=people,$LDAP_BASE" -w wrong >/dev/null 2>&1; then
    fx_fail "bind with wrong password succeeded"
fi

# Entries can also be added while the server is running.
fx_ldap_add "$DATA/bob.ldif"
fx_ldap_search '(uid=bob)' dn >"$FX_ROOT/out"
fx_assert_contains "$FX_ROOT/out" "uid=bob"

# Early stop; normally you do not need this, teardown does it.
pid=$(cat "$FX_RUNDIR/slapd.pid")
fx_ldap_stop
fx_alive "$pid" && fx_fail "slapd still running after fx_ldap_stop"

# ---------------------------------------------------------------------------
fx_step "second start: ldaps:// with generated certificate, fixed suffix"
# The library keeps its state in global variables, so a second, differently
# configured instance is started from a child shell.  Escaped variables
# (\$LDAP_URI) are expanded by the child, unescaped ones by this script.
cat >"$FX_ROOT/tls.sh" <<SUB
set -eu
. "$LIB/fixtures.sh"
fx_init
fx_load ldap
# --tls creates a CA and a server certificate (valid for 3 days) and makes
# slapd listen on ldaps://.  Clients trust the CA through LDAPTLS_CACERT,
# which fx_ldap_start exports.
fx_ldap_start --tls --suffix dc=test,dc=invalid --ldif "$DATA/people-test.ldif"
case \$LDAP_URI in ldaps://*) ;; *) fx_fail "not an ldaps URI: \$LDAP_URI" ;; esac
ldapwhoami -x -H "\$LDAP_URI" -D "uid=carol,ou=people,dc=test,dc=invalid" -w carolpw
# Negative check: the connection must fail when the CA is not trusted, so we
# know the successful bind above really was verified TLS.
if LDAPTLS_CACERT=/nonexistent ldapwhoami -x -H "\$LDAP_URI" >/dev/null 2>&1; then
    fx_fail "TLS connection worked without the CA"
fi
# Record the pid of the child's slapd in OUR directory ($FX_ROOT is expanded
# by this script when the here document is written) so that we can check it
# after the child has exited.
cp "\$FX_RUNDIR/slapd.pid" "$FX_ROOT/slapd2.pid"
SUB
sh "$FX_ROOT/tls.sh"
# The child's teardown must have stopped ITS slapd.  Check that exact pid, not
# "any process called slapd": testbeds commonly run a system slapd instance
# (started by the slapd package's postinst) which is none of our business.
fx_alive "$(cat "$FX_ROOT/slapd2.pid")" && fx_fail "slapd left behind by sub-test"

echo "ldap: all tests passed"
