From 4f1380a48da7cb0375810f70e68ca6282486dec1 Mon Sep 17 00:00:00 2001
From: jvoisin
Date: Sat, 18 Mar 2023 13:29:22 +0100
Subject: Add a basic testsuite
---
.github/workflows/testsuite.yaml | 19 ++++++++++++
tests/Makefile | 25 ++++++++++++++++
tests/common.h | 62 +++++++++++++++++++++++++++++++++++++++
tests/test_memcpy_dynamic_read.c | 16 ++++++++++
tests/test_memcpy_dynamic_write.c | 16 ++++++++++
tests/test_memcpy_static_read.c | 16 ++++++++++
tests/test_memcpy_static_write.c | 16 ++++++++++
7 files changed, 170 insertions(+)
create mode 100644 .github/workflows/testsuite.yaml
create mode 100644 tests/Makefile
create mode 100644 tests/common.h
create mode 100644 tests/test_memcpy_dynamic_read.c
create mode 100644 tests/test_memcpy_dynamic_write.c
create mode 100644 tests/test_memcpy_static_read.c
create mode 100644 tests/test_memcpy_static_write.c
diff --git a/.github/workflows/testsuite.yaml b/.github/workflows/testsuite.yaml
new file mode 100644
index 0000000..115b145
--- /dev/null
+++ b/.github/workflows/testsuite.yaml
@@ -0,0 +1,19 @@
+name: Test suite
+on:
+ pull_request:
+ push:
+ schedule:
+ - cron: '0 16 * * 5'
+
+jobs:
+ ci:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checking out the code
+ uses: actions/checkout@v3
+ - name: Downloading musl-based toolchain
+ run: wget https://musl.cc/x86_64-linux-musl-native.tgz
+ - name: Extracting musl-based toolchain
+ run: tar xzf ./x86_64-linux-musl-native.tgz
+ - name: Running the testsuite
+ run: make -C tests | grep -zqv FAIL
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 0000000..c48d0b0
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,25 @@
+CC=../x86_64-linux-musl-native/bin/gcc
+CFLAGS=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2
+
+TARGETS=test_memcpy_static_write \
+ test_memcpy_dynamic_write \
+ test_memcpy_static_read \
+ test_memcpy_dynamic_read
+
+.SILENT:
+
+all: $(TARGETS) run
+
+$(TARGETS): %: %.c
+ $(CC) $(CFLAGS) -o $@ $<
+
+run: $(TARGETS)
+ $(foreach EXE, $(TARGETS), \
+ ./$(EXE) 1 2 3 4 5 6 7 8 9 0 >/dev/null && echo "$(EXE) OK" || echo "$(EXE) FAIL" ; \
+ )
+
+clean:
+ $(foreach EXE, $(TARGETS), \
+ rm -f ./$(EXE) \
+ )
+
diff --git a/tests/common.h b/tests/common.h
new file mode 100644
index 0000000..4bd5a4e
--- /dev/null
+++ b/tests/common.h
@@ -0,0 +1,62 @@
+/* Copyright (C) 2004-2020 Free Software Foundation, Inc.
+ This snippet is taken from debug/tst-chk1 in the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ . */
+
+#include
+#include
+#include
+#include
+
+volatile int chk_fail_ok;
+volatile int ret;
+jmp_buf chk_fail_buf;
+
+static void
+handler (int sig)
+{
+ if (chk_fail_ok)
+ {
+ chk_fail_ok = 0;
+ longjmp (chk_fail_buf, 1);
+ }
+ else
+ _exit (127);
+}
+
+void
+__attribute__((constructor))
+set_fortify_handler (void)
+{
+ struct sigaction sa;
+
+ sa.sa_handler = handler;
+ sa.sa_flags = 0;
+ sigemptyset (&sa.sa_mask);
+
+ sigaction (SIGILL, &sa, NULL);
+}
+
+#define FAIL() \
+ do { fprintf (stderr, "Failure on line %d\n", __LINE__); ret = 1; } while (0)
+#define CHK_FAIL_START \
+ chk_fail_ok = 1; \
+ if (! setjmp (chk_fail_buf)) \
+ {
+#define CHK_FAIL_END \
+ chk_fail_ok = 0; \
+ FAIL (); \
+ }
+
diff --git a/tests/test_memcpy_dynamic_read.c b/tests/test_memcpy_dynamic_read.c
new file mode 100644
index 0000000..65cb77a
--- /dev/null
+++ b/tests/test_memcpy_dynamic_read.c
@@ -0,0 +1,16 @@
+#include "common.h"
+
+#include
+
+int main(int argc, char** argv) {
+ char buffer[12] = {0};
+ memcpy(buffer, "1234567890", sizeof(buffer) - 1);
+ puts(buffer);
+
+ CHK_FAIL_START
+ memcpy(buffer, "123456", argc);
+ CHK_FAIL_END
+
+ puts(buffer);
+ return ret;
+}
diff --git a/tests/test_memcpy_dynamic_write.c b/tests/test_memcpy_dynamic_write.c
new file mode 100644
index 0000000..60a594f
--- /dev/null
+++ b/tests/test_memcpy_dynamic_write.c
@@ -0,0 +1,16 @@
+#include "common.h"
+
+#include
+
+int main(int argc, char** argv) {
+ char buffer[8] = {0};
+ memcpy(buffer, "1234567890", sizeof(buffer) - 1);
+ puts(buffer);
+
+ CHK_FAIL_START
+ memcpy(buffer, "1234567890", argc);
+ CHK_FAIL_END
+
+ puts(buffer);
+ return ret;
+}
diff --git a/tests/test_memcpy_static_read.c b/tests/test_memcpy_static_read.c
new file mode 100644
index 0000000..fed67a8
--- /dev/null
+++ b/tests/test_memcpy_static_read.c
@@ -0,0 +1,16 @@
+#include "common.h"
+
+#include
+
+int main(int argc, char** argv) {
+ char buffer[8] = {0};
+ memcpy(buffer, "123456", 4);
+ puts(buffer);
+
+ CHK_FAIL_START
+ memcpy(buffer, "123456", sizeof(buffer));
+ CHK_FAIL_END
+
+ puts(buffer);
+ return ret;
+}
diff --git a/tests/test_memcpy_static_write.c b/tests/test_memcpy_static_write.c
new file mode 100644
index 0000000..1a378f8
--- /dev/null
+++ b/tests/test_memcpy_static_write.c
@@ -0,0 +1,16 @@
+#include "common.h"
+
+#include
+
+int main(int argc, char** argv) {
+ char buffer[8] = {0};
+ memcpy(buffer, "1234567890", sizeof(buffer) - 1);
+ puts(buffer);
+
+ CHK_FAIL_START
+ memcpy(buffer, "1234567890", sizeof(buffer) + 1);
+ CHK_FAIL_END
+
+ puts(buffer);
+ return ret;
+}
--
cgit v1.3