Refactor CBT unit test code into separate compilation modules

From: Mark Syms <mark.syms@citrix.com>

Signed-off-by: Mark Syms <mark.syms@citrix.com>
Reviewed-by: Chandrika Srinivasan <chandrika.srinivasan@citrix.com>

diff --git a/mockatests/cbt/Makefile.am b/mockatests/cbt/Makefile.am
index fa8b3a8..e4dde03 100644
--- a/mockatests/cbt/Makefile.am
+++ b/mockatests/cbt/Makefile.am
@@ -8,7 +8,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/cbt
 check_PROGRAMS = test-cbt-util
 TESTS = test-cbt-util
 
-test_cbt_util_SOURCES = test-cbt-util.c
+test_cbt_util_SOURCES = test-cbt-util.c wrappers.c test-cbt-util-commands.c test-cbt-util-get.c
 test_cbt_util_LDFLAGS = $(top_srcdir)/cbt/libcbtutil.la -lcmocka -luuid
 test_cbt_util_LDFLAGS += -Wl,--wrap=fopen,--wrap=fclose
 # Need to wrap both of these as rpmbuild/mock set -D_FORTIFY_SOURCE=2
diff --git a/mockatests/cbt/test-cbt-util-commands.c b/mockatests/cbt/test-cbt-util-commands.c
new file mode 100644
index 0000000..9b4f789
--- /dev/null
+++ b/mockatests/cbt/test-cbt-util-commands.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2017, Citrix Systems, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. 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.
+ *  3. Neither the name of the copyright holder nor the names of its 
+ *     contributors may be used to endorse or promote products derived from 
+ *     this software without specific prior written permission.
+ *
+ * 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 OWNER
+ * 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 <stdint.h>
+#include <stddef.h>
+#include <stdarg.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <cbt-util-priv.h>
+#include "wrappers.h"
+#include "test-suites.h"
+
+void
+test_get_command_create(void **state)
+{
+	struct command *cmd;
+
+	char* requested_command = { "create" };
+
+	cmd = get_command(requested_command);
+
+	assert_string_equal(cmd->name, "create");
+	assert_ptr_equal(cmd->func, cbt_util_create);
+}
+
+void
+test_get_command_set(void **state)
+{
+	struct command *cmd;
+
+	char* requested_command = { "set" };
+
+	cmd = get_command(requested_command);
+
+	assert_string_equal(cmd->name, "set");
+	assert_ptr_equal(cmd->func, cbt_util_set);
+}
+
+void
+test_get_command_get(void **state)
+{
+	struct command *cmd;
+
+	char* requested_command = { "get" };
+
+	cmd = get_command(requested_command);
+
+	assert_string_equal(cmd->name, "get");
+	assert_ptr_equal(cmd->func, cbt_util_get);
+}
diff --git a/mockatests/cbt/test-cbt-util-get.c b/mockatests/cbt/test-cbt-util-get.c
new file mode 100644
index 0000000..4ae621d
--- /dev/null
+++ b/mockatests/cbt/test-cbt-util-get.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2017, Citrix Systems, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. 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.
+ *  3. Neither the name of the copyright holder nor the names of its 
+ *     contributors may be used to endorse or promote products derived from 
+ *     this software without specific prior written permission.
+ *
+ * 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 OWNER
+ * 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 <stdint.h>
+#include <string.h>
+#include <stddef.h>
+#include <stdarg.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <uuid/uuid.h>
+
+#include <cbt-util-priv.h>
+#include "wrappers.h"
+#include "test-suites.h"
+
+struct cbt_log_metadata {
+	uuid_t parent;
+	uuid_t child;
+	int    consistent;
+};
+
+void test_cbt_util_get_flag(void **state)
+{
+	int result;
+	char* args[] = { "cbt-util", "-n", "test_disk.log", "-f" };
+	void *log_meta;
+	char *output;
+
+	log_meta = malloc(sizeof(struct cbt_log_metadata));
+
+	((struct cbt_log_metadata*)log_meta)->consistent = 1;
+	FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r");
+
+	will_return(__wrap_fopen, test_log);
+	expect_value(__wrap_fclose, fp, test_log);
+
+	output = setup_vprintf_mock(1024);
+
+	result = cbt_util_get(4, args);
+
+	assert_int_equal(result, 0);
+	assert_string_equal(output, "1\n");
+}
diff --git a/mockatests/cbt/test-cbt-util.c b/mockatests/cbt/test-cbt-util.c
index 4140dd6..4dc6348 100644
--- a/mockatests/cbt/test-cbt-util.c
+++ b/mockatests/cbt/test-cbt-util.c
@@ -1,148 +1,40 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <stdarg.h>
+/*
+ * Copyright (c) 2017, Citrix Systems, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. 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.
+ *  3. Neither the name of the copyright holder nor the names of its 
+ *     contributors may be used to endorse or promote products derived from 
+ *     this software without specific prior written permission.
+ *
+ * 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 OWNER
+ * 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 <stddef.h>
+#include <stdarg.h>
 #include <setjmp.h>
 #include <cmocka.h>
-#include <uuid/uuid.h>
-
-#include <cbt-util-priv.h>
-
-extern int cbt_util_create(int , char **);
-extern int cbt_util_set(int , char **);
-extern int cbt_util_get(int , char **);
-
-struct cbt_log_metadata {
-	uuid_t parent;
-	uuid_t child;
-	int    consistent;
-};
-
-static int tests_running = 1;
-
-FILE *
-__wrap_fopen(const char *path, const char *mode)
-{
-	return (FILE*) mock();
-}
-
-void __real_fclose(FILE *fp);
-
-void
-__wrap_fclose(FILE *fp)
-{
-	if (tests_running) {
-		check_expected_ptr(fp);
-	}
-	__real_fclose(fp);
-}
-
-int
-wrap_vprintf(const char *format, va_list ap)
-{
-	int bufsize = mock();
-	char* buf = mock();
-
-	int len = vsnprintf(buf, bufsize, format, ap);
-
-	assert_in_range(len, 0, bufsize);
-
-	return len;
-}
-
-int
-__wrap_printf(const char *format, ...)
-{
-	va_list ap;
-	va_start(ap, format);
-
-	return wrap_vprintf(format, ap);
-}
-
-int
-__wrap___printf_chk (int __flag, const char *format, ...)
-{
-	va_list ap;
-	va_start(ap, format);
-
-	return wrap_vprintf(format, ap);
-}
-
-void
-test_get_command_create(void **state)
-{
-	struct command *cmd;
-
-	char* requested_command = { "create" };
-
-	cmd = get_command(requested_command);
-
-	assert_string_equal(cmd->name, "create");
-	assert_ptr_equal(cmd->func, cbt_util_create);
-}
-
-void
-test_get_command_set(void **state)
-{
-	struct command *cmd;
-
-	char* requested_command = { "set" };
-
-	cmd = get_command(requested_command);
-
-	assert_string_equal(cmd->name, "set");
-	assert_ptr_equal(cmd->func, cbt_util_set);
-}
-
-void
-test_get_command_get(void **state)
-{
-	struct command *cmd;
-
-	char* requested_command = { "get" };
-
-	cmd = get_command(requested_command);
-
-	assert_string_equal(cmd->name, "get");
-	assert_ptr_equal(cmd->func, cbt_util_get);
-}
-
-
-void test_cbt_util_get_flag(void **state)
-{
-	int result;
-	char* args[] = { "cbt-util", "-n", "test_disk.log", "-f" };
-	void *log_meta;
-	char output[1024];
-
-	log_meta = malloc(sizeof(struct cbt_log_metadata));
-
-	((struct cbt_log_metadata*)log_meta)->consistent = 1;
-	FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r");
-
-	will_return(__wrap_fopen, test_log);
-	expect_value(__wrap_fclose, fp, test_log);
-
-	will_return(wrap_vprintf, 1024);
-	will_return(wrap_vprintf, output);
-
-	result = cbt_util_get(4, args);
-
-	assert_int_equal(result, 0);
-	assert_string_equal(output, "1\n");
-}
-
-const struct CMUnitTest cbt_command_tests[] = {
-	cmocka_unit_test(test_get_command_create),
-	cmocka_unit_test(test_get_command_set),
-	cmocka_unit_test(test_get_command_get)
-};
 
-const struct CMUnitTest cbt_get_tests[] = {
-	cmocka_unit_test(test_cbt_util_get_flag)
-};
+#include "wrappers.h"
+#include "test-suites.h"
 
 int main(void)
 {
@@ -151,7 +43,7 @@ int main(void)
 		cmocka_run_group_tests_name("Get tests", cbt_get_tests, NULL, NULL);
 
 	/* Need to flag that the tests are done so that the fclose mock goes quiescent */
