Blame SOURCES/cyrus-sasl-2.1.27-Add-basic-test-infrastructure.patch

b0654d
From 82e299e970461c153a036bb1fbc84e808f926e12 Mon Sep 17 00:00:00 2001
b0654d
From: Simo Sorce <simo@redhat.com>
b0654d
Date: Tue, 5 May 2020 14:06:57 -0400
b0654d
Subject: [PATCH] Add basic test infrastructure
b0654d
b0654d
First test is for SASL/GSSAPI
b0654d
b0654d
Backport of upstream commit id:
b0654d
18ff41d5d18f61c2ded7235dad1d9618aa84784b
b0654d
b0654d
Signed-off-by: Simo Sorce <simo@redhat.com>
b0654d
---
b0654d
 Makefile.am          |   2 +-
b0654d
 configure.ac         |   3 +-
b0654d
 tests/Makefile.am    |  79 +++++++++++++++++++
b0654d
 tests/runtests.py    | 179 +++++++++++++++++++++++++++++++++++++++++++
b0654d
 tests/t_common.c     |  68 ++++++++++++++++
b0654d
 tests/t_common.h     |  15 ++++
b0654d
 tests/t_gssapi_cli.c |  95 +++++++++++++++++++++++
b0654d
 tests/t_gssapi_srv.c | 111 +++++++++++++++++++++++++++
b0654d
 8 files changed, 550 insertions(+), 2 deletions(-)
b0654d
 create mode 100644 tests/Makefile.am
b0654d
 create mode 100755 tests/runtests.py
b0654d
 create mode 100644 tests/t_common.c
b0654d
 create mode 100644 tests/t_common.h
b0654d
 create mode 100644 tests/t_gssapi_cli.c
b0654d
 create mode 100644 tests/t_gssapi_srv.c
b0654d
b0654d
diff --git a/Makefile.am b/Makefile.am
b0654d
index 83dae6f..fc24509 100644
b0654d
--- a/Makefile.am
b0654d
+++ b/Makefile.am
b0654d
@@ -70,7 +70,7 @@ else
b0654d
 INSTALLOSX = 
b0654d
 endif
b0654d
 
b0654d
-SUBDIRS=include sasldb common lib plugins utils $(PWC) $(SAM) $(JAV) $(SAD)
b0654d
+SUBDIRS=include sasldb common lib plugins utils $(PWC) $(SAM) $(JAV) $(SAD) tests
b0654d
 EXTRA_DIST=config doc docsrc win32 mac dlcompat-20010505 NTMakefile \
b0654d
     INSTALL.TXT libsasl2.pc.in
b0654d
 
b0654d
diff --git a/configure.ac b/configure.ac
b0654d
index ca5936a..c1d2182 100644
b0654d
--- a/configure.ac
b0654d
+++ b/configure.ac
b0654d
@@ -1575,7 +1575,8 @@ java/javax/Makefile
b0654d
 java/javax/security/Makefile
b0654d
 java/javax/security/auth/Makefile
b0654d
 java/javax/security/auth/callback/Makefile
b0654d
-pwcheck/Makefile)
b0654d
+pwcheck/Makefile
b0654d
+tests/Makefile)
b0654d
 
