Example #1
0
    def test_write_a_file_in_sandbox(self):
        sandbox = Sandbox(54321)
        file_contents = b"I want to write a file in sandbox"
        file_name = "write_file_test.txt"
        files = [(file_name, file_contents)]

        # copy
        result = sandbox.write_files_in_sandbox(files, '/')
        self.assertTrue(result)

        # check existence
        result = sandbox.exec('[ -e "/write_file_test.txt" ]')
        self.assertEqual(0, result.get('ExitCode'))

        # check contents
        result = sandbox.exec('cat /write_file_test.txt')
        self.assertEqual(file_contents, result.get('Output'))
Example #2
0
    def test_exec_echo4(self):
        sandbox = Sandbox(54321)
        test_str = "A-Judge Sandbox method test"
        test_cmd = "echo -n " + test_str
        result = sandbox.exec(test_cmd)

        self.assertEqual(test_str, str(result.get('Output'), 'utf-8'))
        self.assertEqual(0, result.get('ExitCode'))
Example #3
0
    def test_exec_echo3(self):
        sandbox = Sandbox(54321)
        test_str = "a1234567999918"
        test_cmd = "echo -n " + test_str
        result = sandbox.exec(test_cmd)

        self.assertEqual(test_str, str(result.get('Output'), 'utf-8'))
        self.assertEqual(0, result.get('ExitCode'))
Example #4
0
 def test_exec_echo(self):
     sandbox = Sandbox()
     test_str = "Hello World"
     test_cmd = "echo -n " + test_str
     result = sandbox.exec(test_cmd)
     del sandbox
     self.assertEqual(test_str, str(result.get('Output'),'utf-8'))
     self.assertEqual(0, result.get('ExitCode'))
Example #5
0
    def test_copy_file_to_sandbox(self):
        sandbox = Sandbox(54321)
        file_path = self.PROJECT_ROOT.joinpath('./tests/resources/server.py')

        # copy
        result = sandbox.copy_file_to_sandbox(file_path, '/')
        self.assertTrue(result)

        # check existence
        result = sandbox.exec('[ -e "/server.py" ]')
        self.assertEqual(0, result.get('ExitCode'))

        # cehck contents
        result = sandbox.exec('cat /server.py')
        fileobj = open(file_path, "rb")
        file_contents = fileobj.read()
        fileobj.close()
        self.assertEqual(file_contents, result.get('Output'))
Example #6
0
    def prepare(self, runtime_context: RuntimeContext) -> Sandbox:
        sandbox = Sandbox(54321)
        input_files = [("solution.cc", str.encode(runtime_context.source_code))
                       ]
        for input_name, input_content in runtime_context.problem_metadata.inputs.items(
        ):
            input_files.append((input_name, str.encode(input_content)))

        res = sandbox.write_files_in_sandbox(input_files, "/workdir")
        if res is False:
            raise Exception

        res = sandbox.exec("mkdir /workdir/outputs")
        if res.get('ExitCode') is not 0:
            raise Exception

        res = sandbox.exec(
            "g++ -std=c++11 -O2 -Wall /workdir/solution.cc -o /workdir/solution"
        )
        if res.get('ExitCode') is not 0:
            print('compile error')
            raise Exception

        return sandbox
Example #7
0
    def run(self, runtime_context: RuntimeContext, sandbox: Sandbox):
        user_outputs = {}
        run_info = {}
        cmd_holder = "/workdir/judge_client {0} {1} ".format(runtime_context.problem_metadata.memory_limit,\
                                                             runtime_context.problem_metadata.time_limit)
        cmd_holder = cmd_holder + "/workdir/{0} " + \
                     "/workdir/outputs/{1}.out " + \
                     "/workdir/{1}.err /workdir/solution"

        for input_name, input in runtime_context.problem_metadata.inputs.items(
        ):
            basename = input_name.split('.')[0]
            cmd = cmd_holder.format(input_name, basename)
            run_info[basename + ".out"] = sandbox.exec(cmd).get('Output')

        outputs = sandbox.get_files_from_sandbox("/workdir/outputs/")
        for name, output in outputs:
            user_outputs[name] = str(output, 'UTF-8')

        return {'RunInfo': run_info, 'UserOutput': user_outputs}
Example #8
0
    def test_send_and_get_files_from_sandbox(self):
        sandbox = Sandbox(54321)

        # get .py files under tests/resources/
        files = []
        resources_path = self.PROJECT_ROOT.joinpath('./tests/resources')
        for path in resources_path.glob('*.py'):
            file_obj = open(path, 'rb')
            files.append((path.name, file_obj.read()))
            file_obj.close()

        # make directory for them
        res = sandbox.exec('mkdir /workdir/test_copy')
        self.assertEqual(0, res.get('ExitCode'))

        # send them all
        res = sandbox.write_files_in_sandbox(files, '/workdir/test_copy')
        self.assertTrue(res)

        # get and check
        res = sandbox.get_files_from_sandbox('/workdir/test_copy/.')
        self.assertListEqual(files, res)