]> eyrie.org Git - kerberos/krb5-strength.git/commitdiff
Add test suite for util/messages-krb5
authorRuss Allbery <rra@stanford.edu>
Tue, 1 Oct 2013 20:52:30 +0000 (13:52 -0700)
committerRuss Allbery <rra@stanford.edu>
Tue, 1 Oct 2013 20:52:30 +0000 (13:52 -0700)
.gitignore
Makefile.am
tests/TESTS
tests/util/messages-krb5-t.c [new file with mode: 0644]

index da4021145f40411079888a2e8af28bebb167239f..e05d45b58b1f7e5ae0f4a5dfa41921fa146a24eb 100644 (file)
@@ -30,6 +30,7 @@
 /tests/portable/strlcpy-t
 /tests/runtests
 /tests/tap/libtap.a
+/tests/util/messages-krb5-t
 /tests/util/messages-t
 /tests/util/xmalloc
 /tools/heimdal-strength
index 496487dca5b5e34a5cad40df1f3f63902f70976f..b78cf90c771302798b2766fa378b487e83e6b5f5 100644 (file)
@@ -113,10 +113,10 @@ warnings:
        $(MAKE) V=0 CFLAGS='$(WARNINGS)' $(check_PROGRAMS)
 
 # The bits below are for the test suite, not for the main package.
-check_PROGRAMS = tests/heimdal/plugin-t tests/mit/plugin-t              \
-       tests/portable/asprintf-t tests/portable/snprintf-t              \
-       tests/portable/strlcat-t tests/portable/strlcpy-t tests/runtests \
-       tests/util/messages-t tests/util/xmalloc
+check_PROGRAMS = tests/heimdal/plugin-t tests/mit/plugin-t                 \
+       tests/portable/asprintf-t tests/portable/snprintf-t                 \
+       tests/portable/strlcat-t tests/portable/strlcpy-t tests/runtests    \
+       tests/util/messages-krb5-t tests/util/messages-t tests/util/xmalloc
 if EMBEDDED_CRACKLIB
     check_PROGRAMS += cracklib/packer
 endif
@@ -150,6 +150,8 @@ tests_portable_strlcpy_t_SOURCES = tests/portable/strlcpy-t.c \
 tests_portable_strlcpy_t_LDADD = tests/tap/libtap.a portable/libportable.la
 tests_util_messages_t_LDADD = tests/tap/libtap.a util/libutil.a \
        portable/libportable.la
+tests_util_messages_krb5_t_LDADD = tests/tap/libtap.a util/libutil.a \
+       portable/libportable.la $(KRB5_LIBS)
 tests_util_xmalloc_LDADD = util/libutil.a portable/libportable.la
 
 # The dictionary is used by the tests and needs to be built first.
index 2c73e6304ceb04795f8f6bdc39187d5f68e7fef0..525e584bd1a0c7787e6e5a7c201035edfcb40346 100644 (file)
@@ -9,4 +9,5 @@ portable/strlcat
 portable/strlcpy
 tools/cdbmake-wordlist
 util/messages
+util/messages-krb5
 util/xmalloc
diff --git a/tests/util/messages-krb5-t.c b/tests/util/messages-krb5-t.c
new file mode 100644 (file)
index 0000000..35a4632
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * Test suite for Kerberos error handling routines.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <rra@stanford.edu>
+ * Copyright 2010, 2011, 2013
+ *     The Board of Trustees of the Leland Stanford Junior University
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <config.h>
+#include <portable/krb5.h>
+#include <portable/system.h>
+
+#include <tests/tap/basic.h>
+#include <tests/tap/process.h>
+#include <util/macros.h>
+#include <util/messages-krb5.h>
+#include <util/messages.h>
+#include <util/xmalloc.h>
+
+
+/*
+ * Test functions.
+ */
+static void
+test_warn(void *data UNUSED)
+{
+    krb5_context ctx;
+    krb5_error_code code;
+    krb5_principal princ;
+
+    code = krb5_init_context(&ctx);
+    if (code < 0)
+        die_krb5(ctx, code, "cannot create context");
+    code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
+    if (code < 0)
+        warn_krb5(ctx, code, "principal parse failed");
+    else
+        die("unexpected success parsing principal");
+    exit(0);
+}
+
+static void
+test_die(void *data UNUSED)
+{
+    krb5_context ctx;
+    krb5_error_code code;
+    krb5_principal princ;
+
+    code = krb5_init_context(&ctx);
+    if (code < 0)
+        die_krb5(ctx, code, "cannot create context");
+    code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
+    if (code < 0)
+        die_krb5(ctx, code, "principal parse failed");
+    else
+        die("unexpected success parsing principal");
+    exit(0);
+}
+
+
+/*
+ * Run the tests.
+ */
+int
+main(void)
+{
+    krb5_context ctx;
+    krb5_error_code code;
+    krb5_principal princ;
+    const char *message;
+    char *wanted;
+
+    plan(6 * 3);
+
+    /* First, we have to get what the correct error message is. */
+    code = krb5_init_context(&ctx);
+    if (code < 0)
+        bail("cannot create context");
+    code = krb5_parse_name(ctx, "foo@bar@EXAMPLE.COM", &princ);
+    message = krb5_get_error_message(ctx, code);
+
+    xasprintf(&wanted, "principal parse failed: %s\n", message);
+    is_function_output(test_warn, NULL, 0, wanted, "warn_krb5");
+    is_function_output(test_die, NULL, 1, wanted, "die_krb5");
+    free(wanted);
+
+    message_program_name = "msg-test";
+    xasprintf(&wanted, "msg-test: principal parse failed: %s\n", message);
+    is_function_output(test_warn, NULL, 0, wanted, "warn_krb5 with name");
+    is_function_output(test_die, NULL, 1, wanted, "die_krb5 with name");
+    free(wanted);
+
+    message_handlers_warn(0);
+    is_function_output(test_warn, NULL, 0, "", "warn_krb5 with no handlers");
+    message_handlers_die(0);
+    is_function_output(test_die, NULL, 1, "", "warn_krb5 with no handlers");
+
+    krb5_free_error_message(ctx, message);
+    krb5_free_context(ctx);
+    return 0;
+}