b0654d
 AC_MSG_NOTICE([
b0654d
 
b0654d
diff --git a/tests/Makefile.am b/tests/Makefile.am
b0654d
new file mode 100644
b0654d
index 0000000..1edf010
b0654d
--- /dev/null
b0654d
+++ b/tests/Makefile.am
b0654d
@@ -0,0 +1,79 @@
b0654d
+# Makefile.am -- automake input for cyrus-sasl tests
b0654d
+# Simo Sorce
b0654d
+#
b0654d
+################################################################
b0654d
+# Copyright (c) 2000 Carnegie Mellon University.  All rights reserved.
b0654d
+#
b0654d
+# Redistribution and use in source and binary forms, with or without
b0654d
+# modification, are permitted provided that the following conditions
b0654d
+# are met:
b0654d
+#
b0654d
+# 1. Redistributions of source code must retain the above copyright
b0654d
+#    notice, this list of conditions and the following disclaimer.
b0654d
+#
b0654d
+# 2. Redistributions in binary form must reproduce the above copyright
b0654d
+#    notice, this list of conditions and the following disclaimer in
b0654d
+#    the documentation and/or other materials provided with the
b0654d
+#    distribution.
b0654d
+#
b0654d
+# 3. The name "Carnegie Mellon University" must not be used to
b0654d
+#    endorse or promote products derived from this software without
b0654d
+#    prior written permission. For permission or any other legal
b0654d
+#    details, please contact
b0654d
+#      Office of Technology Transfer
b0654d
+#      Carnegie Mellon University
b0654d
+#      5000 Forbes Avenue
b0654d
+#      Pittsburgh, PA  15213-3890
b0654d
+#      (412) 268-4387, fax: (412) 268-7395
b0654d
+#      tech-transfer@andrew.cmu.edu
b0654d
+#
b0654d
+# 4. Redistributions of any form whatsoever must retain the following
b0654d
+#    acknowledgment:
b0654d
+#    "This product includes software developed by Computing Services
b0654d
+#     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
b0654d
+#
b0654d
+# CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
b0654d
+# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
b0654d
+# AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
b0654d
+# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
b0654d
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
b0654d
+# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
b0654d
+# OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
b0654d
+#
b0654d
+################################################################
b0654d
+
b0654d
+AM_CPPFLAGS=-I$(top_srcdir)/include -DPLUGINDIR='"${top_srcdir}/plugins/.libs"'
b0654d
+
b0654d
+COMMON_LDADD = ../lib/libsasl2.la $(GSSAPIBASE_LIBS) $(GSSAPI_LIBS) $(LIB_SOCKET)
b0654d
+
b0654d
+t_gssapi_cli_SOURCES = \
b0654d
+	t_common.c \
b0654d
+	t_gssapi_cli.c
b0654d
+
b0654d
+t_gssapi_cli_LDADD = $(COMMON_LDADD)
b0654d
+
b0654d
+t_gssapi_srv_SOURCES = \
b0654d
+	t_common.c \
b0654d
+	t_gssapi_srv.c
b0654d
+
b0654d
+t_gssapi_srv_LDADD = $(COMMON_LDADD)
b0654d
+
b0654d
+check_PROGRAMS = \
b0654d
+	t_gssapi_cli \
b0654d
+	t_gssapi_srv \
b0654d
+	$(NULL)
b0654d
+
b0654d
+noinst_PROGRAMS = $(check_PROGRAMS)
b0654d
+
b0654d
+EXTRA_DIST = \
b0654d
+	runtests.py \
b0654d
+	$(NULL)
b0654d
+
b0654d
+all: $(check_PROGRAMS)
b0654d
+
b0654d
+check:
b0654d
+if MACOSX
b0654d
+# skip Mac OSX for now
b0654d
+else
b0654d
+	$(srcdir)/runtests.py $(CHECKARGS)
b0654d
+endif
b0654d
diff --git a/tests/runtests.py b/tests/runtests.py
b0654d
new file mode 100755
b0654d
index 0000000..f645adf
b0654d
--- /dev/null
b0654d
+++ b/tests/runtests.py
b0654d
@@ -0,0 +1,179 @@
b0654d
+#!/usr/bin/python3
b0654d
+
b0654d
+import argparse
b0654d
+import os
b0654d
+import shutil
b0654d
+import signal
b0654d
+import subprocess
b0654d
+import time
b0654d
+from string import Template
b0654d
+
b0654d
+
b0654d
+def setup_socket_wrappers(testdir):
b0654d
+    """ Try to set up socket wrappers """
b0654d
+    wrapdir = os.path.join(testdir, 'w')
b0654d
+    os.makedirs(wrapdir)
b0654d
+
b0654d
+    wrappers = subprocess.Popen(['pkg-config', '--exists', 'socket_wrapper'])
b0654d
+    wrappers.wait()
b0654d
+    if wrappers.returncode != 0:
b0654d
+        raise Exception('Socket Wrappers not available')
b0654d
+
b0654d
+    wrappers = subprocess.Popen(['pkg-config', '--exists', 'nss_wrapper'])
b0654d
+    wrappers.wait()
b0654d
+    if wrappers.returncode != 0:
b0654d
+        raise Exception('NSS Wrappers not available')
b0654d
+
b0654d
+    hosts = os.path.join(wrapdir, 'hosts')
b0654d
+    with open(hosts, 'w+') as conffile:
b0654d
+        conffile.write('127.0.0.9 host.realm.test')
b0654d
+
b0654d
+    return {'LD_PRELOAD': 'libsocket_wrapper.so libnss_wrapper.so',
b0654d
+            'SOCKET_WRAPPER_DIR': wrapdir,
b0654d
+            'SOCKET_WRAPPER_DEFAULT_IFACE': '9',
b0654d
+            'NSS_WRAPPER_HOSTNAME': 'host.realm.test',
b0654d
+            'NSS_WRAPPER_HOSTS': hosts}
b0654d
+
b0654d
+
b0654d
+KERBEROS_CONF = '''
b0654d
+[libdefaults]
b0654d
+  default_realm = REALM.TEST
b0654d
+  dns_lookup_realm = false
b0654d
+  dns_lookup_kdc = false
b0654d
+  rdns = false
b0654d
+  ticket_lifetime = 24h
b0654d
+  forwardable = yes
b0654d
+  default_ccache_name = FILE://${TESTDIR}/ccache
b0654d
+  udp_preference_limit = 1
b0654d
+
b0654d
+[domain_realm]
b0654d
+  .realm.test = REALM.TEST
b0654d
+  realm.test = REALM.TEST
b0654d
+
b0654d
+[realms]
b0654d
+ REALM.TEST = {
b0654d
+  kdc = 127.0.0.9
b0654d
+  admin_server = 127.0.0.9
b0654d
+  acl_file = ${TESTDIR}/kadm.acl
b0654d
+  dict_file = /usr/share/dict/words
b0654d
+  admin_keytab = ${TESTDIR}/kadm.keytab
b0654d
+  database_name = ${TESTDIR}/kdc.db
b0654d
+  key_stash_file = ${TESTDIR}/kdc.stash
b0654d
+ }
b0654d
+
b0654d
+[kdcdefaults]
b0654d
+ kdc_ports = 88
b0654d
+ kdc_tcp_ports = 88
b0654d
+
b0654d
+[logging]
b0654d
+  kdc = FILE:${TESTDIR}/kdc.log
b0654d
+  admin_server = FILE:${TESTDIR}/kadm.log
b0654d
+  default = FILE:${TESTDIR}/krb5.log
b0654d
+'''
b0654d
+
b0654d
+
b0654d
+def setup_kdc(testdir, env):
b0654d
+    """ Setup KDC and start process """
b0654d
+    krbconf = os.path.join(testdir, 'krb.conf')
b0654d
+    env['KRB5_CONFIG'] = krbconf
b0654d
+
b0654d
+    kenv = {'KRB5_KDC_PROFILE': krbconf,
b0654d
+            'PATH': '/sbin:/bin:/usr/sbin:/usr/bin'}
b0654d
+    kenv.update(env)
b0654d
+
b0654d
+    # KDC/KRB5 CONFIG
b0654d
+    templ = Template(KERBEROS_CONF)
b0654d
+    text = templ.substitute({'TESTDIR': testdir})
b0654d
+    with open(krbconf, 'w+') as conffile:
b0654d
+        conffile.write(text)
b0654d
+
b0654d
+    testlog = os.path.join(testdir, 'kdc.log')
b0654d
+    log = open(testlog, 'a')
b0654d
+
b0654d
+    subprocess.check_call([
b0654d
+        "kdb5_util", "create",
b0654d
+        "-r", "REALM.TEST", "-s", "-P", "password"
b0654d
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
b0654d
+
b0654d
+    kdc = subprocess.Popen(['krb5kdc', '-n'], env=kenv, preexec_fn=os.setsid)
b0654d
+    time.sleep(5)
b0654d
+
b0654d
+    # Add a user and genrate a keytab
b0654d
+    keytab = os.path.join(testdir, "user.keytab")
b0654d
+    subprocess.check_call([
b0654d
+        "kadmin.local", "-q",
b0654d
+        "addprinc -randkey user"
b0654d
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
b0654d
+
b0654d
+    subprocess.check_call([
b0654d
+        "kadmin.local", "-q",
b0654d
+        "ktadd -k {} user".format(keytab)
b0654d
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
b0654d
+    env['KRB5_CLIENT_KTNAME'] = keytab
b0654d
+
b0654d
+    # Add a service and genrate a keytab
b0654d
+    keytab = os.path.join(testdir, "test.keytab")
b0654d
+    subprocess.check_call([
b0654d
+        "kadmin.local", "-q",
b0654d
+        "addprinc -randkey test/host.realm.test"
b0654d
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
b0654d
+
b0654d
+    subprocess.check_call([
b0654d
+        "kadmin.local", "-q",
b0654d
+        "ktadd -k {} test/host.realm.test".format(keytab)
b0654d
+        ], stdout=log, stderr=log, env=kenv, timeout=5)
b0654d
+    env['KRB5_KTNAME'] = keytab
b0654d
+
b0654d
+    return kdc, env
b0654d
+
b0654d
+
b0654d
+def gssapi_tests(testdir):
b0654d
+    """ SASL/GSSAPI Tests """
b0654d
+    env = setup_socket_wrappers(testdir)
b0654d
+    kdc, kenv = setup_kdc(testdir, env)
b0654d
+    #print("KDC: {}, ENV: {}".format(kdc, kenv))
b0654d
+    kenv['KRB5_TRACE'] = os.path.join(testdir, 'trace.log')
b0654d
+
b0654d
+    try:
b0654d
+        srv = subprocess.Popen(["../tests/t_gssapi_srv"],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        srv.stdout.readline() # Wait for srv to say it is ready
b0654d
+        cli = subprocess.Popen(["../tests/t_gssapi_cli"],
b0654d
+                               stdout=subprocess.PIPE,
b0654d
+                               stderr=subprocess.PIPE, env=kenv)
b0654d
+        try:
b0654d
+            cli.wait(timeout=5)
b0654d
+            srv.wait(timeout=5)
b0654d
+        except Exception as e:
b0654d
+            print("Failed on {}".format(e));
b0654d
+            cli.kill()
b0654d
+            srv.kill()
b0654d
+        if cli.returncode != 0 or srv.returncode != 0:
b0654d
+            raise Exception("CLI ({}): {} --> SRV ({}): {}".format(
b0654d
+                cli.returncode, cli.stderr.read().decode('utf-8'),
b0654d
+                srv.returncode, srv.stderr.read().decode('utf-8')))
b0654d
+    except Exception as e:
b0654d
+        print("FAIL: {}".format(e))
b0654d
+
b0654d
+    print("PASS: CLI({}) SRV({})".format(
b0654d
+        cli.stdout.read().decode('utf-8').strip(),
b0654d
+        srv.stdout.read().decode('utf-8').strip()))
b0654d
+
b0654d
+    os.killpg(kdc.pid, signal.SIGTERM)
b0654d
+
b0654d
+
b0654d
+if __name__ == "__main__":
b0654d
+
b0654d
+    P = argparse.ArgumentParser(description='Cyrus SASL Tests')
b0654d
+    P.add_argument('--testdir', default=os.path.join(os.getcwd(), '.tests'),
b0654d
+                   help="Directory for running tests")
b0654d
+    A = vars(P.parse_args())
b0654d
+
b0654d
+    T = A['testdir']
b0654d
+
b0654d
+    if os.path.exists(T):
b0654d
+        shutil.rmtree(T)
b0654d
+    os.makedirs(T)
b0654d
+
b0654d
+    gssapi_tests(T)
b0654d
diff --git a/tests/t_common.c b/tests/t_common.c
b0654d
new file mode 100644
b0654d
index 0000000..7168b2f
b0654d
--- /dev/null
b0654d
+++ b/tests/t_common.c
b0654d
@@ -0,0 +1,68 @@
b0654d
+/* TBD, add (C) */
b0654d
+
b0654d
+#include <t_common.h>
b0654d
+
b0654d
+void s_error(const char *hdr, ssize_t ret, ssize_t len, int err)
b0654d
+{
b0654d
+    fprintf(stderr, "%s l:%ld/%ld [%d] %s",
b0654d
+            hdr, ret, len, err, strerror(err));
b0654d
+    exit(-1);
b0654d
+}
b0654d
+
b0654d
+void send_string(int sd, const char *s, unsigned int l)
b0654d
+{
b0654d
+    ssize_t ret;
b0654d
+
b0654d
+fprintf(stderr, "s:%u ", l);
b0654d
+fflush(stderr);
b0654d
+
b0654d
+    ret = send(sd, &l, sizeof(l), 0);
b0654d
+    if (ret != sizeof(l)) s_error("send size", ret, sizeof(l), errno);
b0654d
+
b0654d
+    if (l == 0) return;
b0654d
+
b0654d
+    ret = send(sd, s, l, 0);
b0654d
+    if (ret != l) s_error("send data", ret, l, errno);
b0654d
+}
b0654d
+
b0654d
+void recv_string(int sd, char *buf, unsigned int *buflen)
b0654d
+{
b0654d
+    unsigned int l;
b0654d
+    ssize_t ret;
b0654d
+
b0654d
+    ret = recv(sd, &l, sizeof(l), MSG_WAITALL);
b0654d
+    if (ret != sizeof(l)) s_error("recv size", ret, sizeof(l), errno);
b0654d
+
b0654d
+    if (l == 0) {
b0654d
+fprintf(stderr, "r:0 ");
b0654d
+fflush(stderr);
b0654d
+        *buflen = 0;
b0654d
+        return;
b0654d
+    }
b0654d
+
b0654d
+    if (*buflen < l) s_error("recv len", l, *buflen, E2BIG);
b0654d
+
b0654d
+    ret = recv(sd, buf, l, 0);
b0654d
+    if (ret != l) s_error("recv data", ret, l, errno);
b0654d
+
b0654d
+fprintf(stderr, "r:%ld ", ret);
b0654d
+fflush(stderr);
b0654d
+    *buflen = ret;
b0654d
+}
b0654d
+
b0654d
+void saslerr(int why, const char *what)
b0654d
+{
b0654d
+    fprintf(stderr, "%s: %s", what, sasl_errstring(why, NULL, NULL));
b0654d
+}
b0654d
+
b0654d
+int getpath(void *context __attribute__((unused)), const char **path)
b0654d
+{
b0654d
+    if (! path) {
b0654d
+        return SASL_BADPARAM;
b0654d
+    }
b0654d
+
b0654d
+    *path = PLUGINDIR;
b0654d
+    return SASL_OK;
b0654d
+}
b0654d
+
b0654d
+
b0654d
diff --git a/tests/t_common.h b/tests/t_common.h
b0654d
new file mode 100644
b0654d
index 0000000..4ee1976
b0654d
--- /dev/null
b0654d
+++ b/tests/t_common.h
b0654d
@@ -0,0 +1,15 @@
b0654d
+/* TBD, add (C) */
b0654d
+
b0654d
+#include "config.h"
b0654d
+
b0654d
+#include <errno.h>
b0654d
+#include <stdio.h>
b0654d
+#include <sys/socket.h>
b0654d
+
b0654d
+#include <sasl.h>
b0654d
+
b0654d
+void s_error(const char *hdr, ssize_t ret, ssize_t len, int err);
b0654d
+void send_string(int sd, const char *s, unsigned int l);
b0654d
+void recv_string(int sd, char *buf, unsigned int *buflen);
b0654d
+void saslerr(int why, const char *what);
b0654d
+int getpath(void *context __attribute__((unused)), const char **path);
b0654d
diff --git a/tests/t_gssapi_cli.c b/tests/t_gssapi_cli.c
b0654d
new file mode 100644
b0654d
index 0000000..c833c05
b0654d
--- /dev/null
b0654d
+++ b/tests/t_gssapi_cli.c
b0654d
@@ -0,0 +1,95 @@
b0654d
+/* TBD, add (C) */
b0654d
+
b0654d
+#include "t_common.h"
b0654d
+
b0654d
+#include <stdlib.h>
b0654d
+#include <stdarg.h>
b0654d
+#include <ctype.h>
b0654d
+#include <string.h>
b0654d
+
b0654d
+#ifdef HAVE_UNISTD_H
b0654d
+#include <unistd.h>
b0654d
+#endif
b0654d
+
b0654d
+#include <arpa/inet.h>
b0654d
+#include <saslplug.h>
b0654d
+
b0654d
+static int setup_socket(void)
b0654d
+{
b0654d
+    struct sockaddr_in addr;
b0654d
+    int sock, ret;
b0654d
+
b0654d
+    sock = socket(AF_INET, SOCK_STREAM, 0);
b0654d
+    if (sock < 0) s_error("socket", 0, 0, errno);
b0654d
+
b0654d
+    addr.sin_family = AF_INET;
b0654d
+    addr.sin_addr.s_addr = inet_addr("127.0.0.9");
b0654d
+    addr.sin_port = htons(9000);
b0654d
+
b0654d
+    ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
b0654d
+    if (ret != 0) s_error("connect", 0, 0, errno);
b0654d
+
b0654d
+    return sock;
b0654d
+}
b0654d
+
b0654d
+int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
b0654d
+{
b0654d
+    sasl_callback_t callbacks[2] = {};
b0654d
+    char buf[8192];
b0654d
+    const char *chosenmech;
b0654d
+    sasl_conn_t *conn;
b0654d
+    const char *data;
b0654d
+    unsigned int len;
b0654d
+    int sd;
b0654d
+    int r;
b0654d
+
b0654d
+    /* initialize the sasl library */
b0654d
+    callbacks[0].id = SASL_CB_GETPATH;
b0654d
+    callbacks[0].proc = (sasl_callback_ft)&getpath;
b0654d
+    callbacks[0].context = NULL;
b0654d
+    callbacks[1].id = SASL_CB_LIST_END;
b0654d
+    callbacks[1].proc = NULL;
b0654d
+    callbacks[1].context = NULL;
b0654d
+
b0654d
+    r = sasl_client_init(callbacks);
b0654d
+    if (r != SASL_OK) exit(-1);
b0654d
+
b0654d
+    r = sasl_client_new("test", "host.realm.test", NULL, NULL, NULL, 0, &conn;;
b0654d
+    if (r != SASL_OK) {
b0654d
+        saslerr(r, "allocating connection state");
b0654d
+        exit(-1);
b0654d
+    }
b0654d
+
b0654d
+    r = sasl_client_start(conn, "GSSAPI", NULL, &data, &len, &chosenmech);
b0654d
+    if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
+	saslerr(r, "starting SASL negotiation");
b0654d
+	printf("\n%s\n", sasl_errdetail(conn));
b0654d
+	exit(-1);
b0654d
+    }
b0654d
+
b0654d
+    sd = setup_socket();
b0654d
+
b0654d
+    while (r == SASL_CONTINUE) {
b0654d
+        send_string(sd, data, len);
b0654d
+        len = 8192;
b0654d
+        recv_string(sd, buf, &len;;
b0654d
+
b0654d
+	r = sasl_client_step(conn, buf, len, NULL, &data, &len;;
b0654d
+	if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
+	    saslerr(r, "performing SASL negotiation");
b0654d
+	    printf("\n%s\n", sasl_errdetail(conn));
b0654d
+	    exit(-1);
b0654d
+        }
b0654d
+    }
b0654d
+
b0654d
+    if (r != SASL_OK) exit(-1);
b0654d
+
b0654d
+    if (len > 0) {
b0654d
+        send_string(sd, data, len);
b0654d
+    }
b0654d
+
b0654d
+    fprintf(stdout, "DONE\n");
b0654d
+    fflush(stdout);
b0654d
+    return 0;
b0654d
+}
b0654d
+
b0654d
diff --git a/tests/t_gssapi_srv.c b/tests/t_gssapi_srv.c
b0654d
new file mode 100644
b0654d
index 0000000..29f538d
b0654d
--- /dev/null
b0654d
+++ b/tests/t_gssapi_srv.c
b0654d
@@ -0,0 +1,111 @@
b0654d
+/* TBD, add (C) */
b0654d
+
b0654d
+#include "t_common.h"
b0654d
+
b0654d
+#include <stdlib.h>
b0654d
+#include <stdarg.h>
b0654d
+#include <ctype.h>
b0654d
+#include <string.h>
b0654d
+
b0654d
+#ifdef HAVE_UNISTD_H
b0654d
+#include <unistd.h>
b0654d
+#endif
b0654d
+
b0654d
+#include <arpa/inet.h>
b0654d
+#include <saslplug.h>
b0654d
+
b0654d
+static int setup_socket(void)
b0654d
+{
b0654d
+    struct sockaddr_in addr;
b0654d
+    int sock, ret, sd;
b0654d
+
b0654d
+    sock = socket(AF_INET, SOCK_STREAM, 0);
b0654d
+    if (sock < 0) s_error("socket", 0, 0, errno);
b0654d
+
b0654d
+    addr.sin_family = AF_INET;
b0654d
+    addr.sin_addr.s_addr = inet_addr("127.0.0.9");
b0654d
+    addr.sin_port = htons(9000);
b0654d
+
b0654d
+    ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
b0654d
+    if (ret != 0) s_error("bind", 0, 0, errno);
b0654d
+
b0654d
+    ret = listen(sock, 1);
b0654d
+    if (ret != 0) s_error("listen", 0, 0, errno);
b0654d
+
b0654d
+    /* signal we are ready */
b0654d
+    fprintf(stdout, "READY\n");
b0654d
+    fflush(stdout);
b0654d
+
b0654d
+    /* block until the client connects */
b0654d
+    sd = accept(sock, NULL, NULL);
b0654d
+    if (sd < 0) s_error("accept", 0, 0, errno);
b0654d
+
b0654d
+    close(sock);
b0654d
+    return sd;
b0654d
+}
b0654d
+
b0654d
+int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
b0654d
+{
b0654d
+    sasl_callback_t callbacks[2] = {};
b0654d
+    char buf[8192];
b0654d
+    sasl_conn_t *conn;
b0654d
+    const char *data;
b0654d
+    unsigned int len;
b0654d
+    int sd;
b0654d
+    int r;
b0654d
+
b0654d
+    /* initialize the sasl library */
b0654d
+    callbacks[0].id = SASL_CB_GETPATH;
b0654d
+    callbacks[0].proc = (sasl_callback_ft)&getpath;
b0654d
+    callbacks[0].context = NULL;
b0654d
+    callbacks[1].id = SASL_CB_LIST_END;
b0654d
+    callbacks[1].proc = NULL;
b0654d
+    callbacks[1].context = NULL;
b0654d
+
b0654d
+    r = sasl_server_init(callbacks, "t_gssapi_srv");
b0654d
+    if (r != SASL_OK) exit(-1);
b0654d
+
b0654d
+    r = sasl_server_new("test", "host.realm.test", NULL, NULL, NULL,
b0654d
+                        callbacks, 0, &conn;;
b0654d
+    if (r != SASL_OK) {
b0654d
+        saslerr(r, "allocating connection state");
b0654d
+        exit(-1);
b0654d
+    }
b0654d
+
b0654d
+    sd = setup_socket();
b0654d
+
b0654d
+    len = 8192;
b0654d
+    recv_string(sd, buf, &len;;
b0654d
+
b0654d
+    r = sasl_server_start(conn, "GSSAPI", buf, len, &data, &len;;
b0654d
+    if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
+	saslerr(r, "starting SASL negotiation");
b0654d
+	printf("\n%s\n", sasl_errdetail(conn));
b0654d
+	exit(-1);
b0654d
+    }
b0654d
+
b0654d
+    while (r == SASL_CONTINUE) {
b0654d
+        send_string(sd, data, len);
b0654d
+        len = 8192;
b0654d
+        recv_string(sd, buf, &len;;
b0654d
+
b0654d
+	r = sasl_server_step(conn, buf, len, &data, &len;;
b0654d
+	if (r != SASL_OK && r != SASL_CONTINUE) {
b0654d
+	    saslerr(r, "performing SASL negotiation");
b0654d
+	    printf("\n%s\n", sasl_errdetail(conn));
b0654d
+	    exit(-1);
b0654d
+	}
b0654d
+
b0654d
+    }
b0654d
+
b0654d
+    if (r != SASL_OK) exit(-1);
b0654d
+
b0654d
+    if (len > 0) {
b0654d
+        send_string(sd, data, len);
b0654d
+    }
b0654d
+
b0654d
+    fprintf(stdout, "DONE\n");
b0654d
+    fflush(stdout);
b0654d
+    return 0;
b0654d
+}
b0654d
+
b0654d
-- 
b0654d
2.18.2
b0654d