From 46436e8a1be0b537f32027f962be629151345e59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonathan=20Sch=C3=B6bel?= <jonathan@xn--schbel-yxa.info>
Date: Mon, 20 Jun 2022 22:45:48 +0200
Subject: [PATCH] added test for CMS

Tests are done using check, allowing to integrate the tests into the
Autotools.
Methods that are part of another unit, but are called in this unit
aren't tested as this would interfere with the idea of unittests. This
also applies for purely wrapper functions, where a call is passed to
another unit.
---
 Makefile.am       |  2 +-
 configure.ac      |  3 +-
 tests/Makefile.am | 19 +++++++++++
 tests/test_cms.c  | 82 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 104 insertions(+), 2 deletions(-)
 create mode 100644 tests/Makefile.am
 create mode 100644 tests/test_cms.c

diff --git a/Makefile.am b/Makefile.am
index 51d6d52..bd75038 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,5 @@
 ## Process this file with automake to produce Makefile.in
 
-SUBDIRS = src
+SUBDIRS = src tests
 
 ACLOCAL_AMFLAGS = --install -I build-macro
diff --git a/configure.ac b/configure.ac
index f32e54e..750a622 100644
--- a/configure.ac
+++ b/configure.ac
@@ -30,6 +30,7 @@ AC_CHECK_HEADER([string.h])
 
 # Makefiles
 AC_CONFIG_FILES([Makefile
-		 src/Makefile])
+		 src/Makefile
+		 tests/Makefile])
 
 AC_OUTPUT
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..4db975e
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,19 @@
+## Process this file with automake to produce Makefile.in
+
+AM_CFLAGS = -Wall -Wextra $(CHECK_CFLAGS)
+
+TESTS =
+TESTS += sefht_cms_test
+
+check_PROGRAMS = $(TESTS)
+
+AM_CPPFLAGS =
+AM_CPPFLAGS += -I$(top_srcdir)/src
+
+LDADD = $(CHECK_LIBS)
+
+sefht_cms_test_SOURCES = test_cms.c
+sefht_cms_test_LDADD =
+sefht_cms_test_LDADD += $(top_builddir)/src/cms.o
+sefht_cms_test_LDADD += $(top_builddir)/src/data.o
+sefht_cms_test_LDADD += $(LDADD)
diff --git a/tests/test_cms.c b/tests/test_cms.c
new file mode 100644
index 0000000..1d4f0f5
--- /dev/null
+++ b/tests/test_cms.c
@@ -0,0 +1,82 @@
+/*
+ * test_cms.c
+ *
+ * Copyright 2022 Jonathan Schöbel <jonathan@Ubermos-2019>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ *
+ */
+
+
+#include <check.h>
+#include <stdlib.h>
+
+#include "error.h"
+#include "cms.h"
+
+
+START_TEST(test_cms)
+{
+	struct SH_Error error;
+	struct SH_Cms * cms;
+
+	cms = SH_Cms_new (NULL);
+	ck_assert_int_ne ((long int) cms, (long int) NULL);
+
+	SH_Cms_free (cms, NULL);
+
+	cms = SH_Cms_new (&error);
+	ck_assert_int_ne ((long int) cms, (long int) NULL);
+	ck_assert_int_eq (error.type, SUCCESS);
+
+	SH_Cms_free (cms, &error);
+	ck_assert_int_eq (error.type, SUCCESS);
+}
+END_TEST
+
+Suite * cms_suite (void)
+{
+	Suite *s;
+	TCase *tc_core;
+
+	s = suite_create ("Testsuite SeFHT CMS");
+
+	/* Core test case */
+	tc_core = tcase_create ("Core");
+
+	tcase_add_test (tc_core, test_cms);
+	suite_add_tcase (s, tc_core);
+
+	return s;
+}
+
+int main (int argc, char **argv)
+{
+	int number_failed;
+	Suite *s;
+	SRunner *sr;
+
+	s = cms_suite ();
+	sr = srunner_create (s);
+
+	srunner_run_all (sr, CK_NORMAL);
+	number_failed = srunner_ntests_failed (sr);
+	srunner_free (sr);
+
+	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
-- 
GitLab