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

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