Ejemplo n.º 1
0
    def test_make_mfd(self):
        args, files = {}, {}
        body, content_type = make_mfd({'arg1': 'value1'}, {
            'file0': [
                HTTPFile(filename='file0.rar',
                         body='ARCHIVE',
                         content_type='some/type\r\n\r\nBAD DATA')
            ],
            'file1': [
                HTTPFile(filename='file1.png',
                         body='CAT PICTURE',
                         content_type=media_types.IMAGE_PNG)
            ],
            'file2': [HTTPFile(filename='file2.txt', body='TEXT')],
            'file3': [
                HTTPFile(filename=r'file3-"part1".unknown', body='BODY1'),
                HTTPFile(filename=r'file3-\part2\.unknown', body='BODY2'),
            ],
        })

        parse_body_arguments(to_unicode(content_type), body, args, files)

        self.assertEqual(args['arg1'], [b'value1'])

        self.assertEqual(files['file0'][0]['filename'], 'file0.rar')
        self.assertEqual(files['file0'][0]['body'], b'ARCHIVE')
        self.assertEqual(files['file0'][0]['content_type'],
                         'some/type    BAD DATA')

        self.assertEqual(files['file1'][0]['filename'], 'file1.png')
        self.assertEqual(files['file1'][0]['body'], b'CAT PICTURE')
        self.assertEqual(files['file1'][0]['content_type'],
                         media_types.IMAGE_PNG)

        self.assertEqual(files['file2'][0]['filename'], 'file2.txt')
        self.assertEqual(files['file2'][0]['body'], b'TEXT')
        self.assertEqual(files['file2'][0]['content_type'],
                         media_types.TEXT_PLAIN)

        self.assertEqual(files['file3'][0]['filename'],
                         r'file3-"part1".unknown')
        self.assertEqual(files['file3'][0]['body'], b'BODY1')
        self.assertEqual(files['file3'][0]['content_type'],
                         media_types.APPLICATION_OCTET_STREAM)
        self.assertEqual(files['file3'][1]['filename'],
                         r'file3-\part2\.unknown')
        self.assertEqual(files['file3'][1]['body'], b'BODY2')
        self.assertEqual(files['file3'][1]['content_type'],
                         media_types.APPLICATION_OCTET_STREAM)
Ejemplo n.º 2
0
    def post(self, task_name):
        task = self.get_task(task_name)
        if task is None:
            raise tornado.web.HTTPError(404)

        # Only set the official bit when the user can compete and we are not in
        # analysis mode.
        official = self.r_params["actual_phase"] == 0

        query_args = dict()

        # fill in any missing files with text contents
        for filename in task.submission_format:
            if filename not in self.request.files:
                ta_input = self.get_argument('ta_' + filename)

                # potentially append new file
                if ta_input:
                    stored_filename = filename
                    language = self.get_argument("language", None)
                    if language:
                        extension = get_language(language).source_extension
                        stored_filename = filename.replace(".%l", extension)

                    logger.info("Appending file from contents: " +
                                stored_filename)
                    ta_file = HTTPFile(filename=stored_filename,
                                       body=ta_input.encode(),
                                       type='text/plain')
                    self.request.files[filename] = [ta_file]

        try:
            submission = accept_submission(self.sql_session,
                                           self.service.file_cacher,
                                           self.current_user, task,
                                           self.timestamp, self.request.files,
                                           self.get_argument("language",
                                                             None), official)
            self.sql_session.commit()
        except UnacceptableSubmission as e:
            logger.info("Sent error: `%s' - `%s'", e.subject, e.formatted_text)
            self.notify_error(e.subject, e.text, e.text_params)
        else:
            self.service.evaluation_service.new_submission(
                submission_id=submission.id)
            self.notify_success(
                N_("Submission received"),
                N_("Your submission has been received "
                   "and is currently being evaluated."))
            # The argument (encrypted submission id) is not used by CWS
            # (nor it discloses information to the user), but it is
            # useful for automatic testing to obtain the submission id).
            query_args["submission_id"] = \
                encrypt_number(submission.id, config.secret_key)
            query_args["tab"] = "submissions"

        # self.redirect(self.contest_url("tasks", task.name, "submissions",
        #                               **query_args))
        self.redirect(
            self.contest_url("tasks", task.name, "full", **query_args))
Ejemplo n.º 3
0
    def test_make_mfd(self):
        args, files = {}, {}
        body, content_type = make_mfd({'arg1': 'value1'}, {
            'file0': [
                HTTPFile(filename='file0.rar',
                         body='ARCHIVE',
                         content_type='some/type\r\n\r\nBAD DATA')
            ],
            'file1': [
                HTTPFile(filename='file1.png',
                         body='CAT PICTURE',
                         content_type='image/png')
            ],
            'file2': [HTTPFile(filename='file2.txt', body='TEXT')],
            'file3': [
                HTTPFile(filename=r'file3-"part1".unknown', body='BODY1'),
                HTTPFile(filename=r'file3-\part2\.unknown', body='BODY2'),
            ],
        })

        parse_body_arguments(to_unicode(content_type), body, args, files)

        self.assertEqual(args['arg1'], [b'value1'])

        self.assertEqual(files['file0'][0]['filename'], 'file0.rar')
        self.assertEqual(files['file0'][0]['body'], b'ARCHIVE')
        self.assertEqual(files['file0'][0]['content_type'],
                         'some/type    BAD DATA')

        self.assertEqual(files['file1'][0]['filename'], 'file1.png')
        self.assertEqual(files['file1'][0]['body'], b'CAT PICTURE')
        self.assertEqual(files['file1'][0]['content_type'], 'image/png')

        self.assertEqual(files['file2'][0]['filename'], 'file2.txt')
        self.assertEqual(files['file2'][0]['body'], b'TEXT')
        self.assertEqual(files['file2'][0]['content_type'], 'text/plain')

        self.assertEqual(files['file3'][0]['filename'],
                         r'file3-"part1".unknown')
        self.assertEqual(files['file3'][0]['body'], b'BODY1')
        self.assertEqual(files['file3'][0]['content_type'],
                         'application/octet-stream')
        self.assertEqual(files['file3'][1]['filename'],
                         r'file3-\part2\.unknown')
        self.assertEqual(files['file3'][1]['body'], b'BODY2')
        self.assertEqual(files['file3'][1]['content_type'],
                         'application/octet-stream')
