Example #1
0
    def setUp(self):
        super(TestCheckerStep, self).setUp()
        # By default, any file request succeeds.
        self.file_cacher = MagicMock()
        self.sandbox = FakeIsolateSandbox(self.file_cacher)

        patcher = patch("cms.grading.steps.trusted.trusted_step")
        self.addCleanup(patcher.stop)
        self.mock_trusted_step = patcher.start()

        patcher = patch("cms.grading.steps.trusted.logger.error",
                        wraps=trusted.logger.error)
        self.addCleanup(patcher.stop)
        self.mock_logger_error = patcher.start()
Example #2
0
    def setUp(self):
        super(TestCompilationStep, self).setUp()
        self.sandbox = FakeIsolateSandbox(None)

        patcher = patch("cms.grading.steps.compilation.logger.error")
        self.mock_logger_error = patcher.start()
        self.addCleanup(patcher.stop)
Example #3
0
    def setUp(self):
        super(TestTrustedStep, self).setUp()
        self.sandbox = FakeIsolateSandbox(None)

        patcher = patch("cms.grading.steps.trusted.logger.error",
                        wraps=trusted.logger.error)
        self.addCleanup(patcher.stop)
        self.mock_logger_error = patcher.start()
Example #4
0
 def setUp(self):
     super(TestExtractOutcomeAndText, self).setUp()
     self.sandbox = FakeIsolateSandbox(None)
     self.sandbox.stdout_file = "o"
     self.sandbox.stderr_file = "e"
Example #5
0
class TestExtractOutcomeAndText(unittest.TestCase):

    def setUp(self):
        super(TestExtractOutcomeAndText, self).setUp()
        self.sandbox = FakeIsolateSandbox(None)
        self.sandbox.stdout_file = "o"
        self.sandbox.stderr_file = "e"

    def test_success(self):
        self.sandbox.fake_file("o", b"0.45\n")
        self.sandbox.fake_file("e", "你好.\n".encode("utf-8"))
        outcome, text = extract_outcome_and_text(self.sandbox)
        self.assertEqual(outcome, 0.45)
        self.assertEqual(text, ["你好."])

    def test_following_lines_ignored(self):
        self.sandbox.fake_file("o", b"0.45\nNothing\n")
        self.sandbox.fake_file("e", b"Text to return.\nto see here")
        outcome, text = extract_outcome_and_text(self.sandbox)
        self.assertEqual(outcome, 0.45)
        self.assertEqual(text, ["Text to return."])

    def test_works_without_newlines(self):
        self.sandbox.fake_file("o", b"0.45")
        self.sandbox.fake_file("e", b"Text to return.")
        outcome, text = extract_outcome_and_text(self.sandbox)
        self.assertEqual(outcome, 0.45)
        self.assertEqual(text, ["Text to return."])

    def test_text_is_stripped(self):
        self.sandbox.fake_file("o", b"   0.45\t \nignored")
        self.sandbox.fake_file("e", b"\t  Text to return.\r\n")
        outcome, text = extract_outcome_and_text(self.sandbox)
        self.assertEqual(outcome, 0.45)
        self.assertEqual(text, ["Text to return."])

    def test_text_is_translated(self):
        self.sandbox.fake_file("o", b"0.45\n")
        self.sandbox.fake_file("e", b"translate:success\n")
        outcome, text = extract_outcome_and_text(self.sandbox)
        self.assertEqual(outcome, 0.45)
        self.assertEqual(text, ["Output is correct"])

    def test_failure_not_a_float(self):
        self.sandbox.fake_file("o", b"not a float\n")
        self.sandbox.fake_file("e", b"Text to return.\n")
        with self.assertRaises(ValueError):
            extract_outcome_and_text(self.sandbox)

    def test_failure_invalid_utf8(self):
        self.sandbox.fake_file("o", b"0.45")
        self.sandbox.fake_file("e", INVALID_UTF8)
        with self.assertRaises(ValueError):
            extract_outcome_and_text(self.sandbox)

    @unittest.skipIf(PY2, "Python 2 does not have FileNotFoundError.")
    def test_failure_missing_file(self):
        self.sandbox.fake_file("o", b"0.45\n")
        with self.assertRaises(FileNotFoundError):
            extract_outcome_and_text(self.sandbox)
Example #6
0
class TestCheckerStep(unittest.TestCase):

    def setUp(self):
        super(TestCheckerStep, self).setUp()
        # By default, any file request succeeds.
        self.file_cacher = MagicMock()
        self.sandbox = FakeIsolateSandbox(self.file_cacher)

        patcher = patch("cms.grading.steps.trusted.trusted_step")
        self.addCleanup(patcher.stop)
        self.mock_trusted_step = patcher.start()

        patcher = patch("cms.grading.steps.trusted.logger.error",
                        wraps=trusted.logger.error)
        self.addCleanup(patcher.stop)
        self.mock_logger_error = patcher.start()

    def assertLoggedError(self, logged=True):
        if logged:
            self.mock_logger_error.assert_called()
        else:
            self.mock_logger_error.assert_not_called()

    def set_checker_output(self, outcome, text):
        self.sandbox.stdout_file = "stdout_file"
        self.sandbox.stderr_file = "stderr_file"
        if outcome is not None:
            self.sandbox.fake_file(self.sandbox.stdout_file, outcome)
        if text is not None:
            self.sandbox.fake_file(self.sandbox.stderr_file, text)

    def test_success(self):
        self.mock_trusted_step.return_value = (True, True, {})
        self.set_checker_output(b"0.123\n", b"Text.\n")

        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (True, 0.123, ["Text."]))
        self.file_cacher.get_file_to_fobj.assert_has_calls([
            call("c_dig", ANY),
            call("i_dig", ANY),
            call("co_dig", ANY),
        ], any_order=True)
        self.mock_trusted_step.assert_called_once_with(
            self.sandbox, [["./checker", trusted.CHECKER_INPUT_FILENAME,
                            trusted.CHECKER_CORRECT_OUTPUT_FILENAME, "o"]])
        self.assertLoggedError(False)

    def test_sandbox_failure(self):
        self.mock_trusted_step.return_value = (False, None, None)
        # Output files are ignored.
        self.set_checker_output(b"0.123\n", b"Text.\n")

        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_checker_failure(self):
        self.mock_trusted_step.return_value = (True, False, {})
        # Output files are ignored.
        self.set_checker_output(b"0.123\n", b"Text.\n")

        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_missing_checker(self):
        ret = checker_step(self.sandbox, None, "i_dig", "co_dig", "o")

        self.mock_trusted_step.assert_not_called()
        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_checker_already_in_sandbox(self):
        self.sandbox.fake_file(trusted.CHECKER_FILENAME, b"something")
        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_input_already_in_sandbox(self):
        self.sandbox.fake_file(trusted.CHECKER_INPUT_FILENAME, b"something")
        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_correct_output_already_in_sandbox(self):
        self.sandbox.fake_file(trusted.CHECKER_CORRECT_OUTPUT_FILENAME,
                               b"something")
        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_invalid_checker_outcome(self):
        self.mock_trusted_step.return_value = (True, True, {})
        self.set_checker_output(b"A0.123\n", b"Text.\n")

        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_invalid_checker_text(self):
        self.mock_trusted_step.return_value = (True, True, {})
        self.set_checker_output(b"0.123\n", INVALID_UTF8)

        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_missing_checker_outcome(self):
        self.mock_trusted_step.return_value = (True, True, {})
        self.set_checker_output(None, b"Text.\n")

        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()

    def test_missing_checker_text(self):
        self.mock_trusted_step.return_value = (True, True, {})
        self.set_checker_output(b"0.123\n", None)

        ret = checker_step(self.sandbox, "c_dig", "i_dig", "co_dig", "o")

        self.assertEqual(ret, (False, None, None))
        self.assertLoggedError()
Example #7
0
 def setUp(self):
     super().setUp()
     self.sandbox = FakeIsolateSandbox(None)
Example #8
0
class TestGenericStep(unittest.TestCase):
    def setUp(self):
        super(TestGenericStep, self).setUp()
        self.sandbox = FakeIsolateSandbox(None)

    def test_single_command_success(self):
        self.sandbox.fake_execute_data(True, b"o", "你好".encode("utf-8"), 0.1,
                                       0.5, 1000, "OK")

        stats = generic_step(self.sandbox,
                             ONE_COMMAND,
                             "name",
                             collect_output=True)

        # Stdout and stderr are encoded in UTF-8.
        self.assertEqual(
            stats,
            get_stats(0.1,
                      0.5,
                      1000 * 1024,
                      Sandbox.EXIT_OK,
                      stdout="o",
                      stderr="你好"))
        # Generic step always redirects stdout and stderr.
        self.assertEqual(self.sandbox.stdout_file, "name_stdout_0.txt")
        self.assertEqual(self.sandbox.stderr_file, "name_stderr_0.txt")

    def test_single_command_success_no_collect_output(self):
        self.sandbox.fake_execute_data(True, b"o", "你好".encode("utf-8"), 0.1,
                                       0.5, 1000, "OK")

        stats = generic_step(self.sandbox,
                             ONE_COMMAND,
                             "name",
                             collect_output=False)

        # No output collected on stats.
        self.assertEqual(stats,
                         get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_OK))
        # Generic step always redirects stdout and stderr.
        self.assertEqual(self.sandbox.stdout_file, "name_stdout_0.txt")
        self.assertEqual(self.sandbox.stderr_file, "name_stderr_0.txt")

    def test_single_command_nonzero_return(self):
        self.sandbox.fake_execute_data(True, b"o", b"e", 0.1, 0.5, 1000, "RE")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertEqual(
            stats, get_stats(0.1, 0.5, 1000 * 1024,
                             Sandbox.EXIT_NONZERO_RETURN))

    def test_single_command_failed_timeout(self):
        self.sandbox.fake_execute_data(True, b"o", b"e", 0.1, 0.5, 1000, "TO")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertEqual(
            stats, get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_TIMEOUT))

    def test_single_command_failed_timeout_wall(self):
        self.sandbox.fake_execute_data(
            True,
            b"o",
            b"e",
            0.1,
            0.5,
            1000,
            "TO",
            message="Time limit exceeded (wall clock)")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertEqual(
            stats, get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_TIMEOUT_WALL))

    def test_single_command_failed_signal(self):
        self.sandbox.fake_execute_data(True,
                                       b"o",
                                       b"e",
                                       0.1,
                                       0.5,
                                       1000,
                                       "SG",
                                       signal=11)

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertEqual(
            stats,
            get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_SIGNAL, signal=11))

    def test_single_command_sandbox_failed(self):
        self.sandbox.fake_execute_data(False, b"o", b"e", 0.1, 0.5, 1000, "XX")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertIsNone(stats)

    def test_multiple_commands_success(self):
        self.sandbox.fake_execute_data(True, b"o1", b"e1", 0.1, 0.5, 1000,
                                       "OK")
        self.sandbox.fake_execute_data(True, b"o2", b"e2", 1.0, 5.0, 10000,
                                       "OK")

        stats = generic_step(self.sandbox,
                             TWO_COMMANDS,
                             "name",
                             collect_output=True)

        # 2 commands executed, with exec_num 0 and 1
        self.assertEquals(self.sandbox.exec_num, 1)
        # Stats are the combination of the two.
        self.assertEqual(
            stats,
            get_stats(
                1.1,  # sum
                5.5,  # sum
                10000 * 1024,  # max
                Sandbox.EXIT_OK,
                stdout="o1\n===\no2",
                stderr="e1\n===\ne2"))

    def test_multiple_commands_failure_terminates_early(self):
        self.sandbox.fake_execute_data(True, b"o1", b"e1", 0.1, 0.5, 1000,
                                       "RE")
        self.sandbox.fake_execute_data(True, b"o2", b"e2", 1.0, 5.0, 10000,
                                       "OK")

        stats = generic_step(self.sandbox,
                             TWO_COMMANDS,
                             "name",
                             collect_output=True)

        # 1 command executed (generic terminates early), with exec_num 0.
        self.assertEquals(self.sandbox.exec_num, 0)
        # Stats are only for the first command.
        self.assertEqual(
            stats,
            get_stats(0.1,
                      0.5,
                      1000 * 1024,
                      Sandbox.EXIT_NONZERO_RETURN,
                      stdout="o1",
                      stderr="e1"))

    def test_multiple_commands_sandbox_failure_terminates_early(self):
        self.sandbox.fake_execute_data(False, b"o1", b"e1", 0.1, 0.5, 1000,
                                       "XX")
        self.sandbox.fake_execute_data(True, b"o2", b"e2", 1.0, 5.0, 10000,
                                       "OK")

        stats = generic_step(self.sandbox, TWO_COMMANDS, "name")

        # 1 command executed (generic terminates early), with exec_num 0.
        self.assertEquals(self.sandbox.exec_num, 0)
        self.assertIsNone(stats)

    def test_invalid_utf8_in_output(self):
        self.sandbox.fake_execute_data(True, b"o" + INVALID_UTF8 + b"1",
                                       b"e" + INVALID_UTF8 + b"2", 0.1, 0.5,
                                       1000, "OK")

        stats = generic_step(self.sandbox,
                             ONE_COMMAND,
                             "name",
                             collect_output=True)

        # UTF-8 invalid parts are replaced with funny question marks (\uFFFD).
        assertRegex(self, stats["stdout"], "^o.*1$")
        assertRegex(self, stats["stderr"], "^e.*2$")
Example #9
0
 def setUp(self):
     super().setUp()
     self.sandbox = FakeIsolateSandbox(None)
     self.sandbox.stdout_file = "o"
     self.sandbox.stderr_file = "e"
Example #10
0
class TestGenericStep(unittest.TestCase):

    def setUp(self):
        super().setUp()
        self.sandbox = FakeIsolateSandbox(None)

    def test_single_command_success(self):
        self.sandbox.fake_execute_data(
            True, b"o", "你好".encode("utf-8"), 0.1, 0.5, 1000, "OK")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name",
                             collect_output=True)

        # Stdout and stderr are encoded in UTF-8.
        self.assertEqual(
            stats, get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_OK,
                             stdout="o", stderr="你好"))
        # Generic step always redirects stdout and stderr.
        self.assertEqual(self.sandbox.stdout_file, "name_stdout_0.txt")
        self.assertEqual(self.sandbox.stderr_file, "name_stderr_0.txt")

    def test_single_command_success_no_collect_output(self):
        self.sandbox.fake_execute_data(
            True, b"o", "你好".encode("utf-8"), 0.1, 0.5, 1000, "OK")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name",
                             collect_output=False)

        # No output collected on stats.
        self.assertEqual(
            stats, get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_OK))
        # Generic step always redirects stdout and stderr.
        self.assertEqual(self.sandbox.stdout_file, "name_stdout_0.txt")
        self.assertEqual(self.sandbox.stderr_file, "name_stderr_0.txt")

    def test_single_command_nonzero_return(self):
        self.sandbox.fake_execute_data(True, b"o", b"e", 0.1, 0.5, 1000, "RE")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertEqual(stats, get_stats(0.1, 0.5, 1000 * 1024,
                                          Sandbox.EXIT_NONZERO_RETURN))

    def test_single_command_failed_timeout(self):
        self.sandbox.fake_execute_data(True, b"o", b"e", 0.1, 0.5, 1000, "TO")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertEqual(stats, get_stats(0.1, 0.5, 1000 * 1024,
                                          Sandbox.EXIT_TIMEOUT))

    def test_single_command_failed_timeout_wall(self):
        self.sandbox.fake_execute_data(
            True, b"o", b"e", 0.1, 0.5, 1000, "TO",
            message="Time limit exceeded (wall clock)")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertEqual(stats, get_stats(0.1, 0.5, 1000 * 1024,
                                          Sandbox.EXIT_TIMEOUT_WALL))

    def test_single_command_failed_signal(self):
        self.sandbox.fake_execute_data(
            True, b"o", b"e", 0.1, 0.5, 1000, "SG", signal=11)

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertEqual(stats, get_stats(0.1, 0.5, 1000 * 1024,
                                          Sandbox.EXIT_SIGNAL, signal=11))

    def test_single_command_sandbox_failed(self):
        self.sandbox.fake_execute_data(
            False, b"o", b"e", 0.1, 0.5, 1000, "XX")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name")

        self.assertIsNone(stats)

    def test_multiple_commands_success(self):
        self.sandbox.fake_execute_data(
            True, b"o1", b"e1", 0.1, 0.5, 1000, "OK")
        self.sandbox.fake_execute_data(
            True, b"o2", b"e2", 1.0, 5.0, 10_000, "OK")

        stats = generic_step(self.sandbox, TWO_COMMANDS, "name",
                             collect_output=True)

        # 2 commands executed, with exec_num 0 and 1
        self.assertEquals(self.sandbox.exec_num, 1)
        # Stats are the combination of the two.
        self.assertEqual(stats, get_stats(1.1,  # sum
                                          5.5,  # sum
                                          10_000 * 1024,  # max
                                          Sandbox.EXIT_OK,
                                          stdout="o1\n===\no2",
                                          stderr="e1\n===\ne2"))

    def test_multiple_commands_failure_terminates_early(self):
        self.sandbox.fake_execute_data(
            True, b"o1", b"e1", 0.1, 0.5, 1000, "RE")
        self.sandbox.fake_execute_data(
            True, b"o2", b"e2", 1.0, 5.0, 10_000, "OK")

        stats = generic_step(self.sandbox, TWO_COMMANDS, "name",
                             collect_output=True)

        # 1 command executed (generic terminates early), with exec_num 0.
        self.assertEquals(self.sandbox.exec_num, 0)
        # Stats are only for the first command.
        self.assertEqual(stats, get_stats(
            0.1, 0.5, 1000 * 1024, Sandbox.EXIT_NONZERO_RETURN,
            stdout="o1", stderr="e1"))

    def test_multiple_commands_sandbox_failure_terminates_early(self):
        self.sandbox.fake_execute_data(
            False, b"o1", b"e1", 0.1, 0.5, 1000, "XX")
        self.sandbox.fake_execute_data(
            True, b"o2", b"e2", 1.0, 5.0, 10_000, "OK")

        stats = generic_step(self.sandbox, TWO_COMMANDS, "name")

        # 1 command executed (generic terminates early), with exec_num 0.
        self.assertEquals(self.sandbox.exec_num, 0)
        self.assertIsNone(stats)

    def test_invalid_utf8_in_output(self):
        self.sandbox.fake_execute_data(
            True,
            b"o" + INVALID_UTF8 + b"1",
            b"e" + INVALID_UTF8 + b"2",
            0.1, 0.5, 1000, "OK")

        stats = generic_step(self.sandbox, ONE_COMMAND, "name",
                             collect_output=True)

        # UTF-8 invalid parts are replaced with funny question marks (\uFFFD).
        self.assertRegex(stats["stdout"], "^o.*1$")
        self.assertRegex(stats["stderr"], "^e.*2$")
Example #11
0
 def setUp(self):
     super(TestGenericStep, self).setUp()
     self.sandbox = FakeIsolateSandbox(None)
Example #12
0
class TestExecutionStats(unittest.TestCase):

    def setUp(self):
        super().setUp()
        self.sandbox = FakeIsolateSandbox(None)
        self.sandbox.stdout_file = "stdout.txt"
        self.sandbox.stderr_file = "stderr.txt"

    def test_success(self):
        # Tell the fake sandbox the command data to fake, and execute it.
        self.sandbox.fake_execute_data(
            True, b"o", b"e", 0.1, 0.5, 1000, "OK")
        self.sandbox.execute_without_std(["command"], wait=True)

        stats = execution_stats(self.sandbox)
        self.assertEqual(stats,
                         get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_OK))

    def test_success_signal_exit(self):
        self.sandbox.fake_execute_data(
            True, b"o", b"e", 0.1, 0.5, 1000, "SG", signal=11)
        self.sandbox.execute_without_std(["command"], wait=True)

        stats = execution_stats(self.sandbox)
        self.assertEqual(stats,
                         get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_SIGNAL,
                                   signal=11))

    def test_success_with_output(self):
        self.sandbox.fake_execute_data(
            True, b"o", b"e", 0.1, 0.5, 1000, "OK")
        self.sandbox.execute_without_std(["command"], wait=True)

        stats = execution_stats(self.sandbox, collect_output=True)
        self.assertEqual(stats,
                         get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_OK,
                                   stdout="o", stderr="e"))

    def test_invalid_utf8(self):
        self.sandbox.fake_execute_data(
            True,
            b"o" + INVALID_UTF8 + b"1",
            b"e" + INVALID_UTF8 + b"2",
            0.1, 0.5, 1000, "OK")

        self.sandbox.execute_without_std(["command"], wait=True)

        stats = execution_stats(self.sandbox, collect_output=True)

        # UTF-8 invalid parts are replaced with funny question marks (\uFFFD).
        self.assertRegex(stats["stdout"], "^o.*1$")
        self.assertRegex(stats["stderr"], "^e.*2$")
Example #13
0
 def setUp(self):
     super(TestGenericStep, self).setUp()
     self.sandbox = FakeIsolateSandbox(None)
Example #14
0
class TestExecutionStats(unittest.TestCase):
    def setUp(self):
        super(TestExecutionStats, self).setUp()
        self.sandbox = FakeIsolateSandbox(None)
        self.sandbox.stdout_file = "stdout.txt"
        self.sandbox.stderr_file = "stderr.txt"

    def test_success(self):
        # Tell the fake sandbox the command data to fake, and execute it.
        self.sandbox.fake_execute_data(True, b"o", b"e", 0.1, 0.5, 1000, "OK")
        self.sandbox.execute_without_std(["command"], wait=True)

        stats = execution_stats(self.sandbox)
        self.assertEqual(stats,
                         get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_OK))

    def test_success_signal_exit(self):
        self.sandbox.fake_execute_data(True,
                                       b"o",
                                       b"e",
                                       0.1,
                                       0.5,
                                       1000,
                                       "SG",
                                       signal=11)
        self.sandbox.execute_without_std(["command"], wait=True)

        stats = execution_stats(self.sandbox)
        self.assertEqual(
            stats,
            get_stats(0.1, 0.5, 1000 * 1024, Sandbox.EXIT_SIGNAL, signal=11))

    def test_success_with_output(self):
        self.sandbox.fake_execute_data(True, b"o", b"e", 0.1, 0.5, 1000, "OK")
        self.sandbox.execute_without_std(["command"], wait=True)

        stats = execution_stats(self.sandbox, collect_output=True)
        self.assertEqual(
            stats,
            get_stats(0.1,
                      0.5,
                      1000 * 1024,
                      Sandbox.EXIT_OK,
                      stdout="o",
                      stderr="e"))

    def test_invalid_utf8(self):
        self.sandbox.fake_execute_data(True, b"o" + INVALID_UTF8 + b"1",
                                       b"e" + INVALID_UTF8 + b"2", 0.1, 0.5,
                                       1000, "OK")

        self.sandbox.execute_without_std(["command"], wait=True)

        stats = execution_stats(self.sandbox, collect_output=True)

        # UTF-8 invalid parts are replaced with funny question marks (\uFFFD).
        assertRegex(self, stats["stdout"], "^o.*1$")
        assertRegex(self, stats["stderr"], "^e.*2$")
Example #15
0
 def setUp(self):
     super().setUp()
     self.sandbox = FakeIsolateSandbox(None)
     self.sandbox.stdout_file = "o"
     self.sandbox.stderr_file = "e"
Example #16
0
 def setUp(self):
     super(TestExecutionStats, self).setUp()
     self.sandbox = FakeIsolateSandbox(None)
     self.sandbox.stdout_file = "stdout.txt"
     self.sandbox.stderr_file = "stderr.txt"
Example #17
0
 def setUp(self):
     super().setUp()
     self.sandbox = FakeIsolateSandbox(None)