Skip to content

Commit

Permalink
Implement initial test framework
Browse files Browse the repository at this point in the history
This should make testing across multiple platforms easier.
  • Loading branch information
mkozlowski committed Sep 27, 2024
1 parent d671568 commit bdf159d
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,16 @@ $(B)/libencrypt.so: libencrypt.c
@size $@


tests: $(B)/memcr
$(MAKE) -C tests CC=$(CC) MEMCR=../$<


clean:
rm -f $(B)/*.o $(B)/*.s $(B)/*.bin $(B)/parasite-blob.h $(B)/memcr $(B)/memcr-client $(B)/libencrypt.so
$(MAKE) -C tests clean


help:
@echo 'Clean target:'
@echo ' clean - remove generated files'
@echo ''
@echo 'Build targets:'
@echo ' all - build all targets'
@echo ' memcr - build memcr binary'
Expand All @@ -181,5 +184,11 @@ help:
@echo ' COMPRESS_LZ4=1 - compile in support for memory dump LZ4 compression'
@echo ' CHECKSUM_MD5=1 - compile in support for memory dump MD5 checksumming'
@echo ' ENCRYPT=1 - compile libencrypt.so that can be preloaded for memcr'
@echo ''
@echo 'Test targets:'
@echo ' tests - build and run memcr tests'
@echo ''
@echo 'Clean targets:'
@echo ' clean - remove generated files'

.PHONY: all clean help
.PHONY: all tests clean help
25 changes: 25 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
MAKEFLAGS += -rR

ifeq ("$(origin CC)", "default")
undefine CC
endif

CC ?= $(CROSS_COMPILE)gcc

CFLAGS = -Wall -Werror -g -O2

SRC = $(wildcard *.c)
BIN = $(SRC:%.c=%)

MEMCR ?= ../memcr

all: $(BIN)
@./run.sh $(MEMCR)

$(BIN): %: %.c Makefile
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

clean:
rm -f $(BIN)

.PHONY: all clean
46 changes: 46 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh

#set -x

# $1 should point to memcr executable
[ -n "$1" ] || exit 1
[ -x "$1" ] || exit 2

MEMCR=$1

CUID=$(id -u)
if [ "$CUID" != "0" ]; then
DO="sudo"
else
DO=""
fi

TESTS="test-malloc"

for TEST in $TESTS; do
echo "######## running $TEST ########"

# start the test
./$TEST &

# wait
sleep 0.2

# memcr
$DO $MEMCR -p $! -n -f
if [ $? -ne 0 ]; then
kill $!
echo "[-] $TEST failed"
break
fi

# stop the test
kill -USR1 $!
wait $!
if [ $? -eq 0 ]; then
echo "[+] $TEST passed"
else
echo "[-] $TEST failed"
break
fi
done
61 changes: 61 additions & 0 deletions tests/test-malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <assert.h>

#define PFX "[test-malloc] "

#define TEST_SIZE (16 * 1024 * 1024) /* 16 MB */

static volatile sig_atomic_t signalled;

static char mema[TEST_SIZE];

static void sighandler(int num)
{
signalled = num;
}

int main(int argc, char *argv[])
{
int ret;
void *memb;

signal(SIGUSR1, sighandler);

printf(PFX "pid %d\n", getpid());

memset(mema, 0x5b, sizeof(mema));

memb = malloc(TEST_SIZE);
assert(memb);

memset(mema, 0x5b, sizeof(mema));
memcpy(memb, mema, sizeof(mema));

ret = memcmp(mema, memb, sizeof(mema));
assert(ret == 0);

printf(PFX "mema %d kB @ %p\n", TEST_SIZE / 1024, mema);
printf(PFX "memb %d kB @ %p\n", TEST_SIZE / 1024, memb);

printf(PFX "waiting for SIGUSR1\n");

while (!signalled)
usleep(10 * 1000);

printf(PFX "signalled (%s)\n", strsignal(signalled));

ret = memcmp(mema, memb, sizeof(mema));
assert(ret == 0);

printf(PFX "ok\n");

free(memb);
return 0;
}

0 comments on commit bdf159d

Please sign in to comment.