13f085823Sopenharmony_ci#!/usr/bin/env python
23f085823Sopenharmony_ci# -*- coding:utf-8 -*-
33f085823Sopenharmony_ci#
43f085823Sopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
53f085823Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
63f085823Sopenharmony_ci# you may not use this file except in compliance with the License.
73f085823Sopenharmony_ci# You may obtain a copy of the License at
83f085823Sopenharmony_ci#
93f085823Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
103f085823Sopenharmony_ci#
113f085823Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
123f085823Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
133f085823Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
143f085823Sopenharmony_ci# See the License for the specific language governing permissions and
153f085823Sopenharmony_ci# limitations under the License.
163f085823Sopenharmony_ci#
173f085823Sopenharmony_ci
183f085823Sopenharmony_ciimport re
193f085823Sopenharmony_ciimport os
203f085823Sopenharmony_ciimport stat
213f085823Sopenharmony_ci
223f085823Sopenharmony_ciFLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL
233f085823Sopenharmony_ciMODES = stat.S_IWUSR | stat.S_IRUSR
243f085823Sopenharmony_ci
253f085823Sopenharmony_ci
263f085823Sopenharmony_ciclass RunResult():
273f085823Sopenharmony_ci    SUCCESS = 0
283f085823Sopenharmony_ci
293f085823Sopenharmony_ci    RUN_ENV_ERROR = -1
303f085823Sopenharmony_ci    RUN_NO_DEVICE_ERROR = -2
313f085823Sopenharmony_ci    RUN_CONFIG_FORMAT_ERROR = -3
323f085823Sopenharmony_ci    RUN_CONFIG_FORMAT_NOCOLON_ERROR = -4
333f085823Sopenharmony_ci    RUN_FUZZER_BIN_NOT_FOUND_ERROR = -5
343f085823Sopenharmony_ci    RUN_CONNECT_ERROR = -6
353f085823Sopenharmony_ci    RUN_EXEC_ERROR = -7
363f085823Sopenharmony_ci    RUN_CONFIG_DICT_ERROR = -8
373f085823Sopenharmony_ci    RUN_LINK_ERROR = -9
383f085823Sopenharmony_ci
393f085823Sopenharmony_ci
403f085823Sopenharmony_ci    def __init__(self, code, data):
413f085823Sopenharmony_ci        self.code = code
423f085823Sopenharmony_ci        self.data = data
433f085823Sopenharmony_ci        self.payload = {}
443f085823Sopenharmony_ci
453f085823Sopenharmony_ci        self.crash_info = {
463f085823Sopenharmony_ci            "run_times": 0,
473f085823Sopenharmony_ci            "log": "",
483f085823Sopenharmony_ci            "project": "",
493f085823Sopenharmony_ci            "speed": 0,
503f085823Sopenharmony_ci            "summary": "No vulnerable",
513f085823Sopenharmony_ci            "command_log": "",
523f085823Sopenharmony_ci            "vulnerable": False,
533f085823Sopenharmony_ci            "backtrace": "",
543f085823Sopenharmony_ci            "cov": 0,
553f085823Sopenharmony_ci            "libscov": {},
563f085823Sopenharmony_ci            "report_progress": 0
573f085823Sopenharmony_ci        }
583f085823Sopenharmony_ci
593f085823Sopenharmony_ci    @staticmethod
603f085823Sopenharmony_ci    def filter_log(log_str):
613f085823Sopenharmony_ci        ansi_escape = re.compile(r'''
623f085823Sopenharmony_ci            \x1B
633f085823Sopenharmony_ci            (?:
643f085823Sopenharmony_ci                [@-Z\\-_]
653f085823Sopenharmony_ci            |
663f085823Sopenharmony_ci                \[
673f085823Sopenharmony_ci                [0-?]*
683f085823Sopenharmony_ci                [ -/]*
693f085823Sopenharmony_ci                [@-~]
703f085823Sopenharmony_ci            )
713f085823Sopenharmony_ci        ''', re.VERBOSE)
723f085823Sopenharmony_ci        result = ansi_escape.sub('', log_str)
733f085823Sopenharmony_ci        return result
743f085823Sopenharmony_ci
753f085823Sopenharmony_ci    # 获取日志
763f085823Sopenharmony_ci    def get_log(self):
773f085823Sopenharmony_ci        return "code :{}, msg: {}".format(self.code, self.data)
783f085823Sopenharmony_ci
793f085823Sopenharmony_ci    # 分析结果
803f085823Sopenharmony_ci    def analysis(self, result, outdir):
813f085823Sopenharmony_ci        pass
823f085823Sopenharmony_ci
833f085823Sopenharmony_ci    def write_analysis_result(self, analysis_ressult_path, html_format=True):
843f085823Sopenharmony_ci        if os.path.exists(analysis_ressult_path):
853f085823Sopenharmony_ci            os.remove(analysis_ressult_path)
863f085823Sopenharmony_ci        with os.fdopen(os.open(analysis_ressult_path, FLAGS, MODES), 'wb') as f:
873f085823Sopenharmony_ci            if html_format:
883f085823Sopenharmony_ci                f.write(RunResult.filter_log(render_detail(self.crash_info)))
893f085823Sopenharmony_ci            else:
903f085823Sopenharmony_ci                f.write(RunResult.filter_log(self.crash_info["backtrace"]))
913f085823Sopenharmony_ci
923f085823Sopenharmony_ci
933f085823Sopenharmony_ciif __name__ == "__main__":
943f085823Sopenharmony_ci    cmd_log = ""
953f085823Sopenharmony_ci    res_obj = RunResult(0, "OK")
963f085823Sopenharmony_ci    res_obj.analysis(cmd_log, "../../../out/")
973f085823Sopenharmony_ci    print(res_obj.crash_info)
983f085823Sopenharmony_ci
99