From 90c4a52b4812295facdd64ae35dc2fcf6c3b6dc4 Mon Sep 17 00:00:00 2001 From: Fabian Freyer Date: Sun, 18 Feb 2018 15:23:07 +0000 Subject: [PATCH] tests: incorporate test kernel Contributes to #1, #2 --- .gitignore | 3 ++- Makefile | 1 + tests/Kyuafile | 1 + tests/Makefile | 33 ++++++++++++++++++++++-- tests/data-multiboot.h | 42 ++++++++++++++++++++++++++++++ tests/test-multiboot.c | 58 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 tests/data-multiboot.h create mode 100644 tests/test-multiboot.c diff --git a/.gitignore b/.gitignore index 3a65f80..0169cf8 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ peda-session-* !.codecov.yml tests/test-allocator -tests/test-mock-bhyveload \ No newline at end of file +tests/test-mock-bhyveload +tests/test-multiboot diff --git a/Makefile b/Makefile index 21c864d..c3aa838 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ CFLAGS+= -g OBJS= loader.o multiboot.o allocator.o LIBELF= -lelf +.export LIBELF .PHONY: all all: libmultiboot.so libmultiboot.so.full libmultiboot.a libmultiboot_p.a diff --git a/tests/Kyuafile b/tests/Kyuafile index 8876eaa..e2872cf 100644 --- a/tests/Kyuafile +++ b/tests/Kyuafile @@ -4,3 +4,4 @@ test_suite("libmultiboot") atf_test_program{name="test-allocator", } atf_test_program{name="test-mock-bhyveload", } +atf_test_program{name="test-multiboot", } diff --git a/tests/Makefile b/tests/Makefile index 668a6d3..11c6bee 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,8 +1,19 @@ -LDFLAGS=-L/usr/local/lib +LIBELF= -lelf LIBATF_C=/usr/local/lib/libatf-c.a +LDFLAGS=-L/usr/local/lib + +UNAME_S!=uname -s +OUTPUT_BFD=elf64-x86-64 +.if ${UNAME_S:MFreeBSD} +OUTPUT_BFD=elf64-x86-64-freebsd +.endif + +MB_DIR=data/multiboot +MB_TESTCASES=data_multiboot_mmap.o data_multiboot_modules.o + .PHONY: all -all: test-allocator test-loader test-mock-bhyveload +all: test-allocator test-loader test-mock-bhyveload test-multiboot .PHONY: check check: all @@ -11,12 +22,30 @@ check: all .PHONY: clean clean: + $(MAKE) -C data/multiboot clean rm -f test-allocator.o rm -f test-mock-bhyveload.o rm -f mock/bhyveload.o rm -f test-allocator rm *.gcda *.gcno *.gcov +.PHONY: ${MB_DIR}/mmap.elf +${MB_DIR}/mmap.elf: + $(MAKE) -C data/multiboot mmap.elf + +.PHONY: ${MB_DIR}/modules.elf +${MB_DIR}/modules.elf: + $(MAKE) -C data/multiboot modules.elf + +data_multiboot_mmap.o: ${MB_DIR}/mmap.elf + $(OBJCOPY) --input binary --binary-architecture i386 --output elf64-x86-64-freebsd $> $@ + +data_multiboot_modules.o: ${MB_DIR}/modules.elf + $(OBJCOPY) --input binary --binary-architecture i386 --output elf64-x86-64-freebsd $> $@ + +test-multiboot: test-multiboot.o ${MB_TESTCASES} ../libmultiboot_p.a + $(CC) ${LDFLAGS} ${CFLAGS} -o $@ $> ${LIBATF_C} ${LIBELF} + test-mock-bhyveload: test-mock-bhyveload.o mock/bhyveload.o mock/compat-strlcat.o mock/compat-strlcpy.o $(CC) ${LDFLAGS} ${CFLAGS} -o $@ $> ${LIBATF_C} ${LIBELF} diff --git a/tests/data-multiboot.h b/tests/data-multiboot.h new file mode 100644 index 0000000..88f6451 --- /dev/null +++ b/tests/data-multiboot.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2018, Fabian Freyer + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of the 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 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. + */ + +#pragma once + +#pragma once + +#define MB_TESTCASE(name) \ + extern char _binary_data_multiboot_ ## name ## _elf_end; \ + extern char _binary_data_multiboot_ ## name ## _elf_start; \ + +#define MB_TESTDATA_SYMBOL(name, suffix) _binary_data_multiboot_ ## name ## _elf_ ## suffix +#define MB_TESTDATA_START(name) &MB_TESTDATA_SYMBOL(name, start) +#define MB_TESTDATA_END(name) &MB_TESTDATA_SYMBOL(name, end) +#define MB_TESTDATA_SIZE(name) (MB_TESTDATA_START(name) - MB_TESTDATA_END(name)) + +MB_TESTCASE(mmap) +MB_TESTCASE(modules) diff --git a/tests/test-multiboot.c b/tests/test-multiboot.c new file mode 100644 index 0000000..c67e133 --- /dev/null +++ b/tests/test-multiboot.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018, Fabian Freyer + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of the 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 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 +#include +#include + +ATF_TC(testdata); +ATF_TC_HEAD(testdata, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test whether multiboot test data exists"); +} +ATF_TC_BODY(testdata, tc) +{ + printf("%p", MB_TESTDATA_START(mmap)); + ATF_CHECK_EQ_MSG(0, (NULL == MB_TESTDATA_START(mmap)), + "mmap testcase data not found"); + ATF_CHECK_EQ_MSG(0, (NULL == MB_TESTDATA_END(mmap)), + "mmap testcase data end not found"); + ATF_CHECK_EQ_MSG(0, (0 < MB_TESTDATA_SIZE(mmap)), "mmap test size"); + + ATF_CHECK_EQ_MSG(0, (NULL == MB_TESTDATA_START(modules)), + "modules testcase data not found"); + ATF_CHECK_EQ_MSG(0, (NULL == MB_TESTDATA_END(modules)), + "modules testcase data end not found"); + ATF_CHECK_EQ_MSG(0, (0 < MB_TESTDATA_SIZE(modules)), "mmap test size"); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, testdata); + + return atf_no_error(); +}