summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2015-06-08 15:00:34 +0100
committerSam Thursfield <sam.thursfield@codethink.co.uk>2015-06-08 15:00:34 +0100
commitb502a569b4f5c1b16b97b9813df1983c9e776a65 (patch)
treed9bca49cd9ea7e38a9d8b7c57136482354055f0b
parent52d17b49c1da72d43a41c0fd3bfdcf6a4308e0d3 (diff)
downloadsandboxlib-b502a569b4f5c1b16b97b9813df1983c9e776a65.tar.gz
Commit programs.py under correct name
This means the test suite actually works now.
-rw-r--r--tests/programs.py (renamed from tests/programs.c)81
1 files changed, 61 insertions, 20 deletions
diff --git a/tests/programs.c b/tests/programs.py
index 340bc28..02ae798 100644
--- a/tests/programs.c
+++ b/tests/programs.py
@@ -23,24 +23,48 @@ that will run in a chroot and will work the same on all platforms, or build
minimal, self-contained tester programs using tools in the host OS.
I picked the second approach: to test the sandboxes using statically linked C
-programs. Each C program below should be small, self-contained and should test
-one thing.
+programs. Each C program below should be simple, portable, self-contained and
+should test one thing.
'''
+import py
+import pytest
+
+import subprocess
+import tempfile
+
+
def build_c_program(source_code, output_path, compiler_args=None):
- '''Compile a temporary C program.'''
+ '''Compile a temporary C program.
+
+ This assumes that the host system has 'cc' available.
+
+ '''
compiler_args = compiler_args or []
with tempfile.NamedTemporaryFile(suffix='.c') as f:
- f.write(WRITEABLE_PATHS_TEST_CODE.encode('utf-8'))
+ f.write(source_code.encode('utf-8'))
f.flush()
subprocess.check_call(
- ['gcc', '-static', f.name, '-o', str(output_path)])
+ ['cc', '-static', f.name, '-o', str(output_path)])
+
+
+@pytest.fixture(scope='session')
+def session_tmpdir(request):
+ '''Workaround for a limitation of the py.test 'tmpdir' fixture.
+
+ See: <https://stackoverflow.com/questions/25525202/>
+
+ '''
+ dir = py.path.local(tempfile.mkdtemp())
+ request.addfinalizer(lambda: dir.remove(rec=1))
+ # Any extra setup here
+ return dir
+
-# Test if a file or directory exists.
FILE_OR_DIRECTORY_EXISTS_TEST_PROGRAM = """
#include <stdio.h>
#include <sys/stat.h>
@@ -50,12 +74,12 @@ int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Expected 1 argument: filename to try to read from.");
- return 1;
+ return 2;
}
if (stat(argv[1], &stat_data) != 0) {
printf("Did not find %s.", argv[1]);
- return 2;
+ return 1;
}
printf("%s exists", argv[1]);
@@ -63,14 +87,23 @@ int main(int argc, char *argv[]) {
};
"""
-@pytest.fixture(scope='module')
-def file_exists_test_program(self, tmpdir):
- program_path = tmpdir.join('writable-paths-tester')
+
+@pytest.fixture(scope='session')
+def file_or_directory_exists_test_program(session_tmpdir):
+ '''Returns the path to a program that tests if a file or directory exists.
+
+ The program takes a path on the commandline, and returns 0 if the path
+ points to an existing file or directory, 1 if it doesn't exist, or 2 on
+ error.
+
+ '''
+ program_path = session_tmpdir.join('test-file-or-directory-exists')
build_c_program(
- FILE_OR_DIRECTORY_EXISTS_TEST_PROGRAM, program_path, compiler_args=['-static'])
+ FILE_OR_DIRECTORY_EXISTS_TEST_PROGRAM, program_path,
+ compiler_args=['-static'])
return program_path
-# Test if a file can be written to.
+
FILE_IS_WRITABLE_TEST_PROGRAM = """
#include <stdio.h>
@@ -79,20 +112,20 @@ int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Expected 1 argument: filename to try to write to.");
- return 1;
+ return 2;
}
file = fopen(argv[0], "w");
if (file == NULL) {
printf("Couldn't open %s for writing.", argv[1]);
- return 2;
+ return 1;
}
if (fputc('!', file) != '!') {
printf("Couldn't write to %s.", argv[1]);
fclose(file);
- return 3;
+ return 1;
}
fclose(file);
@@ -101,9 +134,17 @@ int main(int argc, char *argv[]) {
};
"""
-@pytest.fixture(scope='module')
-def file_is_writable_test_program(self, tmpdir):
- program_path = tmpdir.join('writable-paths-tester')
+
+@pytest.fixture(scope='session')
+def file_is_writable_test_program(session_tmpdir):
+ '''Returns the path to a program that test if a file is writable.
+
+ The program takes a path on the commandline, and return 0 if the given
+ path is a file that can be written to, 2 if the given path cannot be
+ written to, or 2 on error.
+
+ '''
+ program_path = session_tmpdir.join('test-file-is-writable')
build_c_program(
- FILE_IS_WRITEABLE_TEST_PROGRAM, program_path, compiler_args=['-static'])
+ FILE_IS_WRITABLE_TEST_PROGRAM, program_path, compiler_args=['-static'])
return program_path