def _test(inwer, in_file, use_sandboxes, callback): with TemporaryCwd(): upload_files() inwer_bin = compile(inwer, '/inwer.e')['out_file'] with TemporaryCwd(): env = { 'in_file': in_file, 'exe_file': inwer_bin, 'use_sandboxes': use_sandboxes, 'inwer_output_limit': SMALL_OUTPUT_LIMIT, } renv = run_inwer(env) print_env(renv) if callback: callback(renv)
def _test(ingen, re_string, upload_dir, use_sandboxes, callback): with TemporaryCwd(): upload_files() ingen_bin = compile(ingen, '/ingen.e')['out_file'] with TemporaryCwd(): env = { 're_string': re_string, 'collected_files_path': '/' + upload_dir, 'exe_file': ingen_bin, 'use_sandboxes': use_sandboxes, 'ingen_output_limit': SMALL_OUTPUT_LIMIT, } renv = run_ingen(env) print_env(renv) if callback: callback(renv)
def test_output_compilation_and_running(source): with TemporaryCwd(): upload_files() result_env = run( { 'source_file': source, 'compiler': 'output-only', } ) eq_(result_env['result_code'], 'OK') eq_(result_env['exec_info'], {'mode': 'output-only'}) ft.download(result_env, 'out_file', tempcwd('out.txt')) ft.download({'source_file': source}, 'source_file', tempcwd('source.txt')) with open(tempcwd('out.txt'), 'r') as outfile: with open(tempcwd('source.txt'), 'r') as sourcefile: eq_(outfile.read(), sourcefile.read()) post_run_env = run_from_executors( { 'exec_info': result_env['exec_info'], 'exe_file': result_env['out_file'], 'check_output': True, 'hint_file': source, }, executor=None, ) eq_(post_run_env['result_code'], 'OK') ft.download(post_run_env, 'out_file', tempcwd('out.txt')) ft.download({'source_file': source}, 'source_file', tempcwd('source.txt')) with open(tempcwd('out.txt'), 'r') as outfile: with open(tempcwd('source.txt'), 'r') as sourcefile: eq_(outfile.read(), sourcefile.read())
def test_truncating_output(): with TemporaryCwd(): upload_files() checker_bin = compile('/chk-output-too-long.c', '/chk-output-too-long.e')['out_file'] with TemporaryCwd(): renv = compile_and_run('/output-too-long.c', { 'in_file': '/input', 'check_output': True, 'hint_file': '/input', 'chk_file': checker_bin, }, DetailedUnprotectedExecutor(), use_sandboxes=False) length = len(renv['result_string']) if length > RESULT_STRING_LENGTH_LIMIT: raise AssertionError("result_string too long, %d > %d" % (length, RESULT_STRING_LENGTH_LIMIT))
def _test_transparent_exec(source, executor, callback, kwargs): with TemporaryCwd(): upload_files() ft.download({'path': '/somefile'}, 'path', tempcwd('somefile')) result_env = compile_and_execute(source, executor, **kwargs) print_env(result_env) if callback: callback(result_env)
def _test(): with TemporaryCwd(): upload_files() checker_bin = compile(checker, '/chk.e')['out_file'] with TemporaryCwd(): executor = SupervisedExecutor(use_program_return_code=True) if \ sandboxed else DetailedUnprotectedExecutor() renv = compile_and_run('/add_print.c', { 'in_file': '/input', 'check_output': True, 'hint_file': '/hint', 'chk_file': checker_bin, 'untrusted_checker': True, }, executor, use_sandboxes=sandboxed) print_env(renv) if callback: callback(renv)
def _test(message, compiler, source, program_args=None): with TemporaryCwd(): upload_files() compile_and_run({ 'source_file': source, 'compiler': compiler, 'out_file': '/out', }, message, program_args)
def _test(message, compiler, source): with TemporaryCwd(): upload_files() compile_and_run({ 'source_file': source, 'compiler': compiler, 'out_file': '/out', 'compilation_result_size_limit': COMPILATION_RESULT_SIZE_LIMIT, }, message)
def test_running_cases(source, executor, callback): with TemporaryCwd(): upload_files() result_env = compile_and_run(source, { 'in_file': '/input', 'exec_time_limit': 1000 }, executor) print_env(result_env) callback(result_env)
def test_common_memory_limiting(source, mem_limit, executor, callback): with TemporaryCwd(): upload_files() result_env = compile_and_run(source, { 'in_file': '/input', 'exec_mem_limit': mem_limit }, executor) print_env(result_env) callback(result_env)
def _test(source, time_limit, executor, callback): with TemporaryCwd(): upload_files() result_env = compile_and_run(source, { 'in_file': '/input', 'exec_time_limit': time_limit }, executor) print_env(result_env) callback(result_env)
def _test(message, compiler, source, sources, includes=()): with TemporaryCwd(): upload_files() compile_and_run({ 'source_file': source, 'additional_includes': includes, 'additional_sources': sources, 'compiler': compiler, 'out_file': '/out', }, message)
def test_uploading_out(): with TemporaryCwd(): upload_files() renv = compile_and_run('/add_print.c', { 'in_file': '/input', 'out_file': '/output', 'upload_out': True, }, DetailedUnprotectedExecutor()) print_env(renv) ft.download({'path': '/output'}, 'path', 'd_out') in_('84', open(tempcwd('d_out')).read())
def test_zip(): with TemporaryCwd(): upload_files() compile_and_run( "/echo.c", { 'in_file': '/input.zip', 'out_file': '/output', 'exec_mem_limit': 102400 }, DetailedUnprotectedExecutor()) ft.download({'in_file': '/input'}, 'in_file', 'out.expected') ft.download({'out_file': '/output'}, 'out_file', 'out.real') ok_(filecmp.cmp(tempcwd('out.expected'), tempcwd('out.real')))
def test_outputting_non_utf8(): if ENABLE_SANDBOXES: with TemporaryCwd(): upload_files() renv = compile_and_run('/output-non-utf8.c', { 'in_file': '/input', 'check_output': True, 'hint_file': '/input', }, SupervisedExecutor(), use_sandboxes=True) print_env(renv) in_('42', renv['result_string']) ok_(renv['result_string'])
def _test_size_and_out_limit(message, compiler, source): with TemporaryCwd(): upload_files() compile_fail({ 'source_file': source, 'compiler': compiler, 'out_file': '/out', 'compilation_time_limit': time_limit, 'compilation_real_time_limit': time_hard_limit, 'compilation_result_size_limit': COMPILATION_RESULT_SIZE_LIMIT, 'compilation_mem_limit': mem_limit * 2**10, 'compilation_output_limit': COMPILATION_OUTPUT_LIMIT, }, message)
def _test(message, compiler, source, sources, archive, unexpected_files): with TemporaryCwd(inner_directory='one_more_level'): upload_files() compile_and_run({ 'source_file': source, 'additional_sources': sources, 'additional_archive': archive, 'compiler': compiler, 'out_file': '/out', }, message) for f in unexpected_files: ok_(not os.path.exists(f))
def test_execute(): with TemporaryCwd(): rc, out = execute(['echo', '2']) eq_(rc, 0) eq_(out, '2\n') rc, out = execute(['exit', '1'], ignore_errors=True) eq_(rc, 1) assert_raises(ExecError, execute, ['exit', '1']) rc, out = execute(['mkdir', tempcwd('spam')]) eq_(rc, 0) rc, out = execute(['ls', tempcwd()]) in_('spam', out)
def _test_large_limit(message, compiler, source): with TemporaryCwd(): upload_files() result_env = compile_fail({ 'source_file': source, 'compiler': compiler, 'out_file': '/out', 'compilation_time_limit': time_limit, 'compilation_real_time_limit': time_hard_limit, 'compilation_output_limit': 100 * DEFAULT_COMPILER_OUTPUT_LIMIT }, message) ok_(len(result_env['compiler_output']) > DEFAULT_COMPILER_OUTPUT_LIMIT)
def test_compilation_with_additional_library( message, compiler, source, sources, includes ): with TemporaryCwd(): upload_files() compile_and_run( { 'source_file': source, 'additional_includes': includes, 'additional_sources': sources, 'compiler': compiler, 'out_file': '/out', }, message, )
def run(environ): """Performs the work passed in ``environ``. Returns the modified ``environ``. It might be modified in-place by work implementations. The following keys in ``environ`` have special meaning: ``job_type`` Mandatory key naming the job to be run. ``prefilters`` Optional list of filter names to apply before performing the work. ``postfilters`` Optional list of filter names to apply after performing the work. The following are added during processing: ``worker`` Hostname of the machine running the job (i.e. the machine executing this function). Refer to :ref:`sio-workers-filters` for more information about filters. """ with TemporaryCwd(): try: if environ.get('filetracker_url', None): init_instance(environ['filetracker_url']) environ = _run_filters('prefilters', environ) environ = _add_meta(environ) environ = first_entry_point('sio.jobs', environ['job_type'])(environ) environ['result'] = 'SUCCESS' environ = _run_filters('postfilters', environ) except Failure as e: environ = _save_failure(e, environ) try: environ = _run_filters('postfilters', environ) except Failure as e: pass return environ
def _test(message, compiler, source): with TemporaryCwd(): upload_files() compile_and_run({ 'source_file': source, 'additional_includes': { 'c': '/library.h', 'cpp': '/library.h', }, 'additional_sources': { 'c': '/library.c', 'cpp': '/library.cpp', 'pas': '******', 'java': '/library.java', }, 'compiler': compiler, 'out_file': '/out', }, message)
def compile_and_run(compiler_env, expected_output, program_args=None): """Helper function for compiling, launching and testing the result of a program. """ # Dummy sandbox doesn't support asking for versioned filename out_file = compiler_env['out_file'] if compiler_env.get('compiler', '').endswith('-java'): compiler_env['compilation_time_limit'] = 180000 with TemporaryCwd('compile'): result_env = run(compiler_env) print_env(result_env) eq_(result_env['result_code'], 'OK') binary = ft.download({'path': out_file}, 'path') os.chmod(binary, stat.S_IXUSR | os.stat(binary).st_mode) if not program_args: program_args = [] executor = UnprotectedExecutor() frkwargs = {} # Hack for java if compiler_env.get('compiler') == 'default-java': executor = PRootExecutor('compiler-java.1_8') frkwargs['proot_options'] = ['-b', '/proc'] frunner = get_file_runner(executor, result_env) with frunner: renv = frunner(binary, program_args, stderr=sys.__stderr__, capture_output=True, **frkwargs) eq_(renv['return_code'], 0) eq_(renv['stdout'].decode().strip(), expected_output) os.remove(binary)
def _test_raw(cmd, executor, callback, kwargs): with TemporaryCwd(): with executor: result_env = executor(cmd, **kwargs) print_env(result_env) callback(result_env)