Exemplo n.º 1
0
 def run_assertions(reactor: ReactorInterface):
     cb1 = ReactorCommand.create_callback(callback=ROUTE_CALLBACK,
                                          envelope=env1)
     cb2 = ReactorCommand.create_callback(callback=ROUTE_CALLBACK,
                                          envelope=env2)
     reactor._run_callback.assert_has_calls(
         [call(cb1), call(cb2)], any_order=False)
Exemplo n.º 2
0
        def assert_sub(composer: Composer):
            from cilantro.messages.reactor.reactor_command import ReactorCommand
            from cilantro.protocol.states.decorators import StateInput
            from unittest.mock import call
            # cb = ReactorCommand.create_callback(callback=StateInput.INPUT, envelope=env)
            # composer.interface.router.route_callback.assert_called_once_with(cb)

            expected_cb = call(
                ReactorCommand.create_callback(callback=StateInput.INPUT,
                                               envelope=env))
            unexpected_cb = call(
                ReactorCommand.create_callback(
                    callback=StateInput.LOOKUP_FAILED, envelope=evil_env))

            call_args = composer.manager.router.route_callback.call_args_list

            # # DEBUG STUFF
            # from cilantro.logger.base import get_logger
            # l = get_logger("assert sub")
            # l.debug('EXPECTED')
            # l.critical(expected_cb)
            # l.debug('UNEXPECTED')
            # l.critical(unexpected_cb)
            # l.important2("got dat call args: {}".format(call_args))
            # # END DEBUG STUFF

            # composer.interface.router.route_callback.assert_has_calls([expected_cb], any_order=True)

            assert expected_cb in call_args, "Expected callback {} to be in call_args {}".format(
                expected_cb, call_args)
            assert unexpected_cb not in call_args, "Did not expect callback {} to be in call_args {}".format(
                unexpected_cb, call_args)
Exemplo n.º 3
0
        def run_assertions(composer: Composer):
            from cilantro.messages.reactor.reactor_command import ReactorCommand
            from cilantro.protocol.states.decorators import StateInput
            from unittest.mock import call

            cb1 = ReactorCommand.create_callback(callback=StateInput.INPUT,
                                                 envelope=env1)
            cb2 = ReactorCommand.create_callback(callback=StateInput.INPUT,
                                                 envelope=env2)
            composer.manager.router.route_callback.assert_has_calls(
                [call(cb1), call(cb2)], any_order=True)
Exemplo n.º 4
0
        def assert_sub(composer: Composer):
            from cilantro.messages.reactor.reactor_command import ReactorCommand
            from cilantro.protocol.states.decorators import StateInput
            from unittest.mock import call

            callback1 = ReactorCommand.create_callback(
                callback=StateInput.INPUT, envelope=env1)
            callback2 = ReactorCommand.create_callback(
                callback=StateInput.INPUT, envelope=env2)
            calls = [call(callback1), call(callback2)]

            call_args = composer.manager.router.route_callback.call_args_list
            composer.manager.router.route_callback.assert_has_calls(
                calls, any_order=True)
Exemplo n.º 5
0
 def connection_drop(self, node):
     if self.event_sock: self.event_sock.send_json({'event':'disconect', 'ip':node.ip, 'vk': self.ironhouse.pk2vk.get(node.public_key) })
     callback = ReactorCommand.create_callback(
         callback=StateInput.CONN_DROPPED,
         ip=node.ip
     )
     log.debug("Sending callback failure to mainthread {}".format(callback))
Exemplo n.º 6
0
 def assert_router(composer: Composer):
     from cilantro.protocol.states.decorators import StateInput
     from cilantro.messages.reactor.reactor_command import ReactorCommand
     cb = ReactorCommand.create_callback(callback=StateInput.REQUEST,
                                         envelope=request_env,
                                         header=dealer_id)
     composer.manager.router.route_callback.assert_called_with(cb)
Exemplo n.º 7
0
 def assert_sub(composer: Composer):
     from cilantro.messages.reactor.reactor_command import ReactorCommand
     from cilantro.protocol.states.decorators import StateInput
     cb = ReactorCommand.create_callback(callback=StateInput.INPUT,
                                         envelope=env)
     composer.interface.router.route_callback.assert_called_once_with(
         cb)