Ejemplo n.º 4
0
    def test_update_config_upload_code(self):
        config = _prepare_script_config_object(
            'Conf X', script=upload_script('Conf X.sh'))
        body = bytes.fromhex('4D5A')
        self.config_service.update_config(
            self.admin_user, config, 'confX.json',
            HTTPFile(filename='whatever', body=body))

        _validate_config(self, 'confX.json', config)
        _validate_code(self, _default_script_path('Conf X'), body)
        self.assertTrue(is_executable(_default_script_path('Conf X')))
Ejemplo n.º 5
0
    def test_upload_code(self):
        config = _prepare_script_config_object(
            'Conf X', script=upload_script('anything'))

        self.config_service.create_config(
            self.admin_user, config,
            HTTPFile(filename='my name.sh', body=b'xyz'))

        script_path = _default_script_path('my name')
        _validate_config(self, 'Conf_X.json', config)
        _validate_code(self, script_path, b'xyz')
        self.assertTrue(is_executable(script_path))
Ejemplo n.º 6
0
def my_parse_multipart_form_data(boundary, data, arguments, files):
    """Parses a ``multipart/form-data`` body.

    The ``boundary`` and ``data`` parameters are both byte strings.
    The dictionaries given in the arguments and files parameters
    will be updated with the contents of the body.
    """
    # The standard allows for the boundary to be quoted in the header,
    # although it's rare (it happens at least for google app engine
    # xmpp).  I think we're also supposed to handle backslash-escapes
    # here but I'll save that until we see a client that uses them
    # in the wild.
    if boundary.startswith(b'"') and boundary.endswith(b'"'):
        boundary = boundary[1:-1]
    final_boundary_index = data.rfind(b"--" + boundary + b"--")
    if final_boundary_index == -1:
        warning("Invalid multipart/form-data: no final boundary")
        return
    parts = data[:final_boundary_index].split(b"--" + boundary + b"\r\n")
    for part in parts:
        if not part:
            continue
        eoh = part.find(b"\r\n\r\n")
        if eoh == -1:
            warning("multipart/form-data missing headers")
            continue
        headers = HTTPHeaders.parse(part[:eoh].decode("utf-8"))
        disp_header = headers.get("Content-Disposition", "")
        disposition, disp_params = _parse_header(disp_header)
        
        if len(disp_header)==0:
            # This is the part before first delimiter and in the original tornado.httputil it's validated for proper Content-Disposition,
            # which is wrong
            pass
        else:
            if len(disp_header) and disposition != "form-data" or not part.endswith(b"\r\n"):
                warning("Invalid multipart/form-data")
                continue
            value = part[eoh + 4:-2]
            if not disp_params.get("name"):
                warning("multipart/form-data value missing name")
                continue
            name = disp_params["name"]
            if disp_params.get("filename"):
                ctype = headers.get("Content-Type", "application/unknown")
                files.setdefault(name, []).append(HTTPFile(  # type: ignore
                    filename=disp_params["filename"], body=value,
                    content_type=ctype))
            else:
                arguments.setdefault(name, []).append(value)
Ejemplo n.º 7
0
    def test_update_config_script_update_when_code_loading_problems(
            self, mode, original_script_path, expected_exception,
            expected_message):

        copyfile(sys.executable, os.path.join(test_utils.temp_folder,
                                              'python'))

        body = None
        if mode == 'new_code':
            script = {
                'mode':
                'new_code',
                'code':
                'abcdef',
                'path':
                original_script_path
                if not is_blank(original_script_path) else 'anything'
            }
        elif mode == 'upload_script':
            script = {
                'mode':
                'upload_script',
                'path':
                original_script_path
                if not is_blank(original_script_path) else 'anything'
            }
            body = HTTPFile(filename='whatever', body=b'xyz')
        else:
            script = None

        _create_script_config_file('ConfA',
                                   name='ConfA',
                                   script_path=original_script_path)

        config = _prepare_script_config_object('ConfA', script=script)
        if expected_exception is not None:
            self.assertRaisesRegex(expected_exception, expected_message,
                                   self.config_service.update_config,
                                   self.admin_user, config, 'ConfA.json', body)
        else:
            self.config_service.update_config(self.admin_user, config,
                                              'ConfA.json', body)
Ejemplo n.º 8
0
    def test_script_update_exceptions(self, mode, filename, content, username,
                                      expected_exception, expected_message):
        body = None
        if mode == 'new_code':
            script = {'mode': 'new_code', 'code': content, 'path': filename}
        elif mode == 'upload_script':
            script = {'mode': 'upload_script', 'path': filename}
            body = HTTPFile(filename='whatever',
                            body=content.encode('utf8')) if content else None
        elif mode == 'new_path':
            script = script_path(filename)
        elif mode is None:
            script = None
        else:
            script = {'mode': mode, 'path': filename}

        config = _prepare_script_config_object('Conf X', script=script)
        self.assertRaisesRegex(expected_exception, expected_message,
                               self.config_service.create_config,
                               User(username, {}), config, body)