-	tests_running = 0;
+	disable_mocks();
 
 	return result;
 }
diff --git a/mockatests/cbt/test-suites.h b/mockatests/cbt/test-suites.h
new file mode 100644
index 0000000..038d1ac
--- /dev/null
+++ b/mockatests/cbt/test-suites.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2017, Citrix Systems, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. 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.
+ *  3. Neither the name of the copyright holder nor the names of its 
+ *     contributors may be used to endorse or promote products derived from 
+ *     this software without specific prior written permission.
+ *
+ * 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 OWNER
+ * 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 <setjmp.h>
+#include <cmocka.h>
+
+#ifndef __TEST_SUITES_H__
+#define __TEST_SUITES_H__
+
+void test_get_command_create(void **state);
+void test_get_command_set(void **state);
+void test_get_command_get(void **state);
+
+void test_cbt_util_get_flag(void **state);
+
+/* Functions under test */
+
+extern int cbt_util_create(int , char **);
+extern int cbt_util_set(int , char **);
+extern int cbt_util_get(int , char **);
+
+
+static const struct CMUnitTest cbt_command_tests[] = {
+	cmocka_unit_test(test_get_command_create),
+	cmocka_unit_test(test_get_command_set),
+	cmocka_unit_test(test_get_command_get)
+};
+
+static const struct CMUnitTest cbt_get_tests[] = {
+	cmocka_unit_test(test_cbt_util_get_flag)
+};
+
+#endif /* __TEST_SUITES_H__ */
diff --git a/mockatests/cbt/wrappers.c b/mockatests/cbt/wrappers.c
new file mode 100644
index 0000000..0cf4f98
--- /dev/null
+++ b/mockatests/cbt/wrappers.c
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2017, Citrix Systems, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. 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.
+ *  3. Neither the name of the copyright holder nor the names of its 
+ *     contributors may be used to endorse or promote products derived from 
+ *     this software without specific prior written permission.
+ *
+ * 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 OWNER
+ * 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 <string.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "wrappers.h"
+
+static int tests_running = 1;
+
+FILE *
+__wrap_fopen(void)
+{
+	return (FILE*) mock();
+}
+
+void __real_fclose(FILE *fp);
+
+void
+__wrap_fclose(FILE *fp)
+{
+	if (tests_running) {
+		check_expected_ptr(fp);
+	}
+	__real_fclose(fp);
+}
+
+int
+wrap_vprintf(const char *format, va_list ap)
+{
+	int bufsize = mock();
+	char* buf = mock();
+
+	int len = vsnprintf(buf, bufsize, format, ap);
+
+	assert_in_range(len, 0, bufsize);
+
+	return len;
+}
+
+int
+__wrap_printf(const char *format, ...)
+{
+	va_list ap;
+	va_start(ap, format);
+
+	return wrap_vprintf(format, ap);
+}
+
+int
+__wrap___printf_chk (int __flag, const char *format, ...)
+{
+	va_list ap;
+	va_start(ap, format);
+
+	return wrap_vprintf(format, ap);
+}
+
+char *setup_vprintf_mock(int size)
+{
+	char *buf;
+
+	buf = malloc(size);
+
+	will_return(wrap_vprintf, size);
+	will_return(wrap_vprintf, buf);
+
+	return buf;
+}
+
+void disable_mocks()
+{
+	tests_running = 0;
+}
diff --git a/mockatests/cbt/wrappers.h b/mockatests/cbt/wrappers.h
new file mode 100644
index 0000000..9141bfc
--- /dev/null
+++ b/mockatests/cbt/wrappers.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017, Citrix Systems, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. 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.
+ *  3. Neither the name of the copyright holder nor the names of its 
+ *     contributors may be used to endorse or promote products derived from 
+ *     this software without specific prior written permission.
+ *
+ * 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 OWNER
+ * 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 <stdio.h>
+
+#ifndef __WRAPPERS_H__
+#define __WRAPPERS_H__
+
+FILE * __wrap_fopen(void);
+
+void __wrap_fclose(FILE *fp);
+
+char *setup_vprintf_mock(int size);
+
+void disable_mocks();
+
+#endif /* __WRAPPERS_H__ */
