From 327b84b18185b6febcd5f61df06d563c02ecf769 Mon Sep 17 00:00:00 2001 From: neok-m4700 Date: Mon, 15 Feb 2021 18:31:00 +0100 Subject: [PATCH] rework testing under tox --- MANIFEST.in | 2 +- test_doc.py => check_doc.py | 11 ++--------- doc/install.rst | 2 +- tests/crash/run.sh | 27 +++++++++++++++++++++++++++ tests/crash/stack_overflow.c | 5 +++-- tests/test_strace.py | 11 +++++++++++ tox.ini | 4 ++-- 7 files changed, 47 insertions(+), 15 deletions(-) rename test_doc.py => check_doc.py (83%) mode change 100755 => 100644 create mode 100644 tests/crash/run.sh diff --git a/MANIFEST.in b/MANIFEST.in index 653548e..82eb382 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -22,7 +22,7 @@ include setup_cptrace.py # Tests include runtests.py -include test_doc.py +include check_doc.py include tests/test_*.py include tests/crash/*.c include tests/crash/BSDmakefile diff --git a/test_doc.py b/check_doc.py old mode 100755 new mode 100644 similarity index 83% rename from test_doc.py rename to check_doc.py index aef5cac..8f7b022 --- a/test_doc.py +++ b/check_doc.py @@ -2,6 +2,7 @@ from doctest import testfile, ELLIPSIS, testmod from sys import exit, path as sys_path from os.path import dirname +import importlib def testDoc(filename, name=None): @@ -13,17 +14,9 @@ def testDoc(filename, name=None): print("--- %s: End of tests" % filename) -def importModule(name): - mod = __import__(name) - components = name.split('.') - for comp in components[1:]: - mod = getattr(mod, comp) - return mod - - def testModule(name): print("--- Test module %s" % name) - module = importModule(name) + module = importlib.import_module(name) failure, nb_test = testmod(module) if failure: exit(1) diff --git a/doc/install.rst b/doc/install.rst index e5652bf..f01fa7f 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -84,7 +84,7 @@ Run tests manually Type:: python3 runtests.py - python3 test_doc.py + python3 check_doc.py It's also possible to run a specific test:: diff --git a/tests/crash/run.sh b/tests/crash/run.sh new file mode 100644 index 0000000..90806d8 --- /dev/null +++ b/tests/crash/run.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +trace() { + ( + set +e + python ../../strace.py -e execve $1; ec=$? + if [ $ec -gt 0 ]; then + exit $(($ec - 128 - $2)) + fi + ) +} + +if command -v gcc && command -v make && command -v kill; then + make || exit + + # trace ./invalid_read $(kill -l SEGV) |& tee /dev/stderr | grep -q 'Invalid read from' + trace ./invalid_read $(kill -l SEGV) # 2>&1 | grep -q 'Invalid read from' + trace ./invalid_write $(kill -l SEGV) # 2>&1 | grep -q 'Invalid write to' + trace ./stack_overflow $(kill -l SEGV) # 2>&1 | grep -q 'STACK OVERFLOW!' + trace ./call_null $(kill -l SEGV) + trace ./abort $(kill -l ABRT) + trace ./div_zero $(kill -l FPE) + trace ./socket_ipv4_tcp + trace ./pthread + trace ./execve + trace ./fork +fi diff --git a/tests/crash/stack_overflow.c b/tests/crash/stack_overflow.c index 45f26a9..9baa710 100644 --- a/tests/crash/stack_overflow.c +++ b/tests/crash/stack_overflow.c @@ -1,7 +1,8 @@ char toto() { - char buffer[4096]; - buffer[0] = 0; + volatile unsigned char buffer[4096]; + buffer[0] = 1; + buffer[4095] = 0; toto(); return buffer[0] + buffer[sizeof(buffer)-1]; } diff --git a/tests/test_strace.py b/tests/test_strace.py index 39ff14c..b586600 100755 --- a/tests/test_strace.py +++ b/tests/test_strace.py @@ -6,12 +6,16 @@ import tempfile import unittest import signal +import shutil +import platform STRACE = os.path.normpath( os.path.join(os.path.dirname(__file__), '..', 'strace.py')) AARCH64 = (getattr(os.uname(), 'machine', None) == 'aarch64') +UNTESTED = platform.system() not in ('Linux', 'FreeBSD') + class TestStrace(unittest.TestCase): def strace(self, *args): @@ -106,6 +110,13 @@ def test_socket(self): "import socket; socket.socket(socket.AF_INET,socket.SOCK_STREAM).close()", br'^socket\(AF_INET, SOCK_STREAM(\|SOCK_CLOEXEC)?') + @unittest.skipIf(UNTESTED, 'Untested system/OS') + def test_crash(self): + dn = os.path.join(os.path.dirname(__file__), 'crash') + shell = shutil.which('bash') + if shell: + self.assertEqual(subprocess.call([shell, '-e', 'run.sh'], cwd=dn), 0) + if __name__ == "__main__": unittest.main() diff --git a/tox.ini b/tox.ini index a15647a..70f70ab 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ envlist = py3, pep8 [testenv] basepython = python3 commands= - python test_doc.py + python check_doc.py python runtests.py -v [testenv:py3] @@ -13,7 +13,7 @@ basepython = python3 [testenv:pep8] deps = flake8 commands = - flake8 ptrace/ tests/ gdb.py runtests.py setup_cptrace.py setup.py strace.py SYSCALL_PROTOTYPES.codegen.py test_doc.py + flake8 ptrace/ tests/ gdb.py runtests.py setup_cptrace.py setup.py strace.py SYSCALL_PROTOTYPES.codegen.py check_doc.py [flake8] # E501 line too long (88 > 79 characters)