Skip to content

Commit

Permalink
Add unit tests for sort_key_data
Browse files Browse the repository at this point in the history
  • Loading branch information
SaharahSarah authored and greghudson committed Aug 10, 2016
1 parent ef1e428 commit b978883
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/lib/kdb/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,24 @@ 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

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
Expand All @@ -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@

99 changes: 99 additions & 0 deletions src/lib/kdb/t_sort_key_data.c
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit b978883

Please sign in to comment.