krb5 commit: Add unit tests for sort_key_data
Greg Hudson
ghudson at mit.edu
Wed Aug 10 12:33:51 EDT 2016
https://github.com/krb5/krb5/commit/b97888354c97803b4d1617d28b7b597489825693
commit b97888354c97803b4d1617d28b7b597489825693
Author: Sarah Day <sarahday at mit.edu>
Date: Fri Jan 22 09:37:10 2016 -0500
Add unit tests for sort_key_data
.gitignore | 1 +
src/lib/kdb/Makefile.in | 10 ++++-
src/lib/kdb/t_sort_key_data.c | 99 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 109 insertions(+), 1 deletions(-)
diff --git a/.gitignore b/.gitignore
index 4baf11a..933c21d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -298,6 +298,7 @@ local.properties
/src/lib/kdb/adb_err.[ch]
+/src/lib/kdb/t_sort_key_data
/src/lib/kdb/t_stringattr
/src/lib/kdb/t_ulog
/src/lib/kdb/test.ulog
diff --git a/src/lib/kdb/Makefile.in b/src/lib/kdb/Makefile.in
index ea756a6..5da22df 100644
--- a/src/lib/kdb/Makefile.in
+++ b/src/lib/kdb/Makefile.in
@@ -45,13 +45,14 @@ STLIBOBJS= \
kdb_log.o \
keytab.o
-EXTRADEPSRCS= t_stringattr.c t_ulog.c
+EXTRADEPSRCS= t_stringattr.c t_ulog.c t_sort_key_data.c
all-unix: all-liblinks
install-unix: install-libs
clean-unix:: clean-liblinks clean-libs clean-libobjs
$(RM) adb_err.c adb_err.h t_stringattr.o t_stringattr
$(RM) t_ulog.o t_ulog test.ulog
+ $(RM) t_sort_key_data.o t_sort_key_data
check-unix: t_ulog
$(RUN_TEST) ./t_ulog test.ulog
@@ -59,6 +60,9 @@ check-unix: t_ulog
check-pytests: t_stringattr
$(RUNPYTEST) $(srcdir)/t_stringattr.py $(PYTESTFLAGS)
+check-cmocka: t_sort_key_data
+ $(RUN_TEST) ./t_sort_key_data > /dev/null
+
generate-files-mac: darwin.exports
depend: adb_err.h
@@ -71,6 +75,10 @@ t_ulog: t_ulog.o $(KDB5_DEPLIBS) $(KADM_COMM_DEPLIBS) $(KRB5_BASE_DEPLIBS)
$(CC_LINK) -o $@ t_ulog.o $(KDB5_LIBS) $(KADM_COMM_LIBS) \
$(KRB5_BASE_LIBS)
+t_sort_key_data: t_sort_key_data.o $(KDB5_DEPLIBS) $(KADM_COMM_DEPLIBS) \
+ $(KRB5_BASE_DEPLIBS)
+ $(CC_LINK) -o $@ t_sort_key_data.o \
+ $(KDB5_LIBS) $(KADM_COMM_LIBS) $(CMOCKA_LIBS) $(KRB5_BASE_LIBS)
@lib_frag@
@libobj_frag@
diff --git a/src/lib/kdb/t_sort_key_data.c b/src/lib/kdb/t_sort_key_data.c
new file mode 100644
index 0000000..d03d507
--- /dev/null
+++ b/src/lib/kdb/t_sort_key_data.c
@@ -0,0 +1,99 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/* lib/kdb/t_sort_key_data.c - krb5_dbe_sort_key_data() unit tests */
+/*
+ * Copyright (C) 2015 by the Massachusetts Institute of Technology.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include "kdb.h"
+
+#define KEY(kvno) { \
+ 1, kvno, { ENCTYPE_AES128_CTS_HMAC_SHA1_96, 0 }, \
+ { 16, 0 }, \
+ { (uint8_t *)("\xDC\xEE\xB7\x0B\x3D\xE7\x65\x62" \
+ "\xE6\x89\x22\x6C\x76\x42\x91\x48"), \
+ NULL } \
+ }
+
+static void
+assert_sorted(krb5_key_data *keys, int num_keys)
+{
+ int i;
+
+ for (i = 1; i < num_keys; i++)
+ assert_true(keys[i].key_data_kvno <= keys[i - 1].key_data_kvno);
+}
+
+static void
+test_pre_sorted(void **state)
+{
+ krb5_key_data keys[] = { KEY(5), KEY(5), KEY(4), KEY(3), KEY(3), KEY(2),
+ KEY(2), KEY(1) };
+ int n_keys = sizeof(keys)/sizeof(keys[0]);
+
+ krb5_dbe_sort_key_data(keys, n_keys);
+ assert_sorted(keys, n_keys);
+}
+
+static void
+test_reverse_sorted(void **state)
+{
+ krb5_key_data keys[] = { KEY(1), KEY(2), KEY(2), KEY(3), KEY(3), KEY(3),
+ KEY(4), KEY(5) };
+ int n_keys = sizeof(keys)/sizeof(keys[0]);
+
+ krb5_dbe_sort_key_data(keys, n_keys);
+ assert_sorted(keys, n_keys);
+}
+
+static void
+test_random_order(void **state)
+{
+ krb5_key_data keys[] = { KEY(1), KEY(4), KEY(1), KEY(3), KEY(4), KEY(3),
+ KEY(5), KEY(2) };
+ int n_keys = sizeof(keys)/sizeof(keys[0]);
+
+ krb5_dbe_sort_key_data(keys, n_keys);
+ assert_sorted(keys, n_keys);
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_pre_sorted),
+ cmocka_unit_test(test_reverse_sorted),
+ cmocka_unit_test(test_random_order)
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
More information about the cvs-krb5
mailing list