Exemplo n.º 8
0
 def assert_bad_sub(composer: Composer):
     from cilantro.messages.reactor.reactor_command import ReactorCommand
     from cilantro.protocol.states.decorators import StateInput
     from unittest.mock import call
     cb = call(
         ReactorCommand.create_callback(callback=StateInput.INPUT,
                                        envelope=env))
     assert cb not in composer.manager.router.route_callback.call_args_list
        def assert_sub(composer: Composer):
            from cilantro.messages.reactor.reactor_command import ReactorCommand
            from cilantro.protocol.states.decorators import StateInput
            from unittest.mock import call

            callback1 = ReactorCommand.create_callback(
                callback=StateInput.INPUT, envelope=env1)
            callback2 = ReactorCommand.create_callback(
                callback=StateInput.INPUT, envelope=env2)
            calls = [call(callback1), call(callback2)]

            call_args = composer.interface.router.route_callback.call_args_list

            assert len(call_args) == 2, "route_callback should be called exactly twice, not {} times with {}"\
                                        .format(len(call_args), call_args)
            composer.interface.router.route_callback.assert_has_calls(
                calls, any_order=True)
Exemplo n.º 10
0
    async def _lookup_ip(self, cmd, url, vk, *args, **kwargs):
        ip, node = None, None
        try:
            node, cached = await self.dht.network.lookup_ip(vk)
            # NOTE while secure, this is a more loose connection policy
            self.log.fatal('{} resolves for {}'.format(os.getenv('HOST_IP'),
                                                       node))
            if node and not cached:
                ip = node.ip if type(node) == Node else node.split(':')[0]
                public_key = self.dht.network.ironhouse.vk2pk(vk)
                authorization = await self.dht.network.ironhouse.authenticate(
                    public_key, ip)
                self.log.fatal('{} -> {} is {}'.format(os.getenv('HOST_IP'),
                                                       node, authorization))
                if authorization != 'authorized':
                    node = None
                else:
                    n = Node(node_id=digest(vk),
                             public_key=public_key,
                             ip=ip,
                             port=self.dht.network.network_port)
                    self.dht.network.protocol.router.addContact(n)
                    self.dht.network.connect_to_neighbor(n)

                self.log.fatal([
                    item[0]
                    for item in self.dht.network.bootstrappableNeighbors()
                ])
        except Exception as e:
            delim_line = '!' * 64
            err_msg = '\n\n' + delim_line + '\n' + delim_line
            err_msg += '\n ERROR CAUGHT IN LOOKUP FUNCTION {}\ncalled \w args={}\nand kwargs={}\n'\
                        .format(args, kwargs)
            err_msg += '\nError Message: '
            err_msg += '\n\n{}'.format(traceback.format_exc())
            err_msg += '\n' + delim_line + '\n' + delim_line
            self.log.error(err_msg)

        if node is None:

            kwargs = cmd.kwargs
            callback = ReactorCommand.create_callback(
                callback=StateInput.LOOKUP_FAILED, **kwargs)
            self.log.debug(
                "Sending callback failure to mainthread {}".format(callback))
            self.socket.send(callback.serialize())
            # TODO -- send callback to SM saying hey i couldnt lookup this vk

            return

        # Send interpolated command back through pipeline
        ip = node.ip if type(node) == Node else node
        new_url = IPUtils.interpolate_url(url, ip)
        kwargs = cmd.kwargs
        kwargs['url'] = new_url
        new_cmd = ReactorCommand.create_cmd(envelope=cmd.envelope, **kwargs)

        self._execute_cmd(new_cmd)
Exemplo n.º 11
0
    def call_on_mp(self, callback: str, header: str=None, envelope_binary: bytes=None, **kwargs):
        if header:
            kwargs['header'] = header

        cmd = ReactorCommand.create_callback(callback=callback, envelope_binary=envelope_binary, **kwargs)

        # self.log.critical("\ncalling callback cmd to reactor interface: {}".format(cmd))  # DEBUG line remove this

        self.inproc_socket.send(cmd.serialize())
Exemplo n.º 12
0
 def connection_drop(self):
     if self.daemon:
         callback = ReactorCommand.create_callback(
             callback=StateInput.CONN_DROPPED,
             vk=self.ironhouse.vk,
             ip=self.node.ip)
         log.debug(
             "Sending callback failure to mainthread {}".format(callback))
         self.daemon.socket.send(callback.serialize())
