Initial commit
This commit is contained in:
81
p1-check/tests/Makefile
Normal file
81
p1-check/tests/Makefile
Normal file
@@ -0,0 +1,81 @@
|
||||
#
|
||||
# Simple Test Makefile
|
||||
# Mike Lam, James Madison University, August 2016
|
||||
#
|
||||
# This version of the Makefile includes support for building a test suite. The
|
||||
# recommended framework is Check (http://check.sourceforge.net/). To build and
|
||||
# run the test suite, execute the "test" target. The test suite must be located
|
||||
# in a module called "testsuite". The MODS, LIBS, and OBJS variables work as
|
||||
# they do in the main Makefile.
|
||||
#
|
||||
# To change the default build target (which executes when you just type
|
||||
# "make"), change the right-hand side of the definition of the "default"
|
||||
# target.
|
||||
#
|
||||
# By default, this makefile will build the project with debugging symbols and
|
||||
# without optimization. To change this, edit or remove the "-g" and "-O0"
|
||||
# options in CFLAGS and LDFLAGS accordingly.
|
||||
#
|
||||
# By default, this makefile build the application using the GNU C compiler,
|
||||
# adhering to the C99 standard with all warnings enabled.
|
||||
|
||||
|
||||
# application-specific settings and run target
|
||||
|
||||
EXE=../y86
|
||||
TEST=testsuite
|
||||
MODS=public.o
|
||||
OBJS=../p1-check.o private.o
|
||||
LIBS=
|
||||
|
||||
UTESTOUT=utests.txt
|
||||
ITESTOUT=itests.txt
|
||||
|
||||
default: $(TEST)
|
||||
|
||||
$(EXE):
|
||||
make -C ../
|
||||
|
||||
test: utest itest
|
||||
@echo "========================================"
|
||||
|
||||
utest: $(EXE) $(TEST)
|
||||
@echo "========================================"
|
||||
@echo " UNIT TESTS"
|
||||
@./$(TEST) 2>/dev/null >$(UTESTOUT)
|
||||
@cat $(UTESTOUT) | sed -n -e '/Checks/,$$p' | sed -e 's/^private.*:[EF]://g'
|
||||
|
||||
itest: $(EXE)
|
||||
@echo "========================================"
|
||||
@echo " INTEGRATION TESTS"
|
||||
@./integration.sh | tee $(ITESTOUT)
|
||||
|
||||
|
||||
# compiler/linker settings
|
||||
|
||||
CC=gcc
|
||||
CFLAGS=-g -O0 -Wall --std=c99 -pedantic
|
||||
LDFLAGS=-g -O0
|
||||
|
||||
CFLAGS+=-I/opt/homebrew/include -Wno-gnu-zero-variadic-macro-arguments
|
||||
LDFLAGS+=-L/opt/homebrew/lib
|
||||
LIBS+=-lcheck -lm -lpthread
|
||||
|
||||
ifeq ($(shell uname -s),Linux)
|
||||
LIBS+=-lrt -lsubunit
|
||||
endif
|
||||
|
||||
|
||||
# build targets
|
||||
|
||||
$(TEST): $(TEST).o $(MODS) $(OBJS)
|
||||
$(CC) $(LDFLAGS) -o $(TEST) $^ $(LIBS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $(CFLAGS) $<
|
||||
|
||||
clean:
|
||||
rm -rf $(TEST) $(TEST).o $(MODS) $(UTESTOUT) $(ITESTOUT) outputs valgrind
|
||||
|
||||
.PHONY: default clean test unittest inttest
|
||||
|
||||
1
p1-check/tests/expected/A_bad_magic.txt
Normal file
1
p1-check/tests/expected/A_bad_magic.txt
Normal file
@@ -0,0 +1 @@
|
||||
Failed to read file
|
||||
1
p1-check/tests/expected/A_bad_magic_no_H.txt
Normal file
1
p1-check/tests/expected/A_bad_magic_no_H.txt
Normal file
@@ -0,0 +1 @@
|
||||
Failed to read file
|
||||
4
p1-check/tests/expected/A_invalid_multi_files.txt
Normal file
4
p1-check/tests/expected/A_invalid_multi_files.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
Usage: ../y86 <option(s)> mini-elf-file
|
||||
Options are:
|
||||
-h Display usage
|
||||
-H Show the Mini-ELF header
|
||||
4
p1-check/tests/expected/A_invalid_multi_params.txt
Normal file
4
p1-check/tests/expected/A_invalid_multi_params.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
Usage: ../y86 <option(s)> mini-elf-file
|
||||
Options are:
|
||||
-h Display usage
|
||||
-H Show the Mini-ELF header
|
||||
4
p1-check/tests/expected/A_invalid_param.txt
Normal file
4
p1-check/tests/expected/A_invalid_param.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
Usage: ../y86 <option(s)> mini-elf-file
|
||||
Options are:
|
||||
-h Display usage
|
||||
-H Show the Mini-ELF header
|
||||
4
p1-check/tests/expected/A_missing_filename.txt
Normal file
4
p1-check/tests/expected/A_missing_filename.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
Usage: ../y86 <option(s)> mini-elf-file
|
||||
Options are:
|
||||
-h Display usage
|
||||
-H Show the Mini-ELF header
|
||||
1
p1-check/tests/expected/A_nonexistent_file.txt
Normal file
1
p1-check/tests/expected/A_nonexistent_file.txt
Normal file
@@ -0,0 +1 @@
|
||||
Failed to read file
|
||||
1
p1-check/tests/expected/A_short_header.txt
Normal file
1
p1-check/tests/expected/A_short_header.txt
Normal file
@@ -0,0 +1 @@
|
||||
Failed to read file
|
||||
1
p1-check/tests/expected/A_short_header_no_H.txt
Normal file
1
p1-check/tests/expected/A_short_header_no_H.txt
Normal file
@@ -0,0 +1 @@
|
||||
Failed to read file
|
||||
4
p1-check/tests/expected/B_help.txt
Normal file
4
p1-check/tests/expected/B_help.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
Usage: ../y86 <option(s)> mini-elf-file
|
||||
Options are:
|
||||
-h Display usage
|
||||
-H Show the Mini-ELF header
|
||||
6
p1-check/tests/expected/B_multisegment.txt
Normal file
6
p1-check/tests/expected/B_multisegment.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
01 00 00 01 10 00 04 00 00 00 00 00 45 4c 46 00
|
||||
Mini-ELF version 1
|
||||
Entry point 0x100
|
||||
There are 4 program headers, starting at offset 16 (0x10)
|
||||
There is no symbol table present
|
||||
There is no string table present
|
||||
6
p1-check/tests/expected/B_simple_full.txt
Normal file
6
p1-check/tests/expected/B_simple_full.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
01 00 00 01 10 00 02 00 58 00 70 00 45 4c 46 00
|
||||
Mini-ELF version 1
|
||||
Entry point 0x100
|
||||
There are 2 program headers, starting at offset 16 (0x10)
|
||||
There is a symbol table starting at offset 88 (0x58)
|
||||
There is a string table starting at offset 112 (0x70)
|
||||
6
p1-check/tests/expected/B_stack.txt
Normal file
6
p1-check/tests/expected/B_stack.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
01 00 00 01 10 00 05 00 f4 00 16 01 45 4c 46 00
|
||||
Mini-ELF version 1
|
||||
Entry point 0x100
|
||||
There are 5 program headers, starting at offset 16 (0x10)
|
||||
There is a symbol table starting at offset 244 (0xf4)
|
||||
There is a string table starting at offset 278 (0x116)
|
||||
6
p1-check/tests/expected/B_stripped.txt
Normal file
6
p1-check/tests/expected/B_stripped.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
01 00 00 01 10 00 02 00 00 00 00 00 45 4c 46 00
|
||||
Mini-ELF version 1
|
||||
Entry point 0x100
|
||||
There are 2 program headers, starting at offset 16 (0x10)
|
||||
There is no symbol table present
|
||||
There is no string table present
|
||||
0
p1-check/tests/expected/C_no_output.txt
Normal file
0
p1-check/tests/expected/C_no_output.txt
Normal file
6
p1-check/tests/expected/C_simple_hex-2.txt
Normal file
6
p1-check/tests/expected/C_simple_hex-2.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
01 00 00 01 10 00 02 00 58 00 70 00 45 4c 46 00
|
||||
Mini-ELF version 1
|
||||
Entry point 0x100
|
||||
There are 2 program headers, starting at offset 16 (0x10)
|
||||
There is a symbol table starting at offset 88 (0x58)
|
||||
There is a string table starting at offset 112 (0x70)
|
||||
1
p1-check/tests/expected/C_simple_hex.txt
Normal file
1
p1-check/tests/expected/C_simple_hex.txt
Normal file
@@ -0,0 +1 @@
|
||||
01 00 00 01 10 00 02 00 58 00 70 00 45 4c 46 00
|
||||
BIN
p1-check/tests/inputs/bad-no_elf.o
Normal file
BIN
p1-check/tests/inputs/bad-no_elf.o
Normal file
Binary file not shown.
BIN
p1-check/tests/inputs/bad-short_header.o
Normal file
BIN
p1-check/tests/inputs/bad-short_header.o
Normal file
Binary file not shown.
BIN
p1-check/tests/inputs/multiseg.o
Normal file
BIN
p1-check/tests/inputs/multiseg.o
Normal file
Binary file not shown.
BIN
p1-check/tests/inputs/simple.o
Normal file
BIN
p1-check/tests/inputs/simple.o
Normal file
Binary file not shown.
BIN
p1-check/tests/inputs/stack.o
Normal file
BIN
p1-check/tests/inputs/stack.o
Normal file
Binary file not shown.
BIN
p1-check/tests/inputs/stripped.o
Normal file
BIN
p1-check/tests/inputs/stripped.o
Normal file
Binary file not shown.
87
p1-check/tests/integration.sh
Normal file
87
p1-check/tests/integration.sh
Normal file
@@ -0,0 +1,87 @@
|
||||
#!/bin/bash
|
||||
|
||||
# extract executable name from Makefile
|
||||
EXE=$(grep "EXE=" Makefile | sed -e "s/EXE=//")
|
||||
|
||||
# detect timeout utility (i.e., "timeout" on Linux and "gtimeout" on MacOS)
|
||||
# and set timeout interval
|
||||
TIMEOUT="timeout"
|
||||
TIMEOUT_INTERVAL="3s"
|
||||
$TIMEOUT --help &>/dev/null
|
||||
if [[ $? -ne 0 ]]; then
|
||||
TIMEOUT="gtimeout"
|
||||
fi
|
||||
|
||||
# Valgrind additional output flags
|
||||
VG_FLAGS="--leak-check=full --track-origins=yes"
|
||||
|
||||
function run_test {
|
||||
|
||||
# parameters
|
||||
TAG=$1
|
||||
ARGS=$2
|
||||
PTAG=$(printf '%-30s' "$TAG")
|
||||
|
||||
# file paths
|
||||
OUTPUT=outputs/$TAG.txt
|
||||
DIFF=outputs/$TAG.diff
|
||||
EXPECT=expected/$TAG.txt
|
||||
VALGRND=valgrind/$TAG.txt
|
||||
|
||||
# run test with timeout
|
||||
$TIMEOUT $TIMEOUT_INTERVAL $EXE $ARGS 2>/dev/null >"$OUTPUT"
|
||||
if [ "$?" -lt 124 ]; then
|
||||
|
||||
# no timeout; compare output to the expected version
|
||||
diff -u "$OUTPUT" "$EXPECT" >"$DIFF"
|
||||
if [ -s "$DIFF" ]; then
|
||||
|
||||
# try alternative solution (if it exists)
|
||||
EXPECT=expected/$TAG-2.txt
|
||||
if [ -e "$EXPECT" ]; then
|
||||
diff -u "$OUTPUT" "$EXPECT" >"$DIFF"
|
||||
if [ -s "$DIFF" ]; then
|
||||
echo "$PTAG FAIL (see ${TPREFIX}$DIFF for details)"
|
||||
else
|
||||
echo "$PTAG pass"
|
||||
fi
|
||||
else
|
||||
echo "$PTAG FAIL (see ${TPREFIX}$DIFF for details)"
|
||||
fi
|
||||
else
|
||||
echo "$PTAG pass"
|
||||
fi
|
||||
|
||||
# run valgrind
|
||||
$TIMEOUT $TIMEOUT_INTERVAL valgrind $VG_FLAGS $EXE $ARGS &>$VALGRND
|
||||
else
|
||||
echo "$PTAG FAIL (crash or timeout)"
|
||||
fi
|
||||
}
|
||||
|
||||
# initialize output folders
|
||||
mkdir -p outputs
|
||||
mkdir -p valgrind
|
||||
rm -f outputs/* valgrind/*
|
||||
|
||||
# run individual tests
|
||||
source itests.include
|
||||
|
||||
# check for memory leaks
|
||||
LEAK=`cat valgrind/*.txt | grep 'definitely lost' | grep -v ' 0 bytes in 0 blocks'`
|
||||
if [ -z "$LEAK" ]; then
|
||||
echo "No memory leak found."
|
||||
else
|
||||
echo "Memory leak(s) found. See files listed below for details."
|
||||
grep 'definitely lost' valgrind/*.txt | sed -e 's/:.*$//g' | awk "{print \" - ${TPREFIX}\" \$0}"
|
||||
fi
|
||||
|
||||
# check for uninitialized values
|
||||
LEAK=`cat valgrind/*.txt | grep 'uninitialised value'`
|
||||
if [ -z "$LEAK" ]; then
|
||||
echo "No uninitialized value found."
|
||||
else
|
||||
echo "Uninitialized value(s) found. See files listed below for details."
|
||||
grep 'uninitialised value' valgrind/*.txt | sed -e 's/:.*$//g' | awk "{print \" - ${TPREFIX}\" \$0}"
|
||||
fi
|
||||
|
||||
21
p1-check/tests/itests.include
Normal file
21
p1-check/tests/itests.include
Normal file
@@ -0,0 +1,21 @@
|
||||
# list of integration tests
|
||||
# format: run_test <TAG> <ARGS>
|
||||
# <TAG> used as the root for all filenames (i.e., "expected/$TAG.txt")
|
||||
# <ARGS> command-line arguments to test
|
||||
|
||||
run_test C_simple_hex "-H inputs/simple.o"
|
||||
run_test C_no_output "inputs/simple.o"
|
||||
run_test B_help "-h"
|
||||
run_test B_simple_full "-H inputs/simple.o"
|
||||
run_test B_multisegment "-H inputs/multiseg.o"
|
||||
run_test B_stripped "-H inputs/stripped.o"
|
||||
run_test B_stack "-H inputs/stack.o"
|
||||
run_test A_invalid_param "-x"
|
||||
run_test A_invalid_multi_files "-H inputs/simple.o inputs/multiseg.o"
|
||||
run_test A_invalid_multi_params "-H inputs/simple.o -H inputs/multiseg.o"
|
||||
run_test A_missing_filename "-H"
|
||||
run_test A_nonexistent_file "-H nonexist.o"
|
||||
run_test A_bad_magic "-H inputs/bad-no_elf.o"
|
||||
run_test A_short_header "-H inputs/bad-short_header.o"
|
||||
run_test A_bad_magic_no_H "inputs/bad-no_elf.o"
|
||||
run_test A_short_header_no_H "inputs/bad-short_header.o"
|
||||
BIN
p1-check/tests/private.o
Normal file
BIN
p1-check/tests/private.o
Normal file
Binary file not shown.
26
p1-check/tests/public.c
Normal file
26
p1-check/tests/public.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <check.h>
|
||||
|
||||
#include "../p1-check.h"
|
||||
|
||||
/* read_header is declared and defined */
|
||||
START_TEST (C_sanity_read_header_is_declared)
|
||||
{
|
||||
elf_hdr_t elf;
|
||||
FILE *fp = fopen ("inputs/simple.o", "r");
|
||||
ck_assert (fp != NULL);
|
||||
bool rc = read_header (fp, &elf);
|
||||
ck_assert (rc || !rc);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
void public_tests (Suite *s)
|
||||
{
|
||||
TCase *tc_public = tcase_create ("Public");
|
||||
tcase_add_test (tc_public, C_sanity_read_header_is_declared);
|
||||
suite_add_tcase (s, tc_public);
|
||||
}
|
||||
|
||||
34
p1-check/tests/testsuite.c
Normal file
34
p1-check/tests/testsuite.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <check.h>
|
||||
|
||||
extern void public_tests (Suite *s);
|
||||
extern void private_tests (Suite *s);
|
||||
|
||||
Suite * test_suite (void)
|
||||
{
|
||||
Suite *s = suite_create ("Default");
|
||||
public_tests (s);
|
||||
private_tests (s);
|
||||
return s;
|
||||
}
|
||||
|
||||
void run_testsuite (void)
|
||||
{
|
||||
Suite *s = test_suite ();
|
||||
SRunner *sr = srunner_create (s);
|
||||
srunner_run_all (sr, CK_NORMAL);
|
||||
srunner_free (sr);
|
||||
}
|
||||
|
||||
int main (void)
|
||||
{
|
||||
srand((unsigned)time(NULL));
|
||||
run_testsuite ();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user