f65af0
From efd0a988a7431e3f31f5dde78f66eaec8c7f7f37 Mon Sep 17 00:00:00 2001
979ee0
From: Robbie Harwood <rharwood@redhat.com>
979ee0
Date: Thu, 30 Aug 2018 15:34:31 -0400
979ee0
Subject: [PATCH] Add cmocka unit tests for ipa otpd queue code
979ee0
979ee0
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
979ee0
---
979ee0
 daemons/ipa-otpd/Makefile.am                  |  12 +
979ee0
 .../ipa-otpd/ipa_otpd_queue_cmocka_tests.c    | 212 ++++++++++++++++++
979ee0
 2 files changed, 224 insertions(+)
979ee0
 create mode 100644 daemons/ipa-otpd/ipa_otpd_queue_cmocka_tests.c
979ee0
979ee0
diff --git a/daemons/ipa-otpd/Makefile.am b/daemons/ipa-otpd/Makefile.am
f65af0
index 923e16ebe127a36cea372d656dd913c4ffa09313..c1d6c2053354a59b1fd897f9a67cc7507eed5294 100644
979ee0
--- a/daemons/ipa-otpd/Makefile.am
979ee0
+++ b/daemons/ipa-otpd/Makefile.am
f65af0
@@ -21,3 +21,15 @@ ipa_otpd_SOURCES = bind.c forward.c main.c parse.c query.c queue.c stdio.c
979ee0
 	     $< > $@
979ee0
 
979ee0
 CLEANFILES = $(systemdsystemunit_DATA)
