# Path: kitty/core/actor.py
# class KittyActorInterface(KittyObject):
#     '''
#     Base class for monitors and controllers,
#     its defines (and partially implements) the Kitty Actor API:
# 
#     - :func:`~kitty.core.actor.KittyActorInterface.setup`
#     - :func:`~kitty.core.actor.KittyActorInterface.teardown`
#     - :func:`~kitty.core.actor.KittyActorInterface.pre_test`
#     - :func:`~kitty.core.actor.KittyActorInterface.post_test`
#     - :func:`~kitty.core.actor.KittyActorInterface.is_victim_alive`
#     - :func:`~kitty.core.actor.KittyActorInterface.get_report`
#     '''
# 
#     def __init__(self, name, logger=None, victim_alive_check_delay=0.3):
#         '''
#         :param name: name of the actor
#         :param logger: logger for the actor (default: None)
#         :param victim_alive_check_delay: delay between checks if alive (default: 0.3)
#         '''
#         super(KittyActorInterface, self).__init__(name, logger)
#         self.victim_alive_check_delay = victim_alive_check_delay
#         self.report = None
#         self.test_number = 0
# 
#     def setup(self):
#         '''
#         Called at the beginning of the fuzzing session.
#         You should override it with the actual implementation of victim setup.
#         '''
#         pass
# 
#     def teardown(self):
#         '''
#         Called at the end of the fuzzing session.
#         You should override it with the actual implementation of victim teardown.
#         '''
#         pass
# 
#     def pre_test(self, test_number):
#         '''
#         Called before a test is started. Call super if overriden.
# 
#         :param test_number: current test number
#         '''
#         self.test_number = test_number
#         self.report = Report(self.name)
#         self.report.add('start_time', time.time())
#         self.report.add('test_number', self.test_number)
#         self.report.add('state', 'pre_test')
#         last_log = 0
#         while not self.is_victim_alive():
#             if time.time() - last_log >= 10:
#                 last_log = time.time()
#                 self.logger.warn('waiting for target to be alive')
#             time.sleep(self.victim_alive_check_delay)
#         if last_log > 0:  # only if we logged that we're waiting, should we log that we're now alive
#             self.logger.warn('target is now alive')
# 
#     def post_test(self):
#         '''
#         Called when test is done. Call super if overriden.
#         '''
#         self.report.add('state', 'post_test')
#         self.report.add('stop_time', time.time())
# 
#     def get_report(self):
#         '''
#         :rtype: :class:`~kitty.data.report.Report`
#         :return: a report about the victim since last call to pre_test
#         '''
#         return self.report
# 
#     def is_victim_alive(self):
#         '''
#         Called during pre_test in loop until the target becomes alive
# 
#         :return: whether target is alive (ready for test) or not
# 
#         .. note::
# 
#             by default, it returns true,
#             override if you have a way to check in your actor
#         '''
#         return True


import logging
from kitty.targets import BaseTarget, ServerTarget, ClientTarget
from kitty.core.actor import KittyActorInterface
from mocks.mock_config import Config
from common import BaseTestCase


test_logger = None


def get_test_logger():
    global test_logger
    if test_logger is None:
        logger = logging.getLogger('TestClientFuzzer')
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] -> %(message)s')
        handler = logging.FileHandler('logs/test_target.log', mode='w')
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)
        logger.addHandler(handler)
        test_logger = logger
    return test_logger


def count_calls(fn_name):
    def decorator(func):
        def wrap(self, *args, **kwargs):
            if fn_name not in self.call_count:
                self.call_count[fn_name] = 0
            self.call_count[fn_name] += 1
            return func(self, *args, **kwargs)
        return wrap
    return decorator



# Path: kitty/core/actor.py
# class KittyActorInterface(KittyObject):
#     '''
#     Base class for monitors and controllers,
#     its defines (and partially implements) the Kitty Actor API:
# 
#     - :func:`~kitty.core.actor.KittyActorInterface.setup`
#     - :func:`~kitty.core.actor.KittyActorInterface.teardown`
#     - :func:`~kitty.core.actor.KittyActorInterface.pre_test`
#     - :func:`~kitty.core.actor.KittyActorInterface.post_test`
#     - :func:`~kitty.core.actor.KittyActorInterface.is_victim_alive`
#     - :func:`~kitty.core.actor.KittyActorInterface.get_report`
#     '''
# 
#     def __init__(self, name, logger=None, victim_alive_check_delay=0.3):
#         '''
#         :param name: name of the actor
#         :param logger: logger for the actor (default: None)
#         :param victim_alive_check_delay: delay between checks if alive (default: 0.3)
#         '''
#         super(KittyActorInterface, self).__init__(name, logger)
#         self.victim_alive_check_delay = victim_alive_check_delay
#         self.report = None
#         self.test_number = 0
# 
#     def setup(self):
#         '''
#         Called at the beginning of the fuzzing session.
#         You should override it with the actual implementation of victim setup.
#         '''
#         pass
# 
#     def teardown(self):
#         '''
#         Called at the end of the fuzzing session.
#         You should override it with the actual implementation of victim teardown.
#         '''
#         pass
# 
#     def pre_test(self, test_number):
#         '''
#         Called before a test is started. Call super if overriden.
# 
#         :param test_number: current test number
#         '''
#         self.test_number = test_number
#         self.report = Report(self.name)
#         self.report.add('start_time', time.time())
#         self.report.add('test_number', self.test_number)
#         self.report.add('state', 'pre_test')
#         last_log = 0
#         while not self.is_victim_alive():
#             if time.time() - last_log >= 10:
#                 last_log = time.time()
#                 self.logger.warn('waiting for target to be alive')
#             time.sleep(self.victim_alive_check_delay)
#         if last_log > 0:  # only if we logged that we're waiting, should we log that we're now alive
#             self.logger.warn('target is now alive')
# 
#     def post_test(self):
#         '''
#         Called when test is done. Call super if overriden.
#         '''
#         self.report.add('state', 'post_test')
#         self.report.add('stop_time', time.time())
# 
#     def get_report(self):
#         '''
#         :rtype: :class:`~kitty.data.report.Report`
#         :return: a report about the victim since last call to pre_test
#         '''
#         return self.report
# 
#     def is_victim_alive(self):
#         '''
#         Called during pre_test in loop until the target becomes alive
# 
#         :return: whether target is alive (ready for test) or not
# 
#         .. note::
# 
#             by default, it returns true,
#             override if you have a way to check in your actor
#         '''
#         return True


# Path: tests/test_target.py
import logging
from kitty.targets import BaseTarget, ServerTarget, ClientTarget
from kitty.core.actor import KittyActorInterface
from mocks.mock_config import Config
from common import BaseTestCase



test_logger = None


def get_test_logger():
    global test_logger
    if test_logger is None:
        logger = logging.getLogger('TestClientFuzzer')
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] -> %(message)s')
        handler = logging.FileHandler('logs/test_target.log', mode='w')
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)
        logger.addHandler(handler)
        test_logger = logger
    return test_logger


def count_calls(fn_name):
    def decorator(func):
        def wrap(self, *args, **kwargs):
            if fn_name not in self.call_count:
                self.call_count[fn_name] = 0
            self.call_count[fn_name] += 1
            return func(self, *args, **kwargs)
        return wrap
    return decorator



class TestActor(KittyActorInterface):