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