979ee0
+
979ee0
+TESTS =
979ee0
+check_PROGRAMS =
979ee0
+
979ee0
+if HAVE_CMOCKA
979ee0
+TESTS += queue_tests
979ee0
+check_PROGRAMS += queue_tests
979ee0
+endif
979ee0
+
979ee0
+queue_tests_SOURCES = ipa_otpd_queue_cmocka_tests.c queue.c
979ee0
+queue_tests_CFLAGS = $(CMOCKA_CFLAGS)
979ee0
+queue_tests_LDADD = $(CMOCKA_LIBS)
979ee0
diff --git a/daemons/ipa-otpd/ipa_otpd_queue_cmocka_tests.c b/daemons/ipa-otpd/ipa_otpd_queue_cmocka_tests.c
979ee0
new file mode 100644
979ee0
index 0000000000000000000000000000000000000000..068431e6475bb74b01acbcab22115915dec1a278
979ee0
--- /dev/null
979ee0
+++ b/daemons/ipa-otpd/ipa_otpd_queue_cmocka_tests.c
979ee0
@@ -0,0 +1,212 @@
979ee0
+/*
979ee0
+ * FreeIPA 2FA companion daemon - internal queue tests
979ee0
+ *
979ee0
+ * Author: Robbie Harwood <rharwood@redhat.com>
979ee0
+ *
979ee0
+ * Copyright (C) 2018  Robbie Harwood, Red Hat
979ee0
+ * see file 'COPYING' for use and warranty information
979ee0
+ *
979ee0
+ * This program is free software you can redistribute it and/or modify it
979ee0
+ * under the terms of the GNU General Public License as published by the Free
979ee0
+ * Software Foundation, either version 3 of the License, or (at your option)
979ee0
+ * any later version.
979ee0
+ *
979ee0
+ * This program is distributed in the hope that it will be useful, but WITHOUT
979ee0
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
979ee0
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
979ee0
+ * more details.
979ee0
+ *
979ee0
+ * You should have received a copy of the GNU General Public License along
979ee0
+ * with this program.  If not, see <http://www.gnu.org/licenses/>.
979ee0
+ */
979ee0
+
979ee0
+#include <setjmp.h>
979ee0
+#include <stdarg.h>
979ee0
+#include <stddef.h>
979ee0
+
979ee0
+#include <cmocka.h>
979ee0
+
979ee0
+#include "internal.h"
979ee0
+
979ee0
+/* Bypass otpd queue allocation/freeing to avoid calling into LDAP and
979ee0
+ * krad.  No effort is made to make the types match. */
979ee0
+static struct otpd_queue_item *new_elt(int id)
979ee0
+{
979ee0
+    krb5_error_code ret;
979ee0
+    struct otpd_queue_item *e = NULL;
979ee0
+
979ee0
+    ret = otpd_queue_item_new(NULL, &e);
979ee0
+    assert_int_equal(ret, 0);
979ee0
+    assert_ptr_not_equal(e, NULL);
979ee0
+
979ee0
+    e->msgid = id;
979ee0
+    return e;
979ee0
+}
979ee0
+static void free_elt(struct otpd_queue_item **e)
979ee0
+{
979ee0
+    assert_ptr_not_equal(e, NULL);
979ee0
+    free(*e);
979ee0
+    *e = NULL;
979ee0
+}
979ee0
+static void free_elts(struct otpd_queue *q)
979ee0
+{
979ee0
+    assert_ptr_not_equal(q, NULL);
979ee0
+    for (struct otpd_queue_item *e = otpd_queue_pop(q); e != NULL;
979ee0
+         e = otpd_queue_pop(q))
979ee0
+        free_elt(&e);
979ee0
+}
979ee0
+#define otpd_queue_item_new new_elt
979ee0
+#define otpd_queue_item_free free_elt
979ee0
+#define otpd_queue_free_items free_elts
979ee0
+
979ee0
+static void assert_elt_equal(struct otpd_queue_item *e1,
979ee0
+                             struct otpd_queue_item *e2)
979ee0
+{
979ee0
+    if (e1 == NULL && e2 == NULL)
979ee0
+        return;
979ee0
+    assert_ptr_not_equal(e1, NULL);
979ee0
+    assert_ptr_not_equal(e2, NULL);
979ee0
+    assert_int_equal(e1->msgid, e2->msgid);
979ee0
+}
979ee0
+
979ee0
+static void test_single_insert()
979ee0
+{
979ee0
+    struct otpd_queue q = { NULL };
979ee0
+    struct otpd_queue_item *ein, *eout;
979ee0
+
979ee0
+    ein = new_elt(0);
979ee0
+    otpd_queue_push(&q, ein);
979ee0
+
979ee0
+    eout = otpd_queue_peek(&q);
979ee0
+    assert_elt_equal(ein, eout);
979ee0
+
979ee0
+    eout = otpd_queue_pop(&q);
979ee0
+    assert_elt_equal(ein, eout);
979ee0
+    free_elt(&eout);
979ee0
+
979ee0
+    eout = otpd_queue_pop(&q);
979ee0
+    assert_ptr_equal(eout, NULL);
979ee0
+
979ee0
+    free_elts(&q);
979ee0
+}
979ee0
+
979ee0
+static void test_jump_insert()
979ee0
+{
979ee0
+    struct otpd_queue q = { NULL };
979ee0
+    struct otpd_queue_item *echeck;
979ee0
+
979ee0
+    for (int i = 0; i < 3; i++) {
979ee0
+        struct otpd_queue_item *e = new_elt(i);
979ee0
+        otpd_queue_push_head(&q, e);
979ee0
+
979ee0
+        echeck = otpd_queue_peek(&q);
979ee0
+        assert_elt_equal(e, echeck);
979ee0
+    }
979ee0
+
979ee0
+    free_elts(&q);
979ee0
+}
979ee0
+
979ee0
+static void test_garbage_insert()
979ee0
+{
979ee0
+    struct otpd_queue q = { NULL };
979ee0
+    struct otpd_queue_item *e, *g;
979ee0
+
979ee0
+    g = new_elt(0);
979ee0
+    g->next = g;
979ee0
+    otpd_queue_push(&q, g);
979ee0
+
979ee0
+    e = otpd_queue_peek(&q);
979ee0
+    assert_ptr_equal(e->next, NULL);
979ee0
+
979ee0
+    free_elts(&q);
979ee0
+}
979ee0
+
979ee0
+static void test_removal()
979ee0
+{
979ee0
+    struct otpd_queue q = { NULL };
979ee0
+
979ee0
+    for (int i = 0; i < 3; i++) {
979ee0
+        struct otpd_queue_item *e = new_elt(i);
979ee0
+        otpd_queue_push(&q, e);
979ee0
+    }
979ee0
+    for (int i = 0; i < 3; i++) {
979ee0
+        struct otpd_queue_item *e = otpd_queue_pop(&q);
979ee0
+        assert_ptr_not_equal(e, NULL);
979ee0
+        assert_ptr_equal(e->next, NULL);
979ee0
+        assert_int_equal(e->msgid, i);
979ee0
+        free_elt(&e);
979ee0
+    }
979ee0
+}
979ee0
+
979ee0
+static void pick_id(struct otpd_queue *q, int msgid)
979ee0
+{
979ee0
+    struct otpd_queue_item *e;
979ee0
+
979ee0
+    e = otpd_queue_pop_msgid(q, msgid);
979ee0
+    assert_int_equal(e->msgid, msgid);
979ee0
+    assert_ptr_equal(e->next, NULL);
979ee0
+    free_elt(&e);
979ee0
+    e = otpd_queue_pop_msgid(q, msgid);
979ee0
+    assert_ptr_equal(e, NULL);
979ee0
+}
979ee0
+static void test_pick_removal()
979ee0
+{
979ee0
+    struct otpd_queue q = { NULL };
979ee0
+
979ee0
+    for (int i = 0; i < 4; i++) {
979ee0
+        struct otpd_queue_item *e = new_elt(i);
979ee0
+        otpd_queue_push(&q, e);
979ee0
+    }
979ee0
+
979ee0
+    pick_id(&q, 0); /* first */
979ee0
+    pick_id(&q, 2); /* middle */
979ee0
+    pick_id(&q, 3); /* last */
979ee0
+    pick_id(&q, 1); /* singleton */
979ee0
+
979ee0
+    free_elts(&q);
979ee0
+}
979ee0
+
979ee0
+static void test_iter()
979ee0
+{
979ee0
+    krb5_error_code ret;
979ee0
+    struct otpd_queue q = { NULL };
979ee0
+    const struct otpd_queue *queues[3];
979ee0
+    struct otpd_queue_iter *iter = NULL;
979ee0
+    const krad_packet *p = NULL;
979ee0
+
979ee0
+    for (ptrdiff_t i = 1; i <= 3; i++) {
979ee0
+        struct otpd_queue_item *e = new_elt(i);
979ee0
+        e->req = (void *)i;
979ee0
+        otpd_queue_push(&q, e);
979ee0
+    }
979ee0
+
979ee0
+    queues[0] = &q;
979ee0
+    queues[1] = &q;
979ee0
+    queues[2] = NULL;
979ee0
+    ret = otpd_queue_iter_new(queues, &iter);
979ee0
+    assert_int_equal(ret, 0);
979ee0
+    assert_ptr_not_equal(iter, NULL);
979ee0
+
979ee0
+    for (ptrdiff_t i = 0; i < 6; i++) {
979ee0
+        p = otpd_queue_iter_func(iter, FALSE);
979ee0
+        assert_ptr_equal(p, (void *) (i % 3 + 1));
979ee0
+    }
979ee0
+    p = otpd_queue_iter_func(iter, FALSE);
979ee0
+    assert_ptr_equal(p, NULL);
979ee0
+
979ee0
+    free_elts(&q);
979ee0
+}
979ee0
+
979ee0
+int main(int argc, char *argv[])
979ee0
+{
979ee0
+    const struct CMUnitTest tests[] = {
979ee0
+        cmocka_unit_test(test_single_insert),
979ee0
+        cmocka_unit_test(test_jump_insert),
979ee0
+        cmocka_unit_test(test_garbage_insert),
979ee0
+        cmocka_unit_test(test_removal),
979ee0
+        cmocka_unit_test(test_pick_removal),
979ee0
+        cmocka_unit_test(test_iter),
979ee0
+    };
979ee0
+
979ee0
+    return cmocka_run_group_tests(tests, NULL, NULL);
979ee0
+}
979ee0
-- 
979ee0
2.17.1
979ee0