From: Mark Syms <mark.syms@citrix.com>
Date: Fri, 14 Jul 2017 12:11:55 +0100
CP-23274: Move wrappers to a separate library

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

---
 configure.ac                            |   1 +
 mockatests/Makefile.am                  |   3 +-
 mockatests/cbt/Makefile.am              |   5 +-
 mockatests/cbt/test-cbt-util-commands.c |   2 +-
 mockatests/cbt/test-cbt-util-create.c   |   2 +-
 mockatests/cbt/test-cbt-util-get.c      |   2 +-
 mockatests/cbt/test-cbt-util.c          |   2 +-
 mockatests/cbt/wrappers.c               | 228 --------------------------------
 mockatests/cbt/wrappers.h               |  78 -----------
 mockatests/include/wrappers.h           |  84 ++++++++++++
 mockatests/wrappers/Makefile.am         |   8 ++
 mockatests/wrappers/wrappers.c          | 228 ++++++++++++++++++++++++++++++++
 12 files changed, 330 insertions(+), 313 deletions(-)
 delete mode 100644 mockatests/cbt/wrappers.c
 delete mode 100644 mockatests/cbt/wrappers.h
 create mode 100644 mockatests/include/wrappers.h
 create mode 100644 mockatests/wrappers/Makefile.am
 create mode 100644 mockatests/wrappers/wrappers.c

diff --git a/configure.ac b/configure.ac
index 5b6fe30..96c4913 100644
--- a/configure.ac
+++ b/configure.ac
@@ -83,6 +83,7 @@ drivers/Makefile
 include/Makefile
 tapback/Makefile
 mockatests/Makefile
+mockatests/wrappers/Makefile
 mockatests/cbt/Makefile
 ])
 AC_OUTPUT
diff --git a/mockatests/Makefile.am b/mockatests/Makefile.am
index b5c941c..f741c14 100644
--- a/mockatests/Makefile.am
+++ b/mockatests/Makefile.am
@@ -1,3 +1,4 @@
 # ACLOCAL_AMFLAGS = -I m4
 
-SUBDIRS  = cbt
+SUBDIRS  = wrappers
+SUBDIRS += cbt
diff --git a/mockatests/cbt/Makefile.am b/mockatests/cbt/Makefile.am
index 1edfdac..16541b2 100644
--- a/mockatests/cbt/Makefile.am
+++ b/mockatests/cbt/Makefile.am
@@ -4,13 +4,14 @@ AM_CFLAGS += -fprofile-arcs
 AM_CFLAGS += -ftest-coverage
 AM_CFLAGS += -Og -fno-inline-functions -g
 
-AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/cbt
+AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/cbt -I../include
 
 check_PROGRAMS = test-cbt-util
 TESTS = test-cbt-util
 
-test_cbt_util_SOURCES = test-cbt-util.c wrappers.c test-cbt-util-commands.c test-cbt-util-get.c test-cbt-util-create.c
+test_cbt_util_SOURCES = test-cbt-util.c test-cbt-util-commands.c test-cbt-util-get.c test-cbt-util-create.c
 test_cbt_util_LDFLAGS = $(top_srcdir)/cbt/libcbtutil.la -lcmocka -luuid
+test_cbt_util_LDFLAGS += ../wrappers/libwrappers.la
 test_cbt_util_LDFLAGS += -Wl,--wrap=fopen,--wrap=fclose
 test_cbt_util_LDFLAGS += -Wl,--wrap=malloc,--wrap=free
 # 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
index 2d978b2..cba905f 100644
--- a/mockatests/cbt/test-cbt-util-commands.c
+++ b/mockatests/cbt/test-cbt-util-commands.c
@@ -34,7 +34,7 @@
 #include <cmocka.h>
 
 #include <cbt-util-priv.h>
-#include "wrappers.h"
+#include <wrappers.h>
 #include "test-suites.h"
 
 void
diff --git a/mockatests/cbt/test-cbt-util-create.c b/mockatests/cbt/test-cbt-util-create.c
index 8450408..3e4deae 100644
--- a/mockatests/cbt/test-cbt-util-create.c
+++ b/mockatests/cbt/test-cbt-util-create.c
@@ -35,7 +35,7 @@
 #include <errno.h>
 
 #include <cbt-util-priv.h>
-#include "wrappers.h"
+#include <wrappers.h>
 #include "test-suites.h"
 
 void test_cbt_util_create_success(void **state)
diff --git a/mockatests/cbt/test-cbt-util-get.c b/mockatests/cbt/test-cbt-util-get.c
index 982e6d6..962f1fe 100644
--- a/mockatests/cbt/test-cbt-util-get.c
+++ b/mockatests/cbt/test-cbt-util-get.c
@@ -38,7 +38,7 @@
 #include <uuid/uuid.h>
 
 #include <cbt-util-priv.h>
-#include "wrappers.h"
+#include <wrappers.h>
 #include "test-suites.h"
 
 void test_cbt_util_get_flag(void **state)
diff --git a/mockatests/cbt/test-cbt-util.c b/mockatests/cbt/test-cbt-util.c
index 0afa54d..1ebe3fe 100644
--- a/mockatests/cbt/test-cbt-util.c
+++ b/mockatests/cbt/test-cbt-util.c
@@ -33,7 +33,7 @@
 #include <setjmp.h>
 #include <cmocka.h>
 
-#include "wrappers.h"
+#include <wrappers.h>
 #include "test-suites.h"
 
 int main(void)
diff --git a/mockatests/cbt/wrappers.c b/mockatests/cbt/wrappers.c
deleted file mode 100644
index addb607..0000000
--- a/mockatests/cbt/wrappers.c
+++ /dev/null
@@ -1,228 +0,0 @@
-/*
- * 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 <stdbool.h>
-#include <stdint.h>
-#include <stdarg.h>
-#include <stddef.h>
-#include <setjmp.h>
-#include <cmocka.h>
-#include <errno.h>
-
-#include "wrappers.h"
-
-static int tests_running = 1;
-static int mock_malloc = 0;
-static int mock_fwrite = 0;
-
-void *
-__wrap_malloc(size_t size)
-{
-	bool succeed = true;
-	if (mock_malloc) {
-		succeed = (bool) mock();
-	}
-
-	if (succeed) {
-		void * result = test_malloc(size);
-		/*fprintf(stderr, "Allocated block of %zu bytes at %p\n", size, result);*/
-		return result;
-	}
-	return NULL;
-}
-
-void
-__wrap_free(void *ptr)
-{
-	/*fprintf(stderr, "Freeing block at %p\n", ptr);*/
-	test_free(ptr);
-}
-
-FILE *
-__wrap_fopen(void)
-{
-	FILE *file = (FILE*)mock();
-
-	if (file == NULL) {
-		errno = ENOENT;
-	}
-
-	return file;
-}
-
-void __real_fclose(FILE *fp);
-
-void
-__wrap_fclose(FILE *fp)
-{
-	if (tests_running) {
-		check_expected_ptr(fp);
-	}
-	__real_fclose(fp);
-}
-
-int
-__real_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
-
-int
-__wrap_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
-{
-	if (mock_fwrite) {
-
-	struct fwrite_data *data = mock();
-
-	size_t remaining = data->size - data->offset;
-	size_t len = size * nmemb;
-
-	assert_in_range(len, 0, remaining);
-	memcpy(data->buf + data->offset, ptr, len);
-	
-	data->offset += len;
-
-	return len;
-	}
-	return __real_fwrite(ptr, size, nmemb, stream);
-}
-
-struct fwrite_data *setup_fwrite_mock(int size)
-{
-	void *buf;
-	struct fwrite_data *data;
-
-	buf = test_malloc(size);
-	memset(buf, 0, size);
-
-	data = test_malloc(sizeof(struct fwrite_data));
-	data->size = size;
-	data->offset = 0;
-	data->buf = buf;
-	data->type = stdout;
-
-	will_return_always(__wrap_fwrite, data);
-
-	return data;
-}
-
-void free_fwrite_data(struct fwrite_data *data)
-{
-	if (data->buf)
-		test_free(data->buf);
-	test_free(data);
-	mock_fwrite = false;
-}
-
-int
-wrap_vprintf(const char *format, va_list ap)
-{
-
-	struct printf_data *data = mock();
-
-	int remaining = data->size - data->offset;
-
-	int len = vsnprintf(data->buf + data->offset, remaining, format, ap);
-
-	assert_in_range(len, 0, remaining);
-
-	data->offset += len;
-
-	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);
-}
-
-int
-__wrap_puts(const char *s)
-{
-	return __wrap_printf("%s\n", s);
-}
-
-struct printf_data *setup_vprintf_mock(int size)
-{
-	char *buf;
-	struct printf_data *data;
-
-	buf = test_malloc(size);
-	memset(buf, 0, size);
-
-	data = test_malloc(sizeof(struct printf_data));
-	data->size = size;
-	data->offset = 0;
-	data->buf = buf;
-
-	will_return_always(wrap_vprintf, data);
-
-	return data;
-}
-
-void free_printf_data(struct printf_data *data)
-{
-	if (data->buf)
-		test_free(data->buf);
-	test_free(data);
-}
-
-
-void malloc_succeeds(bool succeed)
-{
-	mock_malloc = true;
-	will_return(__wrap_malloc, succeed);
-}
-
-void disable_malloc_mock()
-{
-	mock_malloc = false;
-}
-
-void disable_mocks()
-{
-	tests_running = 0;
-}
-
-void enable_mock_fwrite()
-{
-	mock_fwrite = true;
-}
diff --git a/mockatests/cbt/wrappers.h b/mockatests/cbt/wrappers.h
deleted file mode 100644
index 36973e1..0000000
--- a/mockatests/cbt/wrappers.h
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * 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.
- */
-
-#ifndef __WRAPPERS_H__
-#define __WRAPPERS_H__
-
-#include <stdio.h>
-#include <stdbool.h>
-
-struct printf_data {
-	int size;
-	int offset;
-	char *buf;
-};
-
-struct fwrite_data {
-	FILE *type;
-	int size;
-	int offset;
-	char *buf;
-};
-
-FILE * __wrap_fopen(void);
-
-void __wrap_fclose(FILE *fp);
-
-struct fwrite_data *setup_fwrite_mock(int size);
-
-struct printf_data *setup_vprintf_mock(int size);
-
-void free_printf_data(struct printf_data *data);
-
-void free_fwrite_data(struct fwrite_data *data);
-
-/*
- * This enables mocking of malloc and provides a flag to control
- * whether malloc succeeds. Mocking stays in force until a call to
- * disable_malloc_mock.
- *
- * Subsequent calls to mallloc_suceeds will queue successive results
- * for the mock.
- */
-void malloc_succeeds(bool succeed);
-
-void disable_malloc_mock();
-
-void disable_mocks();
-
-void enable_mock_fwrite();
-
-#endif /* __WRAPPERS_H__ */
diff --git a/mockatests/include/wrappers.h b/mockatests/include/wrappers.h
new file mode 100644
index 0000000..cc5119b
--- /dev/null
+++ b/mockatests/include/wrappers.h
@@ -0,0 +1,84 @@
+/*
+ * 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.
+ */
+
+#ifndef __WRAPPERS_H__
+#define __WRAPPERS_H__
+
+#include <stdio.h>
+#include <stdbool.h>
+
+struct printf_data {
+	int size;
+	int offset;
+	char *buf;
+};
+
+struct fwrite_data {
+	FILE *type;
+	int size;
+	int offset;
+	char *buf;
+};
+
+FILE * __wrap_fopen(void);
+
+void __wrap_fclose(FILE *fp);
+
+struct fwrite_data *setup_fwrite_mock(int size);
+
+struct printf_data *setup_vprintf_mock(int size);
+
+void free_printf_data(struct printf_data *data);
+
+void free_fwrite_data(struct fwrite_data *data);
+
+/*
+ * This enables mocking of malloc and provides a flag to control
+ * whether malloc succeeds. Mocking stays in force until a call to
+ * disable_malloc_mock.
+ *
+ * Subsequent calls to mallloc_suceeds will queue successive results
+ * for the mock.
+ */
+void malloc_succeeds(bool succeed);
+
+void disable_malloc_mock();
+
+void disable_mocks();
+
+void enable_mock_fwrite();
+
+int __wrap_printf(const char *format, ...);
+
+int __wrap___printf_chk (int __flag, const char *format, ...);
+
+int __wrap_puts(const char *s);
+
+#endif /* __WRAPPERS_H__ */
diff --git a/mockatests/wrappers/Makefile.am b/mockatests/wrappers/Makefile.am
new file mode 100644
index 0000000..fb77143
--- /dev/null
+++ b/mockatests/wrappers/Makefile.am
@@ -0,0 +1,8 @@
+AM_CFLAGS  = -Wall
+AM_CFLAGS += -Werror
+
+AM_CPPFLAGS = -I../include
+
+noinst_LTLIBRARIES = libwrappers.la
+
+libwrappers_la_SOURCES = wrappers.c
diff --git a/mockatests/wrappers/wrappers.c b/mockatests/wrappers/wrappers.c
new file mode 100644
index 0000000..c7a506d
--- /dev/null
+++ b/mockatests/wrappers/wrappers.c
@@ -0,0 +1,228 @@
+/*
+ * 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 <stdbool.h>
+#include <stdint.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <errno.h>
+
+#include <wrappers.h>
+
+static int tests_running = 1;
+static int mock_malloc = 0;
+static int mock_fwrite = 0;
+
+void *
+__wrap_malloc(size_t size)
+{
+	bool succeed = true;
+	if (mock_malloc) {
+		succeed = (bool) mock();
+	}
+
+	if (succeed) {
+		void * result = test_malloc(size);
+		/*fprintf(stderr, "Allocated block of %zu bytes at %p\n", size, result);*/
+		return result;
+	}
+	return NULL;
+}
+
+void
+__wrap_free(void *ptr)
+{
+	/*fprintf(stderr, "Freeing block at %p\n", ptr);*/
+	test_free(ptr);
+}
+
+FILE *
+__wrap_fopen(void)
+{
+	FILE *file = (FILE*)mock();
+
+	if (file == NULL) {
+		errno = ENOENT;
+	}
+
+	return file;
+}
+
+void __real_fclose(FILE *fp);
+
+void
+__wrap_fclose(FILE *fp)
+{
+	if (tests_running) {
+		check_expected_ptr(fp);
+	}
+	__real_fclose(fp);
+}
+
+int
+__real_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
+int
+__wrap_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+	if (mock_fwrite) {
+
+	struct fwrite_data *data = mock();
+
+	size_t remaining = data->size - data->offset;
+	size_t len = size * nmemb;
+
+	assert_in_range(len, 0, remaining);
+	memcpy(data->buf + data->offset, ptr, len);
+	
+	data->offset += len;
+
+	return len;
+	}
+	return __real_fwrite(ptr, size, nmemb, stream);
+}
+
+struct fwrite_data *setup_fwrite_mock(int size)
+{
+	void *buf;
+	struct fwrite_data *data;
+
+	buf = test_malloc(size);
+	memset(buf, 0, size);
+
+	data = test_malloc(sizeof(struct fwrite_data));
+	data->size = size;
+	data->offset = 0;
+	data->buf = buf;
+	data->type = stdout;
+
+	will_return_always(__wrap_fwrite, data);
+
+	return data;
+}
+
+void free_fwrite_data(struct fwrite_data *data)
+{
+	if (data->buf)
+		test_free(data->buf);
+	test_free(data);
+	mock_fwrite = false;
+}
+
+int
+wrap_vprintf(const char *format, va_list ap)
+{
+
+	struct printf_data *data = mock();
+
+	int remaining = data->size - data->offset;
+
+	int len = vsnprintf(data->buf + data->offset, remaining, format, ap);
+
+	assert_in_range(len, 0, remaining);
+
+	data->offset += len;
+
+	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);
+}
+
+int
+__wrap_puts(const char *s)
+{
+	return __wrap_printf("%s\n", s);
+}
+
+struct printf_data *setup_vprintf_mock(int size)
+{
+	char *buf;
+	struct printf_data *data;
+
+	buf = test_malloc(size);
+	memset(buf, 0, size);
+
+	data = test_malloc(sizeof(struct printf_data));
+	data->size = size;
+	data->offset = 0;
+	data->buf = buf;
+
+	will_return_always(wrap_vprintf, data);
+
+	return data;
+}
+
+void free_printf_data(struct printf_data *data)
+{
+	if (data->buf)
+		test_free(data->buf);
+	test_free(data);
+}
+
+
+void malloc_succeeds(bool succeed)
+{
+	mock_malloc = true;
+	will_return(__wrap_malloc, succeed);
+}
+
+void disable_malloc_mock()
+{
+	mock_malloc = false;
+}
+
+void disable_mocks()
+{
+	tests_running = 0;
+}
+
+void enable_mock_fwrite()
+{
+	mock_fwrite = true;
+}
-- 
1.8.3.1