Exemplo n.º 13
0
    def test_create_callback(self):
        """
        Tests create_callback
        """
        callback = 'route'
        kwargs = {'cats': '18', 'dogs': 'over 9000'}

        cmd = ReactorCommand.create_callback(callback, **kwargs)

        self.assertEqual(cmd.callback, callback)
        for k in kwargs:
            self.assertEqual(cmd.kwargs[k], kwargs[k])
Exemplo n.º 14
0
        def assert_sub(composer: Composer):
            from cilantro.messages.reactor.reactor_command import ReactorCommand
            from cilantro.protocol.states.decorators import StateInput
            from unittest.mock import call

            expected_calls = []
            for env in envs:
                callback = ReactorCommand.create_callback(
                    callback=StateInput.INPUT, envelope=env)
                expected_calls.append(call(callback))

            call_args = composer.manager.router.route_callback.call_args_list
            composer.manager.router.route_callback.assert_has_calls(
                expected_calls, any_order=True)
Exemplo n.º 15
0
    async def _lookup_ip(self, cmd, url, vk, *args, **kwargs):
        ip, node = None, None
        try:
            node, cached = await self.dht.network.lookup_ip(vk)
            # NOTE while secure, this is a more loose connection policy
            self.log.debugv('IP {} resolves {} into {}'.format(
                os.getenv('HOST_IP', '127.0.0.1'), vk, node))
            self.log.debugv(
                '... but is {} authorized? Until next episode!'.format(node))
            if node:
                if not self.dht.network.ironhouse.authorized_nodes.get(
                        node.id):
                    authorization = await self.dht.network.authenticate(node)
                    if not authorization:
                        node = None
            else:
                node = None

        except Exception as e:
            delim_line = '!' * 64
            err_msg = '\n\n' + delim_line + '\n' + delim_line
            err_msg += '\n ERROR CAUGHT IN LOOKUP FOR VK {}\ncalled \w args={}\nand kwargs={}\n'\
                       .format(vk, args, kwargs)
            err_msg += '\nError Message: '
            err_msg += '\n\n{}'.format(traceback.format_exc())
            err_msg += '\n' + delim_line + '\n' + delim_line
            self.log.fatal(err_msg)

        if node is None:
            kwargs = cmd.kwargs
            callback = ReactorCommand.create_callback(
                callback=StateInput.LOOKUP_FAILED, **kwargs)
            self.log.debug(
                "Sending callback failure to mainthread {}".format(callback))
            self.socket.send(callback.serialize())
            # TODO -- send callback to SM saying hey i couldnt lookup this vk

            return

        # Send interpolated command back through pipeline
        ip = node.ip if type(node) == Node else node
        new_url = IPUtils.interpolate_url(url, ip)
        kwargs = cmd.kwargs
        kwargs['url'] = new_url
        new_cmd = ReactorCommand.create_cmd(envelope=cmd.envelope, **kwargs)

        self._execute_cmd(new_cmd)
        def assert_sub(composer: Composer):
            from cilantro.messages.reactor.reactor_command import ReactorCommand
            from cilantro.protocol.states.decorators import StateInput
            from unittest.mock import call

            expected_calls = []
            for env in envs:
                callback = ReactorCommand.create_callback(
                    callback=StateInput.INPUT, envelope=env)
                expected_calls.append(call(callback))

            call_args = composer.interface.router.route_callback.call_args_list

            assert len(call_args) == len(expected_calls), "route_callback should be called exactly {} times, not {} " \
                                                          "times with {}".format(len(expected_calls), len(call_args), call_args)
            composer.interface.router.route_callback.assert_has_calls(
                expected_calls, any_order=True)
Exemplo n.º 17
0
 def assert_dealer(reactor: ReactorInterface):
     cb = ReactorCommand.create_callback(callback=ROUTE_CALLBACK,
                                         envelope=rep_env)
     reactor._run_callback.assert_called_once_with(cb)
Exemplo n.º 18
0
 def run_assertions(reactor: ReactorInterface):
     cb = ReactorCommand.create_callback(callback=ROUTE_REQ_CALLBACK,
                                         envelope=env,
                                         header=DEALER_ID)
     reactor._run_callback.assert_called_once_with(cb)
Exemplo n.º 19
0
 def run_assertions(reactor: ReactorInterface):
     callback = ReactorCommand.create_callback(callback=ROUTE_CALLBACK,
                                               envelope=env)
     reactor._run_callback.assert_called_once_with(callback)