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 compile(source, output='/exe', use_sandboxes=ENABLE_SANDBOXES): ext = os.path.splitext(source.split('@')[0])[1][1:] compiler_env = { 'source_file': source, 'compiler': (use_sandboxes and 'default-' or 'system-') + ext, 'out_file': output, 'compilation_time_limit': 180000, } # Dummy sandbox doesn't support asking for versioned filename out_file = compiler_env['out_file'] result_env = run_compiler(compiler_env) result_env['out_file'] = out_file print_env(result_env) eq_(result_env['result_code'], 'OK') return result_env
def compile_fail(compiler_env, expected_in_compiler_output=None): """Helper function for compiling and asserting that it fails.""" result_env = run(compiler_env) print_env(result_env) eq_(result_env['result_code'], 'CE') if 'compilation_output_limit' not in compiler_env: ok_(len(result_env['compiler_output']) <= DEFAULT_COMPILER_OUTPUT_LIMIT) elif compiler_env['compilation_output_limit'] is not None: ok_(len(result_env['compiler_output']) <= compiler_env['compilation_output_limit']) if expected_in_compiler_output: in_(expected_in_compiler_output, result_env['compiler_output']) return result_env
def test_execute(): with TemporaryCwd(): rc, out = execute(['echo', '2']) eq_(rc, 0) eq_(out, b'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_(b'spam', out)
def inner(env): eq_(env['return_code'], 0) eq_(env['stdout'], expected_output) collected = env['collected_files'] eq_(len(expected_files), len(collected)) for filename, path in six.iteritems(collected): in_(filename, expected_files) unversioned_path = '/%s/%s' % (upload_dir, filename) upload_re_str = r'%s@\d+' % (unversioned_path) upload_re = re.compile(upload_re_str) ok_(upload_re.match(path), 'Unexpected filetracker path') ft.download({'in': unversioned_path}, 'in', filename) eq_(expected_files[filename], open(tempcwd(filename)).read())
def inner(env): eq_(env['return_code'], 0) eq_(env['stdout'], expected_output) collected = env['collected_files'] eq_(len(expected_files), len(collected)) for filename, path in six.iteritems(collected): in_(filename, expected_files) unversioned_path = '/%s/%s' % (upload_dir, filename) upload_re_str = '%s@\d+' % (unversioned_path) upload_re = re.compile(upload_re_str) ok_(upload_re.match(path), 'Unexpected filetracker path') ft.download({'in': unversioned_path}, 'in', filename) eq_(expected_files[filename], open(tempcwd(filename)).read())
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 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 ole(env): eq_('OLE', env['result_code'])
def nochange(env): res_re(1)(env) eq_('42', open(tempcwd('somefile')).read().strip()) ok_(not os.path.exists(tempcwd('./not_existing')))
def check_proot_fail(env): eq_(env['return_code'], 139)
def ret_42(env): eq_(42, env['return_code'])
def ok_42(env): res_ok(env) eq_(42, int(env['result_percentage']))
def inner(env): if(use_sandboxes): eq_(env['result_code'], "OLE") else: eq_(env['result_code'], "OK") eq_(env['stdout'], [b'A' * SMALL_OUTPUT_LIMIT])
def used_1sec(env): eq_('OK', env['result_code']) eq_(1000, env['time_used'])
def lines_split(env): ok_(isinstance(env['stdout'], list)) eq_(len(env['stdout']), 3)
def change(env): res_ok(env) eq_('13', open(tempcwd('somefile')).read().strip()) ok_(os.path.exists(tempcwd('./not_existing')))
def syscall_limit(env): eq_('TLE', env['result_code']) in_('syscalls', env['result_string'])
def inner(env): eq_('TLE', env['result_code']) ok_(env['real_time_used'] > limit)
def res_wa(env): eq_('WA', env['result_code'])
def inner(env): eq_('RV', env['result_code']) in_(msg, env['result_string'])
def check_inwer_faulty(env): eq_(env['result_code'], "OK") ok_(not env['stdout'][0].startswith(b"OK"))
def res_ok(env): eq_('OK', env['result_code'])
def schedule(self): res = self.scheduler.schedule() for tid, wid in res: assert tid in self.tasks self._assignTaskToWorker(wid, self.tasks[tid]) eq_(self._checkInnerState(), 'OK')
def res_tle(env): eq_(env['result_code'], 'TLE')
def inner(env): eq_('RE', env['result_code']) in_(str(reason), env['result